## 第三方支付接入
### 支付功能申請鏈接
[微信開放平臺](https://open.weixin.qq.com/)
[支付寶開放平臺(螞蟻金服開放平臺)](https://open.alipay.com/developmentAccess/developmentAccess.htm)
### 接入教程
[iOS支付寶集成詳細流程](http://www.jianshu.com/p/8a7cf98ea8e7)
[iOS應用之微信支付集成](http://www.jianshu.com/p/94dcc220b2aa)
### URL Schemes及AppKey等配置
[設置第三方AppKey等](設置第三方AppKey等.md)
### 代碼
##### AppDelegate.m
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
@interface AppDelegate () <WXApiDelegate>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WXApi registerApp:WEIXINAPPID]; //初始化微信SDK
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
if ([url.host isEqualToString:@"safepay"]) {
//跳轉支付寶錢包進行支付,處理支付結果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
//返回支付結果
[[NSNotificationCenter defaultCenter] postNotificationName:@"returnPay" object:nil userInfo:resultDic];
}];
} else {
if([url.host isEqualToString:@"qzapp"])
{
return [TencentOAuth HandleOpenURL:url];
}
else if ([url.host isEqualToString:@"oauth"]){
return [WXApi handleOpenURL:url delegate:self];
}
else
{
return [WeiboSDK handleOpenURL:url delegate:self];
}
}
return YES;
}
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
// return [WeiboSDK handleOpenURL:url delegate:self];
if ([url.host isEqualToString:@"safepay"]) {
//跳轉支付寶錢包進行支付,處理支付結果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
//返回支付結果
[[NSNotificationCenter defaultCenter] postNotificationName:@"returnPay" object:nil userInfo:resultDic];
}];
}else if([url.host isEqualToString:@"qzapp"]){
return [TencentOAuth HandleOpenURL:url];
}
else if ([url.host isEqualToString:@"oauth"]){
return [WXApi handleOpenURL:url delegate:self];
}
else
{
return [WeiboSDK handleOpenURL:url delegate:self];
}
return YES;
}
##### 執行頁面.m
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
//支付寶支付
[[AlipaySDK defaultService ] payOrder:info fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSString *resultStatus=[resultDic objectForKey:@"resultStatus"];
if ([resultStatus isEqualToString:@"9000"]) {
NSLog(@"支付成功");
} else{
NSLog(@"支付失敗");
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"returnPay" object:nil userInfo:resultDic];
}];
//微信支付
NSDictionary *data = [result objectForKey:@"data"][@"orderString"];
PayReq* req = [[PayReq alloc] init];
req.openID = [data objectForKey:@"appid"];
//商戶號
req.partnerId = [data objectForKey:@"mch_id"];
//預支付交易id
req.prepayId = [data objectForKey:@"prepay_id"];
//隨機字符串
req.nonceStr = [data objectForKey:@"nonce_str"];
//當前時間
NSDate *datenow = [NSDate date];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
UInt32 timeStamp =[timeSp intValue];
req.timeStamp = timeStamp;
req.package =@"Sign=WXPay";
//MD5加密之后的簽名
req.sign = [data objectForKey:@"sign"];
[WXApi sendReq:req];