Merge pull request #17492 from mozilla/update_SubscriptionManager

fix(payments-stripe): Revises SubscriptionManager.update()
This commit is contained in:
Lisa Chan 2024-08-28 12:50:19 -04:00 коммит произвёл GitHub
Родитель bd68ab7450 a9af5fa402
Коммит d9fd2fb020
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 54 добавлений и 0 удалений

Просмотреть файл

@ -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', () => {

Просмотреть файл

@ -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);
}