iOS支付宝支付方法及注意点

支付宝支付(接网页端JS)


//支付宝支付-该方法由js(网页)端调用
-(void)iosFuncALi:(NSString *)iosFuncString
{
    NSString *appScheme = @"alipayapp";
    
    NSData * data = [iosFuncString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary * orderDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSString * orderString = orderDic[@"msg"];
    
    //服务端自动转码,通过HTML转义回来才是正常的网址
    orderString = [self HTML:orderString];
    
    /*
     9000 订单支付成功
     8000 正在处理中
     4000 订单支付失败
     6001 用户中途取消
     6002 网络连接出错
     */
    
    [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
        //需要在AppDelegate设置里设置processOrderWithPaymentResult方法,该回调才会执行
        NSLog(@"reslut = %@",resultDic);
        
        //传给后端并展示,该方法配合js
        JSValue * sendWXPayResultCode = self.jsContext[@"iosReturn"];
        [sendWXPayResultCode callWithArguments:resultDic[@"resultStatus"]];
        
        
    }];
    
}


//html转义
- (NSString *)HTML:(NSString *)html

{
    
    NSScanner *theScaner = [NSScanner scannerWithString:html];
    
    NSDictionary *dict = @{@"&amp":@"&", @"<":@"<", @">":@">", @" ":@"", @""":@"\"", @"width":@"wid"};
    
    while ([theScaner isAtEnd] == NO) {
        
        for (int i = 0; i <[dict allKeys].count; i ++) {
            
            [theScaner scanUpToString:[dict allKeys][i] intoString:NULL];
            
            html = [html stringByReplacingOccurrencesOfString:[dict allKeys][i] withString:[dict allValues][i]];
            
        }
        
    }
    
    return html;
    
}
  • payOrder: fromScheme:方法要回调必须在AppDelegate的- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options里写入processOrderWithPaymentResult才会执行,不然不会回调。
  • 网址经过服务器端后部分符号被转义(如&amp会被转换成&),需使用HTML转义换成正常的网址,才能使用。

AppDelegate


//微信支付回调
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options
{
 
    if ([url.host isEqualToString:@"safepay"]) {
        //一般只需要调用这一个方法即可
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result1 = %@",resultDic);
//            [[NSNotificationCenter defaultCenter] postNotificationName:kAliPayCallBack object:resultDic];
            
        }];
        [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result2 = %@",resultDic);
            NSString *resultStr = resultDic[@"result"];
            NSLog(@"result3 = %@",resultStr);
        }];
    } else if ([url.host isEqualToString:@"platformapi"]){
        //支付宝钱包快登授权返回 authCode
        [[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result4 = %@",resultDic);
        }];
    }
    
    //此处为微信支付回调,与支付宝无关
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}

  • processOrderWithPaymentResult此方法执行则支付方法payOrder: fromScheme:回调执行

参考:蚂蚁金服-开放平台

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容