diff --git a/framework/commercelayer/README.md b/framework/commercelayer/README.md index f0544f399..475f9ec28 100644 --- a/framework/commercelayer/README.md +++ b/framework/commercelayer/README.md @@ -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: + +[![Deploy to Vercel](https://vercel.co/button)](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). diff --git a/framework/commercelayer/api/endpoints/checkout/checkout.ts b/framework/commercelayer/api/endpoints/checkout/checkout.ts new file mode 100644 index 000000000..5762eb790 --- /dev/null +++ b/framework/commercelayer/api/endpoints/checkout/checkout.ts @@ -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 diff --git a/framework/commercelayer/api/endpoints/checkout/index.ts b/framework/commercelayer/api/endpoints/checkout/index.ts index 491bf0ac9..36a004bf2 100644 --- a/framework/commercelayer/api/endpoints/checkout/index.ts +++ b/framework/commercelayer/api/endpoints/checkout/index.ts @@ -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 + +export type CheckoutEndpoint = CheckoutAPI['endpoint'] + +export const handlers: CheckoutEndpoint['handlers'] = { checkout } + +const checkoutApi = createEndpoint({ + handler: checkoutEndpoint, + handlers, +}) + +export default checkoutApi \ No newline at end of file diff --git a/framework/commercelayer/api/index.ts b/framework/commercelayer/api/index.ts index 9f403d2b3..709159777 100644 --- a/framework/commercelayer/api/index.ts +++ b/framework/commercelayer/api/index.ts @@ -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

= CommerceAPI

+export type CommercelayerAPI

= CommerceAPI

export function getCommerceApi

( customProvider: P = provider as any -): LocalAPI

{ +): CommercelayerAPI

{ return commerceApi(customProvider as any) } diff --git a/framework/commercelayer/auth/use-login.tsx b/framework/commercelayer/auth/use-login.tsx index a3b078338..aba77f834 100644 --- a/framework/commercelayer/auth/use-login.tsx +++ b/framework/commercelayer/auth/use-login.tsx @@ -31,8 +31,6 @@ export const handler: MutationHook = { alert(`User "${email}" has successfully been logged in.`) return token } catch (error) { - console.error(error) - throw new CommerceError({ message: `${error}`, }) diff --git a/framework/commercelayer/commerce.config.json b/framework/commercelayer/commerce.config.json index 3cecf7921..5a06682ff 100644 --- a/framework/commercelayer/commerce.config.json +++ b/framework/commercelayer/commerce.config.json @@ -1,9 +1,7 @@ { "provider": "commercelayer", "features": { - "wishlist": false, - "cart": true, - "search": false, - "customerAuth": true + "customerAuth": true, + "cart": true } } diff --git a/framework/commercelayer/fetcher.ts b/framework/commercelayer/fetcher.ts index 9e3c4a439..27684a4ce 100644 --- a/framework/commercelayer/fetcher.ts +++ b/framework/commercelayer/fetcher.ts @@ -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({