mirror of
https://github.com/vercel/commerce.git
synced 2025-07-29 13:11:22 +00:00
Add ordercloud provider (#500)
* Add ordercloud provider * Fix provider errors * Make submit checkout optional * Make submit checkout optional * Remove nullables when creating endpoint type * Update readme * Log checkout error * Log error * Save token to cookie * Update fetch rest * Use token at checkout Co-authored-by: Luis Alvarez <luis@vercel.com>
This commit is contained in:
99
framework/ordercloud/api/endpoints/cart/add-item.ts
Normal file
99
framework/ordercloud/api/endpoints/cart/add-item.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import type { CartEndpoint } from '.'
|
||||
import type { RawVariant } from '../../../types/product'
|
||||
import type { OrdercloudLineItem } from '../../../types/cart'
|
||||
|
||||
import { serialize } from 'cookie'
|
||||
|
||||
import { formatCart } from '../../utils/cart'
|
||||
|
||||
const addItem: CartEndpoint['handlers']['addItem'] = async ({
|
||||
res,
|
||||
body: { cartId, item },
|
||||
config: { restBuyerFetch, cartCookie, tokenCookie },
|
||||
}) => {
|
||||
// Return an error if no item is present
|
||||
if (!item) {
|
||||
return res.status(400).json({
|
||||
data: null,
|
||||
errors: [{ message: 'Missing item' }],
|
||||
})
|
||||
}
|
||||
|
||||
// Store token
|
||||
let token
|
||||
|
||||
// Set the quantity if not present
|
||||
if (!item.quantity) item.quantity = 1
|
||||
|
||||
// Create an order if it doesn't exist
|
||||
if (!cartId) {
|
||||
const { ID, meta } = await restBuyerFetch(
|
||||
'POST',
|
||||
`/orders/Outgoing`,
|
||||
{}
|
||||
).then((response: { ID: string; meta: { token: string } }) => response)
|
||||
|
||||
// Set the cart id and token
|
||||
cartId = ID
|
||||
token = meta.token
|
||||
|
||||
// Set the cart and token cookie
|
||||
res.setHeader('Set-Cookie', [
|
||||
serialize(tokenCookie, meta.token, {
|
||||
maxAge: 60 * 60 * 24 * 30,
|
||||
expires: new Date(Date.now() + 60 * 60 * 24 * 30 * 1000),
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
path: '/',
|
||||
sameSite: 'lax',
|
||||
}),
|
||||
serialize(cartCookie, cartId, {
|
||||
maxAge: 60 * 60 * 24 * 30,
|
||||
expires: new Date(Date.now() + 60 * 60 * 24 * 30 * 1000),
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
path: '/',
|
||||
sameSite: 'lax',
|
||||
}),
|
||||
])
|
||||
}
|
||||
|
||||
// Store specs
|
||||
let specs: RawVariant['Specs'] = []
|
||||
|
||||
// If a variant is present, fetch its specs
|
||||
if (item.variantId) {
|
||||
specs = await restBuyerFetch(
|
||||
'GET',
|
||||
`/me/products/${item.productId}/variants/${item.variantId}`,
|
||||
null,
|
||||
{ token }
|
||||
).then((res: RawVariant) => res.Specs)
|
||||
}
|
||||
|
||||
// Add the item to the order
|
||||
await restBuyerFetch(
|
||||
'POST',
|
||||
`/orders/Outgoing/${cartId}/lineitems`,
|
||||
{
|
||||
ProductID: item.productId,
|
||||
Quantity: item.quantity,
|
||||
Specs: specs,
|
||||
},
|
||||
{ token }
|
||||
)
|
||||
|
||||
// Get cart
|
||||
const [cart, lineItems] = await Promise.all([
|
||||
restBuyerFetch('GET', `/orders/Outgoing/${cartId}`, null, { token }),
|
||||
restBuyerFetch('GET', `/orders/Outgoing/${cartId}/lineitems`, null, {
|
||||
token,
|
||||
}).then((response: { Items: OrdercloudLineItem[] }) => response.Items),
|
||||
])
|
||||
|
||||
// Format cart
|
||||
const formattedCart = formatCart(cart, lineItems)
|
||||
|
||||
// Return cart and errors
|
||||
res.status(200).json({ data: formattedCart, errors: [] })
|
||||
}
|
||||
|
||||
export default addItem
|
65
framework/ordercloud/api/endpoints/cart/get-cart.ts
Normal file
65
framework/ordercloud/api/endpoints/cart/get-cart.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import type { OrdercloudLineItem } from '../../../types/cart'
|
||||
import type { CartEndpoint } from '.'
|
||||
|
||||
import { serialize } from 'cookie'
|
||||
|
||||
import { formatCart } from '../../utils/cart'
|
||||
|
||||
// Return current cart info
|
||||
const getCart: CartEndpoint['handlers']['getCart'] = async ({
|
||||
req,
|
||||
res,
|
||||
body: { cartId },
|
||||
config: { restBuyerFetch, cartCookie, tokenCookie },
|
||||
}) => {
|
||||
if (!cartId) {
|
||||
return res.status(400).json({
|
||||
data: null,
|
||||
errors: [{ message: 'Invalid request' }],
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
// Get token from cookies
|
||||
const token = req.cookies[tokenCookie]
|
||||
|
||||
// Get cart
|
||||
const cart = await restBuyerFetch(
|
||||
'GET',
|
||||
`/orders/Outgoing/${cartId}`,
|
||||
null,
|
||||
{ token }
|
||||
)
|
||||
|
||||
// Get line items
|
||||
const lineItems = await restBuyerFetch(
|
||||
'GET',
|
||||
`/orders/Outgoing/${cartId}/lineitems`,
|
||||
null,
|
||||
{ token }
|
||||
).then((response: { Items: OrdercloudLineItem[] }) => response.Items)
|
||||
|
||||
// Format cart
|
||||
const formattedCart = formatCart(cart, lineItems)
|
||||
|
||||
// Return cart and errors
|
||||
res.status(200).json({ data: formattedCart, errors: [] })
|
||||
} catch (error) {
|
||||
// Reset cart and token cookie
|
||||
res.setHeader('Set-Cookie', [
|
||||
serialize(cartCookie, cartId, {
|
||||
maxAge: -1,
|
||||
path: '/',
|
||||
}),
|
||||
serialize(tokenCookie, cartId, {
|
||||
maxAge: -1,
|
||||
path: '/',
|
||||
}),
|
||||
])
|
||||
|
||||
// Return empty cart
|
||||
res.status(200).json({ data: null, errors: [] })
|
||||
}
|
||||
}
|
||||
|
||||
export default getCart
|
28
framework/ordercloud/api/endpoints/cart/index.ts
Normal file
28
framework/ordercloud/api/endpoints/cart/index.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { CartSchema } from '../../../types/cart'
|
||||
import type { OrdercloudAPI } from '../..'
|
||||
|
||||
import { GetAPISchema, createEndpoint } from '@commerce/api'
|
||||
import cartEndpoint from '@commerce/api/endpoints/cart'
|
||||
|
||||
import getCart from './get-cart'
|
||||
import addItem from './add-item'
|
||||
import updateItem from './update-item'
|
||||
import removeItem from './remove-item'
|
||||
|
||||
export type CartAPI = GetAPISchema<OrdercloudAPI, CartSchema>
|
||||
|
||||
export type CartEndpoint = CartAPI['endpoint']
|
||||
|
||||
export const handlers: CartEndpoint['handlers'] = {
|
||||
getCart,
|
||||
addItem,
|
||||
updateItem,
|
||||
removeItem,
|
||||
}
|
||||
|
||||
const cartApi = createEndpoint<CartAPI>({
|
||||
handler: cartEndpoint,
|
||||
handlers,
|
||||
})
|
||||
|
||||
export default cartApi
|
45
framework/ordercloud/api/endpoints/cart/remove-item.ts
Normal file
45
framework/ordercloud/api/endpoints/cart/remove-item.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { CartEndpoint } from '.'
|
||||
|
||||
import { formatCart } from '../../utils/cart'
|
||||
import { OrdercloudLineItem } from '../../../types/cart'
|
||||
|
||||
const removeItem: CartEndpoint['handlers']['removeItem'] = async ({
|
||||
req,
|
||||
res,
|
||||
body: { cartId, itemId },
|
||||
config: { restBuyerFetch, tokenCookie },
|
||||
}) => {
|
||||
if (!cartId || !itemId) {
|
||||
return res.status(400).json({
|
||||
data: null,
|
||||
errors: [{ message: 'Invalid request' }],
|
||||
})
|
||||
}
|
||||
|
||||
// Get token from cookies
|
||||
const token = req.cookies[tokenCookie]
|
||||
|
||||
// Remove the item to the order
|
||||
await restBuyerFetch(
|
||||
'DELETE',
|
||||
`/orders/Outgoing/${cartId}/lineitems/${itemId}`,
|
||||
null,
|
||||
{ token }
|
||||
)
|
||||
|
||||
// Get cart
|
||||
const [cart, lineItems] = await Promise.all([
|
||||
restBuyerFetch('GET', `/orders/Outgoing/${cartId}`, null, { token }),
|
||||
restBuyerFetch('GET', `/orders/Outgoing/${cartId}/lineitems`, null, {
|
||||
token,
|
||||
}).then((response: { Items: OrdercloudLineItem[] }) => response.Items),
|
||||
])
|
||||
|
||||
// Format cart
|
||||
const formattedCart = formatCart(cart, lineItems)
|
||||
|
||||
// Return cart and errors
|
||||
res.status(200).json({ data: formattedCart, errors: [] })
|
||||
}
|
||||
|
||||
export default removeItem
|
63
framework/ordercloud/api/endpoints/cart/update-item.ts
Normal file
63
framework/ordercloud/api/endpoints/cart/update-item.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import type { OrdercloudLineItem } from '../../../types/cart'
|
||||
import type { RawVariant } from '../../../types/product'
|
||||
import type { CartEndpoint } from '.'
|
||||
|
||||
import { formatCart } from '../../utils/cart'
|
||||
|
||||
const updateItem: CartEndpoint['handlers']['updateItem'] = async ({
|
||||
req,
|
||||
res,
|
||||
body: { cartId, itemId, item },
|
||||
config: { restBuyerFetch, tokenCookie },
|
||||
}) => {
|
||||
if (!cartId || !itemId || !item) {
|
||||
return res.status(400).json({
|
||||
data: null,
|
||||
errors: [{ message: 'Invalid request' }],
|
||||
})
|
||||
}
|
||||
|
||||
// Get token from cookies
|
||||
const token = req.cookies[tokenCookie]
|
||||
|
||||
// Store specs
|
||||
let specs: RawVariant['Specs'] = []
|
||||
|
||||
// If a variant is present, fetch its specs
|
||||
if (item.variantId) {
|
||||
specs = await restBuyerFetch(
|
||||
'GET',
|
||||
`/me/products/${item.productId}/variants/${item.variantId}`,
|
||||
null,
|
||||
{ token }
|
||||
).then((res: RawVariant) => res.Specs)
|
||||
}
|
||||
|
||||
// Add the item to the order
|
||||
await restBuyerFetch(
|
||||
'PATCH',
|
||||
`/orders/Outgoing/${cartId}/lineitems/${itemId}`,
|
||||
{
|
||||
ProductID: item.productId,
|
||||
Quantity: item.quantity,
|
||||
Specs: specs,
|
||||
},
|
||||
{ token }
|
||||
)
|
||||
|
||||
// Get cart
|
||||
const [cart, lineItems] = await Promise.all([
|
||||
restBuyerFetch('GET', `/orders/Outgoing/${cartId}`, null, { token }),
|
||||
restBuyerFetch('GET', `/orders/Outgoing/${cartId}/lineitems`, null, {
|
||||
token,
|
||||
}).then((response: { Items: OrdercloudLineItem[] }) => response.Items),
|
||||
])
|
||||
|
||||
// Format cart
|
||||
const formattedCart = formatCart(cart, lineItems)
|
||||
|
||||
// Return cart and errors
|
||||
res.status(200).json({ data: formattedCart, errors: [] })
|
||||
}
|
||||
|
||||
export default updateItem
|
Reference in New Issue
Block a user