forked from crowetic/commerce
setup swell checkout
This commit is contained in:
parent
79ed72a710
commit
8a8ef7dbba
@ -1,40 +1,16 @@
|
||||
import isAllowedMethod from '../utils/is-allowed-method'
|
||||
import createApiHandler, {
|
||||
ShopifyApiHandler,
|
||||
} from '../utils/create-api-handler'
|
||||
|
||||
import {
|
||||
SHOPIFY_CHECKOUT_ID_COOKIE,
|
||||
SHOPIFY_CHECKOUT_URL_COOKIE,
|
||||
SHOPIFY_CUSTOMER_TOKEN_COOKIE,
|
||||
} from '../../const'
|
||||
import { SWELL_CHECKOUT_URL_COOKIE } from '../../const'
|
||||
|
||||
import { getConfig } from '..'
|
||||
import associateCustomerWithCheckoutMutation from '../../utils/mutations/associate-customer-with-checkout'
|
||||
|
||||
const METHODS = ['GET']
|
||||
|
||||
const checkoutApi: ShopifyApiHandler<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[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)
|
||||
}
|
||||
}
|
||||
const checkoutUrl = cookies[SWELL_CHECKOUT_URL_COOKIE]
|
||||
|
||||
if (checkoutUrl) {
|
||||
res.redirect(checkoutUrl)
|
||||
|
@ -2,7 +2,7 @@ import useCart, { UseCart } from '@commerce/cart/use-cart'
|
||||
import { Cart } from '@commerce/types'
|
||||
import { SWRHook } from '@commerce/utils/types'
|
||||
import { normalizeCart } from '../utils/normalize'
|
||||
// import { getCustomerQuery, getCustomerToken } from '../utils'
|
||||
import { checkoutCreate, checkoutToCart } from './utils'
|
||||
|
||||
export default useCart as UseCart<typeof handler>
|
||||
|
||||
@ -12,10 +12,10 @@ export const handler: SWRHook<Cart | null> = {
|
||||
method: 'get',
|
||||
},
|
||||
async fetcher({ options, fetch }) {
|
||||
const data = await fetch<any | null>({
|
||||
...options,
|
||||
})
|
||||
return data ? normalizeCart(data) : null
|
||||
const cart = await checkoutCreate(fetch)
|
||||
|
||||
return cart ? normalizeCart(cart) : null
|
||||
// return checkoutToCart({ checkout } as any)
|
||||
},
|
||||
useHook: ({ useData }) => (input) => {
|
||||
return useData({
|
||||
|
@ -1,10 +1,5 @@
|
||||
import {
|
||||
SHOPIFY_CHECKOUT_ID_COOKIE,
|
||||
SHOPIFY_CHECKOUT_URL_COOKIE,
|
||||
SHOPIFY_COOKIE_EXPIRE,
|
||||
} from '../../const'
|
||||
import { SWELL_CHECKOUT_URL_COOKIE } from '../../const'
|
||||
|
||||
// import checkoutCreateMutation from '../../utils/mutations/checkout-create'
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
export const checkoutCreate = async (fetch: any) => {
|
||||
@ -13,16 +8,11 @@ export const checkoutCreate = async (fetch: any) => {
|
||||
method: 'get',
|
||||
})
|
||||
|
||||
// const checkout = data.checkoutCreate?.checkout
|
||||
const checkoutId = cart?.id
|
||||
const checkoutUrl = cart?.checkout_url
|
||||
|
||||
// if (checkoutId) {
|
||||
// const options = {
|
||||
// expires: SHOPIFY_COOKIE_EXPIRE,
|
||||
// }
|
||||
// Cookies.set(SHOPIFY_CHECKOUT_ID_COOKIE, checkoutId, options)
|
||||
// Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout?.webUrl, options)
|
||||
// }
|
||||
if (checkoutUrl) {
|
||||
Cookies.set(SWELL_CHECKOUT_URL_COOKIE, checkoutUrl)
|
||||
}
|
||||
|
||||
return cart
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
export const SHOPIFY_CHECKOUT_ID_COOKIE = 'shopify_checkoutId'
|
||||
|
||||
export const SHOPIFY_CHECKOUT_URL_COOKIE = 'shopify_checkoutUrl'
|
||||
export const SWELL_CHECKOUT_URL_COOKIE = 'swell_checkoutUrl'
|
||||
|
||||
export const SHOPIFY_CUSTOMER_TOKEN_COOKIE = 'shopify_customerToken'
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { SHOPIFY_CHECKOUT_ID_COOKIE, STORE_DOMAIN } from './const'
|
||||
import { SWELL_CHECKOUT_URL_COOKIE, STORE_DOMAIN } from './const'
|
||||
|
||||
import { handler as useCart } from './cart/use-cart'
|
||||
import { handler as useAddItem } from './cart/use-add-item'
|
||||
@ -16,7 +16,7 @@ import fetcher from './fetcher'
|
||||
|
||||
export const swellProvider = {
|
||||
locale: 'en-us',
|
||||
cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE,
|
||||
cartCookie: SWELL_CHECKOUT_URL_COOKIE,
|
||||
storeDomain: STORE_DOMAIN,
|
||||
fetcher,
|
||||
cart: { useCart, useAddItem, useUpdateItem, useRemoveItem },
|
||||
|
@ -3,6 +3,7 @@ const withCommerceConfig = require('./framework/commerce/with-config')
|
||||
|
||||
const isBC = commerce.provider === 'bigcommerce'
|
||||
const isShopify = commerce.provider === 'shopify'
|
||||
const isSwell = commerce.provider === 'swell'
|
||||
|
||||
module.exports = withCommerceConfig({
|
||||
commerce,
|
||||
@ -12,7 +13,7 @@ module.exports = withCommerceConfig({
|
||||
},
|
||||
rewrites() {
|
||||
return [
|
||||
(isBC || isShopify) && {
|
||||
(isBC || isShopify || isSwell) && {
|
||||
source: '/checkout',
|
||||
destination: '/api/bigcommerce/checkout',
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user