4
0
forked from crowetic/commerce

setup swell checkout

This commit is contained in:
Greg Hoskin 2021-04-19 20:37:33 -05:00
parent 79ed72a710
commit 8a8ef7dbba
6 changed files with 17 additions and 50 deletions

View File

@ -1,40 +1,16 @@
import isAllowedMethod from '../utils/is-allowed-method'
import createApiHandler, { import createApiHandler, {
ShopifyApiHandler, ShopifyApiHandler,
} from '../utils/create-api-handler' } from '../utils/create-api-handler'
import { import { SWELL_CHECKOUT_URL_COOKIE } from '../../const'
SHOPIFY_CHECKOUT_ID_COOKIE,
SHOPIFY_CHECKOUT_URL_COOKIE,
SHOPIFY_CUSTOMER_TOKEN_COOKIE,
} from '../../const'
import { getConfig } from '..' import { getConfig } from '..'
import associateCustomerWithCheckoutMutation from '../../utils/mutations/associate-customer-with-checkout'
const METHODS = ['GET']
const checkoutApi: ShopifyApiHandler<any> = async (req, res, config) => { const checkoutApi: ShopifyApiHandler<any> = async (req, res, config) => {
if (!isAllowedMethod(req, res, METHODS)) return
config = getConfig() config = getConfig()
const { cookies } = req const { cookies } = req
const checkoutUrl = cookies[SHOPIFY_CHECKOUT_URL_COOKIE] const checkoutUrl = cookies[SWELL_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) { if (checkoutUrl) {
res.redirect(checkoutUrl) res.redirect(checkoutUrl)

View File

@ -2,7 +2,7 @@ import useCart, { UseCart } from '@commerce/cart/use-cart'
import { Cart } from '@commerce/types' import { Cart } from '@commerce/types'
import { SWRHook } from '@commerce/utils/types' import { SWRHook } from '@commerce/utils/types'
import { normalizeCart } from '../utils/normalize' import { normalizeCart } from '../utils/normalize'
// import { getCustomerQuery, getCustomerToken } from '../utils' import { checkoutCreate, checkoutToCart } from './utils'
export default useCart as UseCart<typeof handler> export default useCart as UseCart<typeof handler>
@ -12,10 +12,10 @@ export const handler: SWRHook<Cart | null> = {
method: 'get', method: 'get',
}, },
async fetcher({ options, fetch }) { async fetcher({ options, fetch }) {
const data = await fetch<any | null>({ const cart = await checkoutCreate(fetch)
...options,
}) return cart ? normalizeCart(cart) : null
return data ? normalizeCart(data) : null // return checkoutToCart({ checkout } as any)
}, },
useHook: ({ useData }) => (input) => { useHook: ({ useData }) => (input) => {
return useData({ return useData({

View File

@ -1,10 +1,5 @@
import { import { SWELL_CHECKOUT_URL_COOKIE } from '../../const'
SHOPIFY_CHECKOUT_ID_COOKIE,
SHOPIFY_CHECKOUT_URL_COOKIE,
SHOPIFY_COOKIE_EXPIRE,
} from '../../const'
// 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 checkoutCreate = async (fetch: any) => {
@ -13,16 +8,11 @@ export const checkoutCreate = async (fetch: any) => {
method: 'get', method: 'get',
}) })
// const checkout = data.checkoutCreate?.checkout const checkoutUrl = cart?.checkout_url
const checkoutId = cart?.id
// if (checkoutId) { if (checkoutUrl) {
// const options = { Cookies.set(SWELL_CHECKOUT_URL_COOKIE, checkoutUrl)
// expires: SHOPIFY_COOKIE_EXPIRE, }
// }
// Cookies.set(SHOPIFY_CHECKOUT_ID_COOKIE, checkoutId, options)
// Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout?.webUrl, options)
// }
return cart return cart
} }

View File

@ -1,6 +1,6 @@
export const SHOPIFY_CHECKOUT_ID_COOKIE = 'shopify_checkoutId' 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' export const SHOPIFY_CUSTOMER_TOKEN_COOKIE = 'shopify_customerToken'

View File

@ -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 useCart } from './cart/use-cart'
import { handler as useAddItem } from './cart/use-add-item' import { handler as useAddItem } from './cart/use-add-item'
@ -16,7 +16,7 @@ import fetcher from './fetcher'
export const swellProvider = { export const swellProvider = {
locale: 'en-us', locale: 'en-us',
cartCookie: SHOPIFY_CHECKOUT_ID_COOKIE, cartCookie: SWELL_CHECKOUT_URL_COOKIE,
storeDomain: STORE_DOMAIN, storeDomain: STORE_DOMAIN,
fetcher, fetcher,
cart: { useCart, useAddItem, useUpdateItem, useRemoveItem }, cart: { useCart, useAddItem, useUpdateItem, useRemoveItem },

View File

@ -3,6 +3,7 @@ const withCommerceConfig = require('./framework/commerce/with-config')
const isBC = commerce.provider === 'bigcommerce' const isBC = commerce.provider === 'bigcommerce'
const isShopify = commerce.provider === 'shopify' const isShopify = commerce.provider === 'shopify'
const isSwell = commerce.provider === 'swell'
module.exports = withCommerceConfig({ module.exports = withCommerceConfig({
commerce, commerce,
@ -12,7 +13,7 @@ module.exports = withCommerceConfig({
}, },
rewrites() { rewrites() {
return [ return [
(isBC || isShopify) && { (isBC || isShopify || isSwell) && {
source: '/checkout', source: '/checkout',
destination: '/api/bigcommerce/checkout', destination: '/api/bigcommerce/checkout',
}, },