зеркало из https://github.com/mozilla/fxa.git
fix(payments-stripe): Revises SubscriptionManager.update()
This commit is contained in:
Родитель
d0cd5bd6f8
Коммит
a9af5fa402
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче