@newageerp/node-cart
v1.0.24
Published
```bash npm install @newageerp/node-cart ```
Readme
Install
npm install @newageerp/node-cartENV variables
- PAYMENTS_PRIMER_BACKEND - if primer is used
- PAYMENTS_PAYPAL_BACKEND - if paypal is used
- PAYMENTS_STRIPE_WALLET_BACKEND - if stripe wallets is used
- PAYMENTS_STRIPE_ORDER_BACKEND - if stripe credit card form is used
- AMQP_CART_QUEUE
- CRM_CART_BACKEND_V3
.env / .env.stg example
CRM_CART_BACKEND_V3=https://cart.crm.apidata.app
AMQP_CART_QUEUE=internal.crm.cart
PAYMENTS_PRIMER_BACKEND=https://primer.crm.apidata.app/app/public/session
PAYMENTS_PAYPAL_BACKEND=https://pp.crm.apidata.app/app/public/rt
PAYMENTS_STRIPE_WALLET_BACKEND=https://stripe.crm.apidata.app/app/public/wallet
PAYMENTS_STRIPE_ORDER_BACKEND=https://stripe.crm.apidata.app/app/public/wallet.env.prod example
PAYMENTS_*** - replace apidata.app with project domain (if this is a test/RnD project, then use the domain of the main project, whose payments are used)
CRM_CART_BACKEND_V3 - replace apidata.app with project domain (if this is a test/RnD project, then use the domain of the main project, whose payments are used)
AMQP_CART_QUEUE possible production variables
- biofema.crm.cart
- cosmic.crm.cart
- mellow.crm.cart
- parenting.crm.cart
- shiftmind.crm.cart
- today.crm.cart
- wingman.crm.cart
middleware.ts
import { cartNextSrv } from '@newageerp/node-cart'
export async function middleware(req: NextRequest) {
const cartResp = await cartNextSrv.checkForRoutes(req)
if (cartResp) {
return cartResp
}
}
export const config = {
matcher: '/((?!_next/static|_next/image|favicon.ico).*)',
}Usage
set data
in examples you can see cartBrowser or cartSrv calls, make sure you are using the correct method depending on the context.
Almost all methods are available in both environments
create remote cart [- IMPORTANT -]
- call it after PaymentWindow was opened
- call it after purchase, before first upsell shown, most often it is
up/[uuid/page
it is safe to call multiple times, only one instance will be created
await cartBrowser.create({
cart: cart.cart.items,
currency: currency,
extraData: {
totals: cart.totals,
},
id, // order uuid for main purchase or `${uuid}-up` for upsell
customerId, // not neccessary if customerId = id (for main purchase)
})
mark cart as paid [- IMPORTANT -]
const cartData = await cartSrv.paid({
pm, // payment method (query param from paid/[uuid] route)
id, // order uuid for main purchase or `${uuid}-up` for upsell
})
add item to cart [- IMPORTANT -]
most often used in upsell flow to add item to the cart
await cartBrowser.appendCartItem({
id, // order uuid for main purchase or `${uuid}-up` for upsell flow
cart: [
{
product: data,
quantity: 1,
},
],
})
start upsell payment (initiate charge) [- IMPORTANT -]
call it after upsell flow.
most often it is up-paid/[uuid] page. This method also marks cart as paid (on success charge), so you don't need call paid method
// id - cart id (order uuid + suffix)
// customerId - order uuid
const cartData = await cartBrowser.upsellPayment({
id,
customerId,
})
get data
in examples you can see cartBrowser or cartSrv calls, make sure you are using the correct method depending on the context.
Almost all methods are available in both environments
get cart items
Returns items in cart ( it doesn't matter if it's paid or not ). Used in upsell flow to check if the user has added something to the cart.
// order uuid for main purchase or `${uuid}-up` for upsell
const cartData = await cartBrowser.get({ id })
get customer paid items
Returns main purchase + upsells. Used in invoice / thank you page.
// customerId - order uuid without suffixes
const cartData = await cartBrowser.getPaid({ customerId })
Utilities
cart total
// just calculate all items total sum
const total = cartUtils.total(items);