Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ describe('PlanCanceller', () => {
expect.objectContaining({
subscription: mockSub,
customer: mockCustomer,
paymentProvider: 'Stripe',
isExcluded: false,
amountRefunded: 1000,
isOwed: false,
Expand All @@ -246,17 +247,23 @@ describe('PlanCanceller', () => {
});
});

describe('success - with refund', () => {
describe('success - PayPal subscription with refund', () => {
const paypalSub = {
...mockSub,
collection_method: 'send_invoice',
} as unknown as Stripe.Subscription;

beforeEach(async () => {
attemptFullRefundStub.mockResolvedValue(1000);
await planCanceller.processSubscription(mockSub);
await planCanceller.processSubscription(paypalSub);
});

it('writes report with refund amount', () => {
it('writes report with PayPal provider and refund amount', () => {
expect(writeReportStub).toHaveBeenCalledWith(
expect.objectContaining({
subscription: mockSub,
subscription: paypalSub,
customer: mockCustomer,
paymentProvider: 'PayPal',
isExcluded: false,
amountRefunded: 1000,
isOwed: false,
Expand Down Expand Up @@ -285,6 +292,7 @@ describe('PlanCanceller', () => {
expect.objectContaining({
subscription: mockSub,
customer: mockCustomer,
paymentProvider: 'Stripe',
isExcluded: false,
amountRefunded: 1000,
isOwed: false,
Expand All @@ -309,6 +317,7 @@ describe('PlanCanceller', () => {
expect.objectContaining({
subscription: mockSub,
customer: mockCustomer,
paymentProvider: 'Stripe',
isExcluded: true,
amountRefunded: null,
isOwed: false,
Expand All @@ -327,6 +336,7 @@ describe('PlanCanceller', () => {
expect.objectContaining({
subscription: mockSub,
customer: null,
paymentProvider: 'Stripe',
isExcluded: false,
amountRefunded: null,
isOwed: false,
Expand All @@ -343,6 +353,7 @@ describe('PlanCanceller', () => {
expect.objectContaining({
subscription: mockSub,
customer: null,
paymentProvider: 'Stripe',
isExcluded: false,
amountRefunded: null,
isOwed: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export class PlanCanceller {
typeof subscription.customer === 'string'
? subscription.customer
: subscription.customer.id;
const paymentProvider =
subscription.collection_method === 'send_invoice' ? 'PayPal' : 'Stripe';

try {
console.log(`Processing ${subscription.id}`);
Expand Down Expand Up @@ -160,6 +162,7 @@ export class PlanCanceller {
await this.writeReport({
subscription,
customer,
paymentProvider,
isExcluded,
amountRefunded,
approximateAmountWasOwed,
Expand All @@ -176,6 +179,7 @@ export class PlanCanceller {
await this.writeReport({
subscription,
customer: null,
paymentProvider,
isExcluded: false,
amountRefunded: null,
approximateAmountWasOwed: null,
Expand Down Expand Up @@ -371,7 +375,8 @@ export class PlanCanceller {
"daysUntilNextBill",
"previousInvoiceAmountDue",
"isOwed",
"error"
"error",
"paymentProvider"
];

const reportCSV = data.join(',') + '\n';
Expand All @@ -385,6 +390,7 @@ export class PlanCanceller {
async writeReport(args: {
subscription: Stripe.Subscription,
customer: Stripe.Customer | null,
paymentProvider: 'Stripe' | 'PayPal',
isExcluded: boolean,
amountRefunded: number | null,
approximateAmountWasOwed: number | null,
Expand All @@ -408,7 +414,8 @@ export class PlanCanceller {
args.daysUntilNextBill ?? "null",
args.previousInvoiceAmountDue ?? "null",
args.isOwed,
args.error
args.error,
args.paymentProvider
];

const reportCSV = data.join(',') + '\n';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ describe('CustomerPlanMover v2', () => {
expect(writeReportStub).toHaveBeenCalledTimes(1);
const reportArgs = writeReportStub.mock.calls[0][0];
expect(reportArgs.subscription.id).toBe('sub_123');
expect(reportArgs.paymentProvider).toBe('Stripe');
expect(reportArgs.isExcluded).toBe(false);
expect(reportArgs.amountRefunded).toBe(null);
expect(reportArgs.approximateAmountWasOwed).toBe(null);
Expand Down Expand Up @@ -442,8 +443,12 @@ describe('CustomerPlanMover v2', () => {
});
});

describe('success - with prorated refund', () => {
describe('success - PayPal subscription with prorated refund', () => {
let attemptRefundStub: jest.Mock;
const paypalSub = {
...mockStripeSubscription,
collection_method: 'send_invoice',
} as unknown as Stripe.Subscription;

beforeEach(async () => {
customerPlanMover = new CustomerPlanMover(
Expand All @@ -470,20 +475,18 @@ describe('CustomerPlanMover v2', () => {

stripeStub.subscriptions.update = jest
.fn()
.mockResolvedValue(mockStripeSubscription);
.mockResolvedValue(paypalSub);

await customerPlanMover.convertSubscription(
mockStripeSubscription,
mockPrice
);
await customerPlanMover.convertSubscription(paypalSub, mockPrice);
});

it('attempts refund', () => {
expect(attemptRefundStub).toHaveBeenCalledWith(mockStripeSubscription);
expect(attemptRefundStub).toHaveBeenCalledWith(paypalSub);
});

it('writes report with refund amount', () => {
it('writes report with PayPal provider and refund amount', () => {
const reportArgs = writeReportStub.mock.calls[0][0];
expect(reportArgs.paymentProvider).toBe('PayPal');
expect(reportArgs.amountRefunded).toBe(500);
expect(reportArgs.isOwed).toBe(false);
expect(reportArgs.error).toBe(false);
Expand Down Expand Up @@ -530,6 +533,7 @@ describe('CustomerPlanMover v2', () => {

it('marks customer as owed', () => {
const reportArgs = writeReportStub.mock.calls[0][0];
expect(reportArgs.paymentProvider).toBe('Stripe');
expect(reportArgs.isOwed).toBe(true);
expect(reportArgs.amountRefunded).toBe(null);
expect(reportArgs.error).toBe(false);
Expand Down Expand Up @@ -581,6 +585,7 @@ describe('CustomerPlanMover v2', () => {

it('writes report marking as excluded', () => {
const reportArgs = writeReportStub.mock.calls[0][0];
expect(reportArgs.paymentProvider).toBe('Stripe');
expect(reportArgs.isExcluded).toBe(true);
expect(reportArgs.error).toBe(false);
expect(reportArgs.amountRefunded).toBe(null);
Expand Down Expand Up @@ -724,6 +729,7 @@ describe('CustomerPlanMover v2', () => {

expect(writeReportStub).toHaveBeenCalledTimes(1);
const reportArgs = writeReportStub.mock.calls[0][0];
expect(reportArgs.paymentProvider).toBe('Stripe');
expect(reportArgs.error).toBe(true);
expect(reportArgs.customer).toBe(null);
expect(reportArgs.isOwed).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export class CustomerPlanMover {
}

async convertSubscription(subscription: Stripe.Subscription, destinationPrice: Stripe.Price) {
const paymentProvider =
subscription.collection_method === 'send_invoice' ? 'PayPal' : 'Stripe';

try {
console.log(`Processing ${subscription.id}`);

Expand All @@ -101,7 +104,7 @@ export class CustomerPlanMover {
const isExcluded = this.isCustomerExcluded(customer.subscriptions.data);

const destinationPriceCurrencyOptionForCurrency = destinationPrice.currency_options?.[subscription.currency];
const destinationPriceUnitAmountForCurrency =
const destinationPriceUnitAmountForCurrency =
destinationPriceCurrencyOptionForCurrency?.unit_amount ??
(typeof destinationPriceCurrencyOptionForCurrency?.unit_amount_decimal === "string"
? Math.round(parseFloat(destinationPriceCurrencyOptionForCurrency.unit_amount_decimal))
Expand Down Expand Up @@ -166,6 +169,7 @@ export class CustomerPlanMover {
await this.writeReport({
subscription,
customer,
paymentProvider,
isExcluded,
amountRefunded,
approximateAmountWasOwed,
Expand All @@ -182,6 +186,7 @@ export class CustomerPlanMover {
await this.writeReport({
subscription,
customer: null,
paymentProvider,
isExcluded: false,
amountRefunded: null,
approximateAmountWasOwed: null,
Expand Down Expand Up @@ -334,7 +339,8 @@ export class CustomerPlanMover {
"daysSinceLastBill",
"previousInvoiceAmountDue",
"isOwed",
"error"
"error",
"paymentProvider"
];

const reportCSV = data.join(',') + '\n';
Expand All @@ -348,6 +354,7 @@ export class CustomerPlanMover {
async writeReport(args: {
subscription: Stripe.Subscription,
customer: Stripe.Customer | null,
paymentProvider: 'Stripe' | 'PayPal',
isExcluded: boolean,
amountRefunded: number | null,
approximateAmountWasOwed: number | null,
Expand All @@ -371,7 +378,8 @@ export class CustomerPlanMover {
args.daysSinceLastBill ?? "null",
args.previousInvoiceAmountDue ?? "null",
args.isOwed,
args.error
args.error,
args.paymentProvider
];

const reportCSV = data.join(',') + '\n';
Expand Down