iOS支付(包括支付宝、微信、银联)封装

 

前言

最近在学习构架,感觉到构架的魅力,自己用构架思想封装了一个支付集合,欢迎使用!

开发环境

1、xcode 8以上

2、iOS 7.0以上

支付SDK的集成

1、支付宝

参照博客 支付宝支付集成 ,里面很详细这里就不多说了。

2、微信支付

参照博客 微信支付集成  

3、银联支付

银联支付要比支付宝及微信要简单

 参照  银联支付集成

工程目录结构




依赖库目录




工厂模式

1、代理统一抽象接口

#import <UIKit/UIKit.h>

#import "TXP_Charge.h"

#import "TXP_PayComplation.h"

//代理模式->目标接口:支付接口

@protocol TXP_IPay <NSObject>


-(void)payWithCharge:(TXP_Charge*)charge controller:(UIViewController*)controller scheme:(NSString*)scheme withComplation:(TXP_PayComplation)complation;


//业务方法二:需要处理支付结果回调(9.0以前回调)

- (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication withComplation:(TXP_PayComplation)complation;


//业务方法三:需要处理支付结果回调(9.0以后回调)

- (BOOL)handleOpenURL:(NSURL *)url withComplation:(TXP_PayComplation)complation;


@end

2、支付宝具体实现类(需要遵守自己TXP_IPay的抽象协议)

@interface TXP_ALiPay ()


@property (nonatomic)TXP_PayComplation complation;


@end

@implementation TXP_ALiPay

- (instancetype)init{

    self = [superinit];

    if (self) {


    }

    return self;

}


- (void)payWithCharge:(TXP_Charge*)charge controller:(UIViewController *)controller scheme:(NSString *)scheme withComplation:(TXP_PayComplation)complation{

    

    if (complation) {

        _complation = complation;

    }


    [[AlipaySDK defaultService]payOrder:charge.orderNofromScheme:scheme callback:^(NSDictionary *resultDic) {

        NSString * state = [NSStringstringWithFormat:@"%@",resultDic[@"resultStatus"]];

        if ([state isEqualToString:@"9000"]) {

           _complation(STR_PAY_SUCCESS,nil);

        }

        else if ([stateisEqualToString:@"6001"])

        {

             _complation(nil,[TXP_PayErrorUtilscreate:TXP_PayErrorActivation]);

            

        }else

        {

            _complation(nil,[TXP_PayErrorUtilscreate:TXP_PayErrorActivation]);

        }

    }];

}


//业务方法二:需要处理支付结果回调(9.0以前回调)

- (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication withComplation:(TXP_PayComplation)complation{

    if (complation) {

        _complation = complation;

    }

    if ([url.hostisEqualToString:@"safepay"]) {

        //跳转支付宝钱包进行支付,处理支付结果

        [[AlipaySDKdefaultService] processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary *resultDic) {

            //            NSLog(@"result = %@",resultDic);

            NSString * state = [NSStringstringWithFormat:@"%@",resultDic[@"resultStatus"]];

            if ([state isEqualToString:@"9000"]) {

                [[NSNotificationCenterdefaultCenter] postNotificationName:@"judgePayStatu"object:self];

            }  else if ([state isEqualToString:@"6001"]){

                [[NSNotificationCenterdefaultCenter] postNotificationName:@"cancelPayment"object:self];

            }

        }];

    }

    return YES;

}


//业务方法三:需要处理支付结果回调(9.0以后回调)

- (BOOL)handleOpenURL:(NSURL *)url withComplation:(TXP_PayComplation)complation{

    

    return [selfhandleOpenURL:url sourceApplication:nilwithComplation:complation];

}


3、微信具体实现类(需要遵守自己TXP_IPay的抽象协议)

@interface TXP_WxPay ()<WXApiDelegate>


@property (nonatomic)TXP_PayComplation complation;


@end

@implementation TXP_WxPay


- (instancetype)init{

    self = [superinit];

    if (self) {

        BOOL isSuccess = [WXApiregisterApp:@"这里填写注册key"];

        if (isSuccess) {

            NSLog(@"注册成功!");

        }

    }

    return self;

}


//业务方法一:需要调用微信支付接口(唤醒微信支付)

-(void)payWithCharge:(TXP_Charge*)charge controller:(UIViewController*)controller scheme:(NSString*)scheme withComplation:(TXP_PayComplation)complation{

    _complation = complation;

    NSString* timeStamp = [NSStringstringWithFormat:@"%@",[charge.credentialobjectForKey:@"timestamp"]];

    PayReq *request = [[PayReqalloc] init];

    request.partnerId = [charge.credentialobjectForKey:@"partnerid"];

    request.prepayId= [charge.credentialobjectForKey:@"prepayid"];

    request.package = @"Sign=WXPay";

    request.nonceStr= [charge.credentialobjectForKey:@"noncestr"];

    request.timeStamp= [timeStamp intValue];

    request.sign= [charge.credentialobjectForKey:@"sign"];

    [WXApi sendReq:request];

}


//业务方法二:需要处理支付结果回调(9.0以前回调)

- (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication withComplation:(TXP_PayComplation)complation{

    if (complation) {

        _complation = complation;

    }

    return [WXApihandleOpenURL:url delegate:self];

}


//业务方法三:需要处理支付结果回调(9.0以后回调)

- (BOOL)handleOpenURL:(NSURL *)url withComplation:(TXP_PayComplation)complation{

    return [selfhandleOpenURL:url sourceApplication:nilwithComplation:complation];

}


-(void)onResp:(BaseResp*)resp{

    if ([resp isKindOfClass:[PayRespclass]]){

        PayResp*response=(PayResp*)resp;

        switch(response.errCode){

            case WXSuccess:

                _complation(STR_PAY_SUCCESS,nil);

                break;

            caseWXErrCodeCommon:

                _complation(nil,[TXP_PayErrorUtilscreate:TXP_PayErrorUnknownError]);

                break;

            caseWXErrCodeUserCancel:

                _complation(nil,[TXP_PayErrorUtilscreate:TXP_PayErrorCancelled]);

                break;

            default:

                _complation(nil,[TXP_PayErrorUtilscreate:TXP_PayErrorCancelled]);

                break;

        }

    }

}


4、银联具体实现类(需要遵守自己 TXP_IPay 的抽象协议)

@interface TXP_UnionPay ()


@property (nonatomic)TXP_PayComplation complation;


@end


@implementation TXP_UnionPay


//业务方法一:需要调用银联支付接口(唤醒银联支付)

//银联支付请求处理

- (void)payWithCharge:(TXP_Charge*)charge controller:(UIViewController *)controller scheme:(NSString *)scheme withComplation:(TXP_PayComplation)complation{

    

    _complation = complation;

    dispatch_sync(dispatch_get_main_queue(), ^{

        //需要支付凭证

        NSString* tn = [charge.credentialobjectForKey:@"tn"];

        NSString* unionPaymode = [charge.credentialobjectForKey:@"mode"];

        BOOL isSuccess = [[UPPaymentControldefaultControl] startPay:tnfromScheme:scheme mode:unionPaymode viewController:controller];

        if (!isSuccess) {

            _complation(nil,[TXP_PayErrorUtilscreate:TXP_PayErrorActivation]);

        }

    });

}


//银联支付回调

//业务方法二:需要处理支付结果回调(9.0以前回调)

- (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication withComplation:(TXP_PayComplation)complation{

    if (complation) {

        _complation = complation;

    }

    [[UPPaymentControldefaultControl] handlePaymentResult:urlcompleteBlock:^(NSString *code,NSDictionary *data) {

        if([code isEqualToString:@"success"]) {

            //结果code为成功时,去商户后台查询一下确保交易是成功的再展示成功

            complation(STR_PAY_SUCCESS,nil);

        }else if([codeisEqualToString:@"fail"]) {

            //交易失败

            complation(nil,[TXP_PayErrorUtilscreate:TXP_PayErrorUnknownError]);

        }else if([codeisEqualToString:@"cancel"]) {

            //交易取消

            complation(nil,[TXP_PayErrorUtilscreate:TXP_PayErrorCancelled]);

        }

    }];

    return YES;

}


//业务方法三:需要处理支付结果回调(9.0以后回调)

- (BOOL)handleOpenURL:(NSURL *)url withComplation:(TXP_PayComplation)complation{

    

    return [selfhandleOpenURL:url sourceApplication:nilwithComplation:complation];

}

@end


单例模式调用方法类

+(instancetype)sharedEngine{

    static TXP_PayEngine* payEngine;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        payEngine = [[self alloc] init];

    });

    return payEngine;

}


- (instancetype)init{

    self = [superinit];

    if (self) {

        [selfregisterChannel];

    }

    return self;

}


//注册支付渠道

-(void)registerChannel{


    _channelDic = @{PAY_UNIONPAY : [[TXP_UnionPayalloc] init],

                    PAY_WXPAY : [[TXP_WxPayalloc] init],

                    PAY_ALIPAY: [[TXP_ALiPayalloc]init]

                    };

}


//处理支付

-(void)payWithCharge:(TXP_Charge*)charge controller:(UIViewController*)controller scheme:(NSString*)scheme withComplation:(TXP_PayComplation)complation{

    

    //验证Controller是否为空

    if (controller == nil) {

        complation(nil,[TXP_PayErrorUtilscreate:TXP_PayErrorViewControllerIsNil]);

        return;

    }

    

    _channel = charge.channel;

   

    id<TXP_IPay> pay = [_channelDicobjectForKey:charge.channel];

    if (pay == nil) {

        complation(nil,[TXP_PayErrorUtilscreate:TXP_PayErrorInvalidChannel]);

        return;

    }

    

    [pay payWithCharge:charge controller:controller scheme:scheme withComplation:complation];

}


//处理回调

- (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication withComplation:(TXP_PayComplation)complation{

    return [[_channelDicobjectForKey:_channel]handleOpenURL:url sourceApplication:sourceApplication withComplation:complation];;

}


- (BOOL)handleOpenURL:(NSURL *)url withComplation:(TXP_PayComplation)complation{

    return [[_channelDicobjectForKey:_channel]handleOpenURL:url sourceApplication:nilwithComplation:complation];

}


调用测试

@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

     ViewController * __weak weakSelf = self;

    TXP_Charge *charge = [[TXP_Chargealloc]init];//此model配置从服务器获得的参数

    charge.channel = PAY_ALIPAY; //支付类型

    [[TXP_PayEnginesharedEngine] payWithCharge:chargecontroller:weakSelf scheme:@"TXPPayDemo"withComplation:^(NSString *result,TXP_PayError *error) {

        //回调

        if (error) {

            //出现了异常

            NSLog(@"%@",[errorgetMsg]);

        }else{

            //支付成功

            NSLog(@"支付成功!");

        }

    }];

}


AppDelegate类里只需一句代码

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

   

    return [[TXP_PayEnginesharedEngine] handleOpenURL:urlwithComplation:nil];

}


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

    return [[TXP_PayEnginesharedEngine] handleOpenURL:urlwithComplation:nil];

}


Git 上传demo


支付demo  有问题留言或者给我发邮件1269456913@qq.com




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值