Developing a Free Trial with Stripe using PHP and JS — Getting Started

Pawel Duda
Busuu Tech
Published in
7 min readJul 2, 2021

--

Busuu’s Free Trial
  1. About me and Busuu
  2. Scope of the article and definitions
  3. Why Stripe?
  4. Payments options — the difference between Setup Intent and Payment Intent
  5. Steps from a Technical standpoint to create a Free Trial with Stripe
  6. Useful Webhooks from Stripe that you can use when implementing a Free Trial with Stripe
  7. Why Developers Adore Stripe
  8. Final Thoughts

Hi! I’m Pawel, a Backend-end engineer at Busuu, based in London, UK. We specialise in language learning and our goal is to break down barriers and empower everyone to learn a new language.

The one thing that is our speciality and what our customers love is learning together as a community, therefore there’s plenty of opportunities to do that in the app i.e. you can have your written or spoken exercises examined by other users and get feedback from them. The learning process is enhanced by AI techniques that analyse your style of learning and suggest which part of the material you should be focusing on to make better progress.

Lunchtime at Busuu

Scope of the article and definitions

I’m going to show a quick overview of how can Stripe be used to create your “Free Trial” feature.

  • How the payment processor Stripe can be employed to create a free trial for your customers.
  • What are the convenient features that Stripe offers to developers.

Let’s talk about the actors in the system:

  • App Developer — Busuu, maintaining communication with Stripe using an API
  • Stripe — online payment processor, a series of APIs
  • Customer — Busuu’s customer, our dear language learner

Why Stripe?

  • Provides a billing product that allows App Developers like Busuu to manage Customers’ subscriptions and issue invoices
  • Sends webhook events to App Developers on user actions or subscription-related events
  • Provides two ways of processing payment APIs:

— The Payments Intent API

— The Setup Intent API

  • Stripe enables JavaScript and React.js versions of libraries to use with the ready elements. Check out their sandbox.
  • Automatic Authentication — Stripe supports both “3D Secure” and “3D Secure 2” — methods of 2 Factor Authentication. 3DS2 is important here as it allows App Developers like Busuu to set up payments in the future where only the first payment requires 2FA and the subsequent will not need the user input. A great convenience to manage subscription payments.
  • Provides APIs that are backward compatible
  • Stripe Radar (automatically assessing whether to take more security measures for a payment request)
  • Reporting with configurable charts (MRR, Churn, Free Trial uptake, etc.)
  • Strong and reliable testing environment (webhook data, HTTP requests’ content, succeeded vs failed requests, logs available to browse and filter through)
  • Compliant with PSD2 and SCA

Payment options —the difference between Setup Intent and Payment Intent

Setup Intent

  • Used to set up the payments in the future
  • Charging cards off-session (Customer not on the site or app). Our customer would authenticate and agree to be charged after the free trial ends.
  • The Setup Intents API is fully integrated with a JavaScript component from Stripe, which lets you use UI elements to securely collect card details on the client side.

Payment Intent

  • Used to process payments now
  • Charging cards on-session (Customer on the site or app)
  • Each PaymentIntent typically correlates with a single shopping cart or customer session in your application.
  • The PaymentIntent encapsulates details about the transaction, such as the supported payment methods, the amount to collect, and the desired currency.

Steps from a technical standpoint to create a Free Trial with Stripe

Step 1

Download Stripe’s library for back-end

  • Multiple languages available
  • When logged in they would include your testing environment credentials by default to make it easier to copy-paste

Step 2

Create a Customer

  • Stripe internally stores user data
  • Using the API we as developers can create “Customers” on Stripe’s side
  • Payments can be made either by a guest or by an already existing customer

Step 3

Create a SetupIntent

  • Use customer’s id
  • The SetupIntent object contains the client secret
  • It is a unique key that the front-end passes to Stripe JavaScript library to collect card information
  • Used to validate and authenticate card detail using credit card networks
  • It is sensitive and should be only visible to the customer

Step 4

Collect card details

  • HTML + JavaScript or React.js library available and customisable
  • Collect a postal code to increase cards acceptance rates and limit fraud
  • In a testing environment, you can temporarily use HTTP and turn enable HTTPS for production

Step 5

Confirm the SetupIntent

  • Use client secret and use JS stripe.ConfirmCardSetup which may take several seconds, during which time UI elements should be blocked and awaiting the response from Stripe.
  • If any additional authentication needs to be done here (2FA) then Stripe’s JS library takes care of that.

Step 6

On back-end side create a subscription with a free trial

The trial_end parameter takes a timestamp indicating the exact moment the trial should end. When creating a subscription, you can alternatively use the trial_period_days argument: an integer representing the number of days the trial should last, from the current moment.

  • An immediate invoice is created with $0
  • The customer is not charged yet!
  • When the trial ends, a new billing cycle starts for the customer
  • You have an option to give your customer a 7 day free trial on the 15th, so then he’s charged on 22nd a prorated amount and then on the 1st billed a standard amount

Step 6

Test payments

Use this test Stripe card 4000 0025 0000 3155 (this is important and specific to SetupIntent as per Stripe’s docs, other test cards might not work) along with CVC, postcode and expiration date in the future. Other cards with insufficient funds, or that would fail authentication are also available on Stripe.

Step 7

Free trial

The customer’s free trial has started, our customer will be charged in the future with the card already authenticated for this purpose.

Useful Webhooks from Stripe that you can use when implementing a Free Trial with Stripe

  • invoice.upcoming (Use it to notify customers about an upcoming subscription renewal)
  • customer.subscription.deleted (Use it to update your internal db premium status and end the subscription.)
  • invoice.updated (Use it to cancel the membership if the last charge attempt has been made to pay for the invoice and there is none planned)
  • customer.subscription.updated (Issued on many occasions but you can use it to detect that the free trial had ended and the subscription was bought. User [‘previous_attributes’][‘status’] and [‘object’][‘status’] to check if the former was trialing and the latter active)
  • customer.subscription.trial_will_end (Issued three days before a subscription’s trial period is scheduled to end, or when a trial is ended immediately)
  • customer.subscription.created (If the webhook data has status equal to trialing([‘data’][‘object’][‘status’]) and both trial_start and trial_end properties ([‘data’][‘object’][‘trial_start’] and [‘data’][‘object’][‘trial_end’]) it means the free trial has been started.)

More events can be found on Stripe’s page.

Why Developers Adore Stripe

Customer Service

What I learnt from my colleague was that Stripe Customer Service is excellent and that their response time is great. Quickly turned out that they were able to point me in the right direction when it comes to allocating time for implementing a feature.

Dashboard

  • Observe the number of API requests
  • Have an overview of webhooks requests and if there are any that failed.
  • See what the response time is
  • Track errors with the granularity to the HTTP method

Data and ease of use

  • easy payments/products/user data browsing and filtering (easy to find i.e. which users are now in a free trial that subscribed since some specific date?)
  • rich webhook data — which type of webhook, the request with details about user and subscription available in the panel, you have also the information whether it’s the subsequent request or the first one
  • switching between production and integration using one button
  • excellent documentation with examples, although sometimes it needs clarification

Final Thoughts

Overall, the Stripe Free Trial implementation takes some front-end and back-end skills and it is a versatile tool that any online product needs to show the full benefits of a premium version. I would recommend developing a small independent “test application” rather than entangling with your current payment API endpoints. Have fun developing it and then check out our careers section in Busuu if you’re up for a challenge like this one. We’re hiring!

There are multiple Back-end teams so if you are looking for a more specialised position there are plenty of choices whether it’s Growth, Learning Experience, Payments, or Live Lessons. The code is tested automatically with the unit, and functional tests, you can use the benefits of Codeception, automated pipelines and have a dedicated integration environment on which you can check the fruits of your work.

We are now a much bigger rocket ship!

Source of the code: stripe.com

--

--