'use client' import React, { useEffect, useState } from 'react' // @ts-ignore import { ApplePay, GooglePay, CreditCard, PaymentForm } from 'react-square-web-payments-sdk' import { observer } from 'mobx-react-lite' import { ApplyTypography, Button, Separator, Skeleton, } from '@hanzo/ui/primitives' import { cn } from '@hanzo/ui/util' import { useCommerce } from '../../../../service/context' import { processSquareCardPayment } from '../../../../util' import type { PaymentMethodComponentProps } from '../../../../types' import { sendFBEvent, sendGAEvent } from '../../../../util/analytics' import ContactInfo from '../contact-form' import PaymentMethods from '../card-icon-row' const PayWithCard: React.FC = observer(({ onDone, transactionStatus, setTransactionStatus, storePaymentInfo, contactForm, }) => { const cmmc = useCommerce() const cardTokenizeResponseReceived = async ( token: any, verifiedBuyer: any ) => { contactForm.handleSubmit(async () => { setTransactionStatus('paid') const res = await processSquareCardPayment(token.token, cmmc.promoAppliedCartTotal, verifiedBuyer.token) if (res) { await storePaymentInfo({paymentMethod: token.details.method ?? null, processed: res}) setTransactionStatus('confirmed') sendGAEvent('purchase', { transaction_id: res.payment?.id, value: res.payment?.amountMoney?.amount, currency: res.payment?.amountMoney?.currency, items: cmmc.cartItems.map((item) => ({ item_id: item.sku, item_name: item.title, item_category: item.familyId, price: item.price, quantity: item.quantity })), }) sendFBEvent('Purchase', { content_ids: cmmc.cartItems.map((item) => item.sku), contents: cmmc.cartItems.map(item => ({ id: item.sku, quantity: item.quantity })), num_items: cmmc.cartItems.length, value: cmmc.promoAppliedCartTotal, currency: 'USD', }) } else { setTransactionStatus('error') } })() } const createVerificationDetails = (): { amount: string; billingContact: { givenName: string; email: string }; currencyCode: string; intent: 'CHARGE' | 'CHARGE_AND_STORE' } => { const {name, email} = contactForm.getValues() return { amount: cmmc.promoAppliedCartTotal.toFixed(2), billingContact: { givenName: name, email, }, currencyCode: 'USD', intent: 'CHARGE' as 'CHARGE', } } const createPaymentRequest = () => ({ countryCode: "US", currencyCode: "USD", lineItems: cmmc.cartItems.map(item => ({ amount: item.price.toFixed(2), label: item.title, id: item.sku, })), requestBillingContact: false, requestShippingContact: false, total: { amount: cmmc.promoAppliedCartTotal.toFixed(2), label: "Total", }, }) /** * Reload payment form after checkout value changes (promo code applied, etc.) * Reloading is required so that Apple Pay and Google Pay buttons are updated for new cart total. */ const [loadingPaymentForm, setLoadingPaymentForm] = useState(false) useEffect(() => { setLoadingPaymentForm(true) const timeout = setTimeout(() => setLoadingPaymentForm(false), 1000) return () => clearTimeout(timeout) }, [cmmc.promoAppliedCartTotal]) if (loadingPaymentForm) { return (
) } return ( {transactionStatus === 'paid' ? (
Processing your payment...
) : transactionStatus === 'confirmed' ? (
Payment confirmed!

Thank you for your purchase.

) : (
or
{/* Imitates hanzo/ui Button and Input styles, I was unable to render the hanzo/ui button outright and keeping the submit form functionality*/} ( )} /> {transactionStatus === 'error' && (

There was an error processing your payment.

)}
)}
) }) export default PayWithCard