如何創建訂單 ( 訂單根據自己公司看是什麼樣的)
如何簽名
如何調用支付接口
都在這個方法裏面了
01.
//
02.
//選中商品調用支付寶快捷支付
03.
//
04.
-
(
void
)tableView:(UITableView
*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
05.
{
06.
/*
07.
*點擊獲取prodcut實例並初始化訂單信息
08.
*/
09.
Product
*product = [_products objectAtIndex:indexPath.row];
10.
11.
/*
12.
*商戶的唯一的parnter和seller。
13.
*本demo將parnter和seller信息存於(AlixPayDemo-Info.plist)中,外部商戶可以考慮存於服務端或本地其他地方。
14.
*簽約後,支付寶會爲每個商戶分配一個唯一的
parnter 和 seller。
15.
*/
16.
//如果partner和seller數據存於其他位置,請改寫下面兩行代碼
17.
NSString
*partner = [[NSBundle mainBundle] objectForInfoDictionaryKey:@
"Partner"
];
18.
NSString
*seller = [[NSBundle mainBundle] objectForInfoDictionaryKey:@
"Seller"
];
19.
20.
//partner和seller獲取失敗,提示
21.
if
([partner
length] ==
0
||
[seller length] ==
0
)
22.
{
23.
UIAlertView
*alert = [[UIAlertView alloc] initWithTitle:@
"提示"
24.
message:@
"缺少partner或者seller。"
25.
delegate:self
26.
cancelButtonTitle:@
"確定"
27.
otherButtonTitles:nil];
28.
[alert
show];
29.
[alert
release];
30.
return
;
31.
}
32.
33.
/*
34.
*生成訂單信息及簽名
35.
*由於demo的侷限性,本demo中的公私鑰存放在AlixPayDemo-Info.plist中,外部商戶可以存放在服務端或本地其他地方。
36.
*/
37.
//將商品信息賦予AlixPayOrder的成員變量
38.
AlixPayOrder
*order = [[AlixPayOrder alloc] init];
39.
order.partner
= partner;
40.
order.seller
= seller;
41.
order.tradeNO
= [self generateTradeNO];
//訂單ID(由商家自行制定)
42.
order.productName
= product.subject;
//商品標題
43.
order.productDescription
= product.body;
//商品描述
44.
order.amount
= [NSString stringWithFormat:@
"%.2f"
,product.price];
//商品價格
45.
order.notifyURL
= @
"http://www.xxx.com"
;
//回調URL
46.
47.
//應用註冊scheme,在AlixPayDemo-Info.plist定義URL
types,用於快捷支付成功後重新喚起商戶應用
48.
NSString
*appScheme = @
"AlixPayDemo"
;
49.
50.
//將商品信息拼接成字符串
51.
NSString
*orderSpec = [order description];
52.
NSLog(@
"orderSpec
= %@"
,orderSpec);
53.
54.
//獲取私鑰並將商戶信息簽名,外部商戶可以根據情況存放私鑰和簽名,只需要遵循RSA簽名規範,並將簽名字符串base64編碼和UrlEncode
55.
id<DataSigner>
signer = CreateRSADataSigner([[NSBundle mainBundle] objectForInfoDictionaryKey:@
"RSA
private key"
]);
56.
NSString
*signedString = [signer signString:orderSpec];
57.
58.
//將簽名成功字符串格式化爲訂單字符串,請嚴格按照該格式
59.
NSString
*orderString = nil;
60.
if
(signedString
!= nil) {
61.
orderString
= [NSString stringWithFormat:@
"%@&sign=\"%@\"&sign_type=\"%@\""
,
62.
orderSpec,
signedString, @
"RSA"
];
63.
64.
//獲取快捷支付單例並調用快捷支付接口
65.
AlixPay
* alixpay = [AlixPay shared];
66.
int
ret
= [alixpay pay:orderString applicationScheme:appScheme];
67.
68.
if
(ret
== kSPErrorAlipayClientNotInstalled) {
69.
UIAlertView
* alertView = [[UIAlertView alloc] initWithTitle:@
"提示"
70.
message:@
"您還沒有安裝支付寶快捷支付,請先安裝。"
71.
delegate:self
72.
cancelButtonTitle:@
"確定"
73.
otherButtonTitles:nil];
74.
[alertView
setTag:
123
];
75.
[alertView
show];
76.
[alertView
release];
77.
}
78.
else
if
(ret
== kSPErrorSignError) {
79.
NSLog(@
"簽名錯誤!"
);
80.
}
81.
82.
}
83.
84.
[tableView
deselectRowAtIndexPath:indexPath animated:YES];
85.
}
01.
//.封裝訂單模型
02.
AlixPayOrder
*order = [[AlixPayOrder alloc] init];
03.
//
生成訂單描述
04.
NSString
*orderSpec = [order description];
05.
06.
//2.簽名
07.
id<DataSigner>
signer = CreateRSADataSigner(@“私鑰key”);
08.
//
傳入訂單描述 進行 簽名
09.
NSString
*signedString = [signer signString:orderSpec];
10.
11.
12.
//3.生成訂單字符串
13.
NSString
*orderString = [NSString stringWithFormat:@
'%@&sign='
%@
'&sign_type='
%@
''
,
14.
orderSpec,
signedString, @
'RSA'
];
15.
16.
//4.調用支付接口
17.
AlixPay
* alixpay = [AlixPay shared];
18.
//
appScheme:商戶自己的協議頭
19.
int
ret
= [alixpay pay:orderString applicationScheme:appScheme];