客戶端集成支付寶、微信支付测蘑,所限還是需要后臺的配合洽故,后臺做一些邏輯數(shù)據(jù)處理,客戶端只需要進(jìn)行第三方集成刀诬、授權(quán)咽扇、調(diào)起支付接口就行了,其實(shí)從客戶端來講還是比較簡單的陕壹。先講講支付寶支付的集成质欲,后續(xù)再講微信支付。
1糠馆、注冊支付寶開發(fā)者賬號嘶伟、創(chuàng)建應(yīng)用 https://openhome.alipay.com/ (這一步就不細(xì)講了,使用第三方不備步驟)榨惠。
創(chuàng)建應(yīng)用后奋早,需要授權(quán)應(yīng)用支持的功能:支付、登錄等功能赠橙。
2耽装、從官網(wǎng)上下載SDK,并導(dǎo)入到工程中
下載SDK解壓后如下文件:
導(dǎo)入SDK文件.png
將上面文件拖入工程后期揪,下一步添加依賴庫:
添加依賴庫.png
3掉奄、寫個(gè)單例類,實(shí)現(xiàn)支付寶支付功能,方便APP中各個(gè)地方的調(diào)用
創(chuàng)建HCAlipayManager 類:
.h文件:
#import <AlipaySDK/AlipaySDK.h>
NS_ASSUME_NONNULL_BEGIN
@interface HCAlipayManager : NSObject
//生成支付寶單例類
+(id)sharePayManager;
//支付寶支付
//aParam 后端返回支付信息
-(void)handleOrderPayWithParams:(NSString *)aParam;
@end
NS_ASSUME_NONNULL_END
.m文件:
@implementation HCAlipayManager
+ (id)sharePayManager{
static HCAlipayManager *asAlixPay = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
asAlixPay = [[HCAlipayManager alloc] init];
});
return asAlixPay;
}
/**
支付寶支付
@param aParam <#aParam description#>
*/
- (void)handleOrderPayWithParams:(NSString *)aParam{
NSLog(@"aParm = %@",aParam);
NSString *appScheme = @"alipayredpag";//appScheme是你在項(xiàng)目中添加的URL Type(別寫錯(cuò))
NSString *orderString = aParam;//aParam[@"payInfo"];
// NOTE: 調(diào)用支付結(jié)果開始支付
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
int statusCode = [resultDic[@"resultStatus"] intValue];
if (statusCode == 9000){
//訂單支付
[[HCToast shareInstance] showToast:@"支付成功"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"alipay_paySuccess" object:nil];
}else{
//交易失敗
// [[NSNotificationCenter defaultCenter] postNotificationName:@"PAY_STATUS" object:@"0"];
[[HCToast shareInstance] showToast:@"支付異常"];
}
}];
}
@end
4姓建、支付成功后跳轉(zhuǎn)
在AppDelegate.m中寫攔截跳轉(zhuǎn)協(xié)議:
#pragma mark - openURL Delegate
///ios9.0之后實(shí)現(xiàn)該方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:
//支付寶支付跳轉(zhuǎn)
if ([url.host isEqualToString:@"safepay"]) {
//跳轉(zhuǎn)支付寶錢包進(jìn)行支付诞仓,處理支付結(jié)果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"支付寶支付跳轉(zhuǎn) result = %@",resultDic);
int statusCode = [resultDic[@"resultStatus"] intValue];
if (statusCode == 9000){
//訂單支付
[[HCToast shareInstance] showToast:@"支付成功"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"alipay_paySuccess" object:nil];
}else{
//交易失敗
// [[NSNotificationCenter defaultCenter] postNotificationName:@"PAY_STATUS" object:@"0"];
[[HCToast shareInstance] showToast:@"支付異常"];
}
}];
}
return YES;
}
///ios9.0之前實(shí)現(xiàn)該方法
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
//支付寶支付跳轉(zhuǎn)
if ([url.host isEqualToString:@"safepay"]) {
//跳轉(zhuǎn)支付寶錢包進(jìn)行支付,處理支付結(jié)果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"支付寶支付跳轉(zhuǎn) result = %@",resultDic);
int statusCode = [resultDic[@"resultStatus"] intValue];
if (statusCode == 9000){
//訂單支付
[[HCToast shareInstance] showToast:@"支付成功"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"alipay_paySuccess" object:nil];
}else{
//交易失敗
// [[NSNotificationCenter defaultCenter] postNotificationName:@"PAY_STATUS" object:@"0"];
[[HCToast shareInstance] showToast:@"支付異常"];
}
}];
}
return YES;
}
///ios9.0之前實(shí)現(xiàn)該方法
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
//支付寶支付跳轉(zhuǎn)
if ([url.host isEqualToString:@"safepay"]) {
//跳轉(zhuǎn)支付寶錢包進(jìn)行支付速兔,處理支付結(jié)果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"支付寶支付跳轉(zhuǎn) result = %@",resultDic);
int statusCode = [resultDic[@"resultStatus"] intValue];
if (statusCode == 9000){
//訂單支付
[[HCToast shareInstance] showToast:@"支付成功"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"alipay_paySuccess" object:nil];
}else{
//交易失敗
// [[NSNotificationCenter defaultCenter] postNotificationName:@"PAY_STATUS" object:@"0"];
[[HCToast shareInstance] showToast:@"支付異常"];
}
}];
}
return YES;
}
5墅拭、在需要支付的地方,調(diào)用支付方法
///調(diào)起支付寶支付
- (void)doAlipayWithParams:(NSString *)params{
NSLog(@"支付寶支付信息:%@",params);
///沒錯(cuò)涣狗,只需要調(diào)用單例方法 一句話搞定谍婉。但是需要注意,params:一般是后臺給你的支付所需的信息镀钓,拼接成字符串傳給支付寶就可以了穗熬。
[[HCAlipayManager sharePayManager] handleOrderPayWithParams:params];
}
結(jié)束了,感謝閱讀~