11import { __experimental_useCheckout as useCheckout } from '@clerk/shared/react' ;
2- import type { BillingMoneyAmount , BillingPaymentMethodResource , ConfirmCheckoutParams } from '@clerk/types' ;
2+ import type { BillingPaymentMethodResource , ConfirmCheckoutParams } from '@clerk/types' ;
33import { useMemo , useState } from 'react' ;
44
55import { Card } from '@/ui/elements/Card' ;
@@ -13,6 +13,7 @@ import { handleError } from '@/ui/utils/errorHandler';
1313
1414import { DevOnly } from '../../common/DevOnly' ;
1515import { useCheckoutContext , usePaymentMethods } from '../../contexts' ;
16+ import { useShowPaymentMethods } from '../../hooks' ;
1617import { Box , Button , Col , descriptors , Flex , Form , localizationKeys , Spinner , Text } from '../../customizables' ;
1718import { ChevronUpDown , InformationCircle } from '../../icons' ;
1819import * as AddPaymentMethod from '../PaymentMethods/AddPaymentMethod' ;
@@ -180,10 +181,13 @@ const useCheckoutMutations = () => {
180181 useTestCard : true ,
181182 } ) ;
182183
184+ const subscribeWithoutPaymentMethod = ( ) => confirmCheckout ( { } ) ;
185+
183186 return {
184187 payWithExistingPaymentMethod,
185188 addPaymentMethodAndPay,
186189 payWithTestCard,
190+ subscribeWithoutPaymentMethod,
187191 } ;
188192} ;
189193
@@ -212,14 +216,14 @@ const CheckoutFormElements = () => {
212216
213217const CheckoutFormElementsInternal = ( ) => {
214218 const { checkout } = useCheckout ( ) ;
215- const { id, totals , isImmediatePlanChange, freeTrialEndsAt } = checkout ;
219+ const { id, isImmediatePlanChange } = checkout ;
216220 const { data : paymentMethods } = usePaymentMethods ( ) ;
217221
218222 const [ paymentMethodSource , setPaymentMethodSource ] = useState < PaymentMethodSource > ( ( ) =>
219223 paymentMethods . length > 0 || __BUILD_DISABLE_RHC__ ? 'existing' : 'new' ,
220224 ) ;
221225
222- const showPaymentMethods = isImmediatePlanChange && ( totals . totalDueNow . amount > 0 || ! ! freeTrialEndsAt ) ;
226+ const showPaymentMethods = useShowPaymentMethods ( ) ;
223227
224228 if ( ! id ) {
225229 return null ;
@@ -254,14 +258,16 @@ const CheckoutFormElementsInternal = () => {
254258 </ >
255259 ) }
256260
257- { paymentMethodSource === 'existing' && (
258- < ExistingPaymentMethodForm
259- paymentMethods = { paymentMethods }
260- totalDueNow = { totals . totalDueNow }
261- />
261+ { showPaymentMethods && paymentMethodSource === 'existing' && (
262+ < ExistingPaymentMethodForm paymentMethods = { paymentMethods } />
262263 ) }
263264
264- { __BUILD_DISABLE_RHC__ ? null : paymentMethodSource === 'new' && < AddPaymentMethodForCheckout /> }
265+ { __BUILD_DISABLE_RHC__
266+ ? null
267+ : showPaymentMethods && paymentMethodSource === 'new' && < AddPaymentMethodForCheckout /> }
268+
269+ { /* Show standalone subscribe button when payment methods are not needed */ }
270+ { ! showPaymentMethods && isImmediatePlanChange && < StandaloneSubscribeButton /> }
265271 </ Col >
266272 ) ;
267273} ;
@@ -364,16 +370,10 @@ const AddPaymentMethodForCheckout = withCardStateProvider(() => {
364370} ) ;
365371
366372const ExistingPaymentMethodForm = withCardStateProvider (
367- ( {
368- totalDueNow,
369- paymentMethods,
370- } : {
371- totalDueNow : BillingMoneyAmount ;
372- paymentMethods : BillingPaymentMethodResource [ ] ;
373- } ) => {
373+ ( { paymentMethods } : { paymentMethods : BillingPaymentMethodResource [ ] } ) => {
374374 const submitLabel = useSubmitLabel ( ) ;
375375 const { checkout } = useCheckout ( ) ;
376- const { paymentMethod, isImmediatePlanChange , freeTrialEndsAt } = checkout ;
376+ const { paymentMethod } = checkout ;
377377
378378 const { payWithExistingPaymentMethod } = useCheckoutMutations ( ) ;
379379 const card = useCardState ( ) ;
@@ -395,7 +395,7 @@ const ExistingPaymentMethodForm = withCardStateProvider(
395395 } ) ;
396396 } , [ paymentMethods ] ) ;
397397
398- const showPaymentMethods = isImmediatePlanChange && ( totalDueNow . amount > 0 || ! ! freeTrialEndsAt ) ;
398+ const showPaymentMethods = useShowPaymentMethods ( ) ;
399399
400400 return (
401401 < Form
@@ -462,3 +462,21 @@ const ExistingPaymentMethodForm = withCardStateProvider(
462462 ) ;
463463 } ,
464464) ;
465+
466+ const StandaloneSubscribeButton = withCardStateProvider ( ( ) => {
467+ const { subscribeWithoutPaymentMethod } = useCheckoutMutations ( ) ;
468+ const submitLabel = useSubmitLabel ( ) ;
469+ const card = useCardState ( ) ;
470+
471+ return (
472+ < Button
473+ elementDescriptor = { descriptors . formButtonPrimary }
474+ onClick = { subscribeWithoutPaymentMethod }
475+ sx = { {
476+ width : '100%' ,
477+ } }
478+ isLoading = { card . isLoading }
479+ localizationKey = { submitLabel }
480+ />
481+ ) ;
482+ } ) ;
0 commit comments