調(diào)用方式
// 支付結(jié)果監(jiān)聽(tīng)
[YWPayHepler shareHelper].payResultBlock = ^(BOOL result, NSString * _Nonnull resultMsg) {
NSLog(@"結(jié)果回調(diào):%@ ---> %@", (result ? @"支付成功" : @"支付失敗"), resultMsg);
};
YWPayHepler .h
#import <Foundation/Foundation.h>
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
#define YWPayResultNotice @"MGPayResultNotice" // 支付結(jié)果通知
NS_ASSUME_NONNULL_BEGIN
@interface YWPayHepler : NSObject
/** 回調(diào) result支付結(jié)果狀態(tài)(成功/失敗) resultMsg支付結(jié)果消息 */
@property (nonatomic, copy) void (^payResultBlock)(BOOL result, NSString *resultMsg);
+ (instancetype)shareHelper;
/**
支付寶支付
拉起本地客戶端支付成功后在[AppDelegate application:openURL:options:]中回調(diào)支付結(jié)果
@param paySign 支付簽名
@param resultBlock 支付結(jié)果回調(diào)(H5網(wǎng)頁(yè)支付回調(diào))
*/
+ (void)aliPayOrderWithSign:(NSString *)paySign
resultBlock:(void (^)(NSDictionary *resultDic))resultBlock;
@end
YWPayHepler .m
#import "YWPayHepler.h"
static YWPayHepler *helper = nil;
@implementation YWPayHepler
+ (instancetype)shareHelper
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
helper = [[YWPayHepler alloc] init];
});
return helper;
}
- (id)init
{
self = [super init];
if (self) {
[self _initHelper];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - init
- (void)_initHelper
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payResultHandleNotification:) name:YWPayResultNotice object:nil];
}
#pragma mark -- 支付回調(diào)(微信支付回調(diào) & 支付寶支付回調(diào))
- (void)payResultHandleNotification:(NSNotification *)notification {
BOOL result = NO;
NSString *resultMsgStr = @"支付結(jié)果";
//支付寶支付 - 回調(diào)(用userInfo)
if (notification.userInfo) {
// 支付結(jié)果解析
NSInteger resultStatus = [notification.userInfo[@"resultStatus"] integerValue];
switch (resultStatus) {
case 9000:
{
result = YES;
resultMsgStr = @"支付成功";
// 更新用戶余額
[MGUserNetworkManager updateUserBalanceBlock:^(BOOL success) {}];
}
break;
case 4000:
resultMsgStr = @"支付失敗";
break;
case 6001:
resultMsgStr = @"用戶中途取消支付";
break;
case 6002:
resultMsgStr = @"支付失敗 - 網(wǎng)絡(luò)連接出錯(cuò)";
break;
case 8000:
resultMsgStr = @"正在處理中";
break;
default:
resultMsgStr = @"支付失敗 - 未知原因";
break;
}
}
//微信支付 - 回調(diào)(用object)
if (notification.object) {
//回調(diào)消息
PayResp *payResp = notification.object;
switch (payResp.errCode) {
case WXSuccess:
result = YES;
resultMsgStr = @"支付成功";
break;
case WXErrCodeCommon:
resultMsgStr = @"支付失敗";//簽名錯(cuò)誤、未注冊(cè)APPID馒稍、項(xiàng)目設(shè)置APPID不正確择卦、注冊(cè)的APPID與設(shè)置的不匹配、其他異常等
break;
case WXErrCodeUserCancel:
resultMsgStr = @"支付取消";
break;
case WXErrCodeSentFail:
resultMsgStr = @"發(fā)送失敗";
break;
case WXErrCodeUnsupport:
resultMsgStr = @"支付不支持";
break;
case WXErrCodeAuthDeny:
resultMsgStr = @"支付授權(quán)失敗";
break;
default:
resultMsgStr = @"支付失敗 - 未知原因";
break;
}
}
// 彈窗支付結(jié)果
[self showAlertWithResultMessage:resultMsgStr result:result];
}
#pragma mark - * * * * * private * * * * *
- (void)showAlertWithResultMessage:(NSString *)msgStr result:(BOOL)result {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:msgStr preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
if (self.payResultBlock) {
self.payResultBlock(result, msgStr);
}
}];
[alertController addAction:okAction];
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIViewController *rootViewController = window.rootViewController;
[rootViewController presentViewController:alertController animated:YES completion:nil];
}
+ (void)aliPayOrderWithSign:(NSString *)paySign resultBlock:(void (^)(NSDictionary * _Nonnull))resultBlock {
// 拿著支付寶訂單簽名使用SDK發(fā)起支付
[[AlipaySDK defaultService] payOrder:paySign fromScheme:YWPayURLScheme callback:resultBlock];
}
@end
### 其他demo參考
/* 微信支付
Log(@"微信支付簽名信息 - %@",signDic);
PayReq *request = [[PayReq alloc] init];
request.partnerId = [signDic[@"partnerid"] mutableCopy];
request.prepayId = [signDic[@"prepayid"] mutableCopy];
request.package = [signDic[@"package"] mutableCopy];
request.nonceStr = [signDic[@"noncestr"] mutableCopy];
int ivalue = [[signDic[@"timestamp"] mutableCopy] intValue];
request.timeStamp = ivalue;
request.sign = [signDic[@"sign"] mutableCopy];
if (![WXApi sendReq: request]) {
NSLog(@"微信支付失敗");
} else {
NSLog(@"微信操作請(qǐng)求成功(并非支付成功妻熊,成功后會(huì)在Appdelegate里面回調(diào))");
}
*/
#pragma mark - * * * * * AppDelegate.m 中的支付回調(diào) * * * * *
// 只適用于iOS9以上灶轰,會(huì)優(yōu)先走這個(gè)方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *, id> *)options {
if ([url.host isEqualToString:@"safepay"]) {
// 支付跳轉(zhuǎn)支付寶錢包進(jìn)行支付羡洁,處理支付結(jié)果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
MGLog(@"支付跳轉(zhuǎn)支付寶錢包進(jìn)行支付,處理支付結(jié)果回調(diào):result = %@",resultDic);
[[NSNotificationCenter defaultCenter] postNotificationName:YWPayResultNotice object:nil userInfo:resultDic];
}];
return YES;
}else if ([url.scheme isEqualToString:WX_KEY]) {
// 微信支付回調(diào)處理(處理后走代理方法:- (void)onResp:(BaseResp *)resp)
return [WXApi handleOpenURL:url delegate:self];
}
return YES;
}
// 微信回調(diào)處理
- (void)onResp:(BaseResp *)resp {
//微信支付回調(diào)
PayResp *payResp = (PayResp *) resp;
[[NSNotificationCenter defaultCenter] postNotificationName: YWPayResultNotice object:payResp userInfo:nil];
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者