@@ -13,7 +13,7 @@ import { handleError } from '@/ui/utils/errorHandler';
1313
1414import { DevOnly } from '../../common/DevOnly' ;
1515import { useCheckoutContext , usePaymentMethods } from '../../contexts' ;
16- import { Box , Button , Col , descriptors , Flex , Form , localizationKeys , Text } from '../../customizables' ;
16+ import { Box , Button , Col , descriptors , Flex , Form , localizationKeys , Spinner , Text } from '../../customizables' ;
1717import { ChevronUpDown , InformationCircle } from '../../icons' ;
1818import * as AddPaymentSource from '../PaymentSources/AddPaymentSource' ;
1919import { PaymentSourceRow } from '../PaymentSources/PaymentSourceRow' ;
@@ -187,13 +187,38 @@ const useCheckoutMutations = () => {
187187
188188const CheckoutFormElements = ( ) => {
189189 const { checkout } = useCheckout ( ) ;
190- const { id, totals, freeTrialEndsAt } = checkout ;
190+ const { id } = checkout ;
191+
192+ const { isLoading } = usePaymentMethods ( ) ;
193+
194+ if ( ! id ) {
195+ return null ;
196+ }
197+
198+ if ( isLoading ) {
199+ return (
200+ < Spinner
201+ sx = { {
202+ margin : 'auto' ,
203+ } }
204+ />
205+ ) ;
206+ }
207+
208+ return < CheckoutFormElementsInternal /> ;
209+ } ;
210+
211+ const CheckoutFormElementsInternal = ( ) => {
212+ const { checkout } = useCheckout ( ) ;
213+ const { id, totals, isImmediatePlanChange, freeTrialEndsAt } = checkout ;
191214 const { data : paymentSources } = usePaymentMethods ( ) ;
192215
193216 const [ paymentMethodSource , setPaymentMethodSource ] = useState < PaymentMethodSource > ( ( ) =>
194217 paymentSources . length > 0 || __BUILD_DISABLE_RHC__ ? 'existing' : 'new' ,
195218 ) ;
196219
220+ const showPaymentMethods = isImmediatePlanChange && ( totals . totalDueNow . amount > 0 || ! ! freeTrialEndsAt ) ;
221+
197222 if ( ! id ) {
198223 return null ;
199224 }
@@ -206,8 +231,7 @@ const CheckoutFormElements = () => {
206231 >
207232 { __BUILD_DISABLE_RHC__ ? null : (
208233 < >
209- { /* only show if there are payment sources and there is a total due now */ }
210- { paymentSources . length > 0 && ( totals . totalDueNow . amount > 0 || ! ! freeTrialEndsAt ) && (
234+ { paymentSources . length > 0 && showPaymentMethods && (
211235 < SegmentedControl . Root
212236 aria-label = 'Payment method source'
213237 value = { paymentMethodSource }
@@ -347,7 +371,7 @@ const ExistingPaymentSourceForm = withCardStateProvider(
347371 } ) => {
348372 const submitLabel = useSubmitLabel ( ) ;
349373 const { checkout } = useCheckout ( ) ;
350- const { paymentSource, freeTrialEndsAt } = checkout ;
374+ const { paymentSource, isImmediatePlanChange , freeTrialEndsAt } = checkout ;
351375
352376 const { payWithExistingPaymentSource } = useCheckoutMutations ( ) ;
353377 const card = useCardState ( ) ;
@@ -369,7 +393,7 @@ const ExistingPaymentSourceForm = withCardStateProvider(
369393 } ) ;
370394 } , [ paymentSources ] ) ;
371395
372- const shouldDefaultBeUsed = totalDueNow . amount === 0 || ! freeTrialEndsAt ;
396+ const showPaymentMethods = isImmediatePlanChange && ( totalDueNow . amount > 0 || ! ! freeTrialEndsAt ) ;
373397
374398 return (
375399 < Form
@@ -380,7 +404,7 @@ const ExistingPaymentSourceForm = withCardStateProvider(
380404 rowGap : t . space . $4 ,
381405 } ) }
382406 >
383- { shouldDefaultBeUsed ? (
407+ { showPaymentMethods ? (
384408 < Select
385409 elementId = 'paymentSource'
386410 options = { options }
0 commit comments