import React, {useState} from 'react'
import Router from 'next/router'
import axios from 'axios'
import ActionBar from '~/components/admin/actionBar'
import {Button, FormController, Input} from '~/components/form'
import OrderSummary from '~/components/orderSummary'
import styles from '../../../../store/checkout/style.module.css'
ManualOrderPayment.getInitialProps = async ({ctx}) => {
const {data: order} = await ctx.axios.get(`/api/orders/${ctx.query.uuid}`)
return {order}
}
export default function ManualOrderPayment({order}) {
const [recipientEmail, setRecipientEmail] = useState('')
const [orderNote, setOrderNote] = useState('')
const validToSubmit = recipientEmail.length > 0 && orderNote.length > 0
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)
? 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)
}
async function markPaid() {
await axios.post(`/api/orders/manual/${order.uuid}/payment`, {
recipientEmail,
reason: orderNote
})
Router.push('/admin/orders')
}
return (
<>
{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) || '-'} |
(Paid by admin) | {formatMoney(0) || '-'} |