Third-party checkout

Use a third-party checkout experience to process customer orders, and make calls to the Recharge API to configure subscriptions.

Leverage a third-party checkout to handle the initial checkout experience completely outside of Recharge. This approach is the most customizable option for checking out customers with the Recharge API integration and lets you own the entire process outside of Recharge's system.

📘

Platform

  • Custom

Overview

After processing the customer’s checkout with your third-party checkout experience, call the Recharge API to set up subscriptions for the recurring products the customer purchased. Using this workflow, your third-party checkout processes all initial purchases. Recharge handles all subsequent recurring purchases.

Handle the initial order through your third-party ecommerce checkout, and then use Recharge's API to:

  • Configure the customer in Recharge
  • Assign a payment token
  • Create the subscription records to process recurring orders

There are three steps required to vault subscriptions within Recharge after the customer checks out:

  1. Create the customer
  2. Create the address
  3. Create the subscription


Step 1 - Create the customer in Recharge

Creating the customer is the first step required to add a subscription to Recharge, after the customer checks out through your third-party checkout.

Use the customer endpoint to look up an existing customer, or create a new one. Before making the customer, you must ensure that the credit card information is processed successfully, and then pass the vaulted payment token through the request.

The following fields are required when creating a customer:

  • Name
  • Email
  • Billing address
  • Vaulted payment processor token
import requests
import json

headers = {
  "Accept": "application/json",
  "Content-Type": "application/json"
  "X-Recharge-Access-Token": "<your_api_token>",
}
url = "https://api.rechargeapps.com/customers"
data = {
  "email": "[email protected]",
  "billing_address1": "3030 Nebraska Avenue",
  "billing_city": "Los Angeles",
  "billing_country": "United States",
  "billing_first_name": "John",
  "billing_last_name": "Smith",
  "billing_phone": "1234567890",
  "billing_province": "California",
  "billing_zip": "90404",
  "first_name": "John",
  "last_name": "Smith",
  "stripe_customer_token": "<customer_payment_token>"
}
result = requests.post(url, json.dumps(data), headers=headers)

See create a customer in the Recharge API Reference Guide for more information.


Step 2 - Create the shipping address

2. Create shipping address

After you create a customer with a valid billing address and payment token, you must create a valid shipping address record. The main data needed for this endpoint is the customer’s shipping address and the customer ID from the previous API call. Use the customer ID in the URL to indicate that you are adding a shipping address to the customer record.

mport json
import requests

headers = {
"Accept": "application/json",
"Content-Type": "application/json" ,
"X-Recharge-Access-Token": "<your_api_token>"

}
url = "https://api.rechargeapps.com/customers/<customer_id>/addresses"
data = {
  "address1": "1776 Washington Street",
  "city": "Los Angeles",
  "company": "ReCharge",
  "country": "United States",
  "first_name": "John",
  "last_name": "Smith",
  "phone": "5551234567",
  "province": "California",
  "shipping_lines_override",
  "zip": "90404",
}

result = requests.post(url, data=json.dumps(data), headers=headers)

See create an address in the Recharge API Reference Guide for more information.


Step 3 - Create the subscription

After creating the address, you must create the subscription record. The main data needed for this endpoint is the subscription properties, address ID, and product ID.

After creating this record, the subscription is fully set up to process on the next scheduled charge date.

import requests
import json

headers = {
  "Accept": "application/json",
  "Content-Type": "application/json",
  "X-Recharge-Access-Token": "<your_api_token>"
}
url = "https://api.rechargeapps.com/subscriptions"
data = {
  "address_id": <address_id>,
  "charge_interval_frequency":"30",
  "next_charge_scheduled_at":"2021-02-10",
  "order_interval_frequency":"30",
  "order_interval_unit":"day",
  "quantity":3,
  "Shopify_variant_id": <product_id>
}

result = requests.post(url, data=json.dumps(data), headers=headers)

See create a subscription in the Recharge API Reference Guide for more information.


Example

The following example explains creating a customer, address, and subscription in succession. After this code executes, the customer’s subscription is vaulted into Recharge so that Recharge can process the customer's subscription on the next scheduled charge date.

import requests
import json

#Checkout Input.  This data would come from your customer's checkout experience.
input = {
    'customer': {
        'email': '[email protected]',
        'first_name': 'John',
        'last_name': 'Smith',
        'billing_first_name': 'John',
        'billing_last_name': 'Smith',
        'billing_address1': '3030 Nebraska Ave',
        'billing_zip': '90404',
        'billing_city': 'Los Angeles',
        'billing_province': 'California',
        'billing_country': 'United States',
        'billing_phone': '1234567890',
        'stripe_customer_token': '<payment_token>',
    },
    'address': {
        'first_name': 'John',
        'last_name': 'Smith',
        'address1': '3030 Nebraska Ave',
        'zip': '90404',
        'city': 'Los Angeles',
        'province': 'California',
        'country': 'United States',
        'phone': '1234567890',
    }
}

# Headers to include in every ReCharge API call
headers = {
    'X-Recharge-Access-Token': '<your_api_token>',
    'Accept': 'application/json',
    'Content-Type': 'application/json',
}

# Data object to hold responses from API calls
data = {}

# Create the Customer record
url = 'https://api.rechargeapps.com/customers'
result = requests.post(url, json.dumps(input['customer']), headers=headers)
data['customer'] = json.loads(result.text)['customer']

# Create the Address record
url = 'https://api.rechargeapps.com/customers/' + str(data['customer']['id']) + '/addresses'
result = requests.post(url, json.dumps(input['address']), headers=headers)
data['address'] = json.loads(result.text)['address']

# Create the Subscription record
url = 'https://api.rechargeapps.com/subscriptions/'
subscription_data = {
    'address_id': <address_id>,
    'next_charge_scheduled_at': '2021-02-10',
    'shopify_variant_id': <product_id>,
    'quantity': 1,
    'order_interval_unit': 'day',
    'order_interval_frequency': 30,
    'charge_interval_frequency': 30,
    'price': 10.0,
}
result = requests.post(url, json.dumps(subscription_data), headers=headers)

#Subscription is now created
print(result.text)

Need Help? Contact Us