1翼抠、集成SDK:
下載并解壓SDK?:下載鏈接咙轩。
2、添加依賴庫
SystemConfiguration.framework
libz.tbd
libc++.tbd
CoreTelephony.framework
CFNetwork.framework
Security.framework
UIKit.framework
Foundation.framework
CoreGraphics.framework
CoreMotion.framework
CoreText.framework
3阴颖、新增?一條URL Scheme:選中?工程Target -> Info -> URLTypes;
identifier隨便寫臭墨,URL scheme此時也可以隨便填寫,但是要保證調(diào)起支付的方法里傳的appScheme參數(shù)和這里填寫的一致
支付寶URL scheme
調(diào)起支付的方法中的參數(shù)scheme要和URL scheme配置中填寫的一樣
4膘盖、添加?白名單:LSApplicationQueriesSchemes新增?名單
支付寶白名單
5、在支付頁面獲取到支付所需要的參數(shù)后調(diào)用支付方法
/**
* 支付寶支付
*/
-(void)handleOrderPayWithParams:(NSDictionary *)aParam
{
NSLog(@"aParm = %@",aParam);
//這里的appScheme一定要與URL Scheme中的一致
NSString *appScheme = @"alisdkofshuyinHotel";
NSString *orderString = aParam[@"orderInfo"];
// NOTE: 調(diào)用支付結(jié)果開始支付
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
int statusCode = [resultDic[@"resultStatus"] intValue];
if (statusCode == 9000) {
//訂單支付
[SVProgressHUD showSuccessWithStatus:@"支付成功尤误!"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"paySuccess" object:nil];
}else {
//交易失敗
// [[NSNotificationCenter defaultCenter] postNotificationName:@"PAY_STATUS" object:@"0"];
[SVProgressHUD showErrorWithStatus:@"支付異常!"];
}
}];
}
6喘落、appDelegate中處理回調(diào)結(jié)果
//iOS9.0以前
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if ([url.host isEqualToString:@"safepay"]) {
// 支付跳轉(zhuǎn)支付寶錢包進行支付暖哨,處理支付結(jié)果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
// 授權(quán)跳轉(zhuǎn)支付寶錢包進行支付,處理支付結(jié)果
[[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
// 解析 auth code
NSString *result = resultDic[@"result"];
NSString *authCode = nil;
if (result.length>0) {
NSArray *resultArr = [result componentsSeparatedByString:@"&"];
for (NSString *subResult in resultArr) {
if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
authCode = [subResult substringFromIndex:10];
break;
}
}
}
NSLog(@"授權(quán)結(jié)果 authCode = %@", authCode?:@"");
}];
}
return YES;
}
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
if ([url.host isEqualToString:@"safepay"]) {
// 支付跳轉(zhuǎn)支付寶錢包進行支付黍聂,處理支付結(jié)果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
if ([resultDic[@"resultStatus"] integerValue] == 9000) {
[SVProgressHUD showSuccessWithStatus:@"支付成功!"];
PaySucceedViewController *vc = [[PaySucceedViewController alloc] init];
vc.backStr = @"1";
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
[self.tb presentViewController:navi animated:NO completion:nil];
}else{
[SVProgressHUD showSuccessWithStatus:@"支付異常!"];
PayFailedViewController *vc = [[PayFailedViewController alloc] init];
vc.backStr = @"1";
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
[self.tb presentViewController:navi animated:NO completion:nil];
}
}];
// 授權(quán)跳轉(zhuǎn)支付寶錢包進行支付,處理支付結(jié)果
[[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
// 解析 auth code
NSString *result = resultDic[@"result"];
NSString *authCode = nil;
if (result.length>0) {
NSArray *resultArr = [result componentsSeparatedByString:@"&"];
for (NSString *subResult in resultArr) {
if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
authCode = [subResult substringFromIndex:10];
break;
}
}
}
NSLog(@"授權(quán)結(jié)果 authCode = %@", authCode?:@"");
}];
}
return YES;
}