一、支付寶支付
1、下載SDK:支付寶SDK下載地址
2、支付流程圖請好好看一看,你就會明白你應該做什么
好了,看完了發其實我們需要做的就是(ps:為了安全起見,簽名都在后臺處理):
? 調用支付寶支付接口
? 處理支付寶返回的支付結果
3、把下載的demo中以下文件(如圖)的導入到自己的工程中
4、在Build Phases選項卡的Link Binary With Libraries中,增加以下依賴: 添加支付寶依賴官方鏈接
5、Xcode設置URL scheme和LSApplicationQueriesSchemes
(LSApplicationQueriesSchemes中alipay是針對于支付寶的其他的是針對于微信和銀聯的,如果不繼承可以不添加,只添加alipay即可)
6、在需要的地方導入#import <AlipaySDK/AlipaySDK.h>,#import "Order.h",#import "RSADataSigner.h"
7、AppDelegate.m文件中,增加引用代碼:
//9.0前的方法
- (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?:@"");
}];
}
return YES;
}
//9.0后的方法
- (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?:@"");
}];
}
return YES;
}
8、根據訂單信息發起支付調用方法,orderString和appScheme。appScheme是app在info.plist注冊的scheme,建議用支付寶注冊的appid。orderstring是一個訂單的字符串,由后臺拼接生成的,還需要簽名的
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSDictionary *dic = resultDic;
NSString *resultStr = EncodeFormDic(dic, @"resultStatus");
NSLog(@"resultStr_%@",resultStr);
[self paymentResult:resultStr];
}];
-(void)paymentResult:(NSString *)resultd {
NSLog(@" = %@",resultd);
//結果處理
if ([resultd isEqualToString:@"9000"]) {
//支付寶回調顯示支付成功后調用自己的后臺的借口確認是否支付成功
[self payOrdCallback:@"1"];
}else if ([resultd isEqualToString:@"6001"]){
[self payCancel];
}else{
[self payFail];
}
}
9、支付寶集成過程中一些錯誤的解決辦法請參考文章
二、微信支付(官方開發文檔)
1、下載SDK:微信SDK下載地址
2、看完支付流程圖,你會我們要做的也是調起微信客戶端發起支付和處理支付結果,和支付寶基本一樣
3、導入SDK
4、導入依賴
1. SystemConfiguration.framework
2. libz.tbd
3. libsqlite3.0.tbd
4.CoreTelephony.framework
5. QuartzCore.framework
5、Xcode設置URL scheme和LSApplicationQueriesSchemes,參照支付寶,圖片中有微信的
6、在需要的地方導入#import "WXApi.h"
7、AppDelegate.m文件中,增加引用代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/**
* 向微信終端注冊ID,這里的APPID一般建議寫成宏,容易維護。@“測試demo”不需用管。這里的id是假的,需要改這里還有target里面的URL Type
*/
[WXApi registerApp:@"wxd930ea5d5a258f4f" withDescription:@"測試demo"];
return YES;
}
//9.0前的方法,為了適配低版本 保留
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [WXApi handleOpenURL:url delegate:self];
}
//9.0后的方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
//這里判斷是否發起的請求為微信支付,如果是的話,用WXApi的方法調起微信客戶端的支付頁面(://pay 之前的那串字符串就是你的APPID,)
return [WXApi handleOpenURL:url delegate:self];
}
//微信SDK自帶的方法,處理從微信客戶端完成操作后返回程序之后的回調方法,顯示支付結果的
-(void) onResp:(BaseResp*)resp
{
if ([resp isKindOfClass:[PayResp class]]) {
PayResp *response = (PayResp *)resp;
switch (response.errCode) {
case WXSuccess:
//服務器端查詢支付通知或查詢API返回的結果再提示成功
NSLog(@"支付成功");
//通過通知告訴支付界面該做哪些操作
[[NSNotificationCenter defaultCenter] postNotificationName:@"WEIXINPAYS" object:nil];
break;
case WXErrCodeCommon:
NSLog(@"支付失敗");
[[NSNotificationCenter defaultCenter] postNotificationName:@"WEIXINPAYF" object:nil];
break;
case WXErrCodeUserCancel:
NSLog(@"用戶取消");
[[NSNotificationCenter defaultCenter] postNotificationName:@"WEIXINPAYC" object:nil];
break;
default:
NSLog(@"支付結果:失敗!retcode = %d, retstr = %@", resp.errCode,resp.errStr);
break;
}
}
}
8、根據后臺返回數據發起支付,需要的參數在方法中有詳細注釋
- (void)gotoWXPay {
PayReq* req = [[PayReq alloc] init];
req.openID = @"wxf7c591d3fdd34070";//appid換成自己的
req.partnerId = @"1245663002";
req.prepayId = @"wx2015060917151801798122a30971214164";//預支付訂單ID
req.nonceStr = @"3Lnr21zhhGO2DwwFbI2amMq0cJcggLGe";//參與簽名的隨機字符串,隨機編碼,為了防止重復的,在后臺生成
req.timeStamp = 1433841320;//參與簽名的時間戳,也是在后臺生成的,為了驗證支付的
req.sign = @"B7C57CCAA2236764E281DA94966046B7";//簽名字符串,也是后臺做的
req.package = @"Sign=WXPay";//這個比較特殊,是固定的,只能是即req.package = Sign=WXPay
[WXApi sendReq:req];
}
在發起支付的界面做微信回調的監聽,并在監聽返回支付成功(也就是payOk:方法中)是調用自己后臺的接口再次確認是否真正支付成功
/* 微信支付回調監聽 */
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payOk:) name:@"WEIXINPAYS" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payCancel) name:@"WEIXINPAYC" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payFail) name:@"WEIXINPAYF" object:nil];
9、注意微信支付只能調用App進行支付,所以需要判斷
if ([WXApi isWXAppInstalled]) {//判斷是否有微信
[self gotoWXPay];//微信支付
}
三、銀聯支付(APP集成官網
1、下載SDK:銀聯SDK下載地址
2、支付流程圖
3、導入SDK中的UPPaymentControl.h和libPaymentControl.a文件
4、導入依賴
5、Xcode設置URL scheme和LSApplicationQueriesSchemes,參照支付寶
6、在需要的地方導入#import "UPPaymentControl.h"
7、AppDelegate.m文件中,增加引用代碼:
//9.0前的方法
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
//銀聯支付處理支付結果
if([url.host isEqualToString:@"uppayresult"]){
[[UPPaymentControl defaultControl] handlePaymentResult:url completeBlock:^(NSString *code, NSDictionary *data) {
if([code isEqualToString:@"success"]) {
// //如果想對結果數據驗簽,可使用下面這段代碼,但建議不驗簽,直接去商戶后臺查詢交易結果
// if(data != nil){
// //數據從NSDictionary轉換為NSString
// NSData *signData = [NSJSONSerialization dataWithJSONObject:data
// options:0
// error:nil];
// NSString *sign = [[NSString alloc] initWithData:signData encoding:NSUTF8StringEncoding];
//
// //此處的verify建議送去商戶后臺做驗簽,如要放在手機端驗,則代碼必須支持更新證書
// if([self verify:sign]) {
// //驗簽成功
// }
// else {
// //驗簽失敗
// }
// }
//
// //結果code為成功時,去商戶后臺查詢一下確保交易是成功的再展示成功
[[NSNotificationCenter defaultCenter] postNotificationName:@"YINLIANPAYS" object:nil];
}else if([code isEqualToString:@"fail"]) {
//交易失敗
[[NSNotificationCenter defaultCenter] postNotificationName:@"YINLIANPAYF" object:nil];
}else if([code isEqualToString:@"cancel"]) {
//交易取消
[[NSNotificationCenter defaultCenter] postNotificationName:@"YINLIANPAYC" object:nil];
}
}];
}
return YES;
}
//9.0后的方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
//銀聯支付處理支付結果
if([url.host isEqualToString:@"uppayresult"]){
[[UPPaymentControl defaultControl] handlePaymentResult:url completeBlock:^(NSString *code, NSDictionary *data) {
if([code isEqualToString:@"success"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"YINLIANPAYS" object:nil];
}else if([code isEqualToString:@"fail"]) {
//交易失敗
[[NSNotificationCenter defaultCenter] postNotificationName:@"YINLIANPAYF" object:nil];
}else if([code isEqualToString:@"cancel"]) {
//交易取消
[[NSNotificationCenter defaultCenter] postNotificationName:@"YINLIANPAYC" object:nil];
}
}];
}
return YES;
}
8、根據后臺返回數據發起支付
if (tn.length > 0) {
//當獲得的tn不為空時,調用支付接口
[[UPPaymentControl defaultControl] startPay:tn fromScheme:@"UPPayDemo" mode:respCode viewController:self];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"銀聯參數配置不正確" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alert show];
}
在發起支付的界面做銀聯回調的監聽,并在監聽返回支付成功(也就是payOk:方法中)時調用自己后臺的接口再次確認是否真正支付成功
/* 銀聯支付回調監聽 */
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payOk:) name:@"YINLIANPAYS" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payCancel) name:@"YINLIANPAYC" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payFail) name:@"YINLIANPAYF" object:nil];
9、官方集成文檔寫的不錯,在你下載的demo里面