Skip to content

fix(clerk-js): Use the credit amount from FAPI for credit proration checkout flow #5792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
6 changes: 6 additions & 0 deletions .changeset/fuzzy-donkeys-post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-js': patch
'@clerk/types': patch
---

Use the `total.proration.credit` to show the proration credit on checkout flow
9 changes: 4 additions & 5 deletions packages/clerk-js/src/ui/components/Checkout/CheckoutForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ export const CheckoutForm = ({
onCheckoutComplete: (checkout: __experimental_CommerceCheckoutResource) => void;
}) => {
const { plan, planPeriod, totals, isImmediatePlanChange } = checkout;

const adjustmentAmount = (totals.proration?.days || 0) * (totals.proration?.ratePerDay.amount || 0);
const showAdjustment = totals.totalDueNow.amount > 0 && adjustmentAmount > 0;
const showAdjustment =
totals.totalDueNow.amount > 0 && totals.proration?.credit?.amount && totals.proration.credit.amount > 0;
const showDowngradeInfo = !isImmediatePlanChange;

return (
Expand Down Expand Up @@ -72,13 +71,13 @@ export const CheckoutForm = ({
<LineItems.Group>
{/* TODO(@Commerce): needs localization */}
<LineItems.Title
title={'Adjustment'}
title={'Credit'}
description={'Prorated credit for the remainder of your subscription.'}
/>
{/* TODO(@Commerce): needs localization */}
{/* TODO(@Commerce): Replace client-side calculation with server-side calculation once data are available in the response */}
<LineItems.Description
text={`- ${totals.proration?.totalProration.currencySymbol}${adjustmentAmount / 100}`}
text={`- ${totals.proration?.credit.currencySymbol}${totals.proration?.credit.amountFormatted}`}
/>
</LineItems.Group>
)}
Expand Down
2 changes: 2 additions & 0 deletions packages/clerk-js/src/utils/commerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const commerceTotalsFromJSON = <
ratePerDay: commerceMoneyFromJSON(data.proration.rate_per_day),
// @ts-ignore
totalProration: commerceMoneyFromJSON(data.proration.total_proration),
// @ts-ignore
credit: commerceMoneyFromJSON(data.proration.credit),
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/types/src/commerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export interface __experimental_CommerceCheckoutTotals {
days: number;
ratePerDay: __experimental_CommerceMoney;
totalProration: __experimental_CommerceMoney;
credit: __experimental_CommerceMoney;
};
}

Expand Down