Billing
Bacs Direct Debit in the UK

Set up a subscription with Bacs Direct Debit in the UK

Learn how to create and charge for a subscription with Bacs Direct Debit.

Use this guide to set up a subscription and charge a customer. It includes some initial product setup, including how much you want to charge for it.

As you complete these steps, use the Developers section in the Dashboard to help test and debug your code. You can find your API keys there as well.

1 Create a product and price Dashboard

Products are what you sell, and you need to create at least one before you can charge for it. This guide uses a stock photo service as an example. Generally, if you only have a few products, you should create and manage them in the Dashboard.

To create a product in the Dashboard:

 

  1. Navigate to the Add a product page.

  2. Enter a Name for your product.

  3. Scroll down to the Pricing section.

  4. Create your price by entering in price data.

  5. Click the Save product button.

 

 

 

This creates the product and any entered prices and automatically opens the product detail page. Prices define how much and how frequently you charge for a product. This includes how much the product costs, what currency to use, and the billing period to use for recurring charges. For each currency you charge, a separate price should be created. Similar to products, if you only have a few prices it’s preferable to manage them in the Dashboard.

In this guide, a recurring quantity is used. This means customers are charged the same amount on a recurring basis. For example, charging 15 GBP a month for a streaming service or 120 GBP for a yearly subscription to an online publication. From the product detail page, click the Add price (or Add another price) button:

  1. In the Price field, enter 15.
  2. In the currency field, select GBP.
  3. Click the Add price button.

You have several other options for creating prices, but this basic example results in a price that:

  • Uses GBP
  • Costs 15 GBP per subscription
  • Charges customers monthly

After you create the price, record the ID so it can be used in subsequent steps. It’s displayed on the pricing page and should look similar to this: price_G0FvDp6vZvdwRZ.

2 Create a Customer Server-side

To reuse a Bacs Direct Debit payment method for future payments, it must be attached to a Customer.

You should create a Customer object when your customer creates an account with your business. Associating the ID of the Customer object with your own internal representation of a customer will enable you to retrieve and use the stored payment method details later. If your customer is making a payment as a guest, you can still create a Customer object before payment and associate it with your internal representation of the customer’s account later.

Create a new customer or retrieve an existing customer to associate with this payment. Include the following code on your server to create a new customer.

curl https://api.stripe.com/v1/customers \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \ -X POST
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' customer = Stripe::Customer.create
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' customer = stripe.Customer.create()
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); $customer = \Stripe\Customer::create();
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; CustomerCreateParams params = CustomerCreateParams.builder() .build(); Customer customer = Customer.create(params);
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); const customer = await stripe.customers.create();
// Set your secret key. Remember to switch 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{} c, _ := customer.New(params)
// Set your secret key. Remember to switch 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 CustomerCreateOptions{}; var service = new CustomerService(); var customer = service.Create(options);

3 Create a Session for collecting payment details Server-side

Before you can accept Direct Debit payments, your customer must provide their bank account information and give permission to debit their account (also known as a mandate). You can collect all of this using a setup mode Session in Stripe Checkout.

To create a setup mode Session, use the mode parameter with a value of setup when creating the Session.

After your customer provides their payment method details, they are redirected to the success_url, a page on your website that informs your customer their payment method was saved successfully.

When your customer clicks on your logo in a Checkout Session without providing their payment method details, Checkout redirects them back to your website by navigating to the cancel_url. Typically, this is the page on your website that the customer viewed prior to redirecting to Checkout.

See the Checkout Session API reference for a complete list of parameters that you can use for Session creation.

curl https://api.stripe.com/v1/checkout/sessions \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \ -d "payment_method_types[]"=bacs_debit \ -d mode=setup \ -d customer="{{CUSTOMER_ID}}" \ -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 switch 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: ['bacs_debit'], mode: 'setup', customer: customer.id, success_url: 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}', cancel_url: 'https://example.com/cancel', )
# Set your secret key. Remember to switch 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=['bacs_debit'], mode='setup', customer=customer.id, success_url='https://example.com/success?session_id={CHECKOUT_SESSION_ID}', cancel_url='https://example.com/cancel', )
// Set your secret key. Remember to switch 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' => ['bacs_debit'], 'mode' => 'setup', 'customer' => $customer->id, 'success_url' => 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}', 'cancel_url' => 'https://example.com/cancel', ]);
// Set your secret key. Remember to switch 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("bacs_debit"); params.put("payment_method_types", paymentMethodTypes); params.put("mode", "setup"); params.put("customer", customer.getId()); 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 switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); const session = await stripe.checkout.sessions.create({ payment_method_types: ['bacs_debit'], mode: 'setup', customer: customer.id, success_url: 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}', cancel_url: 'https://example.com/cancel', });
// Set your secret key. Remember to switch 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{ "bacs_debit", }), Mode: stripe.String(string(stripe.CheckoutSessionModeSetup)), Customer: stripe.String(c.ID), SuccessURL: stripe.String("https://example.com/success?session_id={CHECKOUT_SESSION_ID}"), CancelURL: stripe.String("https://example.com/cancel"), } s, err := session.New(params)
// Set your secret key. Remember to switch 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 { PaymentMethodTypes = new List<string> { "bacs_debit", }, Mode = "setup", Customer = customer.Id, SuccessUrl = "https://example.com/success?session_id={CHECKOUT_SESSION_ID}", CancelUrl = "https://example.com/cancel", }; var sessionService = new SessionService(); var session = sessionService.Create(options);

Make the Session ID available on your success page by including the {CHECKOUT_SESSION_ID} template variable in the success_url as in the above example.

4 Redirect the customer to collect their payment details Client-side

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>
npm install @stripe/stripe-js

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

var stripe = Stripe('pk_test_TYooMQauvdEDq54NiTphI7jx');
import {loadStripe} from '@stripe/stripe-js'; const stripe = await loadStripe('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.

var checkoutButton = document.getElementById('checkout-button'); checkoutButton.addEventListener('click', function() { stripe.redirectToCheckout({ // Make the id field from the Checkout Session creation API response // available to this file, so you can provide it as argument 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`. }); });
const checkoutButton = document.getElementById('checkout-button'); checkoutButton.addEventListener('click', () => { stripe.redirectToCheckout({ // Make the id field from the Checkout Session creation API response // available to this file, so you can provide it as argument here // instead of the {{CHECKOUT_SESSION_ID}} placeholder. sessionId: '{{CHECKOUT_SESSION_ID}}' }) // If `redirectToCheckout` fails due to a browser or network // error, display the localized error message to your customer // using `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.

5 Retrieve the PaymentMethod Server-side

After a customer submits their payment details, retrieve the PaymentMethod object that was created. A PaymentMethod is used to store the customer’s bank account information for future payments. Retrieving the PaymentMethod can be done synchronously using the success_url or asynchronously using 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.

Handle checkout.session.completed webhooks, which contain a Session object. Learn more about setting up webhooks.

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 value of the setup_intent key, which is the ID for the SetupIntent created during the Checkout Session. A SetupIntent is an object used to set up the customer’s bank account information for future payments.

Retrieve the SetupIntent object with the ID.

curl https://api.stripe.com/v1/setup_intents/seti_1EzVO3HssDVaQm2PJjXHmLlM \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc:
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' intent = Stripe::SetupIntent.retrieve('seti_1EzVO3HssDVaQm2PJjXHmLlM')
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' intent = stripe.SetupIntent.retrieve('seti_1EzVO3HssDVaQm2PJjXHmLlM')
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); $intent = \Stripe\SetupIntent::retrieve('seti_1EzVO3HssDVaQm2PJjXHmLlM');
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; SetupIntent intent = Stripe.SetupIntent.retrieve("seti_1EzVO3HssDVaQm2PJjXHmLlM");
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); const intent = await stripe.setupIntents.retrieve('seti_1EzVO3HssDVaQm2PJjXHmLlM');
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc" intent, _ := setupintent.Get("seti_1EzVO3HssDVaQm2PJjXHmLlM", nil)
// Set your secret key. Remember to switch 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 intent = service.Get("seti_1EzVO3HssDVaQm2PJjXHmLlM");

Obtain the session_id from the URL when a user redirects back to your site.

Retrieve the Session object.

curl https://api.stripe.com/v1/checkout/sessions/{{SESSION_ID}} \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \ -d "expand[]"=setup_intent
# Set your secret key. Remember to switch 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.retrieve({ id: '{{SESSION_ID}}', expand: ['setup_intent'], }) intent = session.setup_intent
# Set your secret key. Remember to switch 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.retrieve( id='{{SESSION_ID}}', expand=['setup_intent'], ) intent = session.setup_intent
// Set your secret key. Remember to switch 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::retrieve([ 'id' => '{{SESSION_ID}}', 'expand' => ['setup_intent'], ]); $intent = $session->setup_intent;
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; SessionRetrieveParams params = SessionRetrieveParams.builder() .addExpand("setup_intent") .build(); Session session = Stripe.Session.retrieve( "{{SESSION_ID}}", params, null ); SetupIntent intent = session.getSetupIntentObject();
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); const session = await stripe.checkout.sessions.retrieve( '{{SESSION_ID}}', { expand: ['setup_intent'], } ); const intent = session.setup_intent;
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc" checkoutSessionParams := &stripe.CheckoutSessionParams{} checkoutSessionParams.AddExpand("setup_intent") sess, _ := session.Get("{{SESSION_ID}}", checkoutSessionParams) intent := sess.SetupIntent;
// Set your secret key. Remember to switch 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 SessionGetOptions(); options.AddExpand("setup_intent"); var sessionService = new SessionService(); var session = sessionService.Get("{{SESSION_ID}}", options); var intent = session.SetupIntent;

Note the SetupIntent created during the Checkout Session. A SetupIntent is an object used to set up the customer’s bank account information for future payments.

6 Create the subscription Server-side

Create the Subscription using the Price, Customer and PaymentMethod from the previous steps.

curl https://api.stripe.com/v1/subscriptions \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \ -d customer="{{CUSTOMER_ID}}" \ -d default_payment_method="{{PAYMENT_METHOD_ID}}" \ -d "items[0][price]"="{{PRICE_ID}}"
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' subscription = Stripe::Subscription.create({ customer: intent.customer, default_payment_method: intent.payment_method, items: [{ price: '{{PRICE_ID}}', }], })
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' subscription = stripe.Subscription.create( customer=intent.customer, default_payment_method=intent.payment_method, items=[{ 'price': '{{PRICE_ID}}', }], )
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); $subscription = \Stripe\Subscription::create([ 'customer' => $intent->customer, 'default_payment_method' => $intent->payment_method, 'items' => [[ 'price' => '{{PRICE_ID}}', ]], ]);
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; SubscriptionCreateParams params = SubscriptionCreateParams.builder() .setCustomer(intent.getCustomer()) .setDefaultPaymentMethod(intent.getPaymentMethod()) .addItem( SubscriptionCreateParams.Item.builder() .setPrice("{{PRICE_ID}}") .build()) .build(); Subscription subscription = Subscription.create(params);
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); const subscription = await stripe.subscriptions.create({ customer: intent.customer, default_payment_method: intent.payment_method, items: [{ price: '{{PRICE_ID}}', }], });
// Set your secret key. Remember to switch 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{ Customer: stripe.String(intent.Customer.ID), DefaultPaymentMethod: stripe.String(intent.PaymentMethod.ID), Items: []*stripe.SubscriptionItemsParams{ { Price: stripe.String("{{PRICE_ID}}"), }, }, } s, _ := sub.New(params)
// Set your secret key. Remember to switch 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 SubscriptionCreateOptions { Customer = intent.CustomerId, DefaultPaymentMethod = intent.PaymentMethodId, Items = new List<SubscriptionItemOptions> { new SubscriptionItemOptions { Price = "{{PRICE_ID}}", }, } }; var service = new SubscriptionService(); Subscription subscription = service.Create(options);

Creating subscriptions automatically charges customers because the default payment method is set. After a successful payment, the status in the Stripe Dashboard changes to Active. The price you created earlier determines subsequent billings.

7 Manage the subscription status Server-side

Assuming the initial payment succeeds, the state of the subscription is active and no further action is needed. When payments fail, the status is changed to the Subscription status configured in your automatic collection settings. You should notify the customer on failure and charge them with a different payment method.

8 Test the integration

There are several test bank account numbers you can use in test mode to make sure this integration is ready.

Sort code Account number Description
10-88-00 00012345 The payment succeeds and the Invoice transitions to paid.
10-88-00 90012345 The payment succeeds after three minutes and the Invoice transitions to paid.
10-88-00 33333335 The payment fails with a debit_not_authorized failure code and the Invoice transitions to open. The Mandate becomes inactive and the PaymentMethod can not be used again.
10-88-00 93333335 The payment fails after three minutes with a debit_not_authorized failure code and the Invoice transitions to open. The Mandate becomes inactive and the PaymentMethod can not be used again.
10-88-00 22222227 The payment fails with an insufficient_funds failure code and the Invoice transitions to open. The Mandate remains active and the PaymentMethod can be used again.
10-88-00 92222227 The payment fails after three minutes with an insufficient_funds failure code and the Invoice transitions to open. The Mandate remains active and the PaymentMethod can be used again.
10-88-00 00033333 Payment detail collection succeeds, but the Mandate transitions immediately to inactive.
10-88-00 00044444 The request to set up Bacs Direct Debit fails immediately due to an invalid account number and the customer is prompted to update their information before submitting. Payment details are not collected.

You can test using any of the account numbers provided above. However, as Bacs Direct Debit payments take several days to process, you should use the test account numbers that operate on a three-minute delay to better simulate the behavior of live payments.

Optional Setting the billing cycle

By default, the billing cycles for subscriptions are set automatically when they’re created. For example, if a customer subscribes to a monthly plan on September 7th, they’re billed on the 7th of every month after that. Some businesses prefer to set the billing cycle manually so that their customers are charged at the same time. The billing cycle anchor argument allows you to do this.

curl https://api.stripe.com/v1/subscriptions \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \ -d customer=cus_4fdAW5ftNQow1a \ -d "items[0][price]"=price_CBb6IXqvTLXp3f \ -d billing_cycle_anchor=1604836136
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' subscription = Stripe::Subscription.create({ customer: 'cus_4fdAW5ftNQow1a', items: [ { price: 'price_CBb6IXqvTLXp3f', }, ], billing_cycle_anchor: 1604836136, })
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' subscription = stripe.Subscription.create( customer='cus_4fdAW5ftNQow1a', items=[ { 'price': 'price_CBb6IXqvTLXp3f', }, ], billing_cycle_anchor=1604836136, )
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); $subscription = \Stripe\Subscription::create([ 'customer' => 'cus_4fdAW5ftNQow1a', 'items' => [ [ 'price' => 'price_CBb6IXqvTLXp3f', ], ], 'billing_cycle_anchor' => 1604836136, ]);
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; SubscriptionCreateParams params = SubscriptionCreateParams.builder() .setCustomer("cus_4fdAW5ftNQow1a") .addItem( SubscriptionCreateParams.Item.builder() .setPrice("price_CBb6IXqvTLXp3f") .build()) .setBillingCycleAnchor(1604836136L) .build(); Subscription subscription = Subscription.create(params);
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); const subscription = await stripe.subscriptions.create({ customer: 'cus_4fdAW5ftNQow1a', items: [ { price: 'price_CBb6IXqvTLXp3f', }, ], billing_cycle_anchor: 1604836136, });
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc" items := []*stripe.SubscriptionItemsParams{ { Price: stripe.String("price_CBb6IXqvTLXp3f"), }, } params := &stripe.SubscriptionParams{ Customer: stripe.String("cus_4fdAW5ftNQow1a"), Items: items, BillingCycleAnchor: stripe.Int64(1604836136), } subscription, _ := sub.New(params)
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; var items = new List<SubscriptionItemOptions> { new SubscriptionItemOptions {Price = "price_CBb6IXqvTLXp3f"} }; var options = new SubscriptionCreateOptions { Customer = "cus_4fdAW5ftNQow1a", Items = items, BillingCycleAnchor = DateTimeOffset.FromUnixTimeSeconds(1604836136).UtcDateTime, }; var service = new SubscriptionService(); Subscription subscription = service.Create(options);

When setting the billing cycle manually, the customer is automatically charged a prorated amount for the time between the subscription being created and the billing cycle anchor. If you don’t want to charge customers for this time, you can set the prorate argument to false. You can also combine the billing cycle anchor with trial periods to give users free access to your product and then charge them a prorated amount.

Optional Subscription trials

Free trials allow customers access to your product for a period of time for free. Using free trials is different from setting prorate to false because you can customize how long the free period lasts. You can set the trial period by passing a timestamp in the trial end argument.

curl https://api.stripe.com/v1/subscriptions \ -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \ -d customer=cus_4fdAW5ftNQow1a \ -d "items[0][price]"=price_CBb6IXqvTLXp3f \ -d trial_end=1604231336
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' subscription = Stripe::Subscription.create({ customer: 'cus_4fdAW5ftNQow1a', items: [ { price: 'price_CBb6IXqvTLXp3f', }, ], trial_end: 1604231336, })
# Set your secret key. Remember to switch to your live secret key in production! # See your keys here: https://dashboard.stripe.com/account/apikeys stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' subscription = stripe.Subscription.create( customer='cus_4fdAW5ftNQow1a', items=[ { 'price': 'price_CBb6IXqvTLXp3f', }, ], trial_end=1604231336, )
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); $subscription = \Stripe\Subscription::create([ 'customer' => 'cus_4fdAW5ftNQow1a', 'items' => [ [ 'price' => 'price_CBb6IXqvTLXp3f', ], ], 'trial_end' => 1604231336, ]);
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; SubscriptionCreateParams params = SubscriptionCreateParams.builder() .setCustomer("cus_4fdAW5ftNQow1a") .addItem( SubscriptionCreateParams.Item.builder() .setPrice("price_CBb6IXqvTLXp3f") .build()) .setTrialEnd(1604231336L) .build(); Subscription subscription = Subscription.create(params);
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); const subscription = await stripe.subscriptions.create({ customer: 'cus_4fdAW5ftNQow1a', items: [ { price: 'price_CBb6IXqvTLXp3f', }, ], trial_end: 1604231336, });
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc" items := []*stripe.SubscriptionItemsParams{ { Price: stripe.String("price_CBb6IXqvTLXp3f"), }, } params := &stripe.SubscriptionParams{ Customer: stripe.String("cus_4fdAW5ftNQow1a"), Items: items, TrialEnd: stripe.Int64(1604231336), } subscription, _ := sub.New(params)
// Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys StripeConfiguration.ApiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"; var items = new List<SubscriptionItemOptions> { new SubscriptionItemOptions {Price = "price_CBb6IXqvTLXp3f"} }; var options = new SubscriptionCreateOptions { Customer = "cus_4fdAW5ftNQow1a", Items = items, TrialEnd = DateTimeOffset.FromUnixTimeSeconds(1604231336).UtcDateTime, }; var service = new SubscriptionService(); Subscription subscription = service.Create(options);

You can combine a billing cycle anchor with a free trial if you want. For example, let’s say it’s September 15th and you want to give your customer a free trial for seven days and then start the normal billing cycle on October 1st. You can set the free trial to end on September 22nd and the billing cycle anchor to October 1st. This gives the customer a free trial for seven days and then charges a prorated amount for the time between the trial ending and October 1st. On October 1st, the customer is charged the normal subscription amount for their first full billing cycle.

Was this page helpful?
Questions? Contact us.
Developer tutorials on YouTube.