adam-nir-wTO6MWpMrJk-unsplash

Integrate with the Stripe API

See how to use no-code to build billing applications by directly calling the Stripe API

Introduction

Stripe has been a game-changer for online businesses by simplifying the process of collecting payments from customers. Using the Stripe platform,  it is easy to set up products and create payment links that can be added to your website to collect payments. Unfortunately, each payment collected in this way generates a new customer record. If you would like to tie payments to customers that make repeat purchases on your site, then you will need to do something differently.

You can also use this method to create a single checkout from a shopping cart that has multiple items in different quantities.

There are many third-party plugins and software that have already done this, however, if you like to do things yourself, and build a custom page on your site, then using the Stripe API directly is the way to go.

1. Setup

First of all, you will need to set up your products and pricing on the Stripe site. Each product/pricing combination will generate a pricing ID that looks like this: 

price_1CjYUxAYwuieuLywI3NtK0bP

You will use these pricing IDs later when sending payment links to your customers. Go to the Products tab in Stripe to set these up.

You will also need your key which will be used as a bearer token in API requests. This is available as the “Secret key” on the home page. Note that you can do all of this in “Test Mode” in Stripe until you have perfected the process. There are test credit card numbers that they provide, that can be used to simulate a transaction.

2. Workflow

This example here assumes that a known customer comes to your site, and selects a product and a quantity to purchase for a subscription product. From this information, you will generate a payment link that the user can click on and complete the payment process for that product.

Once the user has paid and entered their credit card information, Stripe will know to automatically bill that customer each month (or year) thereafter.

3. Stripe endpoints

Stripe has a fairly extensive API and many endpoints that you can use. A couple of things to note:

  • Search parameters are not supported, however as this feature is in Beta, it should be available soon 
  • POST uses x-www-form-urlencoded

 

We will use the following 3 endpoints:

Customers: Create (or read) a customer record 

Checkout/Sessions: Creates a checkout URL with the product order details to provide to the customer. 

Subscriptions: Retrieves the details of a subscription created from the checkout process

For further details on these endpoints, consult the Stripe API documentation

 

4. Create a customer

Create customers in Stripe  using the “customers” endpoint. This POST request will create a customer with a unique id of “nasso” and a company name of “Nasso Corporation”

POST https://api.stripe.com/v1/customers?name=Nasso Corporation&id=nasso

 

The id will become the unique identifier for this customer in Stripe. You will be able to see all the subscriptions and products together for this customer in one place. You can set up the main billing email here using this endpoint as well.

Note that the id is optional, and Stripe actually recommends that you let it generate the unique customer id for you.. 

Reference: https://stripe.com/docs/api/customers

 

5. Create a checkout URL

The checkout/sessions endpoint will generate a checkout URL for your customer with the items and quantity that they will be purchasing.

Include the following parameters in the request:

POST https://api.stripe.com/v1/checkout/sessions?customer=nasso&success_url=https://rulyapp.com/success&cancel_url=https://rulyapp.com/cancel&line_items[0][price]=price_1KikSxKYwoinuLywzk8Mw0OX&line_items[0][quantity]=15&mode=subscription&billing_address_collection=required

customer: customer id that was created in Step 4

success_url: A web page that will display after the customer has paid successfully. This can be a “Thank you for your order” page. (this is required)

cancel_url: Same as above, but if the customer cancels the checkout process before completion (also required)

line_items: Array of items (price, quantity) that the customer is purchasing. The “price” is the same price id that was created in Step 1.

mode: Set to “subscription” for a recurring charge

billing_address_collection: Set this to “required” to save the billing address that is collected at the time of payment with the customer record

 

6. Map the POST response

The checkout/session endpoint returns a session object that includes the following 2 properties that you will use.

url: This is the checkout URL that you will pass on to the customer

id: The unique id for this checkout session

Reference: https://stripe.com/docs/api/checkout/sessions

 

7. Verify that the user has checked out

Using the session id retrieved from Step 6, you can use polling to go back to the checkout/session and confirm that the user has checked out. Note that Stripe also provides an alternative webhook method that will post to your site automatically, notifying you that the user has completed checkout.

To check the status use:

GET https://api.stripe.com/v1/checkout/sessions/<<StripeSessionID>>

The screenshot shows this call from an API Operation in the Ruly platform, and mapping the responses from the call to field values in a database table. The endpoint returns the following values (among others):

status: “complete” when the checkout process is finished or “incomplete”

payment_status: either “paid” or “unpaid”

subscription: subscription ID. This will be used later to get details from the subscription endpoint

amount_total: total value of the monthly (yearly) subscription payment. Note that this value is in cents and needs to be divided by 100 to convert to dollars.

amount_subtotal: the value of the subscription payment less tax 

 

8. Check on the subscription status

The checkout process above automatically created a subscription object in Stripe. In Step 5, the mode was set to “subscription”. You can retrieve details on this object by calling the subscription endpoint using the subscription id value returned from Step 7:

GET https://api.stripe.com/v1/subscriptions/<<StripeSubscriptionID>>

There are two important values it returns:

status: either “active” if the subscription is paid and up to date or “inactive”

cancel_at_period_end: if set to true an active subscription will end at the next billing cycle

Reference: https://stripe.com/docs/api/subscriptions/object

 

Summary

I hope you found this guide useful. If you have any questions or comments, you can reach me at stan.marsden@rulyapp.com