当前位置:   article > 正文

支付宝、微信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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

②ResultModel类作为支付完成以后数据回调模型
ResultModel.h

#import <Foundation/Foundation.h>

@interface ResultModel : NSObject

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

+ (instancetype)sharedInstance;

@end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

③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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

④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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

github:demo地址

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/258452
推荐阅读
相关标签
  

闽ICP备14008679号