Solves the issue of not being able to look up an order after clearing the session cart
parent
a834eb441b
commit
a6ed5bc430
@ -1,16 +1,37 @@
|
||||
import {useEffect} from 'react'
|
||||
import Router from 'next/router'
|
||||
|
||||
import {DateTime} from 'luxon'
|
||||
|
||||
CheckoutComplete.getInitialProps = async function({ctx: {query: {session_id}, axios}}){
|
||||
const {data: {status, order}} = await axios.post('/api/orders/current/checkout/verify', {session_id})
|
||||
return {status, order}
|
||||
const {data: orders} = await axios.get('/api/orders')
|
||||
|
||||
const mostRecentOrder = orders.sort(sortOrders)[0]
|
||||
|
||||
return {order: mostRecentOrder}
|
||||
}
|
||||
|
||||
export default function CheckoutComplete({status, order}){
|
||||
export default function CheckoutComplete({order}){
|
||||
return (
|
||||
<pre>
|
||||
{JSON.stringify(order, null, 2)}
|
||||
</pre>
|
||||
)
|
||||
}
|
||||
|
||||
function parsePaymentTime({payment}){
|
||||
if(typeof payment.time === 'string')
|
||||
payment.time = DateTime.fromISO(payment.time)
|
||||
return payment.time
|
||||
}
|
||||
|
||||
function sortTransactions(a,b){
|
||||
const timeA = parsePaymentTime(a)
|
||||
const timeB = parsePaymentTime(b)
|
||||
|
||||
return timeB.diff(timeA).as('seconds')
|
||||
}
|
||||
|
||||
function sortOrders(a,b){
|
||||
const timePaidA = parsePaymentTime(a.transactions.sort(sortTransactions)[0])
|
||||
const timePaidB = parsePaymentTime(b.transactions.sort(sortTransactions)[0])
|
||||
|
||||
return timePaidB.diff(timePaidA).as('seconds')
|
||||
}
|
||||
|
@ -0,0 +1,39 @@
|
||||
import {useState, useEffect} from 'react'
|
||||
import axios from 'axios'
|
||||
import Router from 'next/router'
|
||||
|
||||
|
||||
CheckoutComplete.getInitialProps = async function({ctx: {query: {session_id}}}){
|
||||
return {session_id}
|
||||
}
|
||||
|
||||
export default function CheckoutComplete({session_id}){
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
useEffect(()=>{
|
||||
(async ()=>{
|
||||
const {data: {status}} = await axios.post('/api/orders/current/checkout/verify', {session_id})
|
||||
|
||||
if(status === "succeeded")
|
||||
Router.push('/store/checkout/complete')
|
||||
else
|
||||
setLoading(false)
|
||||
|
||||
})()
|
||||
}, [])
|
||||
|
||||
if(loading)
|
||||
return (
|
||||
<>
|
||||
<h2>Checkout</h2>
|
||||
<p style={{textAlign: 'center'}}>Verifying your payment . . .</p>
|
||||
</>
|
||||
)
|
||||
|
||||
return (
|
||||
<>
|
||||
<h2>Checkout</h2>
|
||||
<p style={{textAlign: 'center'}}>There was a problem with your payment.</p>
|
||||
</>
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue