Skip to content

Commit

Permalink
Merge pull request #16634 from mozilla/FXA-8891
Browse files Browse the repository at this point in the history
feat(stripe-client): add stripe typings utility type and base types
  • Loading branch information
julianpoy authored Mar 27, 2024
2 parents d9a3bc0 + 46c92b3 commit be35da3
Show file tree
Hide file tree
Showing 17 changed files with 383 additions and 103 deletions.
43 changes: 20 additions & 23 deletions libs/payments/legacy/src/lib/stripe-mapper.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ describe('StripeMapperService', () => {
const validMetadata = StripeMetadataWithContentfulFactory({
webIconURL: 'https://example.com/webIconURL',
});
const stripePlan = PlanFactory({
product: ProductFactory({
metadata: validMetadata,
}),
const stripePlan = PlanFactory() as Stripe.Plan;
stripePlan.product = ProductFactory({
metadata: validMetadata,
});
const { mappedPlans, nonMatchingPlans } =
await stripeMapper.mapContentfulToStripePlans(
Expand All @@ -89,10 +88,9 @@ describe('StripeMapperService', () => {
const validMetadata = StripeMetadataWithContentfulFactory({
webIconURL: 'https://example.com/webIconURL',
});
const stripePlan = PlanFactory({
product: ProductFactory({
metadata: validMetadata,
}),
const stripePlan = PlanFactory() as Stripe.Plan;
stripePlan.product = ProductFactory({
metadata: validMetadata,
});
const { mappedPlans, nonMatchingPlans } =
await stripeMapper.mapContentfulToStripePlans(
Expand Down Expand Up @@ -121,10 +119,10 @@ describe('StripeMapperService', () => {
webIconURL: 'https://plan.override/emailIcon',
});
const stripePlan = PlanFactory({
product: ProductFactory({
metadata: validMetadata,
}),
metadata: planOverrideMetadata,
}) as Stripe.Plan;
stripePlan.product = ProductFactory({
metadata: validMetadata,
});
const { mappedPlans, nonMatchingPlans } =
await stripeMapper.mapContentfulToStripePlans(
Expand All @@ -147,10 +145,9 @@ describe('StripeMapperService', () => {
productSet: 'set',
productOrder: 'order',
};
const stripePlan = PlanFactory({
product: ProductFactory({
metadata: productMetadata,
}),
const stripePlan = PlanFactory() as Stripe.Plan;
stripePlan.product = ProductFactory({
metadata: productMetadata,
});
const { mappedPlans, nonMatchingPlans } =
await stripeMapper.mapContentfulToStripePlans(
Expand Down Expand Up @@ -190,18 +187,18 @@ describe('StripeMapperService', () => {
metadata: productMetadata,
});
const stripePlan1 = PlanFactory({
product,
metadata: StripeMetadataWithContentfulFactory({
'product:details:1': 'Detail 1 in English',
}),
});
}) as Stripe.Plan;
stripePlan1.product = product;
const stripePlan2 = PlanFactory({
product,
metadata: StripeMetadataWithContentfulFactory({
'product:details:1': 'Detail 1 in French',
'product:details:2': 'Detail 2 in French',
}),
});
}) as Stripe.Plan;
stripePlan2.product = product;

const { mappedPlans, nonMatchingPlans } =
await stripeMapper.mapContentfulToStripePlans(
Expand Down Expand Up @@ -243,18 +240,18 @@ describe('StripeMapperService', () => {
metadata: productMetadata,
});
const stripePlan1 = PlanFactory({
product,
metadata: StripeMetadataWithContentfulFactory({
'product:details:1': 'Detail 1 in German',
}),
});
}) as Stripe.Plan;
stripePlan1.product = product;
const stripePlan2 = PlanFactory({
product,
metadata: StripeMetadataWithContentfulFactory({
'product:details:1': 'Detail 1 in French',
'product:details:2': 'Detail 2 in French',
}),
});
}) as Stripe.Plan;
stripePlan2.product = product;

const { mappedPlans, nonMatchingPlans } =
await stripeMapper.mapContentfulToStripePlans(
Expand Down
12 changes: 2 additions & 10 deletions libs/payments/paypal/src/lib/paypal.manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,7 @@ describe('PaypalManager', () => {
data: [mockPayPalSubscription],
});

const mockCustomer = CustomerFactory({
subscriptions: {
object: 'list',
data: [mockPayPalSubscription],
has_more: true,
url: '/v1/customers/customer12345/subscriptions',
},
});
const mockCustomer = CustomerFactory();

const expected = [mockPayPalSubscription];

Expand All @@ -224,7 +217,7 @@ describe('PaypalManager', () => {
.mockResolvedValueOnce(mockSubscriptionList);

const result = await paypalManager.getCustomerPayPalSubscriptions(
mockCustomer
mockCustomer.id
);
expect(result).toEqual(expected);
});
Expand Down Expand Up @@ -287,7 +280,6 @@ describe('PaypalManager', () => {
const mockInvoice = InvoiceFactory({
amount_due: 50,
currency: 'usd',
customer: mockCustomer,
});

stripeManager.fetchActiveCustomer = jest
Expand Down
20 changes: 10 additions & 10 deletions libs/payments/paypal/src/lib/paypal.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { Injectable } from '@nestjs/common';
import { Stripe } from 'stripe';

import {
ACTIVE_SUBSCRIPTION_STATUSES,
StripeCustomer,
StripeInvoice,
StripeManager,
} from '@fxa/payments/stripe';
import { AccountDatabase } from '@fxa/shared/db/mysql/account';
Expand Down Expand Up @@ -62,7 +63,7 @@ export class PayPalManager {
* Retrieves the customer’s current paypal billing agreement ID from the
* auth database via the Paypal repository
*/
async getCustomerBillingAgreementId(customer: Stripe.Customer) {
async getCustomerBillingAgreementId(customer: StripeCustomer) {
const paypalCustomer =
await this.paypalCustomerManager.fetchPaypalCustomersByUid(
customer.metadata.userid
Expand All @@ -80,10 +81,8 @@ export class PayPalManager {
/**
* Retrieves PayPal subscriptions
*/
async getCustomerPayPalSubscriptions(customer: Stripe.Customer) {
const subscriptions = await this.stripeManager.getSubscriptions(
customer.id
);
async getCustomerPayPalSubscriptions(customerId: string) {
const subscriptions = await this.stripeManager.getSubscriptions(customerId);
return subscriptions.data.filter(
(sub) =>
ACTIVE_SUBSCRIPTION_STATUSES.includes(sub.status) &&
Expand All @@ -104,8 +103,8 @@ export class PayPalManager {
* Process an invoice when amount is greater than minimum amount
*/
async processNonZeroInvoice(
customer: Stripe.Customer,
invoice: Stripe.Invoice,
customer: StripeCustomer,
invoice: StripeInvoice,
ipaddress?: string
) {
// TODO in M3b: Implement legacy processInvoice as processNonZeroInvoice here
Expand All @@ -128,14 +127,15 @@ export class PayPalManager {
* If amount is less than minimum amount, call processZeroInvoice
* If amount is greater than minimum amount, call processNonZeroInvoice (legacy PaypalHelper processInvoice)
*/
async processInvoice(invoice: Stripe.Invoice) {
async processInvoice(invoice: StripeInvoice) {
if (!invoice.customer) throw new Error('Customer not present on invoice');
const amountInCents = invoice.amount_due;

if (amountInCents < this.stripeManager.getMinimumAmount(invoice.currency)) {
return await this.processZeroInvoice(invoice.id);
} else {
const customer = await this.stripeManager.fetchActiveCustomer(
invoice.customer as string
invoice.customer
);
return await this.processNonZeroInvoice(customer, invoice);
}
Expand Down
1 change: 1 addition & 0 deletions libs/payments/stripe/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from './lib/stripe.client';
export * from './lib/stripe.constants';
export * from './lib/stripe.error';
export * from './lib/stripe.manager';
export * from './lib/stripe.client.types';
4 changes: 2 additions & 2 deletions libs/payments/stripe/src/lib/factories/card.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { faker } from '@faker-js/faker';
import { Stripe } from 'stripe';
import { StripeCard } from '../stripe.client.types';

export const CardFactory = (override?: Partial<Stripe.Card>): Stripe.Card => ({
export const CardFactory = (override?: Partial<StripeCard>): StripeCard => ({
id: 'card',
object: 'card',
address_city: faker.location.city(),
Expand Down
14 changes: 9 additions & 5 deletions libs/payments/stripe/src/lib/factories/customer.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { faker } from '@faker-js/faker';
import { Stripe } from 'stripe';
import { CardFactory } from './card.factory';
import { StripeCustomer } from '../stripe.client.types';

export const CustomerFactory = (
override?: Partial<Stripe.Customer>
): Stripe.Customer => ({
override?: Partial<StripeCustomer>
): StripeCustomer => ({
id: `cus_${faker.string.alphanumeric({ length: 14 })}`,
object: 'customer',
balance: faker.number.int({ max: 1000 }),
created: faker.number.int(),
default_source: CardFactory(),
tax: {
location: null,
automatic_tax: 'supported',
ip_address: faker.internet.ipv4(),
},
default_source: faker.string.uuid(),
description: '',
email: faker.internet.email(),
invoice_settings: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { faker } from '@faker-js/faker';
import { Stripe } from 'stripe';
import { StripeInvoiceLineItem } from '../stripe.client.types';

export const InvoiceLineItemFactory = (
override?: Partial<Stripe.InvoiceLineItem>
): Stripe.InvoiceLineItem => ({
override?: Partial<StripeInvoiceLineItem>
): StripeInvoiceLineItem => ({
id: faker.string.alphanumeric(10),
object: 'line_item',
amount: faker.number.int({ max: 1000 }),
Expand Down
6 changes: 3 additions & 3 deletions libs/payments/stripe/src/lib/factories/invoice.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { faker } from '@faker-js/faker';
import { Stripe } from 'stripe';
import { InvoiceLineItemFactory } from './invoice-line-item.factory';
import { StripeInvoice } from '../stripe.client.types';

export const InvoiceFactory = (
override?: Partial<Stripe.Invoice>
): Stripe.Invoice => ({
override?: Partial<StripeInvoice>
): StripeInvoice => ({
id: `in_${faker.string.alphanumeric({ length: 24 })}`,
object: 'invoice',
account_country: null,
Expand Down
4 changes: 2 additions & 2 deletions libs/payments/stripe/src/lib/factories/plan.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { faker } from '@faker-js/faker';
import { Stripe } from 'stripe';
import { StripePlan } from '../stripe.client.types';

export const PlanFactory = (override?: Partial<Stripe.Plan>): Stripe.Plan => ({
export const PlanFactory = (override?: Partial<StripePlan>): StripePlan => ({
id: `plan_${faker.string.alphanumeric({ length: 24 })}`,
object: 'plan',
active: true,
Expand Down
6 changes: 2 additions & 4 deletions libs/payments/stripe/src/lib/factories/price.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { faker } from '@faker-js/faker';
import { Stripe } from 'stripe';
import { StripePrice } from '../stripe.client.types';

export const PriceFactory = (
override?: Partial<Stripe.Price>
): Stripe.Price => ({
export const PriceFactory = (override?: Partial<StripePrice>): StripePrice => ({
id: `price_${faker.string.alphanumeric({ length: 24 })}`,
object: 'price',
active: true,
Expand Down
6 changes: 3 additions & 3 deletions libs/payments/stripe/src/lib/factories/product.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { faker } from '@faker-js/faker';
import { Stripe } from 'stripe';
import { StripeProduct } from '../stripe.client.types';

export const ProductFactory = (
override?: Partial<Stripe.Product>
): Stripe.Product => ({
override?: Partial<StripeProduct>
): StripeProduct => ({
id: `prod_${faker.string.alphanumeric({ length: 14 })}`,
object: 'product',
active: true,
Expand Down
18 changes: 11 additions & 7 deletions libs/payments/stripe/src/lib/factories/subscription.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { faker } from '@faker-js/faker';
import { Stripe } from 'stripe';
import { PriceFactory } from './price.factory';
import {
StripeApiList,
StripeSubscription,
StripeSubscriptionItem,
} from '../stripe.client.types';

export const SubscriptionFactory = (
override?: Partial<Stripe.Subscription>
): Stripe.Subscription => ({
override?: Partial<StripeSubscription>
): StripeSubscription => ({
id: `sub_${faker.string.alphanumeric({ length: 24 })}`,
object: 'subscription',
application: null,
Expand Down Expand Up @@ -64,8 +68,8 @@ export const SubscriptionFactory = (
});

export const SubscriptionItemFactory = (
override?: Partial<Stripe.SubscriptionItem>
): Stripe.SubscriptionItem => ({
override?: Partial<StripeSubscriptionItem>
): StripeSubscriptionItem => ({
id: `si_${faker.string.alphanumeric({ length: 14 })}`,
object: 'subscription_item',
billing_thresholds: null,
Expand Down Expand Up @@ -102,8 +106,8 @@ export const SubscriptionItemFactory = (
});

export const SubscriptionListFactory = (
override?: Partial<Stripe.ApiList<Stripe.Subscription>>
): Stripe.ApiList<Stripe.Subscription> => ({
override?: Partial<StripeApiList<StripeSubscription>>
): StripeApiList<StripeSubscription> => ({
object: 'list',
url: '/v1/subscriptions',
has_more: false,
Expand Down
Loading

0 comments on commit be35da3

Please sign in to comment.