Creating New Subscriptions with the API Creating New Subscriptions with the API

Creating New Subscriptions with the API

This guide assumes you have already set up your subscription plan with one of the integrated payment providers. Currently, integrations with Stripe, Braintree, and Recurly are available.

The process is similar for all providers. First, we need to create a consumer if one doesn't exist already, and then we are going to create a subscription for that consumer.

 

Creating a new consumer

If the consumer doesn't already exist on Zype, we need to create a consumer before we create the subscription. This step is the same for all providers and is also necessary for purchases, rentals, and any other entitlement types. 

To create the consumer we use the Create Consumer endpoint.

curl --request POST \  
--url https://api.zype.com/consumers \ 

In the payload, email is the only required field, and this must be unique. In our example, we'll also add a name.

{
"consumer": {
"email": "consumer@email.com",
"name": "John Smith"
}
}

As a result of the POST request, we will get a consumer object. We need to save the `id` of the consumer, which is required to create the subscription. 

{
"response": {
"_id": "5fb003109906dd00012ca4b6", <= This is the id we need
...
"email": "consumer@email.com",
...
"name": "John Smith",
...
"subscription_plans": [],
"subscription_ids": []
}
}

If the consumer already exists in Zype, you can get the consumer id using the List consumers endpoint or with the Retrieving Access Token Status OAuth Endpoint.

 

Creating the subscription

Now that we have the conumser_id, we need a payment token or nonce code to be used as payment, these are created client-side and they can be created in different ways using the payment provider tools. By doing this we avoid having the credit card information going to your server. 

Stripe, Braintree, and Recurly all have several methods and best practices to create these tokens.

 

Once we have the token or nonce we have all we need to create the subscription. To do so, we use the Create Subscription endpoint.

curl --request POST \ 
--url https://api.zype.com/subscriptions

The parameters in the payload will change depending on the provider, but all require the following:

  • consumer_id - The id of the consumer we got on the previous step
  • plan_id - The id of the subscription plan we are going to use to create the subscription
  • payment token/nonce 
Provider Required Parameter
Stripe stripe_card_token
Braintree braintree_payment_nonce
Recurly recurly_token_id
Third-Party third_party_id

 

The payload for a new Stripe subscription will look like this:

{
"subscription": {
"plan_id": "5cc74821526dbc6bec76adad",
"consumer_id": "5fad8fae320d89000122ad9b",
"stripe_card_token": "tok_1HnT70HrUCAVNZob6o68XYkW"
}
}

That's it, now our consumer has a subscription in Zype and a subscription was created in your payment provider. Any change made directly on the provider will be updated on Zype using the webhook.