1.支付寶支付
1.1 開發(fā)環(huán)境設(shè)置
1.1.1 添加
AlipaySDK.bundle,
AlipaySDK.framework,
到項目
1.1.2 依賴庫:
libz.tbd,
libc++.tbd,
SystemConfiguration.framework,
CoreTelephony.framework,
QuartzCore.framework,
CoreText.framework,
CoreGraphics.framework,
CoreMotion.framework,
CFNetwork.framework,
AlipaySDK.framework
1.1.3 TARGETS 的 info 中的 URL types 添加 URL schemes (即appId)
1.2 調(diào)起支付
支付頁面:
#import <AlipaySDK/AlipaySDK.h>
NSString *orderString = model.data.payResult;//支付寶支付后臺返回的訂單號
NSString *appScheme = URL_SCHEMES_NAME;//對應(yīng)的支付寶的URL Schemes的名稱;
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {//用于wap支付結(jié)果回調(diào) (非跳轉(zhuǎn)錢包支付)
NSLog(@"result = %@",resultDic);
NSString *resultStatus = [resultDic objectForKey:@"resultStatus"];
//通過觀察者觀察支付結(jié)果調(diào)用對應(yīng)方法
switch ([resultStatus integerValue]) {
case 9000://訂單支付成功
[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange_success_alipay" object:nil];
break;
//case 6001://用戶中途取消
//[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
//break;
//case 4000://訂單支付失敗
//[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
//break;
//case 5000://重復(fù)請求
//[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
//break;
default:
[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
break;
}
}];
1.3 支付結(jié)果回調(diào)
AppDelegate頁面
#import <AlipaySDK/AlipaySDK.h>
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
if ([url.host isEqualToString:@"safepay"]) {
//跳轉(zhuǎn)支付寶錢包進行支付,處理支付結(jié)果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {//處理錢包或者獨立快捷app支付跳回商戶app攜帶的支付結(jié)果
NSLog(@"result = %@",resultDic);
NSString *resultStatus = [resultDic objectForKey:@"resultStatus"];
//通過觀察者觀察支付結(jié)果調(diào)用對應(yīng)方法
switch ([resultStatus integerValue]) {
case 9000://訂單支付成功
[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange_success_alipay" object:nil];
break;
//case 6001://用戶中途取消
//[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
//break;
//case 4000://訂單支付失敗
//[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
//break;
//case 5000://重復(fù)請求
//[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
//break;
default:
[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
break;
}
}];
}
return YES;
}
// 支持所有iOS系統(tǒng)(8.0)
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if ([url.host isEqualToString:@"safepay"]) {
//跳轉(zhuǎn)支付寶錢包進行支付酗洒,處理支付結(jié)果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
NSString *resultStatus = [resultDic objectForKey:@"resultStatus"];
//通過觀察者觀察支付結(jié)果調(diào)用對應(yīng)方法
switch ([resultStatus integerValue]) {
case 9000://訂單支付成功
[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange_success_alipay" object:nil];
break;
//case 6001://用戶中途取消
//[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
//break;
//case 4000://訂單支付失敗
//[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
//break;
//case 5000://重復(fù)請求
//[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
//break;
default:
[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
break;
}
}];
}
return YES;
}
2.微信支付
2.0 開發(fā)環(huán)境設(shè)置
2.0.1 將WeChatSDK
下所有文件添加到項目中
2.0.2 依賴庫:
SystemConfiguration.framework,
libz.tbd,
libsqlite3.0.tbd,
libc++.tbd,
Security.framework,
CoreTelephony.framework,
CFNetwork.framework
2.0.3 Build Setting 中的 Other Linker Flags
添加 “-ObjC
”
2.0.4 Build Setting 中的 Library Search Paths
添加libWeChatSDK.a,WXApi.h,WXApiObject.h
文件所在位置
2.0.5 TARGETS 的 info 中的 URL types 添加 URL schemes (即appId)
2.0.6 TARGETS 的 info 中的 “LSApplicationQueriesSchemes
”添加weixin
2.0.7 TARGETS 的 info 中的 “LSApplicationQueriesSchemes
”添加weixinULAPI
//SDK 1.8.6版本新加
AppDelegate頁面
#import "WXApi.h"
<WXApiDelegate>
2.1 向微信注冊appId
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//微信支付
//舊版注冊方法
[WXApi registerApp:WXKey enableMTA:NO];
//SDK 1.8.6版本注冊方法 universalLink 需要在微信開放平臺設(shè)置
[WXApi registerApp:WXKey universalLink:universalLink];
return YES;
}
2.2 重寫AppDelegate的handleOpenURL和openURL方法
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
[WXApi handleOpenURL:url delegate:self];
return YES;
}
// 支持所有iOS系統(tǒng)(8.0)
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
[WXApi handleOpenURL:url delegate:self];
return YES;
}
2.3 調(diào)起支付
支付頁面
#import "WXApi.h"
把后臺返回的預(yù)支付結(jié)果裝在PayReq后調(diào)用- (BOOL) sendReq:(BaseReq*)req
發(fā)送給微信
WeChatPayDataPayResultModel *data = model.data.payResult;//微信預(yù)支付后臺返回JSON
PayReq* req = [[PayReq alloc] init];
req.partnerId = data.partnerid;//商戶號
req.prepayId = data.prepayid;//預(yù)支付交易會話ID
req.nonceStr = data.noncestr;//隨機字符串
req.timeStamp = data.timestamp.intValue;//時間戳
req.package = data.package;//擴展字段
req.sign = data.sign;//簽名
[WXApi sendReq:req];
2.4 支付結(jié)果回調(diào) —> 如果APP向微信發(fā)送了sendReq請求士修,則onResp會被回調(diào)
- (void)onResp:(BaseResp *)resp
{
//支付返回結(jié)果,實際支付結(jié)果需要去微信服務(wù)器端查詢
NSString *strMsg = [NSString stringWithFormat:@"支付結(jié)果"];
//通過觀察者觀察支付結(jié)果調(diào)用對應(yīng)方法
switch (resp.errCode) {//根據(jù)返回結(jié)果進行處理
case WXSuccess:
strMsg = @"支付結(jié)果:成功樱衷!";
NSLog(@"支付成功-PaySuccess棋嘲,retcode = %d", resp.errCode);
[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange_success_wechat" object:nil];
break;
//case WXErrCodeUserCancel:
//strMsg = [NSString stringWithFormat:@"支付結(jié)果:取消訂單!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
//NSLog(@"取消訂單矩桂,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
//[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
//break;
default:
strMsg = [NSString stringWithFormat:@"支付結(jié)果:失敺幸啤!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
NSLog(@"錯誤,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
[[NSNotificationCenter defaultCenter] postNotificationName:@"controllerChange" object:nil];
break;
}
}
3.遇見問題
3.1 sendReq 調(diào)用失敗
微信開放平臺新使用支付功能用戶 填寫相關(guān)信息中 Universal Link選項為必填
對應(yīng)設(shè)置:
1.TARGETS 的 info 中的 “LSApplicationQueriesSchemes
”添加weixinULAPI
2.[WXApi registerApp:WXKey universalLink:universalLink];
//universalLink
不能設(shè)為nil
或@”“
3.2 支付驗證簽名失敗
后臺設(shè)置的prepayid
等參數(shù)名與官方要求的不相同
如:后臺設(shè)置為prepay_id
官方為prepayId
導(dǎo)致傳值時無法對應(yīng) 出現(xiàn)傳空
3.3 支付寶支付成功不返回app
原因: 接入支付寶支付URL Types 填寫的URL Schemes 為純數(shù)字
iOS接入支付寶支付URL Types 填寫的URL Schemes 不能為純數(shù)字