有没有一种方法可以创建 Stipe 订阅(我正在使用 Java 和 React),该方法需要添加卡以用于将来的订阅付款,并在前端确认期间在一次调用中立即收取第一笔金额。 目前我能够研究的是,您首先需要创建订阅并通过首先在前端调用
SetupIntent
来使用 clientSecret
确认 stripe.confirmCardSetup
,并在成功确认后 - 使用从 检索的
stripe.confirmCardPayment
启动
clientSecret
subscription.getLatestInvoiceObject().getPaymentIntentObject().getClientSecret()
;
是否可以在拨打
stripe.confirmCardPayment
而不是先拨打confirmCardSetup
时将卡详细信息与订阅相关联?我认为如果您confirmCardPayment
与与订阅相关联的clientSecret
,卡详细信息将自动保存用于订阅,而无需先确认卡设置?
Stripe 公共文档有一个非常好的演练,描述了处理此问题的推荐方法:
简而言之,流程是:
latest_invoice.payment_intent.client_secret
传回前端。
payment_settings.save_default_payment_method
参数可用于告诉 Stripe 稍后自动保存付款方式。
https://docs.stripe.com/api/subscriptions/create#create_subscription- payment_settings-save_default_ payment_method这是相关的代码片段,向您展示应如何配置服务器代码以保存默认付款方式并将客户端密钥传回。
// Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
Stripe.apiKey = "your_test_key";
post(
"/create-subscription",
(request, response) -> {
response.type("application/json");
String customerId = request.cookie("customer");
CreateSubscriptionRequest postBody = gson.fromJson(
request.body(),
CreateSubscriptionRequest.class
);
String priceId = postBody.getPriceId();
// Automatically save the payment method to the subscription
// when the first payment is successful
SubscriptionCreateParams.PaymentSettings paymentSettings =
SubscriptionCreateParams.PaymentSettings
.builder()
.setSaveDefaultPaymentMethod(SaveDefaultPaymentMethod.ON_SUBSCRIPTION)
.build();
// Create the subscription. Note we're expanding the Subscription's
// latest invoice and that invoice's payment_intent
// so we can pass it to the front end to confirm the payment
SubscriptionCreateParams subCreateParams = SubscriptionCreateParams
.builder()
.setCustomer(customerId)
.addItem(
SubscriptionCreateParams
.Item.builder()
.setPrice(priceId)
.build()
)
.setPaymentSettings(paymentSettings)
.setPaymentBehavior(SubscriptionCreateParams.PaymentBehavior.DEFAULT_INCOMPLETE)
.addAllExpand(Arrays.asList("latest_invoice.payment_intent"))
.build();
Subscription subscription = Subscription.create(subCreateParams);
Map<String, Object> responseData = new HashMap<>();
responseData.put("subscriptionId", subscription.getId());
responseData.put("clientSecret", subscription.getLatestInvoiceObject().getPaymentIntentObject().getClientSecret());
return StripeObject.PRETTY_PRINT_GSON.toJson(responseData);
}
);