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,22 +3,28 @@ import getCredentials from '@framework/api/utils/getCredentials'
|
|||||||
import { Order } from '@commercelayer/js-sdk'
|
import { Order } from '@commercelayer/js-sdk'
|
||||||
|
|
||||||
const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({
|
const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({
|
||||||
|
req,
|
||||||
res,
|
res,
|
||||||
}) => {
|
}) => {
|
||||||
const id = localStorage.getItem('CL_ORDER') || ''
|
let { orderId, accessToken } = req.query
|
||||||
const credentials = getCredentials()
|
accessToken =
|
||||||
if (id && credentials.accessToken) {
|
typeof accessToken === 'string' ? accessToken.split('; CL_TOKEN=') : ''
|
||||||
const clOrder = await Order.withCredentials(credentials)
|
accessToken = accessToken[accessToken.length - 1]
|
||||||
|
const { endpoint } = getCredentials()
|
||||||
|
if (orderId && accessToken) {
|
||||||
|
const clOrder = await Order.withCredentials({ endpoint, accessToken })
|
||||||
.includes('lineItems')
|
.includes('lineItems')
|
||||||
.find(id, { rawResponse: true })
|
.find(orderId as string, { rawResponse: true })
|
||||||
const checkoutUrl = clOrder.data.attributes.checkout_url
|
const checkoutUrl = clOrder.data.attributes.checkout_url
|
||||||
console.log(checkoutUrl)
|
console.log(checkoutUrl)
|
||||||
|
|
||||||
if (checkoutUrl) {
|
if (checkoutUrl) {
|
||||||
res.redirect(checkoutUrl)
|
res.redirect(checkoutUrl)
|
||||||
} else {
|
} else {
|
||||||
res.redirect('/cart')
|
res.redirect('/cart')
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
res.redirect('/')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,18 +1,17 @@
|
|||||||
import { GetAPISchema, createEndpoint } from '@commerce/api'
|
import { GetAPISchema, createEndpoint, CommerceAPI } from '@commerce/api'
|
||||||
import checkoutEndpoint from '@commerce/api/endpoints/checkout'
|
import checkoutEndpoint from '@commerce/api/endpoints/checkout'
|
||||||
import type { CheckoutSchema } from '@commerce/types/checkout'
|
import type { CheckoutSchema } from '@commerce/types/checkout'
|
||||||
import type { CommercelayerAPI } from '../..'
|
import type { CommercelayerAPI } from '../..'
|
||||||
import checkout from './checkout'
|
import checkout from './checkout'
|
||||||
|
|
||||||
export type CheckoutAPI = GetAPISchema<CommercelayerAPI, CheckoutSchema>
|
|
||||||
|
|
||||||
export type CheckoutEndpoint = CheckoutAPI['endpoint']
|
|
||||||
|
|
||||||
export const handlers: CheckoutEndpoint['handlers'] = { checkout }
|
export const handlers: CheckoutEndpoint['handlers'] = { checkout }
|
||||||
|
|
||||||
|
export type CheckoutAPI = GetAPISchema<CommerceAPI, CheckoutSchema>
|
||||||
|
export type CheckoutEndpoint = CheckoutAPI['endpoint']
|
||||||
|
|
||||||
const checkoutApi = createEndpoint<CheckoutAPI>({
|
const checkoutApi = createEndpoint<CheckoutAPI>({
|
||||||
handler: checkoutEndpoint,
|
handler: checkoutEndpoint,
|
||||||
handlers,
|
handlers,
|
||||||
})
|
})
|
||||||
|
|
||||||
export default checkoutApi
|
export default checkoutApi
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
export type Page = { url: string }
|
export type Page = { url: string }
|
||||||
export type GetAllPagesResult = { pages: Page[] }
|
export type GetAllPagesResult = { pages: Page[] }
|
||||||
import type { LocalConfig } from '../index'
|
import type { CommercelayerAPI } from '../index'
|
||||||
|
|
||||||
export default function getAllPagesOperation() {
|
export default function getAllPagesOperation() {
|
||||||
function getAllPages({
|
function getAllPages({
|
||||||
@ -8,11 +8,17 @@ export default function getAllPagesOperation() {
|
|||||||
preview,
|
preview,
|
||||||
}: {
|
}: {
|
||||||
url?: string
|
url?: string
|
||||||
config?: Partial<LocalConfig>
|
config?: Partial<CommercelayerAPI>
|
||||||
preview?: boolean
|
preview?: boolean
|
||||||
}): Promise<GetAllPagesResult> {
|
}): Promise<GetAllPagesResult> {
|
||||||
|
console.log(`config`, config, preview)
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
pages: [],
|
pages: [
|
||||||
|
{
|
||||||
|
id: 'checkout',
|
||||||
|
url: `/en-US/checkout`,
|
||||||
|
},
|
||||||
|
],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return getAllPages
|
return getAllPages
|
||||||
|
@ -7,7 +7,11 @@ export type PageVariables = {
|
|||||||
|
|
||||||
export default function getPageOperation() {
|
export default function getPageOperation() {
|
||||||
function getPage(): Promise<GetPageResult> {
|
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
|
return getPage
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user