美文网首页
iOS项目集成支付宝功能

iOS项目集成支付宝功能

作者: 07212a79db66 | 来源:发表于2016-08-01 14:50 被阅读58次

    一. 介绍

    1 概述:

    • 内购是用户将钱付款给苹果,之后苹果分成给商户
    • 支付宝是用户将钱付款给支付宝,之后支付宝将钱转入我们的账户

    2 集成支付宝的步骤:

    • 向支付宝申请, 与支付宝签约,获得商户ID(partner)和账号ID(seller)和私钥(privateKey)
    • 下载支付宝SDK
    • 生成订单信息,签名加密
    • 调用支付宝客户端,由支付宝客户端跟支付宝安全服务器打交道
    • 支付完毕后,支付宝客户端会自动跳回到原来的应用程序
    • 在原来的应用程序中显示支付结果给用户看

    3 支付流程过程:

    二.步骤:

    1 下载官方DEMO
    2 将demo里面的SDK及Order.h和.m全部拷贝出来,然后导入需要的框架,如下图.


    3 编译一下,可能会包错误:


    这个错误,解决方案如下


    4 集成支付代码:这里模拟点击cell进行商品的购买

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self buyProduct:self.products[indexPath.row]];
    }
    
    

    支付功能实现核心

    - (void)buyProduct:(Products *)product {
        
        // 1.签约后获取到的商户ID和账号ID和私钥
        NSString *partner = @"";
        NSString *sellerID = @"";
        NSString *privateKey = @"";
        
        // 2.生成订单
        // 2.1.创建订单对象
        Order *order = [[Order alloc] init];
        // 2.2.设置商户ID和账号ID
        order.partner = partner;
        order.sellerID = sellerID;
        
        // 2.3.设置订单号(公司自己的算法决定)
        order.outTradeNO = nil; //订单ID(由商家自行制定)
        // 2.4.设置商品相关的信息
        order.subject = product.subject; //商品标题
        order.body = product.body; //商品描述
        
        order.totalFee = [NSString stringWithFormat:@"%.2f",product.price]; //商品价格
        // 2.5.设置支付宝服务器回调我们服务器的URL
        order.notifyURL =  @"http://www.xxx.com"; //回调URL
        // 2.6.规定写法
        order.service = @"mobile.securitypay.pay";
        order.paymentType = @"1";
        order.inputCharset = @"utf-8";
        order.itBPay = @"10m";
        order.showURL = @"m.alipay.com";
        
        // 3.添加应用程序的URL Scheme
        NSString *urlScheme = @"huodadaAlipayDemo";
        
        // 4.将定义信息拼接成一个字符串
        NSString *orderString = [order description];
        
        // 5.对订单进行签名加密
        id<DataSigner> signer = CreateRSADataSigner(privateKey);
        NSString *signedString = [signer signString:orderString];
        
        // 6.将前面后的字符串格式化为固定格式的订单字符串
        NSString *signedOrderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                                       orderString, signedString, @"RSA"];
        
        // 7.调用支付宝客户端,让用户进行支付
        // 7.1.如果没有安装支付宝客户端,则会弹出webView让用户输入支付宝的账号和密码进行支付.如果是该情况,则会回调下面的block
        // 7.2.如果用户有安装支付宝客户端,则会跳转支付宝客户端进行支付
        [[AlipaySDK defaultService] payOrder:signedOrderString fromScheme:urlScheme callback:^(NSDictionary *resultDic) {
            NSLog(@"reslut = %@",resultDic);
        }];
        
    }
    
    

    然后再AppDelegate添加下面的代码即可

    - (BOOL)application:(UIApplication *)application
                openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication
             annotation:(id)annotation {
        //由于在跳转支付宝客户端支付的过程中,商户 app 在后台很可能被系统 kill 了,所以 pay 接口的 callback 就会失效,请商户对 standbyCallback 返回的回调结果进行处理,就是在这个方 法里面处理跟 callback 一样的逻辑
        
        if ([url.host isEqualToString:@"safepay"]) {
            //跳转支付宝钱包进行支付,处理支付结果
            [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
                NSLog(@"result = %@",resultDic);
            }];
        }
        return YES;
    }
    
    // 9.0以后使用新API接口
    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
    {
        if ([url.host isEqualToString:@"safepay"]) {
            //跳转支付宝钱包进行支付,处理支付结果
            [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
                NSLog(@"result = %@",resultDic);
            }];
        }
        return YES;
    }
    
    

    相关文章

      网友评论

          本文标题:iOS项目集成支付宝功能

          本文链接:https://www.haomeiwen.com/subject/ymgxsttx.html