支付寶支付

weixin_34146805發表於2018-09-10

按照“支付寶開放平臺”的“iOS整合流程”進行整合:
https://docs.open.alipay.com/204/105295/

一、專案配置:

  • 請按照上面給的連結文件進行配置

二、程式碼:

1.在.pch檔案中巨集定義跳轉app的URLSchemes:

//跳轉本app需要用的 URLScheme
#define kAppURLScheme @"AJProject"

2.在用到支付寶支付的地方,倒入標頭檔案:

#import <AlipaySDK/AlipaySDK.h>

3.AJGoodsCashierController.m檔案(收銀臺頁):

#pragma mark - 支付寶相關
/**
 支付寶支付

 @param orderStr 後臺返回的訂單string
 */
-(void)aliPayWithOrderStr:(NSString *)orderStr {
    [AlipaySDK.defaultService payOrder:orderStr fromScheme:kAppURLScheme callback:^(NSDictionary *resultDic) {
        //這是從支付寶H5頁面支付之後走的回撥 - 未離開了本app
        [self postAliPayResultNotificationWithDict:resultDic];
    }];
}
/**傳送支付寶支付結果的通知*/
-(void)postAliPayResultNotificationWithDict:(NSDictionary *)dict {
    if (dict != nil) {
        [NSNotificationCenter.defaultCenter postNotificationName:@"kAliPayResultNotification" object:nil userInfo:dict];
    }
}
/**接收到通知的回撥 - “支付寶支付結果”*/
-(void)getAlipayOrderPayResult:(NSNotification *)notification {
    if (notification.userInfo != nil) {
        NSDictionary * resultDic = notification.userInfo;
        NSString * resultStatus = [resultDic valueForKey:@"resultStatus"];
        if ([resultStatus isEqualToString:@"9000"]) {//支付成功
            self.payResultLabel.text = @"支付成功!";
            [self.payResultImageView setImageWithName:@"icon_pay_paySuccess"];
        } else {//支付失敗
            self.payResultLabel.text = @"支付失敗...";
            [self.payResultImageView setImageWithName:@"icon_pay_payFail"];
        }
        self.payResultBgView.hidden = false;
        [self.view bringSubviewToFront:self.payResultBgView];
    }
}

4.AppDelegate.m檔案:

  • 下面的 processAuthResult 方法,是處理“支付寶app授權結果資訊”的方法,如果需求中有對授權結果的處理,可以選擇呼叫。如果只是處理“支付成功/失敗的結果資訊”,那麼只需要呼叫 processOrderWithPaymentResult 方法即可。
#pragma mark - 支付寶接入
// NOTE: 9.0以前
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    if ([url.host isEqualToString:@"safepay"]) {
        
        // 支付跳轉支付寶錢包進行支付,處理支付結果(從支付寶app支付之後走的回撥 - 離開了本app)
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            [self postAliPayResultNotificationWithDict:resultDic];
        }];
        
        //處理授權結果
        [[AlipaySDK defaultService] processAuthResult: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(@"授權結果 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"]) {
        
        // 支付跳轉支付寶錢包進行支付,處理支付結果(從支付寶app支付之後走的回撥 - 離開了本app)
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            [self postAliPayResultNotificationWithDict:resultDic];
        }];
        
        // 處理授權結果
        [[AlipaySDK defaultService] processAuthResult: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(@"授權結果 authCode = %@", authCode?:@"");
        }];
    
    }
    return YES;
}
/**傳送支付寶支付結果的通知*/
-(void)postAliPayResultNotificationWithDict:(NSDictionary *)dict {
    if (dict != nil) {
        [NSNotificationCenter.defaultCenter postNotificationName:@"kAliPayResultNotification" object:nil userInfo:dict];
    }
}

相關文章