Webhooks allow your application to receive real-time updates about payment statuses from Resmic Pro. This ensures that you are notified immediately when a payment is completed, pending, or failed. You can either provide an endpoint to receive webhook notifications or opt for email updates (not recommended for real-time processing).
Configuring Webhooks
Option 1: Providing a Webhook URL (Recommended)
To receive automatic updates, configure a webhook endpoint in your Resmic Pro dashboard:
Just append the web_hook url while initiating the payment session
Whenever a payment status is updated, Resmic Pro will send a POST request to the configured URL.
You will required Webhook secret to verify the data you received is correct.
const express = require("express");
const crypto = require("crypto");
const app = express();
const port = 3000;
app.use(express.json()); // Middleware to parse JSON request bodies
// Your webhook secret (Get it from Resmic Dashboard)
const WEBHOOK_SECRET = "YOUR_WEBHOOK_SECRET";
// Middleware to verify the webhook signature
const verifyWebhookSignature = async (req, res, next) => {
const clientSignature = req.headers["x-signature"]; // Get the signature from the request headers
// Verify the request using HMAC
const isSignatureValid = await verifyHMAC(req.body, clientSignature, WEBHOOK_SECRET);
if (!isSignatureValid) {
return res.status(401).json({ success: false, message: "Unauthorized access" });
}
next(); // Proceed to the next middleware or route handler
};
// Root route for testing
app.get("/", (req, res) => {
res.send("Hello, Webhooks!");
});
// Webhook endpoint for payment updates
app.post("/payment-webhook", verifyWebhookSignature, async (req, res) => {
const {
session_id,
amount,
blockchain,
token,
from_wallet_address,
transaction_hash,
txTime,
status,
} = req.body;
console.log("Webhook Received:", req.body);
if (status) {
// Handle successful payment logic here (e.g., updating database, sending confirmation emails, etc.)
console.log("â Payment Confirmed for Session:", session_id);
} else {
console.log("â ïļ Payment Status:", status);
}
// Send a response back to acknowledge receipt of the webhook
res.status(200).json({
success: true,
message: "Webhook processed successfully",
data: {
session_id,
txTime,
status,
},
});
});
// Start the Express server
app.listen(port, () => {
console.log(`ð Webhook listener running on http://localhost:${port}`);
});
// Helper function to verify HMAC signature
function verifyHMAC(payload, signature, secretKey) {
const data = JSON.stringify(payload);
const computedSignature = crypto.createHmac("sha256", secretKey).update(data).digest("hex");
return computedSignature === signature;
}
Conclusion
By configuring webhooks, you can automatically track payment updates in real-time. If webhooks are not an option, email notifications can serve as an alternative.