mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
feat(checkout): add draft external checkout endpoint
This commit is contained in:
parent
4f4dd1dbbf
commit
459444382a
@ -16,6 +16,12 @@ cp framework/commercelayer/.env.template .env.local
|
||||
|
||||
Next, add the application credentials from your organization application dashboard in `.env.local`.
|
||||
|
||||
## Checkout
|
||||
|
||||
Demo checkout in this provider is powered by the open-sourced [Commmerce Layer Checkout](https://github.com/commercelayer/commercelayer-checkout) application that provides you with a PCI-compliant, PSD2-compliant, and production-ready checkout flow that lets you easily place orders through the Commerce Layer API. You can setup your own checkout app and deploy to Vercel with one click:
|
||||
|
||||
[](https://vercel.co/new/project?template=https://github.com/commercelayer/commercelayer-checkout)
|
||||
|
||||
## Contribute
|
||||
|
||||
Our commitment to Open Source can be found [here](https://vercel.com/oss).
|
||||
|
25
framework/commercelayer/api/endpoints/checkout/checkout.ts
Normal file
25
framework/commercelayer/api/endpoints/checkout/checkout.ts
Normal file
@ -0,0 +1,25 @@
|
||||
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
|
@ -1 +1,18 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
||||
import { GetAPISchema, createEndpoint } 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 }
|
||||
|
||||
const checkoutApi = createEndpoint<CheckoutAPI>({
|
||||
handler: checkoutEndpoint,
|
||||
handlers,
|
||||
})
|
||||
|
||||
export default checkoutApi
|
@ -10,8 +10,8 @@ import getAllProductPaths from './operations/get-all-product-paths'
|
||||
import getAllProducts from './operations/get-all-products'
|
||||
import getProduct from './operations/get-product'
|
||||
|
||||
export interface LocalConfig extends CommerceAPIConfig {}
|
||||
const config: LocalConfig = {
|
||||
export interface CommercelayerConfig extends CommerceAPIConfig {}
|
||||
const config: CommercelayerConfig = {
|
||||
commerceUrl: '',
|
||||
apiToken: '',
|
||||
cartCookie: '',
|
||||
@ -33,10 +33,10 @@ const operations = {
|
||||
export const provider = { config, operations }
|
||||
|
||||
export type Provider = typeof provider
|
||||
export type LocalAPI<P extends Provider = Provider> = CommerceAPI<P | any>
|
||||
export type CommercelayerAPI<P extends Provider = Provider> = CommerceAPI<P | any>
|
||||
|
||||
export function getCommerceApi<P extends Provider>(
|
||||
customProvider: P = provider as any
|
||||
): LocalAPI<P> {
|
||||
): CommercelayerAPI<P> {
|
||||
return commerceApi(customProvider as any)
|
||||
}
|
||||
|
@ -31,8 +31,6 @@ export const handler: MutationHook<any> = {
|
||||
alert(`User "${email}" has successfully been logged in.`)
|
||||
return token
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
|
||||
throw new CommerceError({
|
||||
message: `${error}`,
|
||||
})
|
||||
|
@ -1,9 +1,7 @@
|
||||
{
|
||||
"provider": "commercelayer",
|
||||
"features": {
|
||||
"wishlist": false,
|
||||
"cart": true,
|
||||
"search": false,
|
||||
"customerAuth": true
|
||||
"customerAuth": true,
|
||||
"cart": true
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ export const fetcher: Fetcher = async ({ url, method, variables, query }) => {
|
||||
method,
|
||||
headers: {
|
||||
Accept: 'application/vnd.api+json',
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
'Content-Type': 'application/vnd.api+json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
|
Loading…
x
Reference in New Issue
Block a user