mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
32 lines
918 B
TypeScript
32 lines
918 B
TypeScript
import type { CheckoutEndpoint } from '.'
|
|
import getCredentials from '@framework/api/utils/getCredentials'
|
|
import { Order } from '@commercelayer/js-sdk'
|
|
|
|
const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({
|
|
req,
|
|
res,
|
|
}) => {
|
|
let { orderId, accessToken } = req.query
|
|
accessToken =
|
|
typeof accessToken === 'string' ? accessToken.split('; CL_TOKEN=') : ''
|
|
accessToken = accessToken[accessToken.length - 1]
|
|
const { endpoint } = getCredentials()
|
|
if (orderId && accessToken) {
|
|
const clOrder = await Order.withCredentials({ endpoint, accessToken })
|
|
.includes('lineItems')
|
|
.find(orderId as string, { rawResponse: true })
|
|
const checkoutUrl = clOrder.data.attributes.checkout_url
|
|
console.log(checkoutUrl)
|
|
|
|
if (checkoutUrl) {
|
|
res.redirect(checkoutUrl)
|
|
} else {
|
|
res.redirect('/cart')
|
|
}
|
|
} else {
|
|
res.redirect('/')
|
|
}
|
|
}
|
|
|
|
export default checkout
|