Create a Subscription Plan PayPal
This tutorial is meant to be straightforward to highlight the steps to create a subscription plan for your service.
Before we start, know that we can use PayPal api for two modes:
- Testing (sandbox): https://api.paypal.com
- Production (live): via this link https://api-m.sandbox.paypal.com
In this tutorial, we’ll use sandbox mode
Steps to create a subscription plan
- Create a PayPal developer account in this link
- Create an app, give it any name, that represents your app for which you want to create a PayPal subscription plan.
- Get client_id and secret_id, you’ll store these two credentials in the environment variables for your app as you’ll need them to get the access token to perform different operations with PayPal (such as new users subscribing to your service).
- Get access token
Run the following command to get the access token and save it somewhere as you’ll need it in the upcoming steps.
curl -v -X POST https://api-m.sandbox.paypal.com/v1/oauth2/token \
-u "YOUR_CLIENT_ID:YOUR_SECRET" \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-d "grant_type=client_credentials"
Or in code in python:
def get_access_token(self):
try:
response = requests.post(
# This url for sandbox; https://api.paypal.com//v1/oauth2/token for live
'https://api-m.sandbox.paypal.com/v1/oauth2/token',
auth=(YOUR_CLIENT_ID, YOUR_CLIENT_SECRET),
data={'grant_type': 'client_credentials'},
headers={'Content-Type': 'application/x-www-form-urlencoded'}
)
response.raise_for_status()
return response.json()['access_token']
except requests.exceptions.RequestException as e:
error_data = response.json()
raise ValueError(error_data.get('error_description', 'Failed to obtain access token'))
5. Create a product
You need to create a product so that you can associate it with your plans. Run the following curl command and use the access token from the previous step to create a given product, and then save somewhere this product ID as you’ll need it in further operations.
curl -v -X POST https://api-m.sandbox.paypal.com/v1/catalogs/products \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-d '{
"name": "Video Streaming Service",
"description": "Video Streaming Service",
"type": "SERVICE",
"category": "SOFTWARE",
"home_url": "https://example.com/home"
}'
6. Create your subscription plans
Use your ACCESS_TOKEN and PRODUCT_ID from the two previous steps, and run the following command to create your subscription plan. In this example, we create a plan that we name “Basic Plan”, you can replace by the name you want for your plan. For this example, the plan is monthly.
curl -v -X POST https://api-m.sandbox.paypal.com/v1/billing/plans \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-d '{
"product_id": "YOUR_PRODUCT_ID",
"name": "Basic Plan",
"description": "Basic Plan",
"billing_cycles": [
{
"tenure_type": "REGULAR",
"sequence": 1,
"frequency": {
"interval_unit": "MONTH",
"interval_count": 1
},
"total_cycles": 0,
"pricing_scheme": {
"fixed_price": {
"value": "100",
"currency_code": "USD"
}
}
}
],
"payment_preferences": {
"auto_bill_outstanding": true,
"setup_fee": {
"value": "0",
"currency_code": "USD"
},
"setup_fee_failure_action": "CONTINUE",
"payment_failure_threshold": 3
},
"taxes": {
"percentage": "0",
"inclusive": false
}
}'
This way, you have created your subscription plan on PayPal. You can also create, view, and update the subscription plans from the UI, buy using the business account from your developer account and logging to https://www.sandbox.paypal.com/billing/subscriptions .