支付寶支付(接網(wǎng)頁(yè)端JS)
//支付寶支付-該方法由js(網(wǎng)頁(yè))端調(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)碼已维,通過(guò)HTML轉(zhuǎn)義回來(lái)才是正常的網(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 = @{@"&":@"&", @"<":@"<", @">":@">", @" ":@"", @""":@"\"", @"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
里寫(xiě)入processOrderWithPaymentResult
才會(huì)執(zhí)行,不然不會(huì)回調(diào)。
- 網(wǎng)址經(jīng)過(guò)服務(wù)器端后部分符號(hào)被轉(zhuǎn)義(如
&
會(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)蛀骇,與支付寶無(wú)關(guān)
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
-
processOrderWithPaymentResult
此方法執(zhí)行則支付方法payOrder: fromScheme:
回調(diào)執(zhí)行
參考:螞蟻金服-開(kāi)放平臺(tái)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者