mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
26 lines
728 B
TypeScript
26 lines
728 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 ({
|
|
res,
|
|
}) => {
|
|
const id = localStorage.getItem('CL_ORDER') || ''
|
|
const credentials = getCredentials()
|
|
if (id && credentials.accessToken) {
|
|
const clOrder = await Order.withCredentials(credentials)
|
|
.includes('lineItems')
|
|
.find(id, { rawResponse: true })
|
|
const checkoutUrl = clOrder.data.attributes.checkout_url
|
|
console.log(checkoutUrl)
|
|
|
|
if (checkoutUrl) {
|
|
res.redirect(checkoutUrl)
|
|
} else {
|
|
res.redirect('/cart')
|
|
}
|
|
}
|
|
}
|
|
|
|
export default checkout
|