A package of Ts.ED framework. See website: https://tsed.io/tutorials/stripe.html
Currently, @tsed/stripe
allows you:
- Configure stripe,
- Create webhooks,
- Use stripe API.
To begin, install the Stripe module for TS.ED:
npm install --save @tsed/stripe
npm install --save stripe
Then import @tsed/stripe
in your Server:
import {Configuration, PlatformApplication} from "@tsed/common";
import "@tsed/stripe";
import {Stripe} from "stripe";
@Configuration({
stripe: {
apiKey: process.env.STRIPE_SECRET_KEY,
webhooks: {
secret: process.env.STRIPE_WEBHOOK_SECRET
},
// Stripe options
apiVersion: "2019-08-08",
httpProxy: new ProxyAgent(process.env.http_proxy)
}
})
export class Server {
@Inject()
stripe: Stripe;
$afterInit() {
// do something with stripe
// this.stripe.customers
}
}
See Stripe options for more details: https://www.npmjs.com/package/stripe
::: warning
Stripe needs to validate signature transaction when you received a webhook notification.
To work properly, you have to remove bodyParser add on $beforeRoutesInit
.
@Configuration({
+ rawBody: true,
+ middlewares: [
+ {use: 'json-parser'},
+ {use: 'urlencoded-parser', options: {extended: true})
+ ]
})
export class Server {
@Inject()
protected app: PlatformApplication;
$beforeRoutesInit() {
- this.app
- .use(bodyParser.json())
- .use(bodyParser.urlencoded({extended: true}));
}
}
::
import {Injectable} from "@tsed/di";
@Injectable()
class MyStripeService {
@Inject()
stripe: Stripe;
$onInit() {
// do something with stripe
this.stripe.on("request", this.onRequest.bind(this));
}
protected onRequest(request: any) {}
}
Stripe can optionally sign the webhook events it sends to your endpoint, allowing you to validate that they were not sent by a third-party. You can read more about it here.
To register a Stripe webhook with Ts.ED, just use the @WebhookEvent
decorator. It'll call for you the stripe.webhooks.constructEvent
with the right parameters:
import {RawBodyParams, HeaderParams, Controller, Context} from "@tsed/common";
import {Stripe} from "stripe";
@Controller("/webhooks")
export class StripWebhookCtrl {
@Inject()
stripe: Stripe;
@Post("/callback")
successPaymentHook(@WebhookEvent() event: Stripe.Event, @Context() ctx: Context) {
ctx.logger.info({name: "Webhook success", event});
return {received: true};
}
}
You can use stripe.webhooks.generateTestHeaderString to mock webhook events that come from Stripe:
import {Stripe} from "stripe";
import {PlatformTest} from "@tsed/common";
import {StripWebhookCtrl} from "./StripWebhookCtrl";
describe("StripWebhookCtrl", () => {
beforeEach(() =>
PlatformTest.create({
stripe: {
apiKey: "fake_api_key",
webhooks: {
secret: "whsec_test_secret"
},
// Stripe options
apiVersion: "2019-08-08"
}
})
);
afterEach(PlatformTest.reset);
it("should call event", async () => {
const stripe = PlatformTest.get<Stripe>(Stripe);
const payload = {
id: "evt_test_webhook",
object: "event"
};
const payloadString = JSON.stringify(payload, null, 2);
const header = stripe.webhooks.generateTestHeaderString({
payload: payloadString,
secret: "whsec_test_secret"
});
const event = stripe.webhooks.constructEvent(payloadString, header, secret);
const ctx = PlatformTest.createRequestContext();
const ctrl = await PlatformTest.invoke<StripWebhookCtrl>(StripWebhookCtrl);
const result = ctrl.successPaymentHook(event, ctx);
expect(result).toEqual({received: true});
});
});
With SuperTest:
import {PlatformTest} from "@tsed/common";
import {PlatformExpress} from "@tsed/platform-express";
import {PlatformTestUtils} from "@tsed/platform-test-utils";
import {expect} from "chai";
import {Stripe} from "stripe";
import SuperTest from "supertest";
import {StripeWebhooksCtrl} from "./StripWebhookCtrl";
import {Server} from "../Server";
const utils = PlatformTestUtils.create({
platform: PlatformExpress,
server: Server,
logger: {
level: "info"
}
});
describe("Stripe", () => {
let request: SuperTest.Agent;
beforeEach(
utils.bootstrap({
mount: {
"/rest": [StripWebhookCtrl]
}
})
);
beforeEach(() => {
request = SuperTest.agent(PlatformTest.callback());
});
afterEach(() => PlatformTest.reset());
it("should call the webhook", async () => {
const stripe = PlatformTest.get<Stripe>(Stripe);
const payload = {
id: "evt_test_webhook",
object: "event"
};
const payloadString = JSON.stringify(payload, null, 2);
const signature = stripe.webhooks.generateTestHeaderString({
payload: payloadString,
secret: "whsec_test_secret"
});
const response = await request.post("/rest/webhooks/callback").send(payloadString).set("stripe-signature", signature).expect(200);
expect(response.body).to.deep.eq({
event: payload,
received: true
});
});
});
Please read contributing guidelines here
Thank you to all our backers! 🙏 [Become a backer]
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
The MIT License (MIT)
Copyright (c) 2016 - 2022 Romain Lenzotti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.