With the release of the subscription APIs from PayPal, it is possible to create your anything-as-a-service buisness faster than ever. SDK integrations provide a native way to integrate with the PayPal APIs in the language of your choice. In this tutorial, we will walk through a sample application built using the PayPal Python SDK, Flask, Bootstrap for UI and Heroku for deployment. The finished app is deployed on Heroku and it’s codebase is available on GitHub.
If you have not already, get your credentials from the PayPal Developer Portal. Afterwards, set up the Python SDK. It may also be worth checking out Flask to see how to set up a simple web application (a hello world is only 7 lines long!). This blogpost gives an introduction to get started with subscription APIs.
Billing plans outline the details of a subscription which can be used for multiple billing agreements between merchants and buyers. To use those APIs in the SDK, import the
BillingAgreement classes in your app, instantiate the
BillingPlan class with the plan attributes and call create() on the instance afterwards.
In the app, the create endpoint maps to the function being responsible for creating a billing plan. The billing plan attributes are fetched by using
request.form from Flask which allows access over HTML form inputs as a Python dictionary.
As a merchant / developer your most common use case may be viewing the billing plans you have already created.
To display the plans in a HTML page, you can use your preferred templating engine. In this tutorial we are going to use Jinja – one of the most used template engines for Python. Also we’ll leverage Bootstrap’s grid system by using the col-lg-7 tag to ensure that the columns line up properly on the HTML list.
A plan needs to be activated after creation to have customers subscribing to it. The activate button sends a
POST request to the activate endpoint, appending the plan’s id as a parameter. url_for is the Flask way of generating a url to the given endpoint with the method provided, activate in the following example.
POST call made on the activate endpoint is handled by the activate method which calls billing_plan.replace with state set to
At this point your users will be able to subscribe to the plan. In our application the user will be presented with his active plans after logging in. The form provides the plan’s id via a
POST request to the subscribe endpoint.
The subscribe endpoint introduces the second key component of our Subscription APIs: the billing agreement. The billing plans act as a template for billing agreements which you can have users subscribe to with
shipping address and other customer specific information.
We instantiate the
BillingAgreement class with the necessary attributes –
request.args.get('id', '') gives us the plan’s id used to create the agreement. After having received the id we can call create() on the instance to create the agreement.
In this example, the
paypal, so the next step of the process is redirecting the user to PayPal for approving the payment. The
billing_agreement created returns a list of HATEOS links where the link with the attribute
approval_url is the url you want to redirect the user to.
Creating an agreement with credit card is possible similar to creating a payment.
After the user has approved the agreement, they will be redirected to the
return_url you have set while creating the billing plan. For this application, that is the execute endpoint. PayPal appends a payment token to your
return_url as a parameter which you need to pass into the
BillingAgreement.execute method of the SDK to execute the agreement.
This is the full flow of creating a billing plan and having a user subscribe to it. You may want to display the details of the created plan to the user – done via calling the
find method of the
BillingAgreement class. A very common use case would be for a user to see their payment history which can be done via calling
search_transactions on the agreement and passing in the start_date and end_date as requested.
Check out the API specs to see what else is possible with billing agreements.
When using Heroku and deploying a Flask-based Python app you’d might want to check out this great tutorial.
We hope that that when your buisness has a need for subscription based payments you’ll consider the PayPal APIs as your solution. When moving into production you’d want to validate your form and use a database and caching to improve your user experience.
- Design Thinking: Out of the Classroom, Into the Fire
- How WooCommerce and Developer Agencies Optimize Payments for Small Businesses with PayPal
- New REST API Feature: Setting a Receiver for Payments
- PayPal is Now Available Through WooCommerce 2.6 Onboarding Wizard
Connect with us!
- April 2017
- March 2017
- January 2017
- December 2016
- October 2016
- September 2016
- July 2016
- May 2016
- March 2016
- November 2015
- September 2015
- August 2015
- June 2015
- April 2015
- March 2015
- November 2014
- October 2014
- August 2014
- July 2014
- March 2014
- February 2014
- January 2014
- December 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- April 2013
- March 2013