@lomray/microservice-payment-stripe
Version:
Stripe payment microservice based on NodeJS & inverted json.
51 lines (47 loc) • 1.88 kB
JavaScript
;
var tslib = require('tslib');
var microserviceHelpers = require('@lomray/microservice-helpers');
var classValidator = require('class-validator');
var remote = require('../../config/remote.js');
var stripe = require('../../services/payment-gateway/stripe.js');
class WebhookInput {
}
tslib.__decorate([
classValidator.IsObject(),
tslib.__metadata("design:type", Object)
], WebhookInput.prototype, "body", void 0);
tslib.__decorate([
classValidator.IsString(),
tslib.__metadata("design:type", String)
], WebhookInput.prototype, "rawBody", void 0);
tslib.__decorate([
classValidator.IsObject(),
tslib.__metadata("design:type", Object)
], WebhookInput.prototype, "query", void 0);
class WebhookOutput {
}
/**
* Endpoint for handling and processing stripe webhooks
*/
const webhook = microserviceHelpers.Endpoint.custom(() => ({
input: WebhookInput,
output: WebhookOutput,
description: 'Handling and processing stripe webhooks.',
}), ({ rawBody, query, payload }) => tslib.__awaiter(void 0, void 0, void 0, function* () {
var _a;
const { webhookKeys } = yield remote();
const webhookKey = webhookKeys === null || webhookKeys === void 0 ? void 0 : webhookKeys[query === null || query === void 0 ? void 0 : query.id];
if (!webhookKey) {
throw new Error(`Webhook key is not provided for id: ${query.id}`);
}
if (!((_a = payload === null || payload === void 0 ? void 0 : payload.headers) === null || _a === void 0 ? void 0 : _a['stripe-signature'])) {
throw new Error('Stripe signature is mot provided.');
}
const service = yield stripe.init();
// Should throw an error if webhook handle was failed
yield service.handleWebhookEvent(rawBody, payload.headers['stripe-signature'], webhookKey, query.id);
return {
isHandled: true,
};
}));
module.exports = webhook;