Stripe支付笔记
stripe-iOS
参考地址:https://stripe/docs/payments/accept-a-payment#ios-submit-payment

安装(略)
支付流程
关于publishable key
stripe有两种环境:test(测试环境)和live(正式环境)。

test环境支付的操作并没有真的执行,只能使用特定的测试账号。(https://stripe/docs/testing)
每个环境对应一对key,分别为publishable 和 secret。(https://stripe/docs/keys#obtain-api-keys)

publishable key可以对外公布,用来创建token。客户端使用publishable key对用户的敏感信息(eg:信用卡)进行加密,得到token。并将token发送给自己的服务器使用。token只能使用一次。(https://stripe/docs/api/tokens)
secret key需要保存在服务端,属于机密信息。可以让服务器没有任何限制的访问stripe。
信用卡&借记卡工作流程
卡工作流程参考官网地址:(https://stripe/docs/payments/cards/overview)
客户端支付流程参考官网地址:(https://stripe/docs/payments/accept-a-payment?integration=elements
https://stripe/docs/payments/integration-builder)
官网给出的标准支付方式,基本流程就是,去服务器请求一个clientScret:
(https://stripe/docs/api/payment_intents/create)
curl https://api.stripe/v1/payment_intents
-u sk_test_4eC39HqLyjWDarjtT1zdp7dc:
-d amount=2000
-d currency=usd
-d “payment_method_types[]”=card
然后加上用户输入的卡信息直接调用SDK的方法。
STPPaymentMethodCardParams *cardParams = paymentCardTextField.cardParams;
STPPaymentIntentParams *paymentIntentParams = [[STPPaymentIntentParams alloc] initWithClientSecret:self.paymentIntentClientSecret];
paymentIntentParams.paymentMethodParams = paymentMethodParams;
STPPaymentHandler *paymentHandler = [STPPaymentHandler sharedHandler];
[paymentHandler confirmPayment:paymentIntentParams withAuthenticationContext:self completion:^(STPPaymentHandlerActionStatus status, STPPaymentIntent * _Nullable intent, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
switch (status) {
case STPPaymentHandlerActionStatusFailed: {
break;
}
case STPPaymentHandlerActionStatusCanceled: {
break;
}
case STPPaymentHandlerActionStatusSucceeded: {
break;
}
default:
break;
}
});
}];

但是这种方式需要用户每次都输入信用卡账号的方式,如果需要将卡信息保存下来,方便用户下一次支付的时候,直接选择某种支付方式或某一张卡片,需要添加如下代码:

paymentIntentParams.savePaymentMethod = @(YES);
这个属性可以自动绑定当前卡片信息到用户名下。需要注意的是:这种情况下需要在请求clientSecrete的时候增加一个参数:customer,即最终的请求应该是:

curl https://api.stripe/v1/payment_intents
-u sk_test_4eC39HqLyjWDarjtT1zdp7dc:
-d amount=2000
-d currency=usd
-d “payment_method_types[]”=card
-d customer=cus_wertyuikjhgfd
这样,用户下一次支付的时候,就可以直接获取到之前已经绑定过的卡信息来直接支付。简单说流程就是,拿到用户的paymentMethod列表,展示给用户,用户选择其中一个支付方式后,将paymentMethod的ID和clientSecrete一起赋值给sdk完成支付流程,代码如下:

STPPaymentIntentParams *paymentIntentParams = [[STPPaymentIntentParams alloc] initWithClientSecret:self.paymentIntentClientSecret];
paymentIntentParams.paymentMethodId = self.paymentMethodID;
paymentIntentParams.savePaymentMethod = @(YES);
除此之外,savePaymentMethod = @(YES)的时候还需要传入需要绑定的paymentMethod,否则会报错。

获取paymentMethod的接口是:

curl https://api.stripe/v1/payment_methods
-u sk_test_4eC39HqLyjWDarjtT1zdp7dc:
-d customer=cus_AJ6bY3VqcaLAEs
-d type=card
-G
更加详细具体的流程如下:

Stripe支付笔记_第1张图片
stripe支付流程.png
客户端支付完成后,SDK会返回支付结果给客户端,服务器端可以使用webhook来获取通知,不过收到的event的数据里面只有stripe的数据,如果想走后续流程,比如:更新订单状态,更改账户余额等。此时至少需要订单ID,可以将自己想要的数据放在creatPaymentIntent这个接口中。(https://stripe/docs/api/payment_intents/create
https://stripe/docs/payments/charges-api#storing-information-in-metadata)

putMetadata(‘orderID’,‘1234567’)
这样webhook的接口收到的event的数据中,metadata里面就会出现订单ID,可以进行下面的操作了。

获取完整的用户支付方式信息
使用上面的方法可以自动绑定用户输入的信用卡信息,获取用户支付方式(卡列表),可以让服务器协助调用API(参考:https://stripe/docs/api/payment_methods/list)
或者直接使用stripe的SDK,其核心是STPPaymentContext和STPPaymentOptionsViewController。

    SEphemeralKeyProvider *keyProvider = [[SEphemeralKeyProvider alloc] init];
    STPCustomerContext *customerContext = [[STPCustomerContext alloc] initWithKeyProvider:keyProvider];
    STPPaymentContext *paymentContext = [[STPPaymentContext alloc] initWithCustomerContext:customerContext];
    paymentContext.delegate = self;
    paymentContext.hostViewController = self;
    [paymentContext pushPaymentOptionsViewController];
    self.paymentContext = paymentContext;

其中的SEphemeralKeyProvider需要实现协议。
实现方法

  • (void)createCustomerKeyWithAPIVersion:(NSString *)apiVersion completion:(STPJSONResponseCompletionBlock)completion {
    [SPaymentDataRequst s_createEphemeralKeyFinishBlock:^(SResponse * _Nonnull response) {
    if (response.responseObject && [response.responseObject isKindOfClass:[NSDictionary class]]) {
    completion(response.responseObject,nil);
    }else {
    completion(nil, response.error);
    }
    }];
    }
    请求API:/api/order/stripe/createEphemeralKey,将返回结果直接回调。确认支付的时候调用

    [self.paymentContext requestPayment];
    STPPaymentContext的代理方法有:

  • (void)paymentContext:(STPPaymentContext *)paymentContext didFailToLoadWithError:(NSError *)error {
    //创建失败,此时返回的是上面的createCustomerKeyWithAPIVersion方法中的报错信息
    }

  • (void)paymentContextDidChange:(STPPaymentContext *)paymentContext {
    if (paymentContext.selectedPaymentOption) {
    //当用户选择的某一种支付方式后,回调。selectedPaymentOption保存了PMID。此处可以保留PMID准备支付。
    }
    }

  • (void)paymentContext:(STPPaymentContext *)paymentContext
    didCreatePaymentResult:(STPPaymentResult *)paymentResult
    completion:(STPPaymentStatusBlock)completion {
    //新添加了支付用的信用后的回调,此处可以保留PMID准备支付。

}

  • (void)paymentContext:(STPPaymentContext *)paymentContext
    didFinishWithStatus:(STPPaymentStatus)status
    error:(nullable NSError *)error {
    //添加信用卡失败的回调
    }
    之后的支付方式与上面一致:

STPPaymentHandler *paymentHandler = [STPPaymentHandler sharedHandler];
更新中……

信用卡二次确认
取消支付
把资金转给你平台上的商家stripe connect
把资金转到自己的银行户头-提现
辅助工具
CLI(终端联调工具)
安装
参考:https://stripe/docs/stripe-cli?platform=ios&lang=objc

更多推荐

Stripe支付 ios