Stripe集成 iOS 端

Stripe集成 iOS 端

因为项目中有遇到需要针对海外用户集成银行卡支付功能,经过考虑选择了Stripe。官方文档地址:https://stripe.com/docs/mobile

因为是海外的,所以文档暂时没有中文的,可以下载demo看代码理解。下面仅给出吊起SDK界面的案例,后面如有需求更改成自己的界面,在做相关介绍。

/*
这里是吊起银行卡列表的界面,可以有选择,添加,编辑删除等选项,因为是原生SDK界面,所有的操作也都SDK内部实现
*/
STPPaymentConfiguration *config = [STPPaymentConfiguration sharedConfiguration];
config.additionalPaymentMethods = STPPaymentMethodTypeNone;     //支持的卡支付类型(银行卡或者Apple Pay等)
config.requiredBillingAddressFields = STPBillingAddressFieldsNone;//账单地址等内容
      
/*
这里就是比较重要的一步,需要和后端配合。StripeAPIClient是我自己创建的一个类,需要遵循<STPEphemeralKeyProvider>协议,详见StripeAPIClient.m文件

init方法可以忽略,自己定义就好,因为后台需要知道当前用户的ID,才能创建一个秘钥
*/
StripeAPIClient *manager = [StripeAPIClient sharedAPIClientWithMemberId:memberId
                                                                        token:token];
STPCustomerContext *customerContext = [[STPCustomerContext alloc] initWithKeyProvider:manager];
      
/*
设置下代理就可以监听方法了,具体代理方法可以查看<STPPaymentMethodsViewControllerDelegate>
*/
STPPaymentMethodsViewController *viewController = [[STPPaymentMethodsViewController alloc] initWithConfiguration:config theme:STPTheme.defaultTheme customerContext:customerContext delegate:self];

//下面是跳转方法,可以忽略
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
[self presentViewController:nav animated:YES completion:nil];
//  StripeAPIClient.m
//  Stripe
//  用于获取用户密令
//
//  Created by yuyang on 2019/3/6.
//

#import "StripeAPIClient.h"
#import <Stripe/Stripe.h>

static StripeAPIClient *_manager = nil;

@interface StripeAPIClient()

@property (nonatomic, copy) NSString *memberId;
@property (nonatomic, copy) NSString *token;

@end

@implementation StripeAPIClient

+ (instancetype)sharedAPIClientWithMemberId:(NSString *)member token:(NSString *)token {
  if (!_manager) {
    _manager = [[StripeAPIClient alloc] init];
    _manager.memberId = member;
    _manager.token = token;
  }
  return _manager;
}

- (void)createCustomerKeyWithAPIVersion:(NSString *)apiVersion completion:(STPJSONResponseCompletionBlock)completion{
  
  /*发起网络请求,拿到后端返给你的信息直接  completion(json,nil);就可以了
    主要后台返回的json格式,因为Stripe只接收json格式,如果遇到不成功,就检查下
  */
}

@end

也没有什么东西,主要就是官方给的Demo不够明确,有什么问题再讨论,代码放在https://github.com/YuStephen/Stripe-Manager

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。