paypal_das_bioAbout the author: Avi (Ananya) Das is a Software Engineer working on Platform at PayPal, where he develops native SDKs, SDK infrastructure, tooling and docs that enable merchants and developers to seamlessly integrate PayPal in their applications.

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.

Setting up

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.

Merchant views

Billing Plans

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 BillingPlan and 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.

The POST call made on the activate endpoint is handled by the activate method which calls billing_plan.replace with state set to ACTIVE.

Customer views

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.

Billing Agreements

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 payment method, shipping address and other customer specific information.

We instantiate the BillingAgreement class with the necessary attributesrequest.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 payment_method is 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 rel being 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.

Future improvements

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.

 

Comments are closed.