## 第三方支付接入
### 支付功能申请链接
[微信开放平台](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];