1. 程式人生 > >iOS支付(包括支付寶、微信、銀聯)封裝

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的抽象協議)

@interfaceTXP_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  有問題留言或者給我發郵件[email protected]



相關推薦

spring_boot_pay支付支付詳細代碼案例

fan target 簽名加密 china 整合 提示 業務 rom sig spring-boot-pay 支付服務:支付寶,微信,銀聯詳細代碼案例(除銀聯支付可以測試以外,支付寶和微信支付測試均需要企業認證,個人無法完成測試),項目啟動前請仔細閱讀 註意事項 。 友情

支付支付詳細程式碼案例

除銀聯支付可以測試以外,支付寶和微信支付測試均需要企業認證,個人無法完成測試微信退款證書,微信商戶平臺(pay.weixin.qq.com)-->賬戶中心-->賬戶設定-->API安全-->證書下載,使用apiclient_cert.p12即可支付寶支付相關引數zfbinfo.prop

iOS支付包括支付封裝

前言 最近在學習構架,感覺到構架的魅力,自己用構架思想封裝了一個支付集合,歡迎使用! 開發環境 1、xcode 8以上 2、iOS 7.0以上 支付SDK的整合 1、支付寶 參照部落格 支付寶支付整合 ,裡面很詳細這裡就不多說了。 2、微信支付 3、銀聯支付 銀聯支付要

通過user-agent判斷h5頁面是在哪個手機AppQQ支付下開啟的

function getAppClient(){   var ua = navigator.userAgent.toLowerCase();   if(ua.match(/MicroMessenger/i)=="micromessenger") {   return "weixin";   }

Android常用的第三方支付支付

生成RSA私鑰 openssl>genrsa -out rsa_private_key.pem 1024 生成RSA公鑰 openssl>rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem 將RSA私鑰轉換成PKCS8

支付移動支付整合

一、支付寶移動支付 業務流程: SDK整合: 解壓介面壓縮檔案(檔名是 WS_MOBILE_PAY_SDK_BASE.zip),找到 IOS 的壓縮檔案(檔名是支付寶移動支付 SDK 標準版(iOS).zip)。 匯入程式碼 步驟1

一個API解決 區分當前使用應用的一切裝置平臺AndroidIOSQQ等等一切有提供支援的

Window Navigator 示例: <div id="example"></div> <script> var txt = ''; txt = "<p>Browser CodeName: " + navigator.appCodeName

Laravel5.1 實現第三方登入認證包括QQ豆瓣

第三方登入認證能簡化使用者登入/註冊的操作,降低使用者登入/註冊的門檻,對提高應用的使用者轉化率很有幫助。 Socialite Laravel 為我們提供了簡單、易用的方式,使用 Laravel Socialite 進行 OAuth(OAuth1 和 OAuth2 都有支

js通過UA判斷iosandroidqqqq瀏覽器

function isIPhone(){ //判斷是否是ios var u = navigator.userAgent; var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端 return is

js 判斷通過什麼開啟安卓蘋果QQ瀏覽器某個app應用…

/* 獲取當前環境: 系統環境: iOS Android PC 瀏覽器環境 微信內建瀏覽器、QQ內建瀏覽器、正常瀏覽器 是否app內開啟 */ var ua = navigator.userAgent.toLowerCase(); //獲取瀏覽器標識並轉換為小寫 va

使用ShareSDK完成第三方QQ登入和分享

這幾天遇到一個需求:做第三方登入和分享。遇到了一些坑,把整個過程整理記錄下來,方便他人,同時也捋一下思路。 當時考慮過把每個平臺的SDK下載下來,一個一個弄,一番取捨後決定還是用ShareSDK。這裡只做了微博、微信和QQ。過程如下: 1.去ShareSDK官網註冊一個賬號方便以後對ShareSDK的

Android ShareSDK第三方登入分別有新浪QQFacebookLinkedinGoogle等等

專案的Demo:快速整合:1. 獲取ShareSDK的AppKey2. 下載SDK下載頁如圖所示:點選下載之後如下圖所以,點選下載SDK的下載頁,展開平臺可以選擇其他的第三方平臺;demo也是這裡下載;下載完之後的解壓目錄如圖所示:3. 快速整合ShareSDK深受大家喜愛,

iOS之OC使用原生控制元件實現分享到QQ簡訊博等的兩種方法

現在任何一款APP都離不開“分享”,產品設計可能是覺得有分享能更好的推廣自己的產品吧。。。 一種是使用UIActivityViewController, 另外一種是使用Social框架中的SLComposeViewController 一、UIActivityViewCon

Android自定義PopWindowQQ彈出選單

前段時間在個人開發的專案中需要用到彈出選單,類似QQ右上角的彈出選單,自己使用popwin的次數也不是很多,其中也遇到過一點問題,今天正好有時間就把一些經驗分享給大家。 先來看看最終實現過後的效果怎麼樣,下面放上圖 自定義的彈出選單是繼承的p

Android 用官方SDK實現第三方qq登入

       最近專案需要做第三方登入,由於以前沒做過,所以也是花了不少時間,走了好多彎路,總算是完成了。依據有福同享有難同當這句話,還有互相學習的原則,還有還有。現在把一些知識點總結一下,供需要者參考,有不足之處還望指點。有兩種方式可實現:一種是用友盟的(它是把qq

Android 用官方SDK實現第三方qq博等分享和登入

      上一篇講了android第三方登入,這一篇大概說一下分享。需要注意幾點:  1.登入和分享功能都必須是簽名之後的apk,才能用  2.再吐槽一下,微信SDK做的真他媽的變態,和登入一樣,必須是例如我的apk,包名(也就是AndroidManifest.

QQ手機號身份證郵箱正則驗證

blog pre 前綴 highlight var logs clas brush test QQ正則驗證 查了下,現在QQ的長度最長是10位數,驗證格式為不以0開頭的5-10位數字就可以了 var reg = /^[1-9]\d{4,9}$/; reg.test(‘01

在Android中使App高速簡單地支持新浪QQfacebook等十幾個主流社交平臺的分享功能

分析 ont renren androidm mod 執行 xen 12px 操作 前言 在如今的APP或者遊戲中,分享功能差點兒已經成為標配。分享功能不但能夠滿足用戶的需求。也能夠為產品帶來很多其它的用戶,甚至能夠對用戶的行為、活躍度、年齡段等情況進行數據統計,使得軟

實現QQ新浪博和百度第三方登錄(Android Studio)

wiki protocol super cli 路徑 參考 syn jar包 all 前言: 對於大多數的APP都有第三方登錄這個功能,自己也做過幾次,最近又有一個新項目用到了第三方登錄,所以特意總結了一下關於第三方登錄的實現,並拿出來與大家一同分享; 各大開放平臺註冊

h5怎麽做分享到QQ 朋友圈 博等功能

必備工具 con null 描述 one url key cts function 微信已經成為我們日常聊天聯系基本的必備工具,所以小菜我首先介紹一下如何調用微信的分享功能。其實除了可以在微信上分享到朋友圈和發送給好友,微信的分享接口還提供了分享到QQ和分享到騰訊微博等,