diff --git a/framework/shopify/api/endpoints/checkout.ts b/framework/shopify/api/endpoints/checkout.ts deleted file mode 100644 index d09c976c3..000000000 --- a/framework/shopify/api/endpoints/checkout.ts +++ /dev/null @@ -1 +0,0 @@ -export default function (_commerce: any) {} diff --git a/framework/shopify/api/endpoints/checkout/checkout.ts b/framework/shopify/api/endpoints/checkout/checkout.ts new file mode 100644 index 000000000..747a53b51 --- /dev/null +++ b/framework/shopify/api/endpoints/checkout/checkout.ts @@ -0,0 +1,38 @@ +import { + SHOPIFY_CHECKOUT_ID_COOKIE, + SHOPIFY_CHECKOUT_URL_COOKIE, + SHOPIFY_CUSTOMER_TOKEN_COOKIE, +} from '@framework/const' +import associateCustomerWithCheckoutMutation from '@framework/utils/mutations/associate-customer-with-checkout' +import type { CheckoutEndpoint } from '.' + +const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({ + req, + res, + config, +}) => { + const { cookies } = req + const checkoutUrl = cookies[SHOPIFY_CHECKOUT_URL_COOKIE] + const customerCookie = cookies[SHOPIFY_CUSTOMER_TOKEN_COOKIE] + + if (customerCookie) { + try { + await config.fetch(associateCustomerWithCheckoutMutation, { + variables: { + checkoutId: cookies[SHOPIFY_CHECKOUT_ID_COOKIE], + customerAccessToken: cookies[SHOPIFY_CUSTOMER_TOKEN_COOKIE], + }, + }) + } catch (error) { + console.error(error) + } + } + + if (checkoutUrl) { + res.redirect(checkoutUrl) + } else { + res.redirect('/cart') + } +} + +export default checkout diff --git a/framework/shopify/api/endpoints/checkout/index.ts b/framework/shopify/api/endpoints/checkout/index.ts new file mode 100644 index 000000000..5d78f451b --- /dev/null +++ b/framework/shopify/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 { ShopifyAPI } 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/shopify/cart/use-cart.tsx b/framework/shopify/cart/use-cart.tsx index a7cfe9416..d920d058a 100644 --- a/framework/shopify/cart/use-cart.tsx +++ b/framework/shopify/cart/use-cart.tsx @@ -6,6 +6,12 @@ import { checkoutCreate, checkoutToCart } from '../utils' import getCheckoutQuery from '../utils/queries/get-checkout-query' import { GetCartHook } from '../types/cart' +import { + GetCheckoutQuery, + GetCheckoutQueryVariables, + CheckoutDetailsFragment, +} from '../schema' + export default useCommerceCart as UseCart export const handler: SWRHook = { diff --git a/framework/shopify/fetcher.ts b/framework/shopify/fetcher.ts index 9e57333c6..9a8d2d8d5 100644 --- a/framework/shopify/fetcher.ts +++ b/framework/shopify/fetcher.ts @@ -8,7 +8,7 @@ const fetcher: Fetcher = async ({ variables, query, }) => { - const { locale, ...vars } = variables + const { locale, ...vars } = variables ?? {} return handleFetchResponse( await fetch(url, { method, diff --git a/next.config.js b/next.config.js index c8694f1e0..c5db9e3b4 100644 --- a/next.config.js +++ b/next.config.js @@ -18,7 +18,7 @@ module.exports = withCommerceConfig({ return [ (isBC || isShopify) && { source: '/checkout', - destination: '/api/bigcommerce/checkout', + destination: '/api/checkout', }, // The logout is also an action so this route is not required, but it's also another way // you can allow a logout!