支付宝APP支付

作者: DamoLong | 来源:发表于2016-12-27 10:24 被阅读0次

    一 商家和支付宝签约,获取取partnerID

    • 搜索支付宝商家,登录商家中心
    1.png
    • 点击签约订单,即可查看


      2.png

    二.进入合作伙伴秘钥管理,给支付宝平台设置公钥,图中为设置完成样式,秘钥的生成在开发文档中有具体说明,不在阐述.

    4.png

    三.APP支付是需要签约才可使用,需到产品大全APP支付中签约

    5.png ![7.png](https://img.haomeiwen.com/i2810736/71d983f53de46e6f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    • 签约需要上传公司资质还有一份APP的说明文档,文档可以问产品要

    四 iOS集成

    • 进入支付宝开放平台中的文档中心,选择APP支付
    ![11.png](https://img.haomeiwen.com/i2810736/85449cf1557cf366.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    • 集成时使用新版,进入支付宝页面时总是提示我系统繁忙,所以用的是老版本的移动支付(主要是一个order文件的不一致,后面会介绍),哪位大神知道什么原因可以留言我.


      8.png
    9.png
    具体代码如下
     (void)doAlipayPay
    {
        //和新版本的区别是新版班需要到开放平台注册应用,获取appID
        /*
    //这里只是为了方便直接向商户展示支付宝的整个支付流程;所以Demo中加签过程直接放在客户端完成;
        //真实App里,privateKey等数据严禁放在客户端,加签过程务必要放在服务端完成;
        //防止商户私密数据泄露,造成不必要的资金损失,及面临各种安全风险;
       { 
        /*============================================================================*/
        /*=======================新版本需要填写商户app申请的,括号这部分不用集成===================================*/
        /*============================================================================*/
        NSString *appID = @"2016***24";
        NSString *privateKey = @"***"; //注意由于字符过长换行时需加上转义字符"/"
        /*============================================================================*/
        }
        /*============================================================================*/
        /*=======================老版本需要填写商户app申请的===================================*/
        /*============================================================================*/
        NSString *partner = @"2088***14";
        NSString *seller = @"2088***14";
        NSString *privateKey = @"**";
        /*============================================================================*/
        /*============================================================================*/
        /*============================================================================*/
        
        //partner和seller获取失败,提示
        if ([partner length] == 0 ||
            [seller length] == 0 ||
            [privateKey length] == 0)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"缺少partner或者seller或者私钥。"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            [alert show];
            return;
        }
        
        /*
         *生成订单信息及签名
         */
        //将商品信息赋予AlixPayOrder的成员变量
        Order *order = [[Order alloc] init];
        order.partner = partner;
        order.sellerID = seller;
        order.outTradeNO = [self generateTradeNO]; //订单ID(由商家自行制定)
        order.subject = @"1"; //商品标题
        order.body = @"测试"; //商品描述
        order.totalFee = [NSString stringWithFormat:@"%.2f",0.01]; //商品价格
        order.notifyURL =  @"https://www.baidu.com"; //回调URL
        
        order.service = @"mobile.securitypay.pay";
        order.paymentType = @"1";
        order.inputCharset = @"utf-8";
        order.itBPay = @"30m";
        order.showURL = @"m.alipay.com";
        
        //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
        NSString *appScheme = @"AirBk2016";
        
        //将商品信息拼接成字符串
        NSString *orderSpec = [order description];
        NSLog(@"orderSpec = %@",orderSpec);
        
        //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
        id<DataSigner> signer = CreateRSADataSigner(privateKey);
        NSString *signedString = [signer signString:orderSpec];
        
        //将签名成功字符串格式化为订单字符串,请严格按照该格式
        NSString *orderString = nil;
        if (signedString != nil) {
            orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                           orderSpec, signedString, @"RSA"];
            
            [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
                NSLog(@"reslut = %@",resultDic);
            }];
        }
    
    }
    
    - (NSString *)generateTradeNO
    {
        static int kNumber = 15;
        
        NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        NSMutableString *resultStr = [[NSMutableString alloc] init];
        srand((unsigned)time(0));
        for (int i = 0; i < kNumber; i++)
        {
            unsigned index = rand() % [sourceStr length];
            NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
            [resultStr appendString:oneStr];
        }
        return resultStr;
    }
    

    代理方法中添加

    - (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) {
                NSLog(@"result = %@",resultDic);
            }];
        }
        return YES;
    }
    
    // NOTE: 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;
    }
    
    

    相关文章

      网友评论

        本文标题:支付宝APP支付

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