Build a Next JS app to manage incoming payments for your customers
api/webhook
folder. We’re going to opt for creating a separate route for each webhook event to keep a clear separation of concerns and since we’re using Next JS’s app based routing system, that means we need to create the following files
lib/signature.ts
file and add in the following code.
types
called task.ts
if you haven’t already done so and paste this inside
class
that manages a Map
. Open up the database/memory-db.ts
file (or create one if you haven’t) and add the following code inside
taskStore
variable is not defined. Because we have chosen to use an in-memory database, in Next, (whether in dev with HMR or in production on Vercel’s serverless functions), the data in this module can be torn down and re-evaluated at any time, so tasks gets reset. In order to avoid this and truly get one bucket of memory to live across all imports (& HMR reloads) in a single Node process, you need to hang it on the Node “global”. So create a new file inside the database
folder called global-db.ts
and add this code
memory-db.ts
file to fix the compile errors.
memory-db
. So go ahead and open up the file repository/taskRepository.ts
and add the following code inside.
record_type
and event_type
, the first step we have to complete is to create webhook subscriptions on Paylias for each endpoint. You can follow the Webhook Implementation guide to create the endpoint subscriptions on Paylias.
record_type
and event_type
for each endpoint on your app`payment-admission:created`
PENDING
`payment-admission-task:created
PENDING
or REJECTED
payment-exception:created
REJECTED
and save the error message.payment-transaction:created
Tasks
in a column viewPENDING
tasks to allow our user to either accept or reject them.cd
into the root directory of your app. Once there copy-paste this command
components
directory.
app/api/tasks/route.ts
and add in the following code
TasksList
component to poll for all tasks every 5 seconds. Lets create the TasksList
component now. Create a new file under components
called TasksLists.tsx
and add in the following code
app/page.tsx
and replace the existing code with the following
PENDING
task.
/api/tasks/[task-id]route.ts
file and add in the code below to implement both the GET
and PUT
endpoints.
PUT
endpoint we’re also calling the Update Admission Task endpoint. You’ll notice that we’re not updating the task in our database yet. If we reject the task, Paylias will trigger an Exception event which our /api/webhook/payment-exception/created
endpoint will catch. This is where our the task will be updated in our system. Similarly, if we approve the task, Paylias will trigger a Transaction event which our /api/webhook/payment-transaction/created
endpoint will catch. This is where we will mark the task as COMPLETED
.
The final step in this guide is to implement the UI for updating a task. We’ll accomplish this using the <Dialog>
component from Shad Cn along with implementing parallel and intercepting routes from Next JS.
Create a new file under the components
directory called UpdateTask.tsx
and add in the following code
app
directory.