談行動支付的文章多半著重於「怎麼把支付叫起來」,講到「付完之後怎麼處理回調」的反而不多。當初我在這裡卡了很久,因此整理成這份筆記。
支付寶相對單純,回調就在叫起支付的那個方法的 block 裡。
NSString *orderString = nil;
if (signedString != nil) {
orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString, @"RSA"];
[[AlipaySDK defaultService] payOrder:orderString
fromScheme:appScheme
callback:^(NSDictionary *resultDic) {
NSString *title = @"支付結果";
NSString *msg;
// 回調結果判斷
if ([resultDic[@"resultStatus"] isEqualToString:@"9000"]) {
msg = @"恭喜您,支付成功!";
} else if ([resultDic[@"resultStatus"] isEqualToString:@"6001"]) {
msg = @"已取消支付!";
} else {
msg = @"支付失敗!";
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:self
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alert show];
}];
}這裡只判斷了成功、取消、失敗三種狀態,其餘狀態碼請對照官方文件。
首先是 URL 回調進入點,這段幾乎照官方文件寫即可,基本不用改。
- (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) {
// App 在背景時可能被系統終止,導致 pay 的 callback 失效,
// 因此需在此處理與 callback 相同的邏輯
}];
[[AlipaySDK defaultService] processAuthResult:url
standbyCallback:^(NSDictionary *resultDic) {
// 同上,處理授權結果
}];
}
if ([sourceApplication isEqualToString:@"com.tencent.xin"]) {
// 微信支付回調
return [WXApi handleOpenURL:url delegate:self];
}
// 其他 SDK(例如社群分享)
return [UMSocialSnsService handleOpenURL:url];
}最後微信會呼叫下面這個方法,實際處理邏輯就寫在這裡。
- (void)onResp:(BaseResp *)resp {
NSString *msg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
NSString *title;
if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
title = @"發送媒體訊息結果";
}
if ([resp isKindOfClass:[PayResp class]]) {
// 這裡只是回報調用結果,實際支付狀態仍需向微信伺服器查詢
title = @"支付結果";
switch (resp.errCode) {
case WXSuccess: {
msg = @"恭喜您,支付成功!";
[MYNotificationCenter postNotificationName:@"weixinPayStatus"
object:nil
userInfo:@{@"status": @"success"}];
break;
}
case WXErrCodeUserCancel: {
msg = @"已取消支付!";
[MYNotificationCenter postNotificationName:@"weixinPayStatus"
object:nil
userInfo:@{@"status": @"cancle"}];
break;
}
default: {
msg = @"支付失敗!";
[MYNotificationCenter postNotificationName:@"weixinPayStatus"
object:nil
userInfo:@{@"status": @"cancle"}];
break;
}
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:self
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alert show];
}因為我們這邊「取消」與「失敗」的後續處理相同,所以兩者用了同一個通知名稱。
為什麼要用通知?因為 onResp: 位於 AppDelegate,在這裡直接跳轉到指定頁面並不方便。改成發通知,由叫起支付的那個頁面(它在叫起前就已經註冊好觀察者)接收後自行跳轉,責任劃分清楚得多。
這是最容易踩到的一塊。我們的付款性質不只一種(儲值、購物等),成功之後要跳的頁面各不相同。但微信的回調方法只告訴你成功/取消/失敗,並不知道你原本在做什麼;而支付過程已經跳離 App,無法把參數直接傳回來。
解法是在叫起支付之前先記下類型標記:
[[NSUserDefaults standardUserDefaults] setObject:@"CHONGZHI"
forKey:@"PAYTYPE"];在每種支付各自的方法中寫入不同的 key,然後在 onResp: 裡讀出來判斷要跳去哪頁。
最後補一個重要提醒:client 端的回調結果只能當作用戶體驗的提示,不可作為出貨或發值的依據。使用者完全可以在支付中途跳開、或收到回調後直接離開 App。真正的訂單狀態必須由伺服器查詢微信/支付寶的查單介面確認,並以伺服器端的異步通知為準。