Updating a Subscription with Checkout

    Learn how to update the payment method used for future invoices.

    Use the following steps to create a Checkout page that collects your customer’s payment details and returns a Payment Method. Then use the Stripe REST APIs to update the payment method used for future invoices:

    1. Create a Checkout setup mode Session on your server
    2. Add Checkout to your website
    3. Retrieve the Session object on your server
    4. Retrieve the SetupIntent object on your server
    5. Attach the PaymentMethod to the Customer object
    6. Set a default payment method for future invoices

    Step 1: Create a Checkout setup mode Session on your server

    To create a setup mode session, use the mode parameter with a value of setup when creating the Session. See the API reference for a complete list of parameters that you can use for session creation.

    Append the {CHECKOUT_SESSION_ID} template variable to the success_url to get access to the session ID after your customer successfully completes a Checkout Session. You will use this ID in step 3.

    Finally, use the setup_intent_data.metadata dictionary to pass your customer’s existing Stripe customer_id and subscription_id to the Checkout Session. You will use these IDs in step 5 and step 6. Note that there other ways to pass this data to your server, but we’ll use metadata for this guide.

    curl https://api.stripe.com/v1/checkout/sessions \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d "payment_method_types[]=card" \
      -d mode=setup \
      -d "setup_intent_data[metadata][customer_id]=cus_FOsk5sbh3ZQpAU" \
      -d "setup_intent_data[metadata][subscription_id]=sub_8epEF0PuRhmltU" \
      -d success_url="https://example.com/success?session_id={CHECKOUT_SESSION_ID}" \
      -d cancel_url="https://example.com/cancel"
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    session = Stripe::Checkout::Session.create(
      payment_method_types: ['card'],
      mode: 'setup',
      setup_intent_data: {
        metadata: {
          customer_id: 'cus_FOsk5sbh3ZQpAU',
          subscription_id: 'sub_8epEF0PuRhmltU',
        },
      },
      success_url: 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
      cancel_url: 'https://example.com/cancel',
    )
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    session = stripe.checkout.Session.create(
      payment_method_types=['card'],
      mode='setup',
      setup_intent_data={
        'metadata': {
          'customer_id': 'cus_FOsk5sbh3ZQpAU',
          'subscription_id': 'sub_8epEF0PuRhmltU',
        },
      },
      success_url='https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
      cancel_url='https://example.com/cancel',
    )
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    $session = \Stripe\Checkout\Session::create([
      'payment_method_types' => ['card'],
      'mode' => 'setup',
      'setup_intent_data' => [
        'metadata' => [
          'customer_id' => 'cus_FOsk5sbh3ZQpAU',
          'subscription_id' => 'sub_8epEF0PuRhmltU',
        ],
      ],
      'success_url' => 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
      'cancel_url' => 'https://example.com/cancel',
    ]);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    Map<String, Object> params = new HashMap<String, Object>();
    
    ArrayList<String> paymentMethodTypes = new ArrayList<>();
    paymentMethodTypes.add("card");
    params.put("payment_method_types", paymentMethodTypes);
    params.put("mode", "setup");
    
    HashMap<String, Object> setupIntentData = new HashMap<String, Object>();
    HashMap<String, Object> metadata = new HashMap<String, Object>();
    metadata.put("customer_id", "cus_FOsk5sbh3ZQpAU");
    metadata.put("subscription_id", "sub_8epEF0PuRhmltU")
    setupIntentData.put("metadata", metadata);
    params.put("setup_intent_data", setupIntentData);
    
    params.put("success_url", "https://example.com/success?session_id={CHECKOUT_SESSION_ID}");
    params.put("cancel_url", "https://example.com/cancel");
    
    Session session = Session.create(params);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    (async () => {
      const session = await stripe.checkout.sessions.create({
        payment_method_types: ['card'],
        mode: 'setup',
        setup_intent_data: {
          metadata: {
            customer_id: 'cus_FOsk5sbh3ZQpAU',
            subscription_id: 'sub_8epEF0PuRhmltU',
          },
        },
        success_url: 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
        cancel_url: 'https://example.com/cancel',
      });
    })();
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    
    params := &stripe.CheckoutSessionParams{
        PaymentMethodTypes: stripe.StringSlice([]string{
            "card",
        }),
        SetupIntentData: &stripe.CheckoutSessionSetupIntentDataParams{
            Params: stripe.Params{
    						Metadata: map[string]string {
    		            "customer_id": "cus_FOsk5sbh3ZQpAU",
    		            "subscription_id": "sub_8epEF0PuRhmltU",
    	        	},
    	 		  },
    		},
        Mode: stripe.String(string(stripe.CheckoutSessionModeSetup)),
        SuccessURL: stripe.String("https://example.com/success"),
        CancelURL: stripe.String("https://example.com/cancel"),
    }
    
    session, err := session.New(params)
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    var options = new SessionCreateOptions {
        PaymentIntentData = new SessionSetupIntentDataOptions {
            Metadata = new Dictionary<String, String>(){
                {"customer_id", "cus_FOsk5sbh3ZQpAU"},
                {"subscription_id", "sub_8epEF0PuRhmltU"},
            }
        },
        Mode = "setup",
        SuccessUrl = "https://example.com/success",
        CancelUrl = "https://example.com/cancel",
    };
    
    var service = new SessionService();
    Session session = service.Create(options);
    

    Step 2: Add Checkout to your website

    To use Checkout on your website, you must add a snippet of code that includes the id field from the session that you created in the previous step.

    Checkout relies on Stripe.js. To get started, include the following script tag on your website—always load it directly from https://js.stripe.com:

    <script src="https://js.stripe.com/v3/"></script>
    

    Next, create an instance of the Stripe object by providing your publishable API key as the first parameter:

    var stripe = Stripe('pk_test_TYooMQauvdEDq54NiTphI7jx');
    
    const stripe = Stripe('pk_test_TYooMQauvdEDq54NiTphI7jx');
    

    When your customer is ready to save or update their payment method, call redirectToCheckout to begin the setup process. This is where you provide the id from session creation as a parameter to Checkout.

    stripe.redirectToCheckout({
      // Make the id field from the Checkout Session creation API response
      // available to this file, so you can provide it as parameter here
      // instead of the {{CHECKOUT_SESSION_ID}} placeholder.
      sessionId: '{{CHECKOUT_SESSION_ID}}'
    }).then(function (result) {
      // If `redirectToCheckout` fails due to a browser or network
      // error, display the localized error message to your customer
      // using `result.error.message`.
    });
    stripe.redirectToCheckout({
      // Make the id field from the Checkout Session creation API response
      // available to this file, so you can provide it as parameter here
      // instead of the {{CHECKOUT_SESSION_ID}} placeholder.
      sessionId: '{{CHECKOUT_SESSION_ID}}'
    }).then((result) => {
      // If `redirectToCheckout` fails due to a browser or network
      // error, display the localized error message to your customer
      // using `result.error.message`.
    });

    This code is typically invoked from an event handler that triggers in response to an action taken by your customer, such as clicking on a setup button.

    Step 3: Retrieve the Session object on your server

    After a customer successfully completes their Checkout Session, you need to retrieve the Session object. There are two ways to do this:

    • Synchronously: Obtain the sessionId from the URL when a user redirects back to your site (appended to the success_url in step 1). Retrieve the session from /v1/checkout/session/:id.
    • Asynchronously: Handle checkout.session.completed webhooks, which contain a Session object. Learn more about setting up webhooks.

    The right choice depends on your tolerance for dropoff, as customers may not always reach the success_url after a successful payment. It’s possible for them close their browser tab before the redirect occurs. Handling webhooks prevents your integration from being susceptible to this form of dropoff.

    After you have retrieved the Session object, get the value of the setup_intent key, which is a pointer to the SetupIntent created during the Checkout Session.

    Example checkout.session.completed payload:

    {
      "id": "evt_1Ep24XHssDVaQm2PpwS19Yt0",
      "object": "event",
      "api_version": "2019-03-14",
      "created": 1561420781,
      "data": {
        "object": {
          "id": "cs_test_MlZAaTXUMHjWZ7DcXjusJnDU4MxPalbtL5eYrmS2GKxqscDtpJq8QM0k",
          "object": "checkout.session",
          "billing_address_collection": null,
          "cancel_url": "https://example.com/cancel",
          "client_reference_id": null,
          "customer": null,
          "customer_email": null,
          "display_items": [],
          "mode": "setup",
          "setup_intent": "seti_1EzVO3HssDVaQm2PJjXHmLlM",
          "submit_type": null,
          "subscription": null,
          "success_url": "https://example.com/success"
        }
      },
      "livemode": false,
      "pending_webhooks": 1,
      "request": {
        "id": null,
        "idempotency_key": null
      },
      "type": "checkout.session.completed"
    }
    

    Note the setup_intent ID for the next step.

    Step 4: Retrieve the SetupIntent on your server

    Using the setup_intent ID, retrieve the SetupIntent object using the /v1/setup_intents/:id endpoint.

    curl https://api.stripe.com/v1/setup_intents/seti_1EzVO3HssDVaQm2PJjXHmLlM \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc:
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    Stripe::SetupIntent.retrieve('seti_1EzVO3HssDVaQm2PJjXHmLlM')
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    stripe.SetupIntent.retrieve('seti_1EzVO3HssDVaQm2PJjXHmLlM')
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    \Stripe\SetupIntent::retrieve('seti_1EzVO3HssDVaQm2PJjXHmLlM');
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    Stripe.SetupIntent.retrieve("seti_1EzVO3HssDVaQm2PJjXHmLlM");
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    stripe.setupIntents.retrieve(
      'seti_1EzVO3HssDVaQm2PJjXHmLlM',
      function(err, setupIntent) {
        // asynchronously called
      }
    );
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    s, _ := setupintent.Get("seti_1EzVO3HssDVaQm2PJjXHmLlM", nil)
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    var service = new SetupIntentService();
    var setupIntent = service.Get("seti_1EzVO3HssDVaQm2PJjXHmLlM");
    

    Example response:

    {
      "id": "seti_1EzVO3HssDVaQm2PJjXHmLlM",
      "object": "setup_intent",
      "application": null,
      "cancellation_reason": null,
      "client_secret": null,
      "created": 1561420781,
      "customer": null,
      "description": null,
      "last_setup_error": null,
      "livemode": false,
      "metadata": {
        "customer_id": "cus_FOsk5sbh3ZQpAU",
        "subscription_id": "sub_8epEF0PuRhmltU"
      },
      "next_action": null,
      "on_behalf_of": null,
      "payment_method": "pm_1F0c9v2eZvKYlo2CJDeTrB4n",
      "payment_method_types": [
        "card"
      ],
      "status": "succeeded",
      "usage": "off_session"
    }
    

    Note the payment_method ID, customer_id and subscription_id for the next steps.

    Step 5: Attach the PaymentMethod to the customer

    Using the customer ID PaymentMethod ID retrieved in step 4, attach the PaymentMethod to the customer using the /v1/payment_methods/:id/attach endpoint.

    curl https://api.stripe.com/v1/payment_methods/pm_1F0c9v2eZvKYlo2CJDeTrB4n/attach \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -X POST \
      -d customer=cus_FOsk5sbh3ZQpAU
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    Stripe::PaymentMethod.attach(
      'pm_1F0c9v2eZvKYlo2CJDeTrB4n',
      {
        customer: 'cus_FOsk5sbh3ZQpAU',
      }
    )
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    stripe.PaymentMethod.attach(
      'pm_1F0c9v2eZvKYlo2CJDeTrB4n',
      customer='cus_FOsk5sbh3ZQpAU'
    )
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    $payment_method = \Stripe\PaymentMethod::retrieve('pm_1F0c9v2eZvKYlo2CJDeTrB4n');
    $payment_method->attach(['customer' => 'cus_FOsk5sbh3ZQpAU']);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    PaymentMethod paymentMethod = PaymentMethod.retrieve("pm_1F0c9v2eZvKYlo2CJDeTrB4n");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("customer", "cus_FOsk5sbh3ZQpAU");
    paymentMethod.attach(params);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    stripe.paymentMethods.attach(
      'pm_1F0c9v2eZvKYlo2CJDeTrB4n',
      {
        customer: 'cus_FOsk5sbh3ZQpAU',
      },
      function(err, paymentMethod) {
        // asynchronously called
      }
    );
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    params := &stripe.PaymentMethodAttachParams{
      Customer: stripe.String("cus_FOsk5sbh3ZQpAU"),
    }
    p, err := paymentmethod.Attach("pm_1F0c9v2eZvKYlo2CJDeTrB4n", params)
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    var service = new PaymentMethodService();
    var options = new PaymentMethodAttachOptions
    {
      Customer = "cus_FOsk5sbh3ZQpAU",
    };
    var paymentMethod = service.Attach("pm_1F0c9v2eZvKYlo2CJDeTrB4n", options);
    

    Step 6: Set a default payment method for future invoices

    There are two ways to ensure that a payment method is used for future invoices:

    • Set it as the Customer’s invoice_settings.default_payment_method
    • Set it as the Subscription’s default_payment_method

    Setting invoice_settings.default_payment_method on the Customer will cause all future invoices for that customer to be paid with the specified payment method.

    Setting default_payment_method on the Subscription will cause all future invoices for that subscription to be paid with the specified payment method, overriding any invoice_settings.default_payment_method set on the associated Customer.

    Set invoice_settings.default_payment_method on the Customer

    Using the customer ID and the PaymentMethod ID retrieved in step 4, set the invoice_settings.default_payment_method for the Customer using the /v1/customers/:id endpoint.

    curl https://api.stripe.com/https://api.stripe.com/v1/customers/cus_FOsk5sbh3ZQpAU \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d "invoice_settings[default_payment_method]=pm_1F0c9v2eZvKYlo2CJDeTrB4n"
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    Stripe::Customer.update(
      'cus_FOsk5sbh3ZQpAU',
      {
        invoice_settings: {default_payment_method: 'pm_1F0c9v2eZvKYlo2CJDeTrB4n'},
      }
    )
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    stripe.Customer.modify(
      'cus_FOsk5sbh3ZQpAU',
      invoice_settings={'default_payment_method': 'pm_1F0c9v2eZvKYlo2CJDeTrB4n'}
    )
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    \Stripe\Customer::update(
      'cus_FOsk5sbh3ZQpAU',
      [
        'invoice_settings' => ['default_payment_method' => 'pm_1F0c9v2eZvKYlo2CJDeTrB4n'],
      ]
    );
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    Customer customer = Customer.retrieve("cus_FOsk5sbh3ZQpAU");
    Map<String, Object> invoice_settings = new HashMap<>();
    invoice_settings.put("default_payment_method", "pm_1F0c9v2eZvKYlo2CJDeTrB4n");
    Map<String, Object> params = new HashMap<>();
    params.put("invoice_settings", invoice_settings);
    customer.update(params);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    stripe.customers.update(
      'cus_FOsk5sbh3ZQpAU',
      {
        invoice_settings: {default_payment_method: 'pm_1F0c9v2eZvKYlo2CJDeTrB4n'},
      },
      function(err, customer) {
        // asynchronously called
      }
    );
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    params := &stripe.CustomerParams{
        InvoiceSettings: &stripe.CustomerInvoiceSettingsParams{
            DefaultPaymentMethod: stripe.String("pm_1F0c9v2eZvKYlo2CJDeTrB4n"),
        },
    }
    
    c, _ := customer.Update("cus_FOsk5sbh3ZQpAU", params)
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    var options = new CustomerUpdateOptions {
        InvoiceSettings = new CustomerInvoiceSettingsOptions {
            DefaultPaymentMethodId = "pm_1F0c9v2eZvKYlo2CJDeTrB4n",
        },
    };
    
    customerService.Update("cus_FOsk5sbh3ZQpAU", options);
    

    All future invoices for this customer will now charge the new PaymentMethod created with the setup mode Checkout Session.

    Set default_payment_method on the Subscription

    Using the subscription ID and the PaymentMethod ID retrieved in step 4, set the default_payment_method for the subscription using the /v1/subscriptions/:id endpoint.

    curl https://api.stripe.com/v1/subscriptions/sub_8epEF0PuRhmltU \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -X POST \
      -d default_payment_method=pm_1F0c9v2eZvKYlo2CJDeTrB4n
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    Stripe::Subscription.update(
      'sub_8epEF0PuRhmltU',
      {
        default_payment_method: 'pm_1F0c9v2eZvKYlo2CJDeTrB4n',
      }
    )
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    stripe.Subscription.modify(
      'sub_8epEF0PuRhmltU',
      default_payment_method='pm_1F0c9v2eZvKYlo2CJDeTrB4n'
    )
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    \Stripe\Subscription::update(
      'sub_8epEF0PuRhmltU',
      [
        'default_payment_method' => 'pm_1F0c9v2eZvKYlo2CJDeTrB4n',
      ]
    );
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    Subscription subscription = Subscription.retrieve("sub_8epEF0PuRhmltU");
    Map<String, Object> params = new HashMap<>();
    params.put("default_payment_method", "pm_1F0c9v2eZvKYlo2CJDeTrB4n");
    subscription.update(params);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    stripe.subscriptions.update(
      'sub_8epEF0PuRhmltU',
      {
        default_payment_method: 'pm_1F0c9v2eZvKYlo2CJDeTrB4n',
      },
      function(err, subscription) {
        // asynchronously called
      }
    );
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    params := &stripe.SubscriptionParams{
        DefaultPaymentMethod: stripe.String("pm_1F0c9v2eZvKYlo2CJDeTrB4n"),
    }
    
    s, _ := subscription.Update("sub_Ffkm3NXSdcHj4c", params)
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    StripeConfiguration.ApiKey = "sk_test_O6oADPLJLi7NzqtXGMgZQ6PP";
    
    var options = new SubscriptionUpdateOptions {
        DefaultPaymentMethodId = "pm_1F0c9v2eZvKYlo2CJDeTrB4n",
    };
    
    var subscriptionService = new SubscriptionService();
    subscriptionService.Update("sub_Ffkrxmb0cvYSoG", options);
    

    All future invoices for this subscription will now charge the new PaymentMethod created with the setup mode Checkout Session, overriding any invoice_settings.default_payment_method set on the associated Customer.

    Next steps

    Congrats! You can now set a default payment method for future invoices using Checkout setup mode. When testing your integration with your test API key, you can use a test card number to ensure that it works correctly.

    Was this page helpful?

    Thank you for helping improve Stripe's documentation. If you need help or have any questions, please consider contacting support.

    Questions?

    We're always happy to help with code or other questions you might have. Search our documentation, contact support, or connect with our sales team. You can also chat live with other developers in #stripe on freenode.

    On this page