Skip to content

Commit 2a30b74

Browse files
fix(payment-stripe): allow multiple webhooks
1 parent ea90395 commit 2a30b74

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

microservices/payment-stripe/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This microservice provides payments mechanism for stipe.
2828
- `MS_API_KEY` - Stripe api key to connect ms with stripe sdk. Default: `example`
2929
- `MS_CONFIG` - JSON onject which contains apiVersion property required by Stripe. Default: `'{"apiVersion": "2022-11-15"}'`
3030
- `MS_PAYMENT_METHODS` - Payment methods allowed using in Stripe. Default: `'["bancontact", "card"]'`
31-
- `MS_WEBHOOK_KEY` - Stripe webhook key to connect ms and work with stripe webhook service. Example: `whsec_c6332a6b339173127d2e6c813112e2f2323b4b2b10eea5ac17c5649a60cf335e`
31+
- `MS_WEBHOOK_KEYS` - Stripe webhook keys to connect ms and work with stripe webhook service. Example: `{"myId":"whsec_c6332a6b339173127d2e6c813112e2f2323b4b2b10eea5ac17c5649a60cf335e"}`
3232
- `MS_PAYOUT_COEFF` - Number for calculating amount of money for transferring to the product owners. Default: `0.3`
3333
- `MS_FEES` - Fees that takes while creating payment intent. Default: `'{ "stableUnit": 30, "paymentPercent": 2.9 }'`
3434
- [See full list `COMMON ENVIRONMENTS`](https://github.com/Lomray-Software/microservice-helpers#common-environments)

microservices/payment-stripe/src/config/remote.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const defaultConfig: IRemoteConfig = {
66
paymentMethods: CONST.MS_PAYMENT_METHODS,
77
apiKey: CONST.MS_API_KEY,
88
config: CONST.MS_CONFIG,
9-
webhookKey: CONST.MS_WEBHOOK_KEY,
9+
webhookKeys: CONST.MS_WEBHOOK_KEYS,
1010
payoutCoeff: CONST.MS_PAYOUT_COEFF,
1111
fees: JSON.parse(CONST.MS_FEES),
1212
};

microservices/payment-stripe/src/constants/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const constants = {
99
MS_API_KEY: process.env.MS_API_KEY ?? '',
1010
MS_CONFIG: JSON.parse(process.env.MS_CONFIG ?? '{"apiVersion": "2022-11-15"}'),
1111
MS_PAYMENT_METHODS: JSON.parse(process.env.MS_PAYMENT_METHODS ?? '["bancontact", "card"]'),
12-
MS_WEBHOOK_KEY: process.env.MS_WEBHOOK_KEY ?? '',
12+
MS_WEBHOOK_KEYS: JSON.parse(process.env.MS_WEBHOOK_KEYS ?? '{}'),
1313
MS_PAYOUT_COEFF: Number(process.env.MS_PAYOUT_COEFF) ?? 0.3,
1414
MS_FEES: process.env.MS_FEES ?? '{ "stableUnit": 30, "paymentPercent": 2.9 }',
1515
};

microservices/payment-stripe/src/interfaces/remote-config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import IFees from '@interfaces/fees';
55
* Microservice remote config
66
*/
77
export interface IRemoteConfig {
8-
webhookKey?: string;
8+
webhookKeys?: {
9+
[id: string]: string; // id (unique key) - secret (webhook Signing secret)
10+
};
911
payoutCoeff?: number;
1012
fees?: IFees;
1113
paymentMethods?: string[];

microservices/payment-stripe/src/methods/stripe/webhook.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ class WebhookInput {
1111

1212
@IsString()
1313
rawBody: string;
14+
15+
@IsObject()
16+
query: {
17+
id: string; // webhook id
18+
};
1419
}
1520

1621
class WebhookOutput {
@@ -24,17 +29,18 @@ const webhook = Endpoint.custom(
2429
() => ({
2530
input: WebhookInput,
2631
output: WebhookOutput,
27-
description: 'Get stripe webhooks handler',
32+
description: 'Handling and processing stripe webhooks.',
2833
}),
29-
async ({ rawBody, payload }) => {
30-
const { webhookKey } = await remoteConfig();
34+
async ({ rawBody, query, payload }) => {
35+
const { webhookKeys } = await remoteConfig();
36+
const webhookKey = webhookKeys?.[query?.id];
3137

3238
if (!webhookKey) {
33-
throw new Error('Webhook key is not provided');
39+
throw new Error(`Webhook key is not provided for id: ${query.id}`);
3440
}
3541

3642
if (!payload?.headers?.['stripe-signature']) {
37-
throw new Error('Stripe signature is mot provided');
43+
throw new Error('Stripe signature is mot provided.');
3844
}
3945

4046
const service = await Factory.create(getManager());

0 commit comments

Comments
 (0)