Handle invalid coupon and display coupon errors

main
Ashelyn Dawn 4 years ago
parent 986b93d50c
commit 00143e477f

@ -63,6 +63,9 @@ router.post('/current/coupon', ensureCart, parseJSON, validate.coupon, validate.
const coupon = await db.coupon.find(req.body.code) const coupon = await db.coupon.find(req.body.code)
if(!coupon)
throw new Error(`No such coupon: "${req.body.code}"`)
const currentTransaction = origOrder const currentTransaction = origOrder
.transactions.find(transaction => ( .transactions.find(transaction => (
transaction.payment_state === 'started' transaction.payment_state === 'started'

@ -151,12 +151,14 @@ order.addAddress = async function (transaction, address){
order.updateTax = async function(transaction_uuid){ order.updateTax = async function(transaction_uuid){
const _order = await order.findForTransaction(transaction_uuid) const _order = await order.findForTransaction(transaction_uuid)
if(!_order.address) if(!_order.address){
throw new Error("Order has no address"); debug("Skipping tax because order has no address yet")
return _order
}
if(_order.address.state !== 'UT'){ if(_order.address.state !== 'UT'){
debug("Skipping tax for state: " + _order.address.state); debug("Skipping tax for state: " + _order.address.state);
return return _order
} }
const {item_total_price, coupon_effective_discount} = _order.transactions.find(t => t.uuid === transaction_uuid) const {item_total_price, coupon_effective_discount} = _order.transactions.find(t => t.uuid === transaction_uuid)

@ -21,6 +21,7 @@ CheckoutSummary.getInitialProps = async function({ctx: {axios}}){
export default function CheckoutSummary({order: _order}){ export default function CheckoutSummary({order: _order}){
const user = useUser(); const user = useUser();
const [order, updateOrder] = useState(_order) const [order, updateOrder] = useState(_order)
const [couponError, setCouponError] = useState(null)
const currentTransaction = order const currentTransaction = order
.transactions.find(transaction => ( .transactions.find(transaction => (
@ -45,9 +46,22 @@ export default function CheckoutSummary({order: _order}){
const onCouponSubmit = async ev => { const onCouponSubmit = async ev => {
if(ev) ev.preventDefault() if(ev) ev.preventDefault()
setCouponError(null)
const code = couponRef.current?.value const code = couponRef.current?.value
const {data: updatedOrder} = await axios.post(`/api/orders/current/coupon`, {code})
updateOrder(updatedOrder) try {
const {data: updatedOrder} = await axios.post(`/api/orders/current/coupon`, {code})
console.log(order, updatedOrder)
updateOrder(updatedOrder)
} catch (err) {
console.log(err.response.data.errors)
const message = err?.response?.data?.error?.message
|| err.response?.data?.errors?.find(e => e.param === 'code')?.msg
|| 'Unknown error applying coupon'
setCouponError(message)
}
} }
// For Stripe checkout // For Stripe checkout
@ -127,9 +141,9 @@ export default function CheckoutSummary({order: _order}){
) )
: ( : (
<div className={styles.horizContainer} style={{maxWidth:'400px', margin: '0 auto'}}> <div className={styles.horizContainer} style={{maxWidth:'400px', margin: '0 auto'}}>
<Input inputRef={couponRef} label="" name="coupon" /> <Input inputRef={couponRef} label="" name="coupon" onChange={() => setCouponError(null)} error={couponError} />
<div style={{maxWidth: '120px', marginLeft: '8px'}}> <div style={{alignSelf:'flex-start', maxWidth: '120px', marginLeft: '8px'}}>
<Button onClick={onCouponSubmit} outline>Save Coupon</Button> <Button onClick={onCouponSubmit} outline>Apply Coupon</Button>
</div> </div>
</div> </div>
) )

Loading…
Cancel
Save