Skip to main content

handle Method

Overview​

The handle method processes raw webhook payloads from payment processors. It verifies webhook signatures, parses the event data, and returns a normalized response with the event type and associated payment details.

Business Use Case: When Stripe sends a webhook notification that a payment succeeded, you need to verify it's authentic and update your order status. This method handles verification and parsing.

Purpose​

ChallengeSolution
Signature verificationAutomatically verifies webhook authenticity
Multiple formatsNormalizes Stripe, Adyen into consistent format
SecurityValidates secrets before processing

Request Fields​

FieldTypeRequiredDescription
merchantEventIdstringYesYour unique event reference
payloadstring/objectYesRaw webhook body
headersobjectYesHTTP headers including signature
webhookSecretstringYesWebhook signing secret

Response Fields​

FieldTypeDescription
eventTypestringType: payment.captured, refund.succeeded, etc.
eventResponseobjectPayment/refund/dispute details
sourceVerifiedbooleanWhether signature was verified
eventStatusstringCOMPLETE, INCOMPLETE

Example​

SDK Setup​

const { EventClient } = require('hyperswitch-prism');

const eventClient = new EventClient({
connector: 'stripe',
apiKey: 'YOUR_API_KEY'
});

Express.js Webhook Handler​

app.post('/webhooks/stripe', express.raw({ type: 'application/json' }), async (req, res) => {
const request = {
merchantEventId: `evt_${Date.now()}`,
payload: req.body,
headers: req.headers,
webhookSecret: process.env.STRIPE_WEBHOOK_SECRET
};

try {
const response = await eventClient.handle(request);

if (response.eventType === 'payment.captured') {
await fulfillOrder(response.eventResponse.paymentsResponse.merchantTransactionId);
} else if (response.eventType === 'refund.succeeded') {
await updateRefundStatus(response.eventResponse.refundsResponse);
}

res.json({ received: true });
} catch (error) {
res.status(400).json({ error: error.message });
}
});

Response​

{
eventType: "payment.captured",
eventResponse: {
paymentsResponse: {
merchantTransactionId: "txn_order_001",
connectorTransactionId: "pi_3Oxxx...",
status: "CAPTURED"
}
},
sourceVerified: true,
eventStatus: "COMPLETE"
}

Event Types​

Event TypeDescription
payment.authorizedPayment authorized, funds held
payment.capturedPayment completed
payment.failedPayment declined
refund.succeededRefund processed
dispute.createdNew chargeback received

Next Steps​