@transcend-io/typescript-webhook-example
Version:
Example of a webhook that can be integrated with Transcend.
59 lines • 2.6 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// Helpers
const helpers_1 = require("./helpers");
const logger_1 = require("./logger");
const scheduleEnricher_1 = __importDefault(require("./scheduleEnricher"));
/**
* Enrichment webhook handler.
* Checks for fraud/legal holds and returns extra identifiers.
*
* @param req - Express request object
* @param res - Express response object
*/
async function handleEnrichmentWebhook(req, res) {
// Verify the incoming webhook is coming from Transcend, and via the Sombra gateway.
try {
await (0, helpers_1.verifyWebhook)(req.headers['x-sombra-token']);
}
catch (error) {
// If the webhook doesn't pass verification, reject it.
return res.status(401).send('You are not Transcend!');
}
logger_1.logger.info(`Received Enrichment webhook - ${req.body.extras.request.link}`);
// Extract metadata from the body
const requestIdentifier = req.body.requestIdentifier.value;
// Check if we should place a hold on this request
const isFraudster = await (0, helpers_1.checkIfFraudster)(requestIdentifier);
const hasLegalHold = await (0, helpers_1.checkForLegalHold)(requestIdentifier);
// In this case, we are automatically cancelling requests from fraudsters.
if (isFraudster) {
res.json({
status: 'CANCELED',
templateId: 'ee54169a-9a87-4e93-aa2d-be733cce9113',
});
logger_1.logger.info(`Successfully responded to Enrichment webhook with CANCELED signal - ${req.body.extras.request.link}`);
return null;
}
// In this case, we are putting a hold on the request so legal can review it.
if (hasLegalHold) {
res.json({
status: 'ON_HOLD',
});
logger_1.logger.info(`Successfully responded to Enrichment webhook with ON_HOLD signal - ${req.body.extras.request.link}`);
return null;
}
// Schedule the enrichment job
let nonce = req.headers['x-transcend-nonce'];
nonce = Array.isArray(nonce) ? nonce.join() : nonce || '';
(0, scheduleEnricher_1.default)(requestIdentifier, nonce, req.body.extras.request.link);
// Indicate we got the webhook
res.status(200).send();
logger_1.logger.info(`Successfully responded to Enrichment webhook - ${req.body.extras.request.link}`);
return null;
}
exports.default = handleEnrichmentWebhook;
//# sourceMappingURL=handleEnrichmentWebhook.js.map
;