UNPKG

@automattic/wpcom-checkout

Version:
57 lines 2.74 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useTogglePaymentMethod } from '@automattic/composite-checkout'; import debugFactory from 'debug'; import { Fragment, useCallback, useEffect } from 'react'; import { GooglePayMark } from '../google-pay-mark'; import { PaymentMethodLogos } from '../payment-method-logos'; import PaymentRequestButton from '../payment-request-button'; import { usePaymentRequestOptions, useStripePaymentRequest, } from './web-pay-utils'; const debug = debugFactory('wpcom-checkout:google-pay-payment-method'); export function createGooglePayMethod(stripe, stripeConfiguration, cartKey) { return { id: 'google-pay', paymentProcessorId: 'google-pay', label: _jsx(GooglePayLabel, {}), submitButton: (_jsx(GooglePaySubmitButton, { stripe: stripe, stripeConfiguration: stripeConfiguration, cartKey: cartKey })), inactiveContent: _jsx(GooglePaySummary, {}), getAriaLabel: () => 'Google Pay', isInitiallyDisabled: true, }; } export function GooglePayLabel() { return (_jsxs(Fragment, { children: [_jsx("span", { children: "Google Pay" }), _jsx(PaymentMethodLogos, { className: "google-pay__logo payment-logos", children: _jsx(GooglePayMark, { fill: "#3C4043" }) })] })); } export function GooglePaySubmitButton({ disabled, onClick, stripe, stripeConfiguration, cartKey, }) { const togglePaymentMethod = useTogglePaymentMethod(); const paymentRequestOptions = usePaymentRequestOptions(stripeConfiguration, cartKey); const onSubmit = useCallback(({ name, paymentMethodToken }) => { debug('submitting stripe payment with key', paymentMethodToken); if (!onClick) { throw new Error('Missing onClick prop; GooglePaySubmitButton must be used as a payment button in CheckoutSubmitButton'); } onClick({ stripe, paymentMethodToken, name, stripeConfiguration, }); }, [onClick, stripe, stripeConfiguration]); const { paymentRequest, allowedPaymentTypes, isLoading } = useStripePaymentRequest({ paymentRequestOptions, onSubmit, stripe, }); useEffect(() => { if (!isLoading) { togglePaymentMethod('google-pay', allowedPaymentTypes.googlePay); } }, [isLoading, allowedPaymentTypes.googlePay, togglePaymentMethod]); if (!allowedPaymentTypes.googlePay) { return null; } return (_jsx(PaymentRequestButton, { disabled: isLoading ? true : disabled, paymentRequest: paymentRequest, paymentType: "google-pay" })); } export function GooglePaySummary() { return _jsx(Fragment, { children: "Google Pay" }); } //# sourceMappingURL=google-pay.js.map