import {useState, useRef} from 'react' import Link from 'next/link' import Router from 'next/router' import getConfig from 'next/config' import styles from './style.module.css' import {Input, Button} from '~/components/form' import useUser from '~/hooks/useUser' import axios from 'axios' import { loadStripe } from '@stripe/stripe-js'; const { publicRuntimeConfig: {STRIPE_PUBLIC_KEY} } = getConfig() const stripePromise = loadStripe(STRIPE_PUBLIC_KEY); import PaypalIcon from '../../../images/icons/paypal.svg' // TODO: Load previous addresses CheckoutSummary.getInitialProps = async function({ctx: {axios}}){ const {data: order} = await axios.get(`/api/orders/current`) return {order} } export default function CheckoutSummary({order: _order}){ const user = useUser(); const [order, updateOrder] = useState(_order) const currentTransaction = order .transactions.find(transaction => ( transaction.payment_state === 'started' )) const {item_total_price, shipping_price, tax_price, coupon_effective_discount} = currentTransaction const {free_shipping} = currentTransaction.coupon || {} const total_price = (item_total_price && shipping_price && tax_price) ? item_total_price + (free_shipping ? 0 : shipping_price) + tax_price - (coupon_effective_discount || 0) : null const formatMoney = money => { if (money === undefined || money === null) return null; return '$' + (money / 100).toFixed(2) } // For coupon input const couponRef = useRef() const onCouponSubmit = async ev => { if(ev) ev.preventDefault() const code = couponRef.current?.value const {data: updatedOrder} = await axios.post(`/api/orders/current/coupon`, {code}) updateOrder(updatedOrder) } // For Stripe checkout const handleStripeButton = async ev => { if(ev) ev.preventDefault() // Create a stripe session on server const {data: sessionId} = await axios.post('/api/orders/current/checkout/stripe') // Allow Stripe to finish initializing const stripe = await stripePromise; const {error} = await stripe.redirectToCheckout({sessionId}) if(error) alert(error.message) } return ( <>

Checkout

Address

{ order.address ? (

{order.address.name}

{order.address.street1}

{order.address.street2}

{order.address.city}, {order.address.state}, {order.address.zip}

(Edit Address)

) : ( <> {user ? (

TODO: Load previous addresses

):(

Log in to use your
saved addresses

)} OR
) }

Coupon (optional)

{ currentTransaction.coupon ? ( <>

Using coupon code: {currentTransaction.coupon.code}
( {coupon_effective_discount > 0 && `Discounting by ${formatMoney(coupon_effective_discount)}`} {coupon_effective_discount > 0 && free_shipping && ' & '} {free_shipping && `${coupon_effective_discount > 0 ? 'f' : 'F'}ree shipping`} )

) : (
) }

Total

{currentTransaction.cart.items.map(({uuid, count, item}) => ( ))} {coupon_effective_discount > 0 && ( )} {tax_price && ( )}
{item.name} {count > 1 ? `(${count})` : ''}: {formatMoney(count * item.price_cents)}
Coupon: - {formatMoney(coupon_effective_discount)}
Shipping: {formatMoney(shipping_price) || '-'}
Sales tax: {formatMoney(tax_price)}
Total: {formatMoney(total_price) || '-'}
{ total_price ? (
) : (
{/* Hit API endpoint to get Stripe session, and then redirect */}
) }
) }