mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
feat: Redirect to external checkout
This commit is contained in:
parent
c2c60d95a5
commit
ce0bcfe33d
@ -3,14 +3,18 @@ import getCredentials from '@framework/api/utils/getCredentials'
|
||||
import { Order } from '@commercelayer/js-sdk'
|
||||
|
||||
const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({
|
||||
req,
|
||||
res,
|
||||
}) => {
|
||||
const id = localStorage.getItem('CL_ORDER') || ''
|
||||
const credentials = getCredentials()
|
||||
if (id && credentials.accessToken) {
|
||||
const clOrder = await Order.withCredentials(credentials)
|
||||
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(id, { rawResponse: true })
|
||||
.find(orderId as string, { rawResponse: true })
|
||||
const checkoutUrl = clOrder.data.attributes.checkout_url
|
||||
console.log(checkoutUrl)
|
||||
|
||||
@ -19,6 +23,8 @@ const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({
|
||||
} else {
|
||||
res.redirect('/cart')
|
||||
}
|
||||
} else {
|
||||
res.redirect('/')
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,15 +1,14 @@
|
||||
import { GetAPISchema, createEndpoint } from '@commerce/api'
|
||||
import { GetAPISchema, createEndpoint, CommerceAPI } from '@commerce/api'
|
||||
import checkoutEndpoint from '@commerce/api/endpoints/checkout'
|
||||
import type { CheckoutSchema } from '@commerce/types/checkout'
|
||||
import type { CommercelayerAPI } from '../..'
|
||||
import checkout from './checkout'
|
||||
|
||||
export type CheckoutAPI = GetAPISchema<CommercelayerAPI, CheckoutSchema>
|
||||
|
||||
export type CheckoutEndpoint = CheckoutAPI['endpoint']
|
||||
|
||||
export const handlers: CheckoutEndpoint['handlers'] = { checkout }
|
||||
|
||||
export type CheckoutAPI = GetAPISchema<CommerceAPI, CheckoutSchema>
|
||||
export type CheckoutEndpoint = CheckoutAPI['endpoint']
|
||||
|
||||
const checkoutApi = createEndpoint<CheckoutAPI>({
|
||||
handler: checkoutEndpoint,
|
||||
handlers,
|
||||
|
@ -1,6 +1,6 @@
|
||||
export type Page = { url: string }
|
||||
export type GetAllPagesResult = { pages: Page[] }
|
||||
import type { LocalConfig } from '../index'
|
||||
import type { CommercelayerAPI } from '../index'
|
||||
|
||||
export default function getAllPagesOperation() {
|
||||
function getAllPages({
|
||||
@ -8,11 +8,17 @@ export default function getAllPagesOperation() {
|
||||
preview,
|
||||
}: {
|
||||
url?: string
|
||||
config?: Partial<LocalConfig>
|
||||
config?: Partial<CommercelayerAPI>
|
||||
preview?: boolean
|
||||
}): Promise<GetAllPagesResult> {
|
||||
console.log(`config`, config, preview)
|
||||
return Promise.resolve({
|
||||
pages: [],
|
||||
pages: [
|
||||
{
|
||||
id: 'checkout',
|
||||
url: `/en-US/checkout`,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
return getAllPages
|
||||
|
@ -7,7 +7,11 @@ export type PageVariables = {
|
||||
|
||||
export default function getPageOperation() {
|
||||
function getPage(): Promise<GetPageResult> {
|
||||
return Promise.resolve({})
|
||||
return Promise.resolve({
|
||||
page: {
|
||||
body: `<script>location.href = '/api/checkout/?orderId=' + localStorage.getItem('CL_ORDER') + '&accessToken=' + document.cookie</script>`,
|
||||
},
|
||||
})
|
||||
}
|
||||
return getPage
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user