iOS支付相关iOS 进阶知识积累

ios继承三大第三方支付功能

2016-04-25  本文已影响818人  菜鸟晋升路

最近在做的一个项目需要添加第三方支付功能,我之前也没做过这样的功能,但没办法只能硬着头皮边学边做啦。
等我把这三个功能做完之后发现实现起来并不难,三个都有许多共性。最难的算支付宝功能,其次是微信支付功能。
在实现支付宝功能的时候碰到很多坑,它们官方文档写的不太清晰,导致我花了很多时间去一个一个解决掉它。
你在添加完它们的库之后运行代码肯定会报一堆的错误,碰到这种情况你不要慌,因为openssl库这个库是用c语言写的所以你需要适配引入oc头文件。
直接代码吧


 Product *product=[[Product alloc]init];
    product.price=0.01f;
    /*============================================================================*/
    /*=======================需要填写商户app申请的===================================*/
    /*============================================================================*/
    //以下数据要去支付宝开发平台申请
    NSString *partner = @"";
    NSString *seller = @"";
    NSString *privateKey =@"";
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.seller = seller;
    order.tradeNO = [self generateTradeNO]; //订单ID(由商家自行制定)
    order.productName = product.subject; //商品标题
    order.productDescription = product.body; //商品描述
    order.amount = [NSString stringWithFormat:@"%.2f",product.price]; //商品价格
    order.notifyURL =  @"http://www.xxx.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 = @"alisdkdemo";
    
    //将商品信息拼接成字符串
    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);
        }];
    }

以上是实现支付宝支付功能的代码,其中partner,seller,privateKey这三个字段是向支付宝开发者平台申请之后才能拿到的,最坑是支付宝都没有提供测试帐号,所以它的官方demo试运行不了的。还有支付宝支付成功回调的时候要特别注意一下:
ios9和ios9之前的系统回调方法是不一样的。

#pragma mark ios9之前支付宝支付成功后回调方法
- (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);
        }];
    }
    //回调微信支付
    [WXApi handleOpenURL:url delegate:self];
    //银联回调
    
    return YES;
}
 #pragma mark ios9支付宝支付成功后回调方法
- (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);
        }];
    }
    NSLog(@"银联结果处理中!");
    
    return YES;
}

微信和银联支付我就不写了,这个不难直接去看我的demo(记得给星)
demo

上一篇 下一篇

猜你喜欢

热点阅读