Skip to content

Commit

Permalink
Revision
Browse files Browse the repository at this point in the history
  • Loading branch information
xlisachan committed Mar 6, 2025
1 parent f8c5f9f commit d10f66f
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 184 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
SubscriptionTitle,
TermsAndPrivacy,
} from '@fxa/payments/ui/server';
import { CartState } from '@fxa/shared/db/mysql/account';
import { auth } from 'apps/payments/next/auth';
import { config } from 'apps/payments/next/config';

Expand All @@ -25,7 +26,7 @@ export interface CheckoutSearchParams {
promotion_code?: string;
}

export default async function CheckoutOtherLayout({
export default async function CheckoutLayout({
children,
params,
}: {
Expand All @@ -50,7 +51,6 @@ export default async function CheckoutOtherLayout({
const purchaseDetails =
cms.defaultPurchase.purchaseDetails.localizations.at(0) ||
cms.defaultPurchase.purchaseDetails;
console.log(cart);
return (
<MetricsWrapper cart={cart}>
{session?.user?.email && (
Expand Down Expand Up @@ -95,14 +95,22 @@ export default async function CheckoutOtherLayout({
countryCode={cart.taxAddress?.countryCode}
postalCode={cart.taxAddress?.postalCode}
/>
{cart.couponCode && (
{(cart.state === CartState.START && (
<CouponForm
cartId={cart.id}
cartVersion={cart.version}
promoCode={cart.couponCode}
readOnly={true}
readOnly={false}
/>
)}
)) ||
(cart.couponCode && (
<CouponForm
cartId={cart.id}
cartVersion={cart.version}
promoCode={cart.couponCode}
readOnly={true}
/>
))}
</section>

<div className="bg-white rounded-b-lg shadow-sm shadow-grey-300 border-t-0 mb-6 pt-4 px-4 pb-14 rounded-t-lg text-grey-600 tablet:clip-shadow tablet:rounded-t-none desktop:px-12 desktop:pb-12">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,17 @@ export default async function CheckoutSuccess({
</div>
<div className="flex items-center justify-between text-grey-400">
<span>
{formatPlanPricing(
cart.latestInvoicePreview?.totalAmount ?? null,
cart.latestInvoicePreview?.currency ?? '',
cart.interval
{l10n.getString(
'success-page-payment-confirmation-amount',
{
amount: cart.latestInvoicePreview?.totalAmount,
interval: cart.interval,
},
formatPlanPricing(
cart.latestInvoicePreview?.totalAmount ?? null,
cart.latestInvoicePreview?.currency ?? '',
cart.interval
)
)}
</span>
{cart.paymentInfo.type === 'external_paypal' ? (
Expand Down
9 changes: 4 additions & 5 deletions libs/payments/cart/src/lib/checkout.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
PriceManager,
ProductManager,
PromotionCodeManager,
STRIPE_CUSTOMER_METADATA,
STRIPE_SUBSCRIPTION_METADATA,
SubscriptionManager,
TaxAddressFactory,
} from '@fxa/payments/customer';
Expand Down Expand Up @@ -447,7 +447,7 @@ describe('CheckoutService', () => {
const mockUpdatedSubscription = StripeResponseFactory(
StripeSubscriptionFactory({
metadata: {
[STRIPE_CUSTOMER_METADATA.SubscriptionPromotionCode]:
[STRIPE_SUBSCRIPTION_METADATA.SubscriptionPromotionCode]:
mockCart.couponCode as string,
},
})
Expand Down Expand Up @@ -476,11 +476,10 @@ describe('CheckoutService', () => {
{
metadata: {
...mockSubscription.metadata,
[STRIPE_CUSTOMER_METADATA.SubscriptionPromotionCode]:
[STRIPE_SUBSCRIPTION_METADATA.SubscriptionPromotionCode]:
mockCart.couponCode,
},
},
mockSubscription.metadata
}
);
});
});
Expand Down
15 changes: 6 additions & 9 deletions libs/payments/cart/src/lib/checkout.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,13 @@ export class CheckoutService {
await this.customerChanged(uid);

if (cart.couponCode) {
await this.subscriptionManager.update(
subscription.id,
{
metadata: {
[STRIPE_CUSTOMER_METADATA.SubscriptionPromotionCode]:
cart.couponCode,
},
await this.subscriptionManager.update(subscription.id, {
metadata: {
...subscription.metadata,
[STRIPE_SUBSCRIPTION_METADATA.SubscriptionPromotionCode]:
cart.couponCode,
},
subscription.metadata
);
});
}
await this.cartManager.finishCart(cart.id, version, {});

Expand Down
24 changes: 9 additions & 15 deletions libs/payments/customer/src/lib/subscription.manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
StripeSubscriptionFactory,
MockStripeConfigProvider,
} from '@fxa/payments/stripe';
import { STRIPE_CUSTOMER_METADATA } from './types';
import { STRIPE_SUBSCRIPTION_METADATA } from './types';
import { SubscriptionManager } from './subscription.manager';
import { MockStatsDProvider } from '@fxa/shared/metrics/statsd';

Expand Down Expand Up @@ -177,21 +177,14 @@ describe('SubscriptionManager', () => {
});

it('updates metadata', async () => {
const mockMetadata = { amount: '1200', currency: 'usd' };
const mockParams = {
metadata: {
[STRIPE_CUSTOMER_METADATA.SubscriptionPromotionCode]: 'test-coupon',
[STRIPE_SUBSCRIPTION_METADATA.SubscriptionPromotionCode]:
'test-coupon',
},
};

const newParams = {
metadata: {
...mockParams.metadata,
...mockMetadata,
},
};

const mockSubscription = StripeSubscriptionFactory(newParams);
const mockSubscription = StripeSubscriptionFactory(mockParams);
const mockResponse = StripeResponseFactory(mockSubscription);

jest
Expand All @@ -200,21 +193,22 @@ describe('SubscriptionManager', () => {

const result = await subscriptionManager.update(
mockSubscription.id,
mockParams,
mockMetadata
mockParams
);

expect(stripeClient.subscriptionsUpdate).toBeCalledWith(
mockSubscription.id,
newParams
mockParams
);
expect(result).toEqual(mockResponse);
});

it('throws if metadata key does not match', async () => {
const mockParams = {
metadata: {
[STRIPE_CUSTOMER_METADATA.SubscriptionPromotionCode]: 'test-coupon',
amount: '1200',
[STRIPE_SUBSCRIPTION_METADATA.SubscriptionPromotionCode]:
'test-coupon',
promotionCode: 'test-coupon',
},
};
Expand Down
10 changes: 4 additions & 6 deletions libs/payments/customer/src/lib/subscription.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
StripeSubscription,
} from '@fxa/payments/stripe';
import { ACTIVE_SUBSCRIPTION_STATUSES } from '@fxa/payments/stripe';
import { STRIPE_CUSTOMER_METADATA } from './types';
import { STRIPE_SUBSCRIPTION_METADATA } from './types';
import { InvalidPaymentIntentError, PaymentIntentNotFoundError } from './error';

@Injectable()
Expand All @@ -38,21 +38,19 @@ export class SubscriptionManager {

async update(
subscriptionId: string,
params?: Stripe.SubscriptionUpdateParams,
metadata?: Stripe.Metadata
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
!Object.values(STRIPE_SUBSCRIPTION_METADATA).includes(
key as STRIPE_SUBSCRIPTION_METADATA
)
) {
throw new Error(`Invalid metadata key: ${key}`);
}
});
params.metadata = { ...params.metadata, ...metadata };
}
return this.stripeClient.subscriptionsUpdate(subscriptionId, params);
}
Expand Down
2 changes: 1 addition & 1 deletion libs/payments/customer/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export interface TaxAmount {

export enum STRIPE_CUSTOMER_METADATA {
PaypalAgreement = 'paypalAgreementId',
SubscriptionPromotionCode = 'appliedPromotionCode',
}

export enum STRIPE_PRICE_METADATA {
Expand All @@ -48,6 +47,7 @@ export enum STRIPE_PRODUCT_METADATA {
export enum STRIPE_SUBSCRIPTION_METADATA {
Currency = 'currency',
Amount = 'amount',
SubscriptionPromotionCode = 'appliedPromotionCode',
}

export enum STRIPE_INVOICE_METADATA {
Expand Down

0 comments on commit d10f66f

Please sign in to comment.