支付宝支付

按照“支付宝开放平台”的“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];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。