Verify signature
The included in each signed request contains a timestamp and one or more signatures. The timestamp is prefixed by t=, and each signature is prefixed by a scheme. Schemes start with v, followed by an integer. The valid schemes are v1 and v2.
MONEI-Signature: t=1492774577,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd,v2=1b622e9c31804ad429912b4a8c10bbcd38e1a169158ed34ac27b54b0848623c0
Signature schemes
MONEI generates signatures using a hash-based message authentication code (HMAC) with SHA-256. Each scheme signs the same payload with a different secret:
| Scheme | Secret | When the header has it |
|---|---|---|
v1 | Your account's default API key (pk_live_ or pk_test_). An account webhook of an account without an API key uses the legacy secret of the endpoint. | When one of these secrets exists |
v2 | A signing secret: the signing secret of the webhook endpoint (whsec_…), or the signing secret of the scoped API key (mksec_…) that created the payment or the subscription | Account webhooks of an endpoint with a signing secret, and callbacks of payments and subscriptions that a scoped API key created |
The HMAC key is the full secret string, with its prefix (pk_, whsec_ or mksec_). Do not decode the secret.
After you roll the signing secret of an endpoint, the header has two v2 signatures for 24 hours: the new secret first, then the old one. The signature is valid when one of them matches your secret.
The header can have more elements than t and v1, and an element can be absent. Do not match the full header with a strict pattern such as t=…,v1=…. Split the header and read each element by its key.
To prevent downgrade attacks, check only the scheme of the secret that you hold. With a signing secret, check v2 and never fall back to v1. With your API key, check v1. Ignore all other schemes.
MONEI signs v1 with your account's default API key, also for payments that a scoped API key creates. A scoped API key (mk_) cannot verify v1. To verify these callbacks without the default API key, check v2 with the signing secret of the scoped key.
A callback has no v2 when the payment or the subscription was created with the default API key or in the dashboard, or when the scoped key that created it is revoked. To verify every status change without the default API key, use an account webhook: MONEI signs it with the signing secret of the endpoint, whichever key created the payment.
Verifying signatures using our official libraries
Use one of our official libraries to verify signatures. You perform the verification by providing the request payload and the MONEI-Signature header. If verification fails, MONEI returns an error.
- Node.js
- PHP
- Python
import express from 'express';
import {Monei, PaymentStatus} from '@monei-js/node-sdk';
// Set your api key. Remember to switch to your live api key in production!
// See your api key here: https://dashboard.monei.com/settings/api
const monei = new Monei('YOUR_API_KEY');
// This example uses Express to receive webhooks
const app = express();
// Endpoint to handle MONEI webhooks
app.post('/checkout/callback', express.raw({type: 'application/json'}), (req, res) => {
const signature = req.headers['monei-signature'];
try {
// Verify the signature using the raw request body and the header
const payment = monei.verifySignature(req.body.toString(), signature);
// Optional: Log the received payment status
console.log(`Webhook received for Payment ID: ${payment.id}, Status: ${payment.status}`);
// Update your order status based on the payment status
if (payment.status === PaymentStatus.SUCCEEDED) {
// Payment successful - fulfill the order
console.log(`Payment ${payment.id} succeeded. Fulfilling order...`);
// Update your database, send confirmation email, etc.
} else if (payment.status === PaymentStatus.FAILED) {
// Payment failed - notify the customer
console.log(`Payment ${payment.id} failed. Notifying customer...`);
// Log the failure, update your database, etc.
} else if (payment.status === PaymentStatus.AUTHORIZED) {
// Payment is authorized but not yet captured
console.log(`Payment ${payment.id} authorized. Capture if needed.`);
// You might want to capture it later using monei.payments.capture()
} else if (payment.status === PaymentStatus.CANCELED) {
// Payment was canceled by the user or system
console.log(`Payment ${payment.id} was canceled.`);
// Update your database accordingly
} else {
// Handle other potential statuses if necessary
console.log(`Unhandled payment status: ${payment.status} for Payment ${payment.id}`);
}
// Acknowledge receipt of the webhook with a 200 OK status
res.status(200).json({received: true});
} catch (error) {
// Handle signature verification failure
console.error('Invalid webhook signature:', error.message);
// Respond with 401 Unauthorized if the signature is invalid
res.status(401).json({error: 'Invalid signature'});
}
});
// Start the server
app.listen(3000, () => {
console.log(`Server listening on port 3000`);
});
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use Monei\Model\PaymentStatus;
use Monei\ApiException;
// Replace YOUR_API_KEY with your actual MONEI API key
$monei = new Monei\MoneiClient('YOUR_API_KEY');
// Parse raw body for signature verification
$rawBody = file_get_contents('php://input');
$signature = $_SERVER['HTTP_MONEI_SIGNATURE'] ?? '';
try {
// Verify the signature
// verifySignature returns the validated Payment object or throws ApiException
$payment = $monei->verifySignature($rawBody, $signature);
// Optional: Log the received payment status
error_log('Webhook received for Payment ID: ' . $payment->getId() . ', Status: ' . $payment->getStatus());
// Update your order status based on the payment status
if ($payment->getStatus() === PaymentStatus::SUCCEEDED) {
// Payment successful - fulfill the order
error_log('Payment ' . $payment->getId() . ' succeeded. Fulfilling order...');
// Update your database, send confirmation email, etc.
} else if ($payment->getStatus() === PaymentStatus::FAILED) {
// Payment failed - notify the customer
error_log('Payment ' . $payment->getId() . ' failed. Notifying customer...');
// Log the failure, update your database, etc.
} else if ($payment->getStatus() === PaymentStatus::AUTHORIZED) {
// Payment is authorized but not yet captured
error_log('Payment ' . $payment->getId() . ' authorized. Capture if needed.');
// You can capture it later using $monei->payments->capture(...)
} else if ($payment->getStatus() === PaymentStatus::CANCELED) {
// Payment was canceled
error_log('Payment ' . $payment->getId() . ' was canceled.');
// Update your database accordingly
} else {
// Handle other potential statuses if necessary
error_log('Unhandled payment status: ' . $payment->getStatus() . ' for Payment ' . $payment->getId());
}
// Acknowledge receipt of the webhook with a 200 OK status
http_response_code(200);
header('Content-Type: application/json'); // Ensure JSON header
echo json_encode(['received' => true]);
} catch (ApiException $e) {
// Handle signature verification failure (ApiException specifically)
error_log('Invalid webhook signature: ' . $e->getMessage());
http_response_code(401); // Respond with 401 Unauthorized
header('Content-Type: application/json'); // Ensure JSON header
echo json_encode(['error' => 'Invalid signature']);
exit(); // Stop script execution after sending error response
} catch (Exception $e) {
// Handle any other unexpected errors during processing
error_log('Webhook processing error: ' . $e->getMessage());
http_response_code(500); // Respond with 500 Internal Server Error
header('Content-Type: application/json');
echo json_encode(['error' => 'Internal server error']);
exit();
}
?>
import os
import Monei
from Monei.exceptions import ApiException
from flask import Flask, request, abort, jsonify
# Replace YOUR_API_KEY with your actual MONEI API key
monei_client = Monei.MoneiClient(api_key="YOUR_API_KEY")
app = Flask(__name__)
# Update route and function name
@app.route('/checkout/callback', methods=['POST'])
def callback():
signature = request.headers.get('MONEI-Signature') # Use correct header key
# Check if signature header exists (optional but good practice)
if not signature:
print("MONEI-Signature header missing")
return jsonify({'error': 'Missing MONEI-Signature header'}), 400
try:
# Verify the signature using raw request data (bytes)
payment = monei_client.verify_signature(request.data, signature)
# Optional: Log the received payment status
payment_id = payment.get('id')
payment_status = payment.get('status')
print(f"Webhook received for Payment ID: {payment_id}, Status: {payment_status}")
# Update your order status based on the payment status
if payment_status == 'SUCCEEDED':
# Payment successful - fulfill the order
print(f"Payment {payment_id} succeeded. Fulfilling order...")
# Update your database, send confirmation email, etc.
pass # Placeholder from user snippet
elif payment_status == 'FAILED':
# Payment failed - notify the customer
print(f"Payment {payment_id} failed. Notifying customer...")
# Log the failure, update your database, etc.
pass # Placeholder from user snippet
elif payment_status == 'AUTHORIZED':
# Payment is authorized but not yet captured
print(f"Payment {payment_id} authorized. Capture if needed.")
# You can capture it later
pass # Placeholder from user snippet
elif payment_status == 'CANCELED':
# Payment was canceled
print(f"Payment {payment_id} was canceled.")
pass # Placeholder from user snippet
else:
print(f"Unhandled payment status: {payment_status} for Payment {payment_id}")
# Acknowledge receipt of the webhook
return jsonify({'received': True}), 200
except ApiException as e: # Invalid or missing signature
print(f"Invalid webhook signature: {e}")
# Return 401 Unauthorized as requested
return jsonify({'error': 'Invalid signature'}), 401
except Exception as e: # Catch other potential errors during processing
print(f"Webhook processing error: {e}")
# Return 500 Internal Server Error for other issues
return jsonify({'error': 'Internal server error'}), 500
if __name__ == '__main__':
app.run(port=3000)
Verify v2 with a signing secret
The Node.js, PHP and Python libraries take the signing secret as a third argument. When you give a secret, the library checks v2 and does not use v1. Without a secret, the library checks v1 with your API key, as before.
- Node.js
- PHP
- Python
// signingSecret is the whsec_… or mksec_… secret from your configuration
const payload = monei.verifySignature(req.body.toString(), signature, signingSecret);
// $signingSecret is the whsec_… or mksec_… secret from your configuration
$payload = $monei->verifySignature($rawBody, $signature, $signingSecret);
# signing_secret is the whsec_… or mksec_… secret from your configuration
payload = monei_client.verify_signature(request.data, signature, signing_secret)
When you add a webhook endpoint, MONEI sends a webhook.test request before you can see the signing secret of the endpoint. Answer this request with a 2xx status, also when you cannot verify its signature yet. Do not process it:
- Node.js
- PHP
- Python
app.post('/webhook', express.raw({type: 'application/json'}), (req, res) => {
const signature = req.headers['monei-signature'];
let payload;
try {
payload = monei.verifySignature(req.body.toString(), signature, signingSecret);
} catch (error) {
let event;
try {
event = JSON.parse(req.body.toString());
} catch {
// The body is not JSON
}
// Acknowledge the test request, but do not process it
res.status(event?.type === 'webhook.test' ? 200 : 401).end();
return;
}
// Process the payload
res.status(200).json({received: true});
});
try {
$payload = $monei->verifySignature($rawBody, $signature, $signingSecret);
} catch (ApiException $e) {
$event = json_decode($rawBody);
// Acknowledge the test request, but do not process it
http_response_code(isset($event->type) && $event->type === 'webhook.test' ? 200 : 401);
exit;
}
import json
from Monei.exceptions import ApiException
@app.route('/webhook', methods=['POST'])
def webhook():
signature = request.headers.get('MONEI-Signature')
try:
payload = monei_client.verify_signature(request.data, signature, signing_secret)
except ApiException:
try:
event = json.loads(request.data)
except ValueError:
event = {} # The body is not JSON
# Acknowledge the test request, but do not process it
return '', 200 if isinstance(event, dict) and event.get('type') == 'webhook.test' else 401
# Process the payload
return {'received': True}, 200
Verifying signatures manually
Step 1: Extract the timestamp and the signature from the header
Split the header, using the , character as the separator, to get a list of elements. Then split each element, using the = character as the separator, to get a prefix and value pair.
The value for the prefix t corresponds to the timestamp. v1 corresponds to the signature made with your API key. Each v2 corresponds to a signature made with a signing secret. Keep every v2: the header can have two. You can discard all other elements.
Step 2: Prepare the signed_payload string
The signed_payload string is created by concatenating:
- The timestamp (as a string)
- The character
. - The actual JSON payload (i.e., the request body)
Step 3: Determine the expected signature
Compute an with the SHA256 hash function. Use the signed_payload string as the message. As the key, use your account's API key for v1, or the signing secret for v2. Use the full secret string, with its prefix, as UTF-8 bytes.
You can find your API key in MONEI Dashboard → Settings → API Access.
You see the signing secret of a webhook endpoint on the page of the endpoint in MONEI Dashboard → Settings → Webhooks. MONEI shows the signing secret of a scoped API key only once, when you create the key.
Step 4: Compare the signatures
Compare the signature in the header to the expected signature. For v2, the signature is valid when one v2 in the header matches. For an equality match, compute the difference between the current timestamp and the received timestamp, then decide if the difference is within your tolerance.
Set that tolerance with retries in mind. A payment callback is signed once, so every retry carries the original timestamp and can arrive hours later. Account webhooks are signed again on each resend.
To protect against timing attacks, use a constant-time string comparison to compare the expected signature to the received signature.