NSString *appID = @"";
// 如下私鑰坛梁,rsa2PrivateKey 或者 rsaPrivateKey 只需要填入一個(gè)
// 如果商戶兩個(gè)都設(shè)置了,優(yōu)先使用 rsa2PrivateKey
// rsa2PrivateKey 可以保證商戶交易在更加安全的環(huán)境下進(jìn)行,建議使用 rsa2PrivateKey
// 獲取 rsa2PrivateKey巩螃,建議使用支付寶提供的公私鑰生成工具生成室囊,
// 工具地址:https://doc.open.alipay.com/docs/doc.htm?treeId=291&articleId=106097&docType=1
NSString *rsa2PrivateKey = @"";
NSString *rsaPrivateKey = @"";
/*============================================================================*/
/*============================================================================*/
/*============================================================================*/
//partner和seller獲取失敗,提示
if ([appID length] == 0 ||
([rsa2PrivateKey length] == 0 && [rsaPrivateKey length] == 0))
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"缺少appId或者私鑰,請(qǐng)檢查參數(shù)設(shè)置"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:^{ }];
return;
}
/*
*生成訂單信息及簽名
*/
//將商品信息賦予AlixPayOrder的成員變量
APOrderInfo* order = [APOrderInfo new];
// NOTE: app_id設(shè)置
order.app_id = appID;
// NOTE: 支付接口名稱
order.method = @"alipay.trade.app.pay";
// NOTE: 參數(shù)編碼格式
order.charset = @"utf-8";
// NOTE: 當(dāng)前時(shí)間點(diǎn)
NSDateFormatter* formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
order.timestamp = [formatter stringFromDate:[NSDate date]];
// NOTE: 支付版本
order.version = @"1.0";
// NOTE: sign_type 根據(jù)商戶設(shè)置的私鑰來決定
order.sign_type = (rsa2PrivateKey.length > 1)?@"RSA2":@"RSA";
// NOTE: 商品數(shù)據(jù)
order.biz_content = [APBizContent new];
order.biz_content.body = @"我是測(cè)試數(shù)據(jù)";
order.biz_content.subject = @"1";
order.biz_content.out_trade_no = [self generateTradeNO]; //訂單ID(由商家自行制定)
order.biz_content.timeout_express = @"30m"; //超時(shí)時(shí)間設(shè)置
order.biz_content.total_amount = [NSString stringWithFormat:@"%.2f", 0.01]; //商品價(jià)格
//將商品信息拼接成字符串
NSString *orderInfo = [order orderInfoEncoded:NO];
NSString *orderInfoEncoded = [order orderInfoEncoded:YES];
NSLog(@"orderSpec = %@",orderInfo);
// NOTE: 獲取私鑰并將商戶信息簽名娃磺,外部商戶的加簽過程請(qǐng)務(wù)必放在服務(wù)端嗜价,防止公私鑰數(shù)據(jù)泄露田度;
// 需要遵循RSA簽名規(guī)范湾宙,并將簽名字符串base64編碼和UrlEncode
NSString *signedString = nil;
APRSASigner* signer = [[APRSASigner alloc] initWithPrivateKey:((rsa2PrivateKey.length > 1)?rsa2PrivateKey:rsaPrivateKey)];
if ((rsa2PrivateKey.length > 1)) {
signedString = [signer signString:orderInfo withRSA2:YES];
} else {
signedString = [signer signString:orderInfo withRSA2:NO];
}
// NOTE: 如果加簽成功樟氢,則繼續(xù)執(zhí)行支付
if (signedString != nil) {
//應(yīng)用注冊(cè)scheme,在AliSDKDemo-Info.plist定義URL types
NSString *appScheme = @"alisdkdemo";
// NOTE: 將簽名成功字符串格式化為訂單字符串,請(qǐng)嚴(yán)格按照該格式
NSString *orderString = [NSString stringWithFormat:@"%@&sign=%@",
orderInfoEncoded, signedString];(該字符串是最終要傳給支付寶第三方的信息包含,包含的訂單和簽名的信息侠鳄,簽名又是訂單的加密算法等生成的嗡害,意思就是訂單信息要用兩次。服務(wù)器給你的信息僅僅需要這個(gè)字符串就行)例如后臺(tái)返回成功處理的信息
// NOTE: 調(diào)用支付結(jié)果開始支付
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
}];
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者