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.
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.
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.
