iOS-快速集成支付宝支付、微信支付的管理类

一:封装支付宝支付

#import 

UIKIT_EXTERN NSNotificationName const XQPAliPaySuccessNotification;
UIKIT_EXTERN NSNotificationName const XQPAliPayFailNotification;

typedef void (^XQPAliPayCompletionBlock) (BOOL);

@interface XQPAliPayManager : NSObject
/*
 单利
 */
+ (instancetype)sharedInstance;


/**
 调起支付

 @param orderStr 后台签名字符串
 */
- (void)payOrder:(NSString *)orderStr;

/**
 *  支付接口
 *
 *  @param orderStr       订单信息
 *  @param completionBlock 支付结果回调Block,用于wap支付结果回调(非跳转钱包支付)
 */
- (void)payOrder:(NSString *)orderStr
        callback:(XQPAliPayCompletionBlock)completionBlock;


/**
 处理AppDelegate里面的回调

 @param url 回调的Url
 */
- (void)processOrderWithPaymentResult:(NSURL *)url;
@end
#import "XQPAliPayManager.h"
#import 

NSNotificationName const XQPAliPaySuccessNotification = @"XQPAliPaySuccessNotification";
NSNotificationName const XQPAliPayFailNotification = @"XQPAliPayFailNotification";

@implementation XQPAliPayManager

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static XQPAliPayManager *sharedInstance;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[XQPAliPayManager alloc] init];
    });
    return sharedInstance;
}

#pragma mark - 跳转网页钱包支付
- (void)payOrder:(NSString *)orderStr {
    // NOTE: 调用支付结果开始支付
    [[AlipaySDK defaultService] payOrder:orderStr fromScheme:@"XQPParkingAliPay" callback:^(NSDictionary *resultDic) {
        NSLog(@"reslut = %@",resultDic);
        [self dealwithPayResult:resultDic];
    }];
}
- (void)payOrder:(NSString *)orderStr
        callback:(XQPAliPayCompletionBlock)completionBlock {
    // NOTE: 调用支付结果开始支付
    [[AlipaySDK defaultService] payOrder:orderStr fromScheme:@"XQPParkingAliPay" callback:^(NSDictionary *resultDic) {
        NSLog(@"reslut = %@",resultDic);
        completionBlock(resultDic);
    }];
}

#pragma mark - 跳转钱包支付
- (void)processOrderWithPaymentResult:(NSURL *)url {
    //跳转支付宝钱包进行支付,处理支付结果
    [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
        NSLog(@"result = %@",resultDic);
        [self dealwithPayResult:resultDic];
    }];
}

// 处理支付结果
- (void)dealwithPayResult:(NSDictionary *)resultDic {
    int statusCode = [resultDic[@"resultStatus"]  intValue];
    if (statusCode == 9000 || statusCode == 8000){
        // 支付成功
        [[NSNotificationCenter defaultCenter] postNotificationName:XQPAliPaySuccessNotification object:nil userInfo:nil];
    } else  {
        // 交易失败
        [[NSNotificationCenter defaultCenter] postNotificationName:XQPAliPayFailNotification object:nil];
    }
}
@end

调起支付宝及其通知结果处理:

// 支付成功的回调
[XQPNotificationCenter addObserver:self selector:@selector(paySuccess:) name:XQPAliPaySuccessNotification object:nil];
// 支付失败的回调
[XQPNotificationCenter addObserver:self selector:@selector(payFail:) name:XQPAliPayFailNotification object:nil];
// 调起支付宝
[[XQPAliPayManager sharedInstance] payOrder:responseObject[@"result"][@"rechargeSign"]];

// 支付宝支付成功的通知
- (void)paySuccess:(NSNotification *)noti {
    [XQPNotificationCenter removeObserver:self];
}
// 支付宝支付失败的通知
- (void)payFail:(NSNotification *)noti {
    [XQPNotificationCenter removeObserver:self];
}

二:封装微信支付

#import 
#import "WXApi.h"

UIKIT_EXTERN NSNotificationName const XQPWeiXinPaySuccessNotification;
UIKIT_EXTERN NSNotificationName const XQPWeiXinPayFailNotification;

@protocol XQPApiManagerDelegate 
@required
// 支付成功的代理
- (void)managerDidRecvPaymentResponse:(PayResp *)response;
@end

@interface XQPWeiXinPayManager : NSObject
/*
 单利
 */
+ (instancetype)sharedInstance;

/*
 调起微信支付
 */
- (void)weiXinPayWithDic:(NSDictionary *)wechatPayDic;

/*
 代理
 */
@property (nonatomic, assign) id delegate;

@end
#import "XQPWeiXinPayManager.h"

NSNotificationName const XQPWeiXinPaySuccessNotification = @"XQPWeiXinPaySuccessNotification";
NSNotificationName const XQPWeiXinPayFailNotification = @"XQPWeiXinPayFailNotification";

@interface XQPWeiXinPayManager()
@property(nonatomic, strong) NSDictionary *wechatPayDic;
@end
@implementation XQPWeiXinPayManager

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static XQPWeiXinPayManager *sharedInstance;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[XQPWeiXinPayManager alloc] init];
    });
    return sharedInstance;
}

- (void)weiXinPayWithDic:(NSDictionary *)wechatPayDic {
    
    if (!XQPDictIsEmpty(wechatPayDic)) {
        self.wechatPayDic = wechatPayDic;
    }
    // 向微信注册您的APPID
    [WXApi registerApp:wechatPayDic[@"appid"]];
    PayReq *req = [[PayReq alloc] init];
    req.openID = [wechatPayDic objectForKey:@"appid"];
    req.partnerId = [wechatPayDic objectForKey:@"partnerid"];
    req.prepayId = [wechatPayDic objectForKey:@"prepayid"];
    req.package = [wechatPayDic objectForKey:@"packages"];
    req.nonceStr = [wechatPayDic objectForKey:@"noncestr"];
    req.timeStamp = [[wechatPayDic objectForKey:@"timestamp"] intValue];
    req.sign = [wechatPayDic objectForKey:@"sign"];
    [WXApi sendReq:req];
}

// 微信支付返回结果回调
- (void)onResp:(BaseResp *)resp {
    
    if ([resp isKindOfClass:[PayResp class]]) {
        PayResp *response = (PayResp *)resp;
        
        if (response.errCode == WXSuccess) {
            // 支付成功
            [[NSNotificationCenter defaultCenter] postNotificationName:XQPWeiXinPaySuccessNotification object:nil userInfo:nil];
        } else {
            // 支付失败
            [[NSNotificationCenter defaultCenter] postNotificationName:XQPWeiXinPayFailNotification object:nil userInfo:nil];
        }
    }
}
@end

调起微信及其通知结果处理:

// 微信成功的回调
[XQPNotificationCenter addObserver:self selector:@selector(paySuccess:) name:XQPWeiXinPaySuccessNotification object:nil];
// 微信失败的回调
[XQPNotificationCenter addObserver:self selector:@selector(payFail:) name:XQPWeiXinPayFailNotification object:nil];
// 调起微信支付
[[XQPWeiXinPayManager sharedInstance] weiXinPayWithDic:responseObject[@"result"]];

// 微信支付成功的通知
- (void)paySuccess:(NSNotification *)noti {
    [XQPNotificationCenter removeObserver:self];
}
// 微信支付失败的通知
- (void)payFail:(NSNotification *)noti {
    [XQPNotificationCenter removeObserver:self];
}

最后

微信和支付宝在Appdelegate里面回调的处理:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if ([url.host isEqualToString:@"safepay"]) {//支付宝
        [[XQPAliPayManager sharedInstance] processOrderWithPaymentResult:url];
    }else if([url.host isEqualToString:@"pay"]){//微信 {
        [WXApi handleOpenURL:url delegate:[XQPWeiXinPayManager sharedInstance]];
    }
    return YES;
}

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
    if ([url.host isEqualToString:@"safepay"]) {//支付宝
        [[XQPAliPayManager sharedInstance] processOrderWithPaymentResult:url];
    }else if([url.host isEqualToString:@"pay"]){//微信
        [WXApi handleOpenURL:url delegate:[XQPWeiXinPayManager sharedInstance]];
    }
    return YES;
}

你可能感兴趣的:(iOS-快速集成支付宝支付、微信支付的管理类)