From a9af5fa4027a5411e66db26ba0de2d68d6723bcf Mon Sep 17 00:00:00 2001 From: Lisa Chan Date: Wed, 28 Aug 2024 11:22:30 -0400 Subject: [PATCH] fix(payments-stripe): Revises SubscriptionManager.update() --- .../src/lib/subscription.manager.spec.ts | 40 +++++++++++++++++++ .../stripe/src/lib/subscription.manager.ts | 14 +++++++ 2 files changed, 54 insertions(+) diff --git a/libs/payments/stripe/src/lib/subscription.manager.spec.ts b/libs/payments/stripe/src/lib/subscription.manager.spec.ts index 8ef9ebfe24..0d6069d96e 100644 --- a/libs/payments/stripe/src/lib/subscription.manager.spec.ts +++ b/libs/payments/stripe/src/lib/subscription.manager.spec.ts @@ -15,6 +15,7 @@ import { StripePaymentIntentFactory } from './factories/payment-intent.factory'; import { StripeSubscriptionFactory } from './factories/subscription.factory'; import { StripeClient } from './stripe.client'; import { MockStripeConfigProvider } from './stripe.config'; +import { STRIPE_CUSTOMER_METADATA } from './stripe.types'; import { SubscriptionManager } from './subscription.manager'; describe('SubscriptionManager', () => { @@ -167,6 +168,45 @@ describe('SubscriptionManager', () => { ); expect(result).toEqual(mockResponse); }); + + it('updates metadata', async () => { + const mockParams = { + metadata: { + [STRIPE_CUSTOMER_METADATA.SubscriptionPromotionCode]: 'test-coupon', + }, + }; + const mockSubscription = StripeSubscriptionFactory(mockParams); + const mockResponse = StripeResponseFactory(mockSubscription); + + jest + .spyOn(stripeClient, 'subscriptionsUpdate') + .mockResolvedValue(mockResponse); + + const result = await subscriptionManager.update( + mockSubscription.id, + mockParams + ); + + expect(stripeClient.subscriptionsUpdate).toBeCalledWith( + mockSubscription.id, + mockParams + ); + expect(result).toEqual(mockResponse); + }); + + it('throws if metadata key does not match', async () => { + const mockParams = { + metadata: { + [STRIPE_CUSTOMER_METADATA.SubscriptionPromotionCode]: 'test-coupon', + promotionCode: 'test-coupon', + }, + }; + const mockSubscription = StripeSubscriptionFactory(mockParams); + + expect(() => + subscriptionManager.update(mockSubscription.id, mockParams) + ).rejects.toThrow('Invalid metadata key: promotionCode'); + }); }); describe('getCustomerPayPalSubscriptions', () => { diff --git a/libs/payments/stripe/src/lib/subscription.manager.ts b/libs/payments/stripe/src/lib/subscription.manager.ts index 23cfebfbfb..5a83999d75 100644 --- a/libs/payments/stripe/src/lib/subscription.manager.ts +++ b/libs/payments/stripe/src/lib/subscription.manager.ts @@ -8,6 +8,7 @@ import { Stripe } from 'stripe'; import { StripeClient } from './stripe.client'; import { StripeSubscription } from './stripe.client.types'; import { ACTIVE_SUBSCRIPTION_STATUSES } from './stripe.constants'; +import { STRIPE_CUSTOMER_METADATA } from './stripe.types'; @Injectable() export class SubscriptionManager { @@ -32,6 +33,19 @@ export class SubscriptionManager { subscriptionId: string, params?: Stripe.SubscriptionUpdateParams ) { + if (params?.metadata) { + const newMetadata = params.metadata; + Object.keys(newMetadata).forEach((key) => { + if ( + !Object.values(STRIPE_CUSTOMER_METADATA).includes( + key as STRIPE_CUSTOMER_METADATA + ) + ) { + throw new Error(`Invalid metadata key: ${key}`); + } + }); + } + return this.stripeClient.subscriptionsUpdate(subscriptionId, params); }