iOS集成支付寶,支付成功之后"收不到回調(diào)"解決
以下方法僅適用于支付寶sdk從 v15.1.0之前的版本 升級(jí)到 v15.1.0版本及之后
看下官方更新日志
IOS版本號(hào):v15.1.0
修改時(shí)間:2016-4-28
更新點(diǎn):
1. 支付結(jié)果返回統(tǒng)一慌植,便于用戶接入。
原有方案:payOrder:fromScheme:callback接口开泽,業(yè)務(wù)傳入callback1,用于支付結(jié)果返回界拦;
processOrderWithPaymentReulst:standbyCallback接口,業(yè)務(wù)傳入callback2,用于支付結(jié)果返回乏矾;
一般一次支付中(跳支付寶客戶端支付)屋剑,都會(huì)涉及到這兩個(gè)接口润匙;原有的規(guī)則是callback1存在,則統(tǒng)一調(diào)用callback1返回支付結(jié)果給業(yè)務(wù)方唉匾,若callback1不存在(app被殺掉的情況)孕讳,則調(diào)用callback2返回支付結(jié)果給業(yè)務(wù)方。
現(xiàn)有方案:payOrder:fromScheme:callback接口巍膘,業(yè)務(wù)會(huì)傳入CompletionBlock1(本地沒(méi)有安裝支付寶客戶端的情況下(走H5收銀臺(tái))厂财,會(huì)通過(guò)該CompletionBlock1反饋結(jié)果); processOrderWithPaymentResult:standByCallback接口峡懈,業(yè)務(wù)會(huì)傳入CompletionBlock2(本地安裝了支付寶客戶端的情況下璃饱,會(huì)通過(guò)該CompletionBlock2反饋結(jié)果)。
可以發(fā)現(xiàn)在 v15.1.0 之后回調(diào)機(jī)制發(fā)生變化,調(diào)用支付寶客戶端支付不會(huì)走payOrder:fromScheme:callback 的回調(diào),只會(huì)走Appdelegate 的方法:
注:iOS 9.0之后方法的變更
//可以看到該方法適用于iOS 4.2 - > iOS 9.0
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation NS_DEPRECATED_IOS(4_2, 9_0, "Please use application:openURL:options:") __TVOS_PROHIBITED;
//iOS 9.0 之后使用下面方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options NS_AVAILABLE_IOS(9_0);
使用如下:
#ifdef IOS9
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
if ([url.host isEqualToString:@"safepay"]) {
//一般只需要調(diào)用這一個(gè)方法即可
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
[[NSNotificationCenter defaultCenter] postNotificationName:kAliPayCallBack object:resultDic];
}];
[[AlipaySDK defaultService] processAuth_V2Result:url
standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
NSString *resultStr = resultDic[@"result"];
NSLog(@"result = %@",resultStr);
}];
}
else if ([url.host isEqualToString:@"platformapi"]){//支付寶錢(qián)包快登授權(quán)返回 authCode
[[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
}
return YES;
}
#else
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if ([url.host isEqualToString:@"safepay"]) {
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
[[AlipaySDK defaultService] processAuth_V2Result:url
standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
NSString *resultStr = resultDic[@"result"];
NSLog(@"result = %@",resultStr);
}];
}
else if ([url.host isEqualToString:@"platformapi"]){//支付寶錢(qián)包快登授權(quán)返回 authCode
[[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
}
return YES;
}
#endif