feat(checkout): add draft external checkout endpoint

This commit is contained in:
Bolaji Ayodeji 2021-08-30 10:01:11 +01:00 committed by Alessandro Casazza
parent 4f4dd1dbbf
commit 459444382a
No known key found for this signature in database
GPG Key ID: 3AF41B06C6495D3D
7 changed files with 56 additions and 12 deletions

View File

@ -16,6 +16,12 @@ cp framework/commercelayer/.env.template .env.local
Next, add the application credentials from your organization application dashboard in `.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:
[![Deploy to Vercel](https://vercel.co/button)](https://vercel.co/new/project?template=https://github.com/commercelayer/commercelayer-checkout)
## Contribute ## Contribute
Our commitment to Open Source can be found [here](https://vercel.com/oss). Our commitment to Open Source can be found [here](https://vercel.com/oss).

View 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

View File

@ -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

View File

@ -10,8 +10,8 @@ import getAllProductPaths from './operations/get-all-product-paths'
import getAllProducts from './operations/get-all-products' import getAllProducts from './operations/get-all-products'
import getProduct from './operations/get-product' import getProduct from './operations/get-product'
export interface LocalConfig extends CommerceAPIConfig {} export interface CommercelayerConfig extends CommerceAPIConfig {}
const config: LocalConfig = { const config: CommercelayerConfig = {
commerceUrl: '', commerceUrl: '',
apiToken: '', apiToken: '',
cartCookie: '', cartCookie: '',
@ -33,10 +33,10 @@ const operations = {
export const provider = { config, operations } export const provider = { config, operations }
export type Provider = typeof provider 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>( export function getCommerceApi<P extends Provider>(
customProvider: P = provider as any customProvider: P = provider as any
): LocalAPI<P> { ): CommercelayerAPI<P> {
return commerceApi(customProvider as any) return commerceApi(customProvider as any)
} }

View File

@ -31,8 +31,6 @@ export const handler: MutationHook<any> = {
alert(`User "${email}" has successfully been logged in.`) alert(`User "${email}" has successfully been logged in.`)
return token return token
} catch (error) { } catch (error) {
console.error(error)
throw new CommerceError({ throw new CommerceError({
message: `${error}`, message: `${error}`,
}) })

View File

@ -1,9 +1,7 @@
{ {
"provider": "commercelayer", "provider": "commercelayer",
"features": { "features": {
"wishlist": false, "customerAuth": true,
"cart": true, "cart": true
"search": false,
"customerAuth": true
} }
} }

View File

@ -15,7 +15,7 @@ export const fetcher: Fetcher = async ({ url, method, variables, query }) => {
method, method,
headers: { headers: {
Accept: 'application/vnd.api+json', Accept: 'application/vnd.api+json',
Authorization: `Bearer ${token.accessToken}`, Authorization: `Bearer ${token.accessToken}`,
'Content-Type': 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json',
}, },
body: JSON.stringify({ body: JSON.stringify({