官方文档地址:https://stripe.com/docs/payments/accept-a-payment
Stripe 一个极简的国际卡支付,集成流程也是比较清晰。
大概步骤就是 前端 从后台拿到 client_secret ,将 卡信息 和 client_secret 传给 Stripe,进行支付,成功后 Stripe 会通知后台, 后台更新订单,前端更新界面。
1、首先导入 Strip 库
pod 'Stripe'
2、 配置环境
在 AppDelegate.m 的 application:didFinishLaunchingWithOptions: 方法中注册 Strip 的key, 这个参数 区分正式环境和测试环境。需要到 Stripe 后台查找
在测试和开发时,请使用测试模式键;在发布应用程序之前,请使用生产模式键。
#define Pay_Stripe_Live_Key @"pk_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define Pay_Stripe_Test_Key @"pk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#if DEBUG
#define kPayStripeEnviroment Pay_Stripe_Test_Key
#else
#define kPayStripeEnviroment Pay_Stripe_Live_Key
#endif
应用启动时,请使用Stripe 可发布密钥配置SDK,以便它可以向Stripe API发出请求。
#import <Stripe/Stripe.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Stripe setDefaultPublishableKey:kPayStripeEnviroment];
}
3、创建结账界面,
Stripe 有自带的界面,我们用自带的来绘制界面
@property (weak) STPPaymentCardTextField *cardTextField;
/// 更新 输入框的大小
STPPaymentCardTextField *cardTextField = [[STPPaymentCardTextField alloc] init];
cardTextField.borderColor = kClearColor;
for (UIView *view in cardTextField.subviews) {
if ([view isKindOfClass:[UIImageView class]]) {
CGRect frame = view.frame;
frame.origin.x = 0;
view.frame = frame;
}
}
self.cardTextField = cardTextField;
// self.cardTextField.backgroundColor = kOrangeColor;
[self addSubview:self.cardTextField];
[self.cardTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.top.mas_equalTo(0);
make.left.mas_equalTo(WindowRatioX(18));
make.right.mas_equalTo(WindowRatioX(-15));
}];
Stripe 平台提供了一些测试卡号
4、客户端从后台请求到client_secret
用户输入卡号后,先判断输入的卡号,过期年月等信息是否有效,再根据order_id 从后台拿到 client_secret
self.cardParams = self.cardTextField.cardParams;
if (self.cardParams.number.length <= 0 || self.cardParams.expMonth.intValue <= 0 || self.cardParams.expYear.intValue <= 0 || self.cardParams.cvc.integerValue <= 0) {
// 请确认卡号信息
return;
}
5、调起Stripe 库进行支付
将从后台拿到的 client_secret 和 用户输入的 self.cardTextField.cardParams 值代入后进行支付。
- (void)stripePayAtClientSecret:(NSString *)clientSecret {
// ...
if (!clientSecret) {
// PaymentIntent hasn't been created;
return;
}
HUDShow();
// Collect card details
STPPaymentMethodParams *paymentMethodParams = [STPPaymentMethodParams paramsWithCard:self.cardParams billingDetails:nil metadata:nil];
STPPaymentIntentParams *paymentIntentParams = [[STPPaymentIntentParams alloc] initWithClientSecret:clientSecret];
paymentIntentParams.paymentMethodParams = paymentMethodParams;
// Submit the payment
STPPaymentHandler *paymentHandler = [STPPaymentHandler sharedHandler];
[paymentHandler confirmPayment:paymentIntentParams withAuthenticationContext:self completion:^(STPPaymentHandlerActionStatus status, STPPaymentIntent *paymentIntent, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
HUDHide();
switch (status) {
case STPPaymentHandlerActionStatusFailed: {
// [self displayAlertWithTitle:@"Payment failed" message:error.localizedDescription ?: @"" restartDemo:NO];
HUDShowMessage(error.localizedDescription);
break;
}
case STPPaymentHandlerActionStatusCanceled: {
HUDShowMessage(error.localizedDescription);
// [self displayAlertWithTitle:@"Payment canceled" message:error.localizedDescription ?: @"" restartDemo:NO];
break;
}
case STPPaymentHandlerActionStatusSucceeded: {
// [self displayAlertWithTitle:@"Payment succeeded" message:paymentIntent.description ?: @"" restartDemo:YES];
[self getOrderStatus];
break;
}
default:
break;
}
});
}];
}
6、检查订单
支付成功后 , Stripe 会通知后台 ,这段时间 ,如果不是实时通讯,我们前端可以通过后台给到的订单状态查询接口进行轮询,(不同业务逻辑不同,我这里是两次轮询,分别间隔三秒,成功跳成功页面,失败跳失败页面,联系客服。)