0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

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

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

* chore: update @logto/cloud dependency
This commit is contained in:
Darcy Ye 2024-08-01 17:19:52 +08:00 committed by GitHub
parent a1f6009cc5
commit 8e9f6e4a0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 53 additions and 12 deletions

View file

@ -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",

View file

@ -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';
@ -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({
@ -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`,
@ -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}
/>
),
}}
>
{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
@ -115,6 +144,7 @@ function CheckoutSuccessCallback() {
currentTenantId,
isCheckoutSuccessful,
isDowngrade,
logtoSkus,
navigate,
navigateTenant,
subscriptionPlans,

View file

@ -2400,8 +2400,8 @@ importers:
specifier: ^29.5.0
version: 29.5.0
'@logto/cloud':
specifier: 0.2.5-3b703da
version: 0.2.5-3b703da(zod@3.23.8)
specifier: 0.2.5-923c26f
version: 0.2.5-923c26f(zod@3.23.8)
'@logto/connector-kit':
specifier: workspace:^4.0.0
version: link:../toolkit/connector-kit
@ -5016,6 +5016,10 @@ packages:
resolution: {integrity: sha512-VCevQnxP5910s/cDYAxoJRim9iH1yN/La0HAlOP6FhVGtZofYwTTfT9AQXC+dZScgydpcFWo4k/6MYOFRtZCLg==}
engines: {node: ^20.9.0}
'@logto/cloud@0.2.5-923c26f':
resolution: {integrity: sha512-NAK9/T7HxEfE2djO6VTekMziOXH6NtbAzwumZcZo0bqIUDGiKlUvted/KY6iqpCdfFOF4aIyKp+pvlQIjj1T6Q==}
engines: {node: ^20.9.0}
'@logto/js@4.1.4':
resolution: {integrity: sha512-6twud1nFBQmj89/aflzej6yD1QwXfPiYmRtyYuN4a7O9OaaW3X/kJBVwjKUn5NC9IUt+rd+jXsI3QJXENfaLAw==}
@ -14650,6 +14654,13 @@ snapshots:
transitivePeerDependencies:
- zod
'@logto/cloud@0.2.5-923c26f(zod@3.23.8)':
dependencies:
'@silverhand/essentials': 2.9.1
'@withtyped/server': 0.13.6(zod@3.23.8)
transitivePeerDependencies:
- zod
'@logto/js@4.1.4':
dependencies:
'@silverhand/essentials': 2.9.1