Skip to content
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

refactor(console): get and check skuId from checkout session #6369

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
2 changes: 1 addition & 1 deletion packages/console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"devDependencies": {
"@fontsource/roboto-mono": "^5.0.0",
"@jest/types": "^29.5.0",
"@logto/cloud": "0.2.5-3b703da",
"@logto/cloud": "0.2.5-923c26f",
"@logto/connector-kit": "workspace:^4.0.0",
"@logto/core-kit": "workspace:^2.5.0",
"@logto/elements": "workspace:^0.0.0",
Expand Down
48 changes: 39 additions & 9 deletions packages/console/src/pages/CheckoutSuccessCallback/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import { useCloudApi } from '@/cloud/hooks/use-cloud-api';
import AppLoading from '@/components/AppLoading';
import { GtagConversionId, reportToGoogle } from '@/components/Conversion/utils';
import PlanName from '@/components/PlanName';
import { isDevFeaturesEnabled } from '@/consts/env';
import { checkoutStateQueryKey } from '@/consts/subscriptions';
import { TenantsContext } from '@/contexts/TenantsProvider';
import useLogtoSkus from '@/hooks/use-logto-skus';
import useSubscriptionPlans from '@/hooks/use-subscription-plans';
import useTenantPathname from '@/hooks/use-tenant-pathname';
import { clearLocalCheckoutSession, getLocalCheckoutSession } from '@/utils/checkout';
Expand All @@ -29,8 +31,11 @@ function CheckoutSuccessCallback() {
const { search } = useLocation();
const checkoutState = new URLSearchParams(search).get(checkoutStateQueryKey);
const { state, sessionId, callbackPage, isDowngrade } = getLocalCheckoutSession() ?? {};

const { data: subscriptionPlans, error: fetchPlansError } = useSubscriptionPlans();
const isLoadingPlans = !subscriptionPlans && !fetchPlansError;
const { data: logtoSkus, error: fetchLogtoSkusError } = useLogtoSkus();
const isLoadingLogtoSkus = !logtoSkus && !fetchLogtoSkusError;

// Note: if we can't get the subscription results in 10 seconds, we will redirect to the console home page
useTimer({
Expand Down Expand Up @@ -61,6 +66,7 @@ function CheckoutSuccessCallback() {

const checkoutTenantId = stripeCheckoutSession?.tenantId;
const checkoutPlanId = stripeCheckoutSession?.planId;
const checkoutSkuId = stripeCheckoutSession?.skuId;

const { data: tenantSubscription } = useSWR(
checkoutTenantId && `/api/tenants/${checkoutTenantId}/subscription`,
Expand All @@ -74,22 +80,45 @@ function CheckoutSuccessCallback() {
);

const isCheckoutSuccessful =
!isLoadingPlans &&
checkoutTenantId &&
stripeCheckoutSession.status === 'complete' &&
checkoutPlanId === tenantSubscription?.planId;
(isDevFeaturesEnabled
? !isLoadingLogtoSkus && checkoutSkuId === tenantSubscription?.planId
: !isLoadingPlans && checkoutPlanId === tenantSubscription?.planId);

useEffect(() => {
if (isCheckoutSuccessful) {
clearLocalCheckoutSession();

const checkoutPlan = subscriptionPlans?.find((plan) => plan.id === checkoutPlanId);
if (checkoutPlan) {
toast.success(
<Trans components={{ name: <PlanName name={checkoutPlan.name} /> }}>
{t(isDowngrade ? 'downgrade_success' : 'upgrade_success')}
</Trans>
);
if (isDevFeaturesEnabled) {
const checkoutSku = logtoSkus?.find((sku) => sku.id === checkoutPlanId);
if (checkoutSku) {
toast.success(
<Trans
components={{
name: (
<PlanName
skuId={checkoutSku.id}
// Generally `checkoutPlanId` and a properly setup of SKU `name` should not be null, we still need to handle the edge case to make the type inference happy.
// Also `name` will be deprecated in the future once the new pricing model is ready.
name={checkoutPlanId ?? checkoutSku.name ?? checkoutSku.id}
darcyYe marked this conversation as resolved.
Show resolved Hide resolved
/>
),
}}
>
{t(isDowngrade ? 'downgrade_success' : 'upgrade_success')}
</Trans>
);
}
} else {
const checkoutPlan = subscriptionPlans?.find((plan) => plan.id === checkoutPlanId);
if (checkoutPlan) {
toast.success(
<Trans components={{ name: <PlanName name={checkoutPlan.name} /> }}>
{t(isDowngrade ? 'downgrade_success' : 'upgrade_success')}
</Trans>
);
}
}

// No need to check `isDowngrade` here, since a downgrade must occur in a tenant with a Pro
Expand All @@ -115,6 +144,7 @@ function CheckoutSuccessCallback() {
currentTenantId,
isCheckoutSuccessful,
isDowngrade,
logtoSkus,
navigate,
navigateTenant,
subscriptionPlans,
Expand Down
33 changes: 22 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading