mirror of
https://github.com/vercel/commerce.git
synced 2025-05-19 07:56:59 +00:00
rename checkoutCreate to createCart and remove Shopify checkout API
Signed-off-by: Loan Laux <loan@outgrow.io>
This commit is contained in:
parent
f52978e1a3
commit
3496b2a155
@ -1,7 +1,7 @@
|
|||||||
import type { CartHandlers } from '..'
|
import type { CartHandlers } from '..'
|
||||||
import {
|
import {
|
||||||
addCartItemsMutation,
|
addCartItemsMutation,
|
||||||
checkoutCreateMutation,
|
createCartMutation,
|
||||||
} from '@framework/utils/mutations'
|
} from '@framework/utils/mutations'
|
||||||
import getCartCookie from '@framework/api/utils/get-cart-cookie'
|
import getCartCookie from '@framework/api/utils/get-cart-cookie'
|
||||||
import {
|
import {
|
||||||
@ -41,7 +41,7 @@ const addItem: CartHandlers['addItem'] = async ({
|
|||||||
if (!item.quantity) item.quantity = 1
|
if (!item.quantity) item.quantity = 1
|
||||||
|
|
||||||
if (cartId === config.dummyEmptyCartId) {
|
if (cartId === config.dummyEmptyCartId) {
|
||||||
const createdCart = await config.fetch(checkoutCreateMutation, {
|
const createdCart = await config.fetch(createCartMutation, {
|
||||||
variables: {
|
variables: {
|
||||||
input: {
|
input: {
|
||||||
shopId: config.shopId,
|
shopId: config.shopId,
|
||||||
|
@ -1,50 +1 @@
|
|||||||
import isAllowedMethod from '../utils/is-allowed-method'
|
export default function () {}
|
||||||
import createApiHandler, {
|
|
||||||
ReactionCommerceApiHandler,
|
|
||||||
} from '../utils/create-api-handler'
|
|
||||||
|
|
||||||
import {
|
|
||||||
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
|
||||||
SHOPIFY_CHECKOUT_URL_COOKIE,
|
|
||||||
REACTION_CUSTOMER_TOKEN_COOKIE,
|
|
||||||
} from '../../const'
|
|
||||||
|
|
||||||
import { getConfig } from '..'
|
|
||||||
import associateCustomerWithCheckoutMutation from '../../utils/mutations/associate-customer-with-checkout'
|
|
||||||
|
|
||||||
const METHODS = ['GET']
|
|
||||||
|
|
||||||
const checkoutApi: ReactionCommerceApiHandler<any> = async (
|
|
||||||
req,
|
|
||||||
res,
|
|
||||||
config
|
|
||||||
) => {
|
|
||||||
if (!isAllowedMethod(req, res, METHODS)) return
|
|
||||||
|
|
||||||
config = getConfig()
|
|
||||||
|
|
||||||
const { cookies } = req
|
|
||||||
const checkoutUrl = cookies[SHOPIFY_CHECKOUT_URL_COOKIE]
|
|
||||||
const customerCookie = cookies[REACTION_CUSTOMER_TOKEN_COOKIE]
|
|
||||||
|
|
||||||
if (customerCookie) {
|
|
||||||
try {
|
|
||||||
await config.fetch(associateCustomerWithCheckoutMutation, {
|
|
||||||
variables: {
|
|
||||||
checkoutId: cookies[REACTION_ANONYMOUS_CART_TOKEN_COOKIE],
|
|
||||||
customerAccessToken: cookies[REACTION_CUSTOMER_TOKEN_COOKIE],
|
|
||||||
},
|
|
||||||
})
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (checkoutUrl) {
|
|
||||||
res.redirect(checkoutUrl)
|
|
||||||
} else {
|
|
||||||
res.redirect('/cart')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default createApiHandler(checkoutApi, {}, {})
|
|
||||||
|
@ -3,13 +3,12 @@ import {
|
|||||||
SHOPIFY_CHECKOUT_URL_COOKIE,
|
SHOPIFY_CHECKOUT_URL_COOKIE,
|
||||||
REACTION_COOKIE_EXPIRE,
|
REACTION_COOKIE_EXPIRE,
|
||||||
} from '../../const'
|
} from '../../const'
|
||||||
|
import createCartMutation from '../../utils/mutations/create-cart'
|
||||||
import checkoutCreateMutation from '../../utils/mutations/checkout-create'
|
|
||||||
import Cookies from 'js-cookie'
|
import Cookies from 'js-cookie'
|
||||||
|
|
||||||
export const checkoutCreate = async (fetch: any) => {
|
export const createCart = async (fetch: any) => {
|
||||||
const data = await fetch({
|
const data = await fetch({
|
||||||
query: checkoutCreateMutation,
|
query: createCartMutation,
|
||||||
variables: {
|
variables: {
|
||||||
input: {
|
input: {
|
||||||
shopId,
|
shopId,
|
||||||
@ -25,10 +24,9 @@ export const checkoutCreate = async (fetch: any) => {
|
|||||||
expires: REACTION_COOKIE_EXPIRE,
|
expires: REACTION_COOKIE_EXPIRE,
|
||||||
}
|
}
|
||||||
Cookies.set(REACTION_ANONYMOUS_CART_TOKEN_COOKIE, checkoutId, options)
|
Cookies.set(REACTION_ANONYMOUS_CART_TOKEN_COOKIE, checkoutId, options)
|
||||||
Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout?.webUrl, options)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return checkout
|
return checkout
|
||||||
}
|
}
|
||||||
|
|
||||||
export default checkoutCreate
|
export default createCart
|
@ -1,2 +1,2 @@
|
|||||||
export { default as checkoutToCart } from './checkout-to-cart'
|
export { default as checkoutToCart } from './checkout-to-cart'
|
||||||
export { default as checkoutCreate } from './checkout-create'
|
export { default as checkoutCreate } from './create-cart'
|
||||||
|
@ -5,8 +5,6 @@ export const REACTION_CART_ID_COOKIE = 'reaction_cartId'
|
|||||||
|
|
||||||
export const REACTION_EMPTY_DUMMY_CART_ID = 'DUMMY_EMPTY_CART_ID'
|
export const REACTION_EMPTY_DUMMY_CART_ID = 'DUMMY_EMPTY_CART_ID'
|
||||||
|
|
||||||
export const SHOPIFY_CHECKOUT_URL_COOKIE = 'shopify_checkoutUrl'
|
|
||||||
|
|
||||||
export const REACTION_CUSTOMER_TOKEN_COOKIE = 'reaction_customerToken'
|
export const REACTION_CUSTOMER_TOKEN_COOKIE = 'reaction_customerToken'
|
||||||
|
|
||||||
export const STORE_DOMAIN = process.env.NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN
|
export const STORE_DOMAIN = process.env.NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN
|
||||||
|
@ -4,7 +4,7 @@ import {
|
|||||||
minOrderQuantityFailureDetailsFragment,
|
minOrderQuantityFailureDetailsFragment,
|
||||||
} from '@framework/utils/queries/get-checkout-query'
|
} from '@framework/utils/queries/get-checkout-query'
|
||||||
|
|
||||||
const checkoutCreateMutation = /* GraphQL */ `
|
const createCartMutation = /* GraphQL */ `
|
||||||
mutation createCartMutation($input: CreateCartInput!) {
|
mutation createCartMutation($input: CreateCartInput!) {
|
||||||
createCart(input: $input) {
|
createCart(input: $input) {
|
||||||
cart {
|
cart {
|
||||||
@ -21,4 +21,4 @@ const checkoutCreateMutation = /* GraphQL */ `
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
export default checkoutCreateMutation
|
export default createCartMutation
|
@ -1,6 +1,6 @@
|
|||||||
export { default as addCartItemsMutation } from './add-cart-items'
|
export { default as addCartItemsMutation } from './add-cart-items'
|
||||||
export { default as createUserMutation } from './create-user'
|
export { default as createUserMutation } from './create-user'
|
||||||
export { default as checkoutCreateMutation } from './checkout-create'
|
export { default as createCartMutation } from './create-cart'
|
||||||
export { default as authenticateMutation } from './authenticate'
|
export { default as authenticateMutation } from './authenticate'
|
||||||
export { default as logoutMutation } from './logout'
|
export { default as logoutMutation } from './logout'
|
||||||
export { default as removeCartItemsMutation } from './remove-cart-items'
|
export { default as removeCartItemsMutation } from './remove-cart-items'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user