iOS开发指南iOS开发随笔支付功能

iOS支付宝、微信的回调问题解决办法

2015-10-12  本文已影响16139人  Raybon_lee

以前我们使用的回调是这个方法
支付宝钱包支付接口开发包2.0标准版(iOS 2.2.1)


补充:

我的猜测: SDK 有一个是notify_url 想必这个在预请求订单消息的时候可以和后台商量一下,加进去,在发送订单交易的时候微信支付完成,后台完成对这个URL 的请求回调,APP,我想者应该能收到回调数据的,如果猜测正确,还望朋友能够及时联系我,把这个文章补充完整。谢谢朋友们的支持!


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    
    return YES;
}

最近稍微研究了一下这个,因为我也很少接触这个,帮朋友分析了一下,
系统目前提供使用新的方法

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url NS_DEPRECATED_IOS(2_0, 9_0, "Please use application:openURL:options:");

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation NS_DEPRECATED_IOS(4_2, 9_0, "Please use application:openURL:options:");

NS_DEPRECATED_IOS(4_2, 9_0, "Please use application:openURL:options:");
这个意思是使用版本是4.2,支持到版本9.0  

目前支付宝的回调需要使用这个新方法也就是下面这种写法,都加上注释了,

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
/*
 9000 订单支付成功
 8000 正在处理中
 4000 订单支付失败
 6001 用户中途取消
 6002 网络连接出错
 */
    if ([url.host isEqualToString:@"safepay"]) {
        //这个是进程KILL掉之后也会调用,这个只是第一次授权回调,同时也会返回支付信息
        [[AlipaySDK defaultService]processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
            NSString * str = resultDic[@"result"];
            NSLog(@"result = %@",str);
        }];
        //跳转支付宝钱包进行支付,处理支付结果,这个只是辅佐订单支付结果回调
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSString * query = [[url query] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            id<DataVerifier> dataVeri = CreateRSADataVerifier(@"public");
            //验证签名是否一致
            if ([dataVeri verifyString:@"22" withSign:@"ee"]) {
                
            }
            NSLog(@"result = %@",resultDic);
            NSString * str = resultDic[@"memo"];
            NSLog(@"memo = %@",str);
            
        }];
        
    }else if ([url.host isEqualToString:@"platformapi"]){
        //授权返回码
        [[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
            
        }];
    }
    
    
    return YES;
}

使用之前的handleopenurl 这个代理方法,我们百度的可能还有其他解决办法,
一、最有效的方法好像是把工程中得info文件中得
Main storyboard file base name
删除后边的value,就可以回调了
二、第二种就是采用支付宝推荐的使用sourceApplication 这个方法进行回调,

模拟支付行为代码

#pragma mark -
#pragma mark   ==============点击订单模拟支付行为==============
//
//选中商品调用支付宝极简支付
//
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    /*
     *点击获取prodcut实例并初始化订单信息
     */
    Product *product = [self.productList objectAtIndex:indexPath.row];
    
    /*
     *商户的唯一的parnter和seller。
     *签约后,支付宝会为每个商户分配一个唯一的 parnter 和 seller。
     */
    
/*============================================================================*/
/*=======================需要填写商户app申请的===================================*/
/*============================================================================*/
    NSString *partner = @"";
    NSString *seller = @"";
    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.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);
        }];
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

授权信息

- (NSString *)description
{
    if (self.appID.length != 16||self.pid.length != 16) {
        return nil;
    }
    NSArray *decriptionArray = @[[NSString stringWithFormat:@"app_id=\"%@\"", self.appID],
                                 [NSString stringWithFormat:@"pid=\"%@\"", self.pid],
                                 [NSString stringWithFormat:@"apiname=\"%@\"", self.apiName?self.apiName:@"com.alipay.account.auth"],
                                 [NSString stringWithFormat:@"app_name=\"%@\"", self.appName?self.appName:@"mc"],
                                 [NSString stringWithFormat:@"biz_type=\"%@\"", self.bizType?self.bizType:@"openservice"],
                                 [NSString stringWithFormat:@"product_id=\"%@\"", self.productID?self.productID:@"WAP_FAST_LOGIN"],
                                 [NSString stringWithFormat:@"scope=\"%@\"", self.scope?self.scope:@"kuaijie"],
                                 [NSString stringWithFormat:@"target_id=\"%@\"", self.targetID?self.targetID:@"20141225xxxx"],
                                 [NSString stringWithFormat:@"auth_type=\"%@\"", self.authType?self.authType:@"AUTHACCOUNT"],
                                 [NSString stringWithFormat:@"sign_date=\"%@\"", self.signDate?self.signDate:@"2014-12-25 00:00:00"],
                                 [NSString stringWithFormat:@"service=\"%@\"", self.service?self.service:@"mobile.securitypay.pay"]];
    
    return [decriptionArray componentsJoinedByString:@"&"];
    
}


对这个地方的写法是不是新手都有疑问呢,说实话,我这也有疑问

//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];

对于id < DataSigner > 我的理解是对于生成的对象要遵循一种协议,也是C的写法,还希望大神看到回复解释一下,这种原理,以前看百度地图的时候看到过

还有个地方需要主要的,验证公钥,这里我只是随便写的,我最近看到公司项目这一块了,就记录一下,以便能帮助更多新手,提高效率

 //跳转支付宝钱包进行支付,处理支付结果,这个只是辅佐订单支付结果回调
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSString * query = [[url query] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            id<DataVerifier> dataVeri = CreateRSADataVerifier(@"public");
            //验证签名是否一致
            if ([dataVeri verifyString:@"22" withSign:@"ee"]) {
                
            }
            NSLog(@"result = %@",resultDic);
            NSString * str = resultDic[@"memo"];
            NSLog(@"memo = %@",str);
            
        }];

上一篇 下一篇

猜你喜欢

热点阅读