Can I confirm payment and save card for future uses in case of subscription in one call?

Is there a way to create Stipe subscription (I am using Java and React) that will require to add card for future subscription payments and charge first amount immediately in one call during confirmation on front end.
For now what I was able to research is that you first need to create Subscription and confirm SetupIntent with clientSecret by calling stripe.confirmCardSetup in front end first and after successful confirmation – initiate stripe.confirmCardPayment with clientSecret retrieved from subscription.getLatestInvoiceObject().getPaymentIntentObject().getClientSecret();

Is it possible to associate card details with subscription when calling stripe.confirmCardPayment instead of calling confirmCardSetup first? I thought that if you confirmCardPayment with clientSecret that is associated with subscription, card details will be automatically saved for subscription without first confirming card setup?

The Stripe public docs have a pretty good walkthrough that describes the recommended way to handle this:

https://docs.stripe.com/billing/subscriptions/build-subscriptions?platform=web&ui=elements&lang=java#create-subscription

In short, the flow is:

  • Your backend creates subscription and uses expansion to pass latest_invoice.payment_intent.client_secret back to frontend.
    • The payment_settings.save_default_payment_method parameter can be used to tell Stripe to automatically save the payment method later on.
      https://docs.stripe.com/api/subscriptions/create#create_subscription-payment_settings-save_default_payment_method
  • Your frontend mounts the payment element using the client secret you get from your backend.
  • When the frontend Stripe payments element is submitted, you use stripe.confirmPayment to confirm payment details immediately and try to charge the card. This is also where Stripe will automatically save the payment method, assuming you passed the correct params in step 1.

Here’s the relevant code snippet that shows you how you should configure your server code to save the default payment method and pass the client secret back.

// 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);
  }
);

New contributor

archiverat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật