Dynamic Payments
Learn how to utilize dynamic payments in your application.
We will assume you have a general understanding of programming. This guide is intended for developers, and we expect you to have a good understanding of REST APIs in general. If you’re not a developer, please skip this section.
We’ll make practical use of dynamic payments so that you can use them effectively in your applications. There are 8 steps required to make this work. We will use TypeScript and Node.js here, but the implementation can be ported to any language.
A practical use case for dynamic payments is, for example, if you have a Discord server and you want to grant a user a role. In this case, you can pass in:
during the payment creation. This will prompt for the Discord username in the checkout process. Once the payment is made, a payment.succeeded
webhook event is triggered. You can then receive the Discord username as discord_username
or fields[0].input
in the request body of the webhook and grant the user their role.
This has many practical applications, for example, a site selling dynamic courses or other customizable products or services.
Grab the secrets
Log in to your account and navigate to the “API Tokens” section. Click the “Create New Token” button. The generated API token will look similar to this: ey.xxxx
. Copy and securely store the API token.
Next in your shop, create a new webhook. The webhook secret will look like this: whsk_xxx
. Copy and securely store the webhook secret.
We are storing these two secrets as
in our .env
file.
Prepare backend
Set up an Express server with TypeScript. First, initialize your project and install the necessary dependencies:
Create a src
folder and add an index.ts
file:
Build webhook receiver
Add a route to receive webhooks and verify the signature:
Generate payment session
Create a function to generate a payment session:
Send checkout link
Create an endpoint to initiate the payment process:
Monitor payment.success event
The webhook receiver we set up in Step 3 will handle the payment.succeeded
event. Make sure to implement the necessary logic in the webhook handler.
Grant app access
In the webhook handler, implement the logic to grant access based on the username:
You've finished!