IOS 支付宝支付开发流程

一、看官方的Demo,那么看到,必须使用的是商户app申请的时候会有的

 

这几个东西由产品经理申请给你,如果遇到真不懂的,让你自己申请(也算是对自己的信任,那就看着官方文档一步步申请,这个没什么困难的,做开发的我这里也不记了。没什么意义。)

 

 二、集成支付宝到自己的工程中

集成?笑话,写下来有什么意义,还不如看官方文档,最方便的当然是cocoapods 几分钟搞定

根据自己的APP,修改下面的参数即可:

 

 

 

说实话,其实这些都是后台去弄的,前端只是点击支付按钮时候,请求后台,让后台给你生成 orderstr 就行了,

-(void)alipayWith:(NSString*)orderStr{
    NSString *appScheme = @"zhifubao";
   
    // NOTE: 调用支付结果开始支付
    [[AlipaySDK defaultService] payOrder:orderStr fromScheme:appScheme callback:^(NSDictionary *resultDic) {
        NSLog(@"reslut = %@",resultDic);
       
        if ([resultDic[@"ResultStatus"] isEqualToString:@"9000"]) {
            
        }else{
            
        }
        
    }];
}

 

使用支付宝客户端支付之后,无法返回原APP系统

1)在AppDelegate.m文件中,

#import <AlipaySDK/AlipaySDK.h>

-(BOOL)application:(UIApplication *)app openURL:(nonnull NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(nonnull id)annotation{
     [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付,处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            NSLog(@"支付宝客户端支付结果result = %@",resultDic);
            if (resultDic && [resultDic objectForKey:@"resultStatus"] && ([[resultDic objectForKey:@"resultStatus"] intValue] == 9000)) {
                
                // 发通知带出支付成功结果
                [[NSNotificationCenter defaultCenter] postNotificationName:@"alpayResult" object:resultDic];
            } else {
                
                // 发通知带出支付失败结果
                [[NSNotificationCenter defaultCenter] postNotificationName:@"alpayResult" object:resultDic];
            }
        }];
    }
    
    return YES;
}

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
     [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付,处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            if (resultDic && [resultDic objectForKey:@"resultStatus"] && ([[resultDic objectForKey:@"resultStatus"] intValue] == 9000)) {
                
                // 发通知带出支付成功结果
              [[NSNotificationCenter defaultCenter] postNotificationName:@"alpayResult" object:resultDic];
            } else {
                
                // 发通知带出支付失败结果
              [[NSNotificationCenter defaultCenter] postNotificationName:@"alpayResult" object:resultDic];
            }
        }];
    }
    
    return YES;
}

这里面我是做了一个通知,告诉支付页面支付结果的

 

 

(2)点击项目名称,点击“Info”选项卡,在“URL Types”选项中,点击“+”,在“URL Schemes”中输入自己的APP标示,如:“alisdkdemo”。“alisdkdemo”来自于以下代码:

支付代码中的:

这里的URL Schemes中输入的alisdkdemo,为测试demo,实际商户的app中要填写独立的scheme,建议跟商户的app有一定的标示度,要做到和其他的商户app不重复,否则可能会导致支付宝返回的结果无法正确跳回商户app

4、可能遇到的错误:

  1:支付宝交易订单处理失败 AL159

  注意:支付宝的金额只能精确到分,所以不能用小数点后面超过两位的浮点型数字。

  所以:应该使用 %.2f 

  2:支付宝支付失败:4000

  检查订单的各种参数,比如订单ID写错为浮点型了。

  3:其他

  检查各种参数和公匙密匙等等

 

 

补充一下,官方流程,做参考用

 

posted @ 2019-07-01 15:36  时光清浅、  阅读(1603)  评论(0编辑  收藏  举报