Anthony Pham
Anthony Pham

Reputation: 141

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.

My endpoint below takes in the customer id and the payment method id. Most of it was pulled from their examples but I added in ".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". 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?

Upvotes: 1

Views: 956

Answers (1)

alex
alex

Reputation: 2774

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.

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

Upvotes: 1

Related Questions