赞
踩
今天和同事闲聊的时候,聊到对接支付宝和微信sdk的事情,聊完以后兴致未尽。顺便去网上搜了一下,怎样做一款sdk,网上的教程少的可怜,能搜到的也是好坏各异,参差不齐。遂下决心,敲一篇博客,把sdk的实现流程讲解一下。给迷茫中的小白一点思路。还是那句话,一万个读者有一万个哈姆雷特,一万个程序员有一万种编码风格,代码仅供参考。最后的效果如图:
sdk内部类文件:
导入sdk后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地址
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。