iOS支付寶支付方法及注意點(diǎn)

支付寶支付(接網(wǎng)頁端JS)


//支付寶支付-該方法由js(網(wǎng)頁)端調(diào)用
-(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"];
    
    //服務(wù)端自動(dòng)轉(zhuǎn)碼,通過HTML轉(zhuǎn)義回來才是正常的網(wǎng)址
    orderString = [self HTML:orderString];
    
    /*
     9000 訂單支付成功
     8000 正在處理中
     4000 訂單支付失敗
     6001 用戶中途取消
     6002 網(wǎng)絡(luò)連接出錯(cuò)
     */
    
    [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
        //需要在AppDelegate設(shè)置里設(shè)置processOrderWithPaymentResult方法,該回調(diào)才會(huì)執(zhí)行
        NSLog(@"reslut = %@",resultDic);
        
        //傳給后端并展示,該方法配合js
        JSValue * sendWXPayResultCode = self.jsContext[@"iosReturn"];
        [sendWXPayResultCode callWithArguments:resultDic[@"resultStatus"]];
        
        
    }];
    
}


//html轉(zhuǎn)義
- (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:方法要回調(diào)必須在AppDelegate的- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options里寫入processOrderWithPaymentResult才會(huì)執(zhí)行,不然不會(huì)回調(diào)。
  • 網(wǎng)址經(jīng)過服務(wù)器端后部分符號(hào)被轉(zhuǎn)義(如&amp會(huì)被轉(zhuǎn)換成&),需使用HTML轉(zhuǎn)義換成正常的網(wǎng)址,才能使用。

AppDelegate


//微信支付回調(diào)
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options
{
 
    if ([url.host isEqualToString:@"safepay"]) {
        //一般只需要調(diào)用這一個(gè)方法即可
        [[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"]){
        //支付寶錢包快登授權(quán)返回 authCode
        [[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result4 = %@",resultDic);
        }];
    }
    
    //此處為微信支付回調(diào),與支付寶無關(guān)
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}

  • processOrderWithPaymentResult此方法執(zhí)行則支付方法payOrder: fromScheme:回調(diào)執(zhí)行

參考:螞蟻金服-開放平臺(tái)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容