Webhooks
Modem Pay’s webhooks notify your server about key events, such as customer creation, payment intent updates, and successful charges. To receive and securely process these notifications, follow the steps outlined here for setting up, validating, and handling webhooks.
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 dedicatedPOST
endpoint 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 theevent
field, while the main data is within thepayload
.
Webhook Security and Signature Validation
To confirm the integrity of incoming events, Modem Pay includes a unique x-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-signature
header from the incoming request. -
Use
modempay.webhooks.composeEventDetails
for Validation:
Pass the payload, signature, and your unique Modem Pay secret to themodempay.webhooks.composeEventDetails
function. 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.
To install the Modem Pay CLI, run:
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:
2. Listen
The listen
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.
Usage:
Options:
--forward-url
: Specifies your local endpoint to receive webhook events.
Example:
This will create a public tunnel pointing to http://localhost:3000/webhook
.
3. Trigger
Simulates events for testing purposes.
Usage:
Supported Event Types:
- Customer Events:
"customer.created"
,"customer.updated"
,"customer.deleted"
- Payment Intent Events:
"payment_intent.created"
,"payment_intent.cancelled"
- Charge Events:
"charge.succeeded"
,"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.
Example:
This sends a 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"
- Charge Events:
"charge.succeeded"
,"charge.cancelled"
,"charge.created"
,"charge.updated"
Example Usage
Here’s an example of using modempay.webhooks.composeEventDetails
to handle incoming webhooks in a Node.js application:
Recommended Response
For each webhook event, respond to Modem Pay with an HTTP 200 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.
Summary
By following these steps, you can securely receive and process webhook events from Modem Pay, automating actions based on customer interactions and payment flows. Make sure to store and securely handle the event data, as each event may carry critical transaction or customer information relevant to your application.