Having the Stripe API’s basic concepts spelt out, we can now dive into the integration with Feathers.js. We will begin by looking at how we can turn our clients into customers, but before we dive in, we first need to set up the stripeClient.
Stripe Client
To set up the Stripe Client, we create a new file called stripe.js in the src. The content is as follows.
This will set up your stripe client using the stripe config you have defined, which is then called from your app.js as a configuration.
Now with this setup, we can start building the payments system!
Turning clients into customers
From the client model view, this is relatively simple. We need to add two new fields to our already existing user schema: the customerId and subscriptionPlanId.
Note that the subscriptionPlanId is set to be required. Whether the customer is supposed to pay or not, you want to have a subscription associated. This allows for easier transitions between subscriptions later on.
Now with the model setup, we want to add some logic to it. Let’s create a createStripeCustomer hook function.
For this function, we extract the email that the user-provided on signup and the assigned _id. We create a customer using the stripeCustomers service that we will define a bit later on. Then we patch the client that we just created with the customerId. In the case that something fails we remove the entire user and return the error. This function is then attached to the clients create.after hook.
The following services are supported by Stripe as well:
Stripe Customer
Now let’s look at the stripe customer service. We have a dedicated model for storing information about the customer. The schema is defined as follows.
Here we store a copy of the customer object from Stripe. For this service, we also define a createStripeCustomer hook function. This function is purely responsible for sending over the required information to Stripe to create a Stripe customer.
You then want to save the result of this request in your stripeCustomers service. In case this fails, remove the stripeCustomer. You are patching the stripeCustomer results in running the after.patch hook, where two methods are present: createTaxId and updateStripeCustomer. First, we will have a look at the createTaxId method.
To create the taxId, we pass the data from the request and send it to stripe using the stripeClient. If the taxId is not present, but it was there earlier, we want to make sure that we remove it from the stripe by calling deleteTaxId. The updateStripeCustomer hook passes all the data that has changed to the stripeClient and requests an update.
Finally, for the customer service, we need to handle deleting the customer. This is done on an after.remove hook. This method is relatively straightforward since for removal just the id is required.
Thus the hooks for the stripeCustomer can be summarised as follows.
This concludes the customers part. Now we can move on to some more exciting details, which are the stripeEvents!
Handling Stripe Events
As mentioned at the beginning Stripe Events are how Stripe send information to your app. The model for this service is pretty simple.
We only store a generated id for this event, making sure that we don’t process the event more than once. This service has only one hook on after.create. There we create an Agenda job, which is a lightweight job scheduling library. Using a job scheduler allows you to quickly respond to the webhook and leave the event’s processing to some later time when the app has some free resources.