@@ -17,6 +17,7 @@ import { Box, Button, Col, descriptors, Flex, Form, localizationKeys, Text } fro
17
17
import { ChevronUpDown , InformationCircle } from '../../icons' ;
18
18
import * as AddPaymentSource from '../PaymentSources/AddPaymentSource' ;
19
19
import { PaymentSourceRow } from '../PaymentSources/PaymentSourceRow' ;
20
+ import { SubscriptionBadge } from '../Subscriptions/badge' ;
20
21
21
22
type PaymentMethodSource = 'existing' | 'new' ;
22
23
@@ -25,7 +26,7 @@ const capitalize = (name: string) => name[0].toUpperCase() + name.slice(1);
25
26
export const CheckoutForm = withCardStateProvider ( ( ) => {
26
27
const { checkout } = useCheckout ( ) ;
27
28
28
- const { id, plan, totals, isImmediatePlanChange, planPeriod } = checkout ;
29
+ const { id, plan, totals, isImmediatePlanChange, planPeriod, freeTrialEndsAt } = checkout ;
29
30
30
31
if ( ! id ) {
31
32
return null ;
@@ -53,6 +54,11 @@ export const CheckoutForm = withCardStateProvider(() => {
53
54
< LineItems . Title
54
55
title = { plan . name }
55
56
description = { planPeriod === 'annual' ? localizationKeys ( 'commerce.billedAnnually' ) : undefined }
57
+ badge = {
58
+ plan . freeTrialEnabled && freeTrialEndsAt ? (
59
+ < SubscriptionBadge subscription = { { status : 'free_trial' } } />
60
+ ) : null
61
+ }
56
62
/>
57
63
< LineItems . Description
58
64
prefix = { planPeriod === 'annual' ? 'x12' : undefined }
@@ -87,6 +93,20 @@ export const CheckoutForm = withCardStateProvider(() => {
87
93
< LineItems . Description text = { `${ totals . pastDue ?. currencySymbol } ${ totals . pastDue ?. amountFormatted } ` } />
88
94
</ LineItems . Group >
89
95
) }
96
+
97
+ { ! ! freeTrialEndsAt && ! ! plan . freeTrialDays && (
98
+ < LineItems . Group variant = 'tertiary' >
99
+ < LineItems . Title
100
+ title = { localizationKeys ( 'commerce.checkout.totalDueAfterTrial' , {
101
+ days : plan . freeTrialDays ,
102
+ } ) }
103
+ />
104
+ < LineItems . Description
105
+ text = { `${ totals . grandTotal ?. currencySymbol } ${ totals . grandTotal ?. amountFormatted } ` }
106
+ />
107
+ </ LineItems . Group >
108
+ ) }
109
+
90
110
< LineItems . Group borderTop >
91
111
< LineItems . Title title = { localizationKeys ( 'commerce.totalDueToday' ) } />
92
112
< LineItems . Description text = { `${ totals . totalDueNow . currencySymbol } ${ totals . totalDueNow . amountFormatted } ` } />
@@ -278,15 +298,32 @@ export const PayWithTestPaymentSource = () => {
278
298
) ;
279
299
} ;
280
300
281
- const AddPaymentSourceForCheckout = withCardStateProvider ( ( ) => {
282
- const { addPaymentSourceAndPay } = useCheckoutMutations ( ) ;
301
+ const useSubmitLabel = ( ) => {
283
302
const { checkout } = useCheckout ( ) ;
284
- const { status, totals } = checkout ;
303
+ const { status, freeTrialEndsAt , totals } = checkout ;
285
304
286
305
if ( status === 'needs_initialization' ) {
287
- return null ;
306
+ throw new Error ( 'Clerk: Invalid state' ) ;
307
+ }
308
+
309
+ if ( freeTrialEndsAt ) {
310
+ return localizationKeys ( 'commerce.startFreeTrial' ) ;
288
311
}
289
312
313
+ if ( totals . totalDueNow . amount > 0 ) {
314
+ return localizationKeys ( 'commerce.pay' , {
315
+ amount : `${ totals . totalDueNow . currencySymbol } ${ totals . totalDueNow . amountFormatted } ` ,
316
+ } ) ;
317
+ }
318
+
319
+ return localizationKeys ( 'commerce.subscribe' ) ;
320
+ } ;
321
+
322
+ const AddPaymentSourceForCheckout = withCardStateProvider ( ( ) => {
323
+ const { addPaymentSourceAndPay } = useCheckoutMutations ( ) ;
324
+ const submitLabel = useSubmitLabel ( ) ;
325
+ const { checkout } = useCheckout ( ) ;
326
+
290
327
return (
291
328
< AddPaymentSource . Root
292
329
onSuccess = { addPaymentSourceAndPay }
@@ -296,15 +333,7 @@ const AddPaymentSourceForCheckout = withCardStateProvider(() => {
296
333
< PayWithTestPaymentSource />
297
334
</ DevOnly >
298
335
299
- { totals . totalDueNow . amount > 0 ? (
300
- < AddPaymentSource . FormButton
301
- text = { localizationKeys ( 'commerce.pay' , {
302
- amount : `${ totals . totalDueNow . currencySymbol } ${ totals . totalDueNow . amountFormatted } ` ,
303
- } ) }
304
- />
305
- ) : (
306
- < AddPaymentSource . FormButton text = { localizationKeys ( 'commerce.subscribe' ) } />
307
- ) }
336
+ < AddPaymentSource . FormButton text = { submitLabel } />
308
337
</ AddPaymentSource . Root >
309
338
) ;
310
339
} ) ;
@@ -317,8 +346,9 @@ const ExistingPaymentSourceForm = withCardStateProvider(
317
346
totalDueNow : CommerceMoneyAmount ;
318
347
paymentSources : CommercePaymentSourceResource [ ] ;
319
348
} ) => {
349
+ const submitLabel = useSubmitLabel ( ) ;
320
350
const { checkout } = useCheckout ( ) ;
321
- const { paymentSource } = checkout ;
351
+ const { paymentSource, freeTrialEndsAt } = checkout ;
322
352
323
353
const { payWithExistingPaymentSource } = useCheckoutMutations ( ) ;
324
354
const card = useCardState ( ) ;
@@ -340,6 +370,8 @@ const ExistingPaymentSourceForm = withCardStateProvider(
340
370
} ) ;
341
371
} , [ paymentSources ] ) ;
342
372
373
+ const isSchedulePayment = totalDueNow . amount > 0 && ! freeTrialEndsAt ;
374
+
343
375
return (
344
376
< Form
345
377
onSubmit = { payWithExistingPaymentSource }
@@ -349,7 +381,7 @@ const ExistingPaymentSourceForm = withCardStateProvider(
349
381
rowGap : t . space . $4 ,
350
382
} ) }
351
383
>
352
- { totalDueNow . amount > 0 ? (
384
+ { isSchedulePayment ? (
353
385
< Select
354
386
elementId = 'paymentSource'
355
387
options = { options }
@@ -399,17 +431,8 @@ const ExistingPaymentSourceForm = withCardStateProvider(
399
431
width : '100%' ,
400
432
} }
401
433
isLoading = { card . isLoading }
402
- >
403
- < Text
404
- localizationKey = {
405
- totalDueNow . amount > 0
406
- ? localizationKeys ( 'commerce.pay' , {
407
- amount : `${ totalDueNow . currencySymbol } ${ totalDueNow . amountFormatted } ` ,
408
- } )
409
- : localizationKeys ( 'commerce.subscribe' )
410
- }
411
- />
412
- </ Button >
434
+ localizationKey = { submitLabel }
435
+ />
413
436
</ Form >
414
437
) ;
415
438
} ,
0 commit comments