Implementing Subscription Based Billing Solution Using PayPal

FaxDroid allow users to sign up for monthly subscription plans. These monthly subscription plans provide several advantages over non subscription plans:

  • A dedicated fax number.
  • Ability to receive fax (in addition to being able to send fax)
  • Monthly free page credits

I integrated with PayPal as a payment processor to bill clients.


Starting a Subscription Plan

The figure below describes how a subscription is started:

 

 

1- Country Selection: First the user selects the country they wish their dedicated fax number to be located in. FaxDroid keeps a stock of dedicated fax numbers. Depending on the country selected it returns both the cost associated with getting a number in that country along with a sample number:

2-Plan Selection: The user will then be able to select the type of monthly plan. FaxDroid offers two different plans:

  • Personal: Costs $12/month and provides 200 fax credits a month
  • Business: Costs $24/month and provides 400 fax credits a month

This cost is different than the cost of the number itself. For example if the user selects a number that costs $5/month and the personal plan, the total monthly cost will be $17/month.

At this point FaxDroid knows the total cost associated with the subscription. It checks if this payment plan has already been created in the past and if so it uses the same PLAN_ID. If not then it calls PayPal to create the payment plan https://developer.paypal.com/docs/api/subscriptions/v1/#plans_create.

3-4-Checkout: On the checkout page the user sees a summary of the total expense associated with this plan. If they wish to proceed they will need to complete the PayPal checkout process. Once done a SUBSCRIPTION_ID is returned to the user.

 

5-Validation: The SUBSCRIPTION_ID returned to the server is checked with what the user is subscribing for to ensure the correct plan is being activated. This is to prevent against potential attacks where the user might use a PLAN_ID from a cheaper plan to register for a more expensive plan.

6-Number Assignment: The number that was shown to the user in step 1 is assigned to the user.

7-Monthly Page Credit: Depending on the plan selected a monthly page credit will be applied to their account, allowing them to send and receive faxes.

8-Activate Subscription: At this point the subscription created in step 5 is in APPROVED state. A call is made to PayPal to activate the subscription (https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_activate)


PayPal Webhooks + Crons

Starting the subscription is only part of the job. Once the subscription is created, FaxDroid needs to monitor for several things:

  • Did the first payment fail?
  • Did the customer cancel their subscription? (through PayPal or the FaxDroid dashboard)
  • Did the subscription plan get suspended?

The above are done through a combination of PayPal Webhooks (https://developer.paypal.com/docs/api-basics/notifications/webhooks/) and daily crons.

First Payment Failure

It’s not uncommon for users to miss a payment. This could be due to expired credit cards or lack of funds. While there are cases when a user might fail their first payment however more often than not this happens from fraudulent users. Anytime there is a payment failure PayPal sends FaxDroid a notification. FaxDroid checks if this is the first payment. If the failure belongs to the first payment the subscription is immediately cancelled and the account is terminated.

Plan Cancellation

Users may wish to cancel their subscription plans. This can happen either through the FaxDroid dashboard or through their PayPal account. In both cases the user will be able to remain on the current plan until the end of the billing cycle. Once the billing cycle ends their plan will be downgraded and their dedicated fax number will be released.

To check for plan cancellations from PayPal a webhook is used. Once a plan is cancelled it is marked as cancelled in the database. A daily cron checks all active-cancelled plans if the billing cycle has ended before downgrading the plan.

Description

Implementing a subscription based billing solution using PayPal.

Skills:

  • Rest API
  • PayPal
  • Angular
  • Php + Symfony
  • MySql + Doctrine