1、微信支付
1)准备
通过cocoapods安装:
#微信支付
pod 'WechatOpenSDK', '~> 1.8.1’
在微信开放平台注册应用,获取APPKey
在plist文件中添加白名单,见文章末尾
在Info中添加URL Types,URL Schemes 为appkey
2)在APPDelegate中导入头文件
#import <WXApi.h>
#import <WXApiObject.h>
遵守协议<WXApiDelegate>
注册微信支付
// 注册微信支付
[WXApi registerApp:kAppKey_Wechat];
3)iOS9之后重写以下方法:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {
// 用于极光分享,可以忽略
BOOL result = [JSHAREService handleOpenUrl:url];
//6.3的新的API调用,是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
// BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url options:options];
if (!result) {
// 其他如支付等SDK的回调
if ([url.host isEqualToString:@"safepay"]) {
//跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
if ([resultDic[@"resultStatus"] isEqualToString:@"9000"]) {
[SVProgressHUD showSuccessWithStatus:@"支付成功"];
} else {
}
}];
return YES;
} else if ([url.scheme isEqualToString:kAppKey_Wechat] && [url.host isEqualToString:@"pay"]) {
return [WXApi handleOpenURL:url delegate:self];
}
return result;
}
4)实现微信支付代理方法:
#pragma mark - WXApiDelegate
-(void) onReq:(BaseReq*)req {
DLog(@"---=-=-=-=-==");
}
-(void)onResp:(BaseResp *)resp {
if ([resp isKindOfClass:[PayResp class]]) {
PayResp * response = (PayResp *)resp; // 微信终端返回给第三方的关于支付结果的结构体
switch (response.errCode) {
case WXSuccess:
{// 支付成功,向后台发送消息
DLog(@"支付成功");
[SVProgressHUD showSuccessWithStatus:@"支付成功"];
}
break;
case WXErrCodeCommon:
{ //签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等
DLog(@"支付失败");
}
break;
case WXErrCodeUserCancel:
{ //用户点击取消并返回
DLog(@"取消支付");
kPostNotificationWithName(@"userCanclePay");
}
break;
case WXErrCodeSentFail:
{ //发送失败
DLog(@"发送失败");
}
break;
case WXErrCodeUnsupport:
{ //微信不支持
DLog(@"微信不支持");
}
break;
case WXErrCodeAuthDeny:
{ //授权失败
DLog(@"授权失败");
}
break;
default:
break;
}
}
}
5)在支付页导入头文件,通过接口获取微信支付的参数,并赋值给消息结构体:PayReq
if ([WXApi isWXAppInstalled]) {
NSDictionary * msgDic = result[@"wxPayInfo"];
PayReq *req = [[PayReq alloc] init];
req.openID = kAppKey_Wechat;
req.partnerId = msgDic[@"partnerid"];
req.prepayId = msgDic[@"prepayid"];
req.package = msgDic[@"package"];
req.nonceStr = msgDic[@"noncestr"];
NSString * stamp = msgDic[@"timestamp"];
req.timeStamp = stamp.intValue;
req.sign = msgDic[@"sign"];
if ([WXApi sendReq:req]) {
DLog(@"吊起成功");
} else {
DLog(@"吊起失败");
}
} else {
[SVProgressHUD showInfoWithStatus:@"该手机没有安装微信"];
}
2、支付宝支付
1)准备
导入AlipaySDK
在蚂蚁金服开放平台注册应用,获取APPKey
在Info中添加URL Types,URL Schemes 为自己书写,保证唯一
导入依赖库:
其中,需要注意的是:
- 如果是Xcode 7.0之后的版本,需要添加libc++.tbd、libz.tbd;
- 如果是Xcode 7.0之前的版本,需要添加libc++.dylib、libz.dylib(如下图)。
在APPDelegate中导入头文件
#import <AlipaySDK/AlipaySDK.h>
2)重写openURL方法,见上文微信支付第4步
3)在支付页通过接口获取支付宝支付的参数,然后调用支付宝接口
// 注意,这里的Scheme参数就是Info中URL Types中填写的URL Schemes
NSString * msgStr = result[@"aliData"];
[[AlipaySDK defaultService] payOrder:msgStr fromScheme:@"alipayforlantingzhibo" callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
if ([resultDic[@"resultStatus"] isEqualToString:@"9000"]) {
[SVProgressHUD showSuccessWithStatus:@"支付成功"];
} else {
}
}];
附:白名单:(部分)
<key>LSApplicationQueriesSchemes</key>
<array>
<string>wechat</string>
<string>weixin</string>
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<string>mqqapi</string>
<string>mqq</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqqconnect</string>
<string>mqqopensdkdataline</string>
<string>mqqopensdkgrouptribeshare</string>
<string>mqqopensdkfriend</string>
<string>mqqopensdkapi</string>
<string>mqqopensdkapiV2</string>
<string>mqqopensdkapiV3</string>
<string>mqqopensdkapiV4</string>
<string>mqzoneopensdk</string>
<string>wtloginmqq</string>
<string>wtloginmqq2</string>
<string>mqqwpa</string>
<string>mqzone</string>
<string>mqzonev2</string>
<string>mqzoneshare</string>
<string>wtloginqzone</string>
<string>mqzonewx</string>
<string>mqzoneopensdkapiV2</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapi</string>
<string>mqqbrowser</string>
<string>mttbrowser</string>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
<string>twitter</string>
<string>twitterauth</string>
<string>JChatPro</string>
</array>