一 、支付宝集成
支付宝官方集成文档
1、下载支付宝官方demo,将AlipaySDK.bundle、AlipaySDK.framework两个框架导入到项目中,如下图:
2、选择工程中 Build Phases 选项卡的 Link Binary With Libraries 中,增加以下依赖:
在Build Phases选项卡的Link Binary With Libraries中,增加以下依赖:
3、设置URL Schemes
4、AppDelegate设置,导入如下代码
- (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);
}];
// 授权跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
// 解析 auth code
NSString *result = resultDic[@"result"];
NSString *authCode = nil;
if (result.length>0) {
NSArray *resultArr = [result componentsSeparatedByString:@"&"];
for (NSString *subResult in resultArr) {
if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
authCode = [subResult substringFromIndex:10];
break;
}
}
}
NSLog(@"授权结果 authCode = %@", authCode?:@"");
}];
}else if([url.host isEqualToString:@"pay"]){
// return [WXApi handleOpenURL:url delegate:[WXApiManager shareInstance]];
}else{
}
return YES;
}
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
if ([url.host isEqualToString:@"safepay"]) {
// 支付跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
// 授权跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
// 解析 auth code
NSString *result = resultDic[@"result"];
NSString *authCode = nil;
if (result.length>0) {
NSArray *resultArr = [result componentsSeparatedByString:@"&"];
for (NSString *subResult in resultArr) {
if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
authCode = [subResult substringFromIndex:10];
break;
}
}
}
NSLog(@"授权结果 authCode = %@", authCode?:@"");
}];
}else if([url.host isEqualToString:@"pay"]){
// return [WXApi handleOpenURL:url delegate:[WXApiManager shareInstance]];
}else{
}
return YES;
}
5、在使用支付宝支付的页面,导入import <AlipaySDK/AlipaySDK.h>头文件,添加如下代码
orderString:提交订单后,服务器返回的编码
appScheme:应用注册scheme,在AliSDKDemo-Info.plist定义URL types
二 、微信集成
1 、下载并安装cocopods
#集成微信
pod 'WechatOpenSDK'
2 、 设置URL Schemes,“URL scheme”为你所注册的应用程序id
3 、 导入如下依赖库
4 、 AppDelegate设置,导入如下代码:
//导入头文件
#import "WXApi.h"
//向微信注册,发起支付必须注册
//要使你的程序启动后微信终端能响应你的程序,必须在代码中向微信终端注册你的id
[WXApi registerApp:@"xxxxxxx" enableMTA:YES];
5 、在使用微信支付的页面,先导入头文件,再调用如下代码:
6 、微信回调
//微信SDK自带的方法,处理从微信客户端完成操作后返回程序之后的回调方法,显示支付结果的
-(void)onResp:(BaseResp*)resp{
//启动微信支付的response
NSString *payResoult = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
if([resp isKindOfClass:[PayResp class]]){
//支付返回结果,实际支付结果需要去微信服务器端查询
switch (resp.errCode) {
case 0:
payResoult = @"支付结果:成功!";
break;
case -1:
payResoult = @"支付结果:失败!";
break;
case -2:
payResoult = @"用户已经退出支付!";
break;
default:
payResoult = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
break;
}
}
}
三 、 银联支付集成
1 、下载对应的ios开发包
下载的ios开发包,对应的文件结构如下图:
2 、 将对应的文件导入项目中,如图:
3 、 使用UPPaymentControl需要添加CFNetwork.framework、SystemConfiguration.framework 、libz、libPaymentControl.a到工程中,添加后如下图:
4 、 在工程info.plist设置中添加一个URL Types回调协议(在UPPayDemo工程中使用“UPPayDemo”作为协议),用于在支付完成后返回商户客户端。请注意URL Schemes需要是唯一的。
5 、 http请求设置(ats)在测试环境测试时,需要在工程对应的plist文件中添加NSAppTransportSecurity Dictionary 并同时设置里面NSAllowsArbitraryLoads 属性值为 YES,具体设置可参照以下截图:
注:发生产环境可删除此设置。向Apple发布正式版本时请删除此设置。
6 、添加协议白名单
7 、 银联支付调用代码:在需要调用支付控件接口的代码文件内引用头文件UPPaymentControl.h。
(注意:如果工程的compile source as 选项的值不是Objective–C++,则引用此头文件的文件类型都要改为.mm)
支付接口调用 ,如图:
如图,到此,支付宝、微信、银联支付到此结束!!!
如有问题还需要参照最新的官方文档。