You can generate your unique Modem Pay webhook secret in the Modem Pay dashboard under Developers > Webhooks.
Setting Up Webhooks
-
Define Your Webhook Endpoint:
Create a dedicatedPOSTendpoint on your server to receive webhook events from Modem Pay. Ensure this endpoint is accessible over HTTPS to maintain security. -
Listening for Events:
Your endpoint will receive a variety of event notifications (e.g.,customer.created,payment_intent.created,charge.succeeded). The event type is provided in theeventfield, while the main data is within thepayload.
Sample Webhook Event
json
Webhook Security and Signature Validation
To confirm the integrity of incoming events, Modem Pay includes a uniquex-modem-signature header in each webhook request. This header allows you to verify that the event originated from Modem Pay.
-
Retrieve the Signature:
In your webhook endpoint, extract thex-modem-signatureheader from the incoming request. -
Use
modempay.webhooks.composeEventDetailsfor Validation:
Pass the payload, signature, and your unique Modem Pay secret to themodempay.webhooks.composeEventDetailsfunction. This function performs the necessary checks, verifying the signature and parsing the payload.
Modem Pay CLI
The Modem Pay CLI is a tool designed to simplify integration testing by offering features like authentication, webhook management, and event simulation only in a test environment.Installation
Ensure that both Node.js and npm are installed on your machine before proceeding.
bash
Commands
1. Login
Authenticates a user and generates an access token, business ID, and account ID. Authentication is required to access other CLI features. Usage:bash
2. Listen
Thelisten command creates a secure public tunnel using Localtunnel to forward webhooks to a local endpoint. This is particularly useful for testing webhooks during development.
Disclaimer:
- The CLI leverages Localtunnel to generate the public URL. In some cases, a public URL might not be provisioned successfully due to network issues.
- This feature should only be used in test environments.
bash
--forward-url: Specifies your local endpoint to receive webhook events.
bash
http://localhost:3000/webhook.
3. Trigger
Simulates events for testing purposes. Usage:bash
- Customer Events:
"customer.created","customer.updated","customer.deleted" - Payment Intent Events:
"payment_intent.created","payment_intent.cancelled" - Charge Events:
"charge.succeeded","charge.failed","charge.cancelled","charge.created","charge.updated"
The
listen command captures both manually triggered events and automatically triggered events from the Modem Pay system. For testing purposes, always use the webhook secret generated by the listen command. This secret is displayed in the terminal and can also be found in the Modem Pay dashboard, but only in test mode.Important: Production secrets and endpoints remain unaffected and are not impacted by the test mode configuration.bash
charge.succeeded event payload to the configured webhook endpoint for testing.
Always respond to webhook events with an HTTP
200 OK status to confirm receipt. This ensures Modem Pay doesn’t retry the webhook unnecessarily.Event Structure
Upon successful validation,modempay.webhooks.composeEventDetails returns an Event object containing the event type and payload data. The event types you may receive include:
- Customer Events:
"customer.created","customer.updated","customer.deleted" - Payment Intent Events:
"payment_intent.created","payment_intent.cancelled","payment_intent.expired" - Charge Events:
"charge.succeeded","charge.cancelled","charge.created","charge.updated","charge.expired" - Payout Events:
"transfer.succeeded","transfer.cancelled","transfer.reversed","transfer.failed","transfer.flagged"
Example Usage
Here’s an example of usingmodempay.webhooks.composeEventDetails to handle incoming webhooks in a Node.js application:
Recommended Response
For each webhook event, respond to Modem Pay with an HTTP200 OK status as confirmation. This ensures Modem Pay won’t re-send the event unless there’s an error response.
Ensure your webhook endpoint quickly responds with an HTTP
200 OK status before performing any tasks that might cause delays or timeouts, such as updating records or sending invoices. This prevents webhook retries and ensures smooth processing.Webhook Retries
Modem Pay ensures reliable delivery of webhook events through an automatic retry mechanism. If your server fails to acknowledge a webhook event with a200 OK HTTP response, Modem Pay will retry the delivery up to 3 times, with each retry spaced 10 minutes apart. A failure could be due to reasons such as server timeouts, network errors, or non-2xx response codes.
To avoid unnecessary retries and ensure smooth webhook processing, always return a 200 OK response immediately, even if you need to carry out time-consuming tasks (like writing to a database or triggering external API calls). It’s recommended to offload such operations to a background worker or job queue.
By following this best practice, you help prevent duplicate processing and reduce the risk of delays or rate-limiting due to repeated webhook attempts.