diff --git a/framework/bigcommerce/api/endpoints/checkout/checkout.ts b/framework/bigcommerce/api/endpoints/checkout/checkout.ts new file mode 100644 index 000000000..517a57950 --- /dev/null +++ b/framework/bigcommerce/api/endpoints/checkout/checkout.ts @@ -0,0 +1,62 @@ +import type { CheckoutEndpoint } from '.' + +const fullCheckout = true + +const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({ + req, + res, + config, +}) => { + const { cookies } = req + const cartId = cookies[config.cartCookie] + + if (!cartId) { + res.redirect('/cart') + return + } + + const { data } = await config.storeApiFetch( + `/v3/carts/${cartId}/redirect_urls`, + { + method: 'POST', + } + ) + + if (fullCheckout) { + res.redirect(data.checkout_url) + return + } + + // TODO: make the embedded checkout work too! + const html = ` + + + + + + Checkout + + + + +
+ + + ` + + res.status(200) + res.setHeader('Content-Type', 'text/html') + res.write(html) + res.end() +} + +export default checkout diff --git a/framework/bigcommerce/api/endpoints/checkout/index.ts b/framework/bigcommerce/api/endpoints/checkout/index.ts new file mode 100644 index 000000000..eaba32e47 --- /dev/null +++ b/framework/bigcommerce/api/endpoints/checkout/index.ts @@ -0,0 +1,18 @@ +import { GetAPISchema, createEndpoint } from '@commerce/api' +import checkoutEndpoint from '@commerce/api/endpoints/checkout' +import type { CheckoutSchema } from '../../../types/checkout' +import type { BigcommerceAPI } 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 diff --git a/framework/bigcommerce/types/checkout.ts b/framework/bigcommerce/types/checkout.ts new file mode 100644 index 000000000..4e2412ef6 --- /dev/null +++ b/framework/bigcommerce/types/checkout.ts @@ -0,0 +1 @@ +export * from '@commerce/types/checkout' diff --git a/framework/commerce/api/endpoints/checkout.ts b/framework/commerce/api/endpoints/checkout.ts new file mode 100644 index 000000000..b39239a6a --- /dev/null +++ b/framework/commerce/api/endpoints/checkout.ts @@ -0,0 +1,35 @@ +import type { CheckoutSchema } from '../../types/checkout' +import { CommerceAPIError } from '../utils/errors' +import isAllowedOperation from '../utils/is-allowed-operation' +import type { GetAPISchema } from '..' + +const checkoutEndpoint: GetAPISchema< + any, + CheckoutSchema +>['endpoint']['handler'] = async (ctx) => { + const { req, res, handlers } = ctx + + if ( + !isAllowedOperation(req, res, { + GET: handlers['checkout'], + }) + ) { + return + } + + try { + const body = null + return await handlers['checkout']({ ...ctx, body }) + } catch (error) { + console.error(error) + + const message = + error instanceof CommerceAPIError + ? 'An unexpected error ocurred with the Commerce API' + : 'An unexpected error ocurred' + + res.status(500).json({ data: null, errors: [{ message }] }) + } +} + +export default checkoutEndpoint diff --git a/framework/commerce/api/index.ts b/framework/commerce/api/index.ts index 6a28a9597..8a08b92ee 100644 --- a/framework/commerce/api/index.ts +++ b/framework/commerce/api/index.ts @@ -8,6 +8,7 @@ import type { LogoutSchema } from '../types/logout' import type { SignupSchema } from '../types/signup' import type { ProductsSchema } from '../types/product' import type { WishlistSchema } from '../types/wishlist' +import type { CheckoutSchema } from '../types/checkout' import { defaultOperations, OPERATIONS, @@ -23,6 +24,7 @@ export type APISchemas = | SignupSchema | ProductsSchema | WishlistSchema + | CheckoutSchema export type GetAPISchema< C extends CommerceAPI, diff --git a/framework/commerce/types/checkout.ts b/framework/commerce/types/checkout.ts new file mode 100644 index 000000000..9e3c7ecfa --- /dev/null +++ b/framework/commerce/types/checkout.ts @@ -0,0 +1,10 @@ +export type CheckoutSchema = { + endpoint: { + options: {} + handlers: { + checkout: { + data: null + } + } + } +} diff --git a/pages/api/bigcommerce/checkout.ts b/pages/api/bigcommerce/checkout.ts deleted file mode 100644 index bd754deab..000000000 --- a/pages/api/bigcommerce/checkout.ts +++ /dev/null @@ -1,3 +0,0 @@ -import checkoutApi from '@framework/api/checkout' - -export default checkoutApi() diff --git a/pages/api/checkout.ts b/pages/api/checkout.ts new file mode 100644 index 000000000..7bf0fd9aa --- /dev/null +++ b/pages/api/checkout.ts @@ -0,0 +1,4 @@ +import checkoutApi from '@framework/api/endpoints/checkout' +import commerce from '@lib/api/commerce' + +export default checkoutApi(commerce)