简体   繁体   English

使用现有客户付款方式(卡)创建 Stripe 订阅

[英]Create Stripe subscription with existing customer payment method (card)

I am unable to create a subscription where the customer already has a existing payment method.我无法在客户已有付款方式的情况下创建订阅。 The dashboard just has the "Incomplete" tag next to the new subscription object.仪表板在新订阅 object 旁边只有“不完整”标签。

My endpoint below takes in the customer id and the payment method id.我下面的端点接受客户 ID 和付款方式 ID。 Most of it was pulled from their examples but I added in ".setDefaultPaymentMethod"其中大部分是从他们的示例中提取的,但我在“.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());
}

Note that I am able to create a successful subscription if I initially create it from my endpoint, get the client secret which is used to collect payment information via the Stripe JavaScript/React library, and have the user submit those payment details via "stripe.confirmCardPayment".请注意,如果我最初从我的端点创建订阅,则可以创建成功的订阅,获取用于通过 Stripe JavaScript/React 库收集付款信息的客户端密码,并让用户通过“stripe.xml”提交这些付款详细信息。确认卡支付”。 Endpoint is below:端点如下:

@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());
}

Any thoughts here?这里有什么想法吗?

You're passing in payment_behavior=default_incomplete - you'll need to confirm the PaymentIntent on the first invoice before the Subscription transitions to status=active .您正在传递payment_behavior=default_incomplete - 您需要在 Subscription 转换为status=active之前确认第一张发票上的 PaymentIntent 。

See https://stripe.com/docs/api/subscriptions/create#create_subscription-payment_behavior请参阅https://stripe.com/docs/api/subscriptions/create#create_subscription-payment_behavior

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM