相對來講不是很難,兩個sdk集成都只需要四個步驟就搞定了!
一、集成支付寶支付
1:導入SDK并添加依賴庫
把iOS包中的壓縮文件中以下文件拷貝到項目文件夾下,并導入到項目工程中:屏幕快照 2018-04-16 下午1.38.13.png
AlipaySDK.framework
AlipaySDK.bundle
在Build Phases選項卡的Link Binary With Libraries中,增加以下依賴:
屏幕快照 2018-04-16 下午1.40.42.png
2:在APPDelegate里邊添加代碼
引入頭文件
#import <AlipaySDK/AlipaySDK.h>
添加支付回調方法
- (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?:@"");
}];
}
//此處是微信支付
if ([url.scheme isEqualToString:@"wxf6e443649d826e8e"])
{
return [WXApi handleOpenURL:url delegate:(id<WXApiDelegate>)self];
}
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?:@"");
}];
}
//此處是微信支付
if ([url.scheme isEqualToString:@"wxf6e443649d826e8e"])
{
return [WXApi handleOpenURL:url delegate:(id<WXApiDelegate>)self];
}
return YES;
}
3.添加URL Scheme配置
在Targets -> Info 下最后一個找到URL Scheme,
點擊“Info”選項卡,在“URL Types”選項中,點擊“+”。
EDA5C4F6-1BC0-4F71-9180-EA31B227F0FB.png
4.在支付地方添加調起支付寶方法
引入頭文件
#import <AlipaySDK/AlipaySDK.h>
支付地方添加調起支付寶代碼
[[AlipaySDK defaultService] payOrder:@"此處是從后臺拿到的訂單簽名信息" fromScheme:@"這里邊填寫第三步配置的URL Scheme" callback:^(NSDictionary *resultDic) {
NSLog(@"=====%@",resultDic);
if ([resultDic[@"resultStatus"]intValue] == 9000) {
NSLog(@"成功");
} else {
NSLog(@"失敗");
}
}];
二、集成微信支付
1:導入SDK并添加依賴庫
屏幕快照 2018-04-16 下午1.59.49.png
記得添加這兩個配置 (畫重點)注意看官方Demo里邊的README,拿起小本子記下來
屏幕快照 2018-04-16 下午2.01.53.png
2:在APPDelegate里邊添加代碼
引入頭文件
#import <WXApi.h>
并添加回調代理
@interface AppDelegate ()<WXApiDelegate>
注冊微信
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WXApi registerApp:@"填寫申請的appid"];
return YES;
}
添加支付回調方法,上邊支付寶集成代碼里邊一樣的代碼
- (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?:@"");
}];
}
//此處是微信支付
if ([url.scheme isEqualToString:@"wxf6e443649d826e8e"])
{
return [WXApi handleOpenURL:url delegate:(id<WXApiDelegate>)self];
}
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?:@"");
}];
}
//此處是微信支付
if ([url.scheme isEqualToString:@"wxf6e443649d826e8e"])
{
return [WXApi handleOpenURL:url delegate:(id<WXApiDelegate>)self];
}
return YES;
}
添加微信支付回調代理方法
//微信回調,有支付結果的時候會回調這個方法
- (void)onResp:(BaseResp *)resp
{
// 支付結果回調
if([resp isKindOfClass:[PayResp class]]){
switch (resp.errCode) {
case WXSuccess:{
//支付返回結果,實際支付結果需要去自己的服務器端查詢
NSNotification *notification = [NSNotification notificationWithName:@"ORDER_PAY_NOTIFICATION" object:@"success"];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
default:{
NSNotification *notification = [NSNotification notificationWithName:@"ORDER_PAY_NOTIFICATION"object:@"fail"];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
}
}
}
3.添加URL Scheme配置
在Targets -> Info 下最后一個找到URL Scheme,
點擊“Info”選項卡,在“URL Types”選項中,點擊“+” 填寫申請的那個APPId
。
EDA5C4F6-1BC0-4F71-9180-EA31B227F0FB.png
4.在支付地方添加調起微信方法
引入頭文件
#import <WXApi.h>
支付地方添加調起微信代碼
if ([WXApi isWXAppInstalled]) {
NSLog(@"已經安裝了微信...");
//這里調用后臺接口獲取訂單的詳細信息,然后調用微信支付方法
}else{
}
#pragma mark 微信支付方法
- (void)WXPayWithAppid:(NSString *)appid partnerid:(NSString *)partnerid prepayid:(NSString *)prepayid package:(NSString *)package noncestr:(NSString *)noncestr timestamp:(NSString *)timestamp sign:(NSString *)sign{
//需要創建這個支付對象
PayReq *req = [[PayReq alloc] init];
//由用戶微信號和AppID組成的唯一標識,用于校驗微信用戶
req.openID = appid;
// 商家id,在注冊的時候給的
req.partnerId = partnerid;
// 預支付訂單這個是后臺跟微信服務器交互后,微信服務器傳給你們服務器的,你們服務器再傳給你
req.prepayId = prepayid;
// 根據財付通文檔填寫的數據和簽名
req.package = package;
// 隨機編碼,為了防止重復的,在后臺生成
req.nonceStr = noncestr;
// 這個是時間戳,也是在后臺生成的,為了驗證支付的
NSString * stamp = timestamp;
req.timeStamp = stamp.intValue;
// 這個簽名也是后臺做的
req.sign = sign;
if ([WXApi sendReq:req]) { //發送請求到微信,等待微信返回onResp
NSLog(@"吊起微信成功...");
}else{
NSLog(@"吊起微信失敗...");
}
}
自此,大功告成
ps:期間微信遇到問題是微信的支付sdk和最早之前集成的sharesdk里邊的微信登錄分享有沖突,找了半天,解決辦法是把最新的支付的sdk替換掉sharesdk里邊的關于微信的sdk,然后解決了。
這里是ShareSdk官方給出的解決方案