Consider following this guide while using the OurPay Sandbox Environment. This will allow you to test your integration without affecting your production data.

Install the OurPay JavaScript SDK

Install the official OurPay SDK:
Terminal

Setting up environment variables

OurPay Access Token

To authenticate with OurPay, you need to create an access token, and supply it to Next.js using a OURPAY_ACCESS_TOKEN environment variable. You can create an organization access token from your organization settings.

Configuring an OurPay API Client

To interact with the OurPay API, create a versioned client with the access token you generated.
Remember to replace sandbox with production when you’re ready to switch to the production environment.

Generating OurPay Checkout Sessions

Next up, we need to create a checkout endpoint to handle the creation of checkout sessions. Go ahead and create a new GET route in Next.js.

Handling OurPay Webhooks

OurPay can send you events about various things happening in your organization. This is very useful for keeping your database in sync with OurPay checkouts, orders, subscriptions, etc. Configuring a webhook is simple. Head over to your organization’s settings page and click on the “Add Endpoint” button to create a new webhook.

Tunneling webhook events to your local development environment

If you’re developing locally, you can use a tool like ngrok to tunnel webhook events to your local development environment. This will allow you to test your webhook handlers without deploying them to a live server. Run the following command to start an ngrok tunnel:
Terminal

Add Webhook Endpoint

  1. Point the Webhook to your-app.com/api/webhook/ourpay. This must be an absolute URL which OurPay can reach. If you use ngrok, the URL will look something like this: https://<your-ngrok-id>.ngrok-free.app/api/webhook/ourpay.
  2. Select which events you want to be notified about. You can read more about the available events in the Events section.
  3. Generate a secret key to sign the requests. This will allow you to verify that the requests are truly coming from OurPay.
  4. Add the secret key to your environment variables.
Terminal

Setting up the Webhook handler

The handler verifies the raw request before branching on the typed event payload. Keep every handler idempotent because webhook deliveries can be retried.

Notifying the client about the event

If you’re building a real-time application, you might want to notify the client about the event. On the confirmation-page, you can listen for the checkout.updated event and update the UI accordingly when it reaches the succeeded status.