File size: 2,454 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { MaterialIcon } from '@automattic/components';
import { FormStatus, useFormStatus } from '@automattic/composite-checkout';
import { formatCurrency } from '@automattic/number-formatters';
import { useShoppingCart } from '@automattic/shopping-cart';
import { styled } from '@automattic/wpcom-checkout';
import { sprintf } from '@wordpress/i18n';
import { useI18n } from '@wordpress/react-i18n';
import {
	useStreamlinedPriceExperiment,
	isStreamlinedPriceCheckoutTreatment,
} from 'calypso/my-sites/plans-features-main/hooks/use-streamlined-price-experiment';
import useCartKey from '../../use-cart-key';

const CreditCardPayButtonWrapper = styled.span`
	display: inline-flex;
	align-items: flex-end;
`;

const StyledMaterialIcon = styled( MaterialIcon )`
	fill: ${ ( { theme } ) => theme.colors.surface };
	margin-right: 0.7em;

	.rtl & {
		margin-right: 0;
		margin-left: 0.7em;
	}
`;

/**
 * The interior of the main submit button in checkout for most payment methods.
 * Payment methods which have a special button (eg: PayPal, Google Pay, Apple
 * Pay) will not use this. See each payment method to be sure how it works.
 *
 * There are also checkout-like forms (eg: "add credit card") which do not use
 * this because they want their submit button to render something different.
 */
export function CheckoutSubmitButtonContent() {
	const { __ } = useI18n();
	const cartKey = useCartKey();
	const [ , streamlinedPriceExperimentAssignment ] = useStreamlinedPriceExperiment();
	const { responseCart } = useShoppingCart( cartKey );
	const isPurchaseFree = responseCart.total_cost_integer === 0;
	const { formStatus } = useFormStatus();

	if ( formStatus === FormStatus.SUBMITTING ) {
		return <>{ __( 'Processing…' ) }</>;
	}

	if ( formStatus !== FormStatus.READY ) {
		return <>{ __( 'Please wait…' ) }</>;
	}

	if ( isPurchaseFree ) {
		return <CreditCardPayButtonWrapper>{ __( 'Complete Checkout' ) }</CreditCardPayButtonWrapper>;
	}

	const total = formatCurrency( responseCart.total_cost_integer, responseCart.currency, {
		isSmallestUnit: true,
		stripZeros: true,
	} );
	return (
		<CreditCardPayButtonWrapper>
			<StyledMaterialIcon icon="credit_card" />
			{ isStreamlinedPriceCheckoutTreatment( streamlinedPriceExperimentAssignment )
				? __( 'Pay now' )
				: sprintf(
						/* translators: %s is the total to be paid in localized currency */
						__( 'Pay %s now' ),
						total
				  ) }
		</CreditCardPayButtonWrapper>
	);
}