Portals
Portals are the simplest way to let your users manage their billing and subscriptions. In this guide you'll learn how to create a basic portal for the most common use case. You can then extend it to more complex scenarios.
Since portals are used to manage billing and subscriptions, you need to have an app that handles billing and subscription system in place. If you don't have one, you can follow the subscriptions guide guide to set up a basic subscription system. Also we recommend to follow the webhooks guide guide to set up the webhooks to handle the events triggered by the payment provider.
In this example, we will use the same project we created in the subscriptions guide to which we added the webhooks in the webhooks guide.
The application
- Clone the
webhooks
branch of the subscriptions repo:
This branch contains the implementation of subscriptions and webhooks, without portals so we can add the portals implementation in the next steps.
- Once you have cloned the repo, open it on your code editor and install the dependencies:
Let's build a simple component for the billing portal
Create a file named portal-card.tsx
inside a src/components
folder and add the following code:
Import the component in the page
Go to the main page.tsx
file and import the PortalCard
component:
Then add the PortalCard
component to the page, you can do it anywhere in the page. In this example we will add it below the SubscriptionCard
component.
Your page.tsx
file should look like this:
If you run the project now with npm run dev
, you will see the PortalCard
component with a button to manage the subscription. You should see something like this:
Configure the webhooks
We will need the identifier to create the portal. To get the identifier we need to receive the webhook event when the subscription is created.
Please refer to the webhooks guide to learn how to set up the webhooks. You can also check the vexor.webhook reference to learn more about the webhook object.
Make sure to have webhooks configured, and... that's it!. You can now let your users manage their billing and subscriptions through the portal.
Test the portal
For testing the portal we will need to follow these three steps:
-
Create a subscription
-
Receive the webhook event with the identifier
-
Pass the identifier to the portal method
We will show you how to do this step by step in the video below.
In a real world scenario, when you create a subscription and receive the webhook event with the identifier, you should save the identifier in your database. In this example we will hardcode the identifier for simplicity purposes.
ππ» If you're using Stripe, the first time you try to create a portal you may get an error response from Vexor with the following message in your console:
The error message is self-explanatory. You just need to go to the Stripe dashboard and set up the customer portal settings in test mode in the Billing Portal Settings. If you don't want to customize the portal, you can use the default one, just click on the "Activate" button and thats it. You can try again after setting up the customer portal settings and the error should be gone.
π Example videos of the complete process may be outdated and you may find slight differences in the syntax, don't worry, the logic is the same... but now it's better π We'll update the videos soon.