[英]Create Stripe subscription with existing customer payment method (card)
我无法在客户已有付款方式的情况下创建订阅。 仪表板在新订阅 object 旁边只有“不完整”标签。
我下面的端点接受客户 ID 和付款方式 ID。 其中大部分是从他们的示例中提取的,但我在“.setDefaultPaymentMethod”中添加了
@GetMapping("/subscriptions/create/{customerId}/{defaultPaymentMethod}")
public void createSubscription(@PathVariable String customerId, @PathVariable String defaultPaymentMethod) throws StripeException {
Stripe.apiKey = "sk_test_XXXXX";
String priceId = "XXXXX";
// Automatically save the payment method to the subscription
// when the first payment is successful.
SubscriptionCreateParams.PaymentSettings paymentSettings =
SubscriptionCreateParams.PaymentSettings
.builder()
.setSaveDefaultPaymentMethod(SubscriptionCreateParams.PaymentSettings.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.Builder subCreateParams = SubscriptionCreateParams.builder()
.setCustomer(customerId)
.addItem(
SubscriptionCreateParams
.Item.builder()
.setPrice(priceId)
.build()
)
.setPaymentSettings(paymentSettings)
.setDefaultPaymentMethod(defaultPaymentMethod)
.setPaymentBehavior(SubscriptionCreateParams.PaymentBehavior.DEFAULT_INCOMPLETE)
.setCollectionMethod(SubscriptionCreateParams.CollectionMethod.CHARGE_AUTOMATICALLY)
.addAllExpand(Arrays.asList("latest_invoice.payment_intent"));
Subscription subscription = Subscription.create(subCreateParams.build());
}
请注意,如果我最初从我的端点创建订阅,则可以创建成功的订阅,获取用于通过 Stripe JavaScript/React 库收集付款信息的客户端密码,并让用户通过“stripe.xml”提交这些付款详细信息。确认卡支付”。 端点如下:
@GetMapping("/subscriptions/create/{customerId}")
public void receiveCreateSubscriptionRequestWithoutPaymentMethod(@PathVariable String customerId) throws StripeException {
Stripe.apiKey = "sk_test_XXXXX";
String priceId = "XXXXX";
// Automatically save the payment method to the subscription
// when the first payment is successful.
SubscriptionCreateParams.PaymentSettings paymentSettings =
SubscriptionCreateParams.PaymentSettings
.builder()
.setSaveDefaultPaymentMethod(SubscriptionCreateParams.PaymentSettings.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.Builder subCreateParams = SubscriptionCreateParams.builder()
.setCustomer(customerId)
.addItem(
SubscriptionCreateParams
.Item.builder()
.setPrice(priceId)
.build()
)
.setPaymentSettings(paymentSettings)
.setPaymentBehavior(SubscriptionCreateParams.PaymentBehavior.DEFAULT_INCOMPLETE)
.setCollectionMethod(SubscriptionCreateParams.CollectionMethod.CHARGE_AUTOMATICALLY)
.addAllExpand(Arrays.asList("latest_invoice.payment_intent"));
Subscription subscription = Subscription.create(subCreateParams.build());
}
这里有什么想法吗?
您正在传递payment_behavior=default_incomplete
- 您需要在 Subscription 转换为status=active
之前确认第一张发票上的 PaymentIntent 。
请参阅https://stripe.com/docs/api/subscriptions/create#create_subscription-payment_behavior
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.