@tsed/stripe
Version:
Stripe package for Ts.ED framework
49 lines (48 loc) • 2.04 kB
JavaScript
var WebhookEventMiddleware_1;
import { __decorate, __metadata, __param } from "tslib";
import "../services/StripeFactory.js";
import { Constant, Inject } from "@tsed/di";
import { BadRequest, InternalServerError } from "@tsed/exceptions";
import { Middleware } from "@tsed/platform-middlewares";
import { Context, HeaderParams, RawBodyParams } from "@tsed/platform-params";
import { Stripe } from "stripe";
import { STRIPE_WEBHOOK_EVENT, STRIPE_WEBHOOK_SIGNATURE } from "../constants/constants.js";
let WebhookEventMiddleware = WebhookEventMiddleware_1 = class WebhookEventMiddleware {
use(signature, body, ctx) {
const { secret, tolerance } = {
...this.webhooks,
...ctx.endpoint.store.get(WebhookEventMiddleware_1)
};
if (!secret) {
throw new InternalServerError("Missing Stripe webhooks secret key. You can get this in your dashboard. See: https://dashboard.stripe.com/webhooks.");
}
try {
ctx.set(STRIPE_WEBHOOK_SIGNATURE, signature);
ctx.set(STRIPE_WEBHOOK_EVENT, this.stripe.webhooks.constructEvent(body, signature, secret, tolerance));
}
catch (err) {
const error = err;
throw new BadRequest(`Stripe webhook error: ${error.message}`, error);
}
}
};
__decorate([
Inject(),
__metadata("design:type", Stripe)
], WebhookEventMiddleware.prototype, "stripe", void 0);
__decorate([
Constant("stripe.webhooks"),
__metadata("design:type", Object)
], WebhookEventMiddleware.prototype, "webhooks", void 0);
__decorate([
__param(0, HeaderParams("stripe-signature")),
__param(1, RawBodyParams()),
__param(2, Context()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, Buffer, Object]),
__metadata("design:returntype", Object)
], WebhookEventMiddleware.prototype, "use", null);
WebhookEventMiddleware = WebhookEventMiddleware_1 = __decorate([
Middleware()
], WebhookEventMiddleware);
export { WebhookEventMiddleware };