支付寶、微信sdk支付流程模擬

今天和同事閒聊的時候,聊到對接支付寶和微信sdk的事情,聊完以後興致未盡。順便去網上搜了一下,怎樣做一款sdk,網上的教程少的可憐,能搜到的也是好壞各異,參差不齊。遂下決心,敲一篇博客,把sdk的實現流程講解一下。給迷茫中的小白一點思路。還是那句話,一萬個讀者有一萬個哈姆雷特,一萬個程序員有一萬種編碼風格,代碼僅供參考。最後的效果如圖:
支付sdk流程圖

sdk內部類文件:
sdk

導入sdk後demo內部類文件:
demo

①UserInfoModel類作爲傳入sdk的數據模型
UserInfoModel.h

#import <Foundation/Foundation.h>

@interface UserInfoModel : NSObject

@property (nonatomic, copy) NSString *money;

@end

②ResultModel類作爲支付完成以後數據回調模型
ResultModel.h

#import <Foundation/Foundation.h>

@interface ResultModel : NSObject

@property (nonatomic, copy)   void(^payResultBlock)(NSString *status, NSDictionary *payDict);

+ (instancetype)sharedInstance;

@end

ResultModel.m

#import "ResultModel.h"

@implementation ResultModel

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

    return resultModel;
}

@end

③PayAndReddem類作爲支付sdk入口
PayAndReddem.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "UserInfoModel.h"

@interface PayAndReddem : NSObject

+ (void)setupPayInfo:(UserInfoModel *)userInfo subClass:(UIViewController *)subCotroller payResult:(void (^)(NSString *status, NSDictionary *payDict))payResultBlock;

@end

PayAndReddem.m

#import "PayAndReddem.h"
#import "ThridViewController.h"
#import "PayModel.h"
#import "ResultModel.h"

@implementation PayAndReddem

+ (void)setupPayInfo:(UserInfoModel *)userInfo subClass:(UIViewController *)subCotroller payResult:(void (^)(NSString *status, NSDictionary *payDict))payResultBlock {
    [ResultModel sharedInstance].payResultBlock = payResultBlock;

    PayModel *payModel = [PayModel sharedPayInfo];
    payModel.money = userInfo.money;
    ThridViewController *payInputVC = [[ThridViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:payInputVC];
    [subCotroller presentViewController:nav animated:YES completion:nil];
}

@end

④SecondViewController,從此處開始調用sdk,並將數據通過PayReddem傳入sdk。在支付完成以後,成功與否均返回此頁面

SecondViewController.m

#import "SecondViewController.h"
#import "PayAndReddem.h"

@interface SecondViewController ()

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *button;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
    self.navigationItem.title = @"購買商品";
    [self setupSubviews];

}

- (void)setupSubviews {
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake((CGRectGetWidth(self.view.bounds)-200)/2, 100, 200, 30)];
    self.textField.borderStyle = UITextBorderStyleNone;
    self.textField.backgroundColor = [UIColor whiteColor];
    self.textField.placeholder = @"請輸入支付金額(分)";
    [self.textField becomeFirstResponder];
    [self.view addSubview:self.textField];

    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.backgroundColor = [UIColor greenColor];
    [self.button setTitle:@"進入支付sdk" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.button.frame = CGRectMake((CGRectGetWidth(self.view.bounds)-200)/2, 150, 200, 35);
    [self.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
}

- (void)buttonClick:(UIButton *)btn {
    BOOL isEmpty = [self checkTextField];

    //進入支付sdk
    if (isEmpty) {
        [PayAndReddem setupPayInfo:[self test] subClass:self payResult:^(NSString *status, NSDictionary *payDict) {
            if ([status isEqualToString:@"success"]) {
                //支付成功,並返回支付成功後的相應字段
                NSLog(@"result_code=%@,result_msg=%@",payDict[@"result_code"],payDict[@"result_msg"]);
            } else if ([status isEqualToString:@"fail"]) {
                //支付失敗
                NSLog(@"支付失敗");
            }
        }];
    }
}

- (BOOL)checkTextField {
    if (self.textField.text.length > 0) {
        return YES;
    } else {
        NSLog(@"金額不能爲空");
        return NO;
    }
}

//傳入支付sdk數據
- (UserInfoModel *)test {
    UserInfoModel *userInfoModel = [[UserInfoModel alloc] init];
    userInfoModel.money = self.textField.text;
    return userInfoModel;
}

@end

github:demo地址

發佈了54 篇原創文章 · 獲贊 83 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章