Extends the official Payload ecommerce plugin by wrapping its payment initiate and confirm-order endpoints. It checks stock and price for products and variants before the payment provider is contacted, then reserves stock with expiring holds. It requires Payload 3.88 or newer and the official ecommerce plugin, and must be listed after it.
Validates variant stock and price for Payload ecommerce, which the official plugin never does, and holds stock from checkout until payment settles.
Installation
npm i payload-stock-reservation1
Stars
0
Forks
0
Open Issues
2 weeks ago
Last Updated
152
Weekly Downloads
v1.0.1
npm Version
38.5 KB
Unpacked Size
0
Dependencies
10 since Sep 7
| Date | Stars |
|---|---|
| Dec 6, 2026 | 1 |
| Dec 5, 2026 | 1 |
| Dec 4, 2026 | 1 |
| Dec 3, 2026 | 1 |
| Dec 2, 2026 | 1 |
| Dec 1, 2026 | 1 |
| Nov 30, 2026 | 1 |
| Nov 29, 2026 | 1 |
| Nov 28, 2026 | 1 |
| Nov 27, 2026 | 1 |
| Nov 26, 2026 | 1 |
| Nov 25, 2026 | 1 |
| Nov 24, 2026 | 1 |
| Nov 23, 2026 | 1 |
| Nov 22, 2026 | 1 |
| Nov 21, 2026 | 1 |
| Nov 20, 2026 | 1 |
| Nov 19, 2026 | 1 |
| Nov 18, 2026 | 1 |
| Nov 17, 2026 | 1 |
| Nov 16, 2026 | 1 |
| Nov 15, 2026 | 1 |
| Nov 14, 2026 | 1 |
| Nov 13, 2026 | 1 |
| Nov 12, 2026 | 1 |
| Nov 11, 2026 | 1 |
| Nov 10, 2026 | 1 |
| Nov 9, 2026 | 1 |
| Nov 8, 2026 | 1 |
| Nov 7, 2026 | 1 |
| Nov 6, 2026 | 1 |
| Nov 5, 2026 | 1 |
| Nov 4, 2026 | 1 |
| Nov 3, 2026 | 1 |
| Nov 2, 2026 | 1 |
| Nov 1, 2026 | 1 |
| Oct 31, 2026 | 1 |
| Oct 30, 2026 | 1 |
| Oct 29, 2026 | 1 |
| Oct 28, 2026 | 1 |
| Oct 27, 2026 | 1 |
| Oct 26, 2026 | 1 |
| Oct 25, 2026 | 1 |
| Oct 24, 2026 | 1 |
| Oct 23, 2026 | 1 |
| Oct 22, 2026 | 1 |
| Oct 21, 2026 | 1 |
| Oct 20, 2026 | 1 |
| Oct 19, 2026 | 1 |
| Oct 18, 2026 | 1 |
| Oct 17, 2026 | 1 |
| Oct 16, 2026 | 1 |
| Oct 15, 2026 | 1 |
| Oct 14, 2026 | 1 |
| Oct 13, 2026 | 1 |
| Oct 12, 2026 | 1 |
| Oct 11, 2026 | 1 |
| Oct 10, 2026 | 1 |
| Oct 9, 2026 | 1 |
| Oct 8, 2026 | 1 |
| Oct 7, 2026 | 1 |
| Oct 6, 2026 | 1 |
| Oct 5, 2026 | 1 |
| Oct 4, 2026 | 1 |
| Oct 3, 2026 | 1 |
| Oct 2, 2026 | 1 |
| Oct 1, 2026 | 1 |
| Sep 30, 2026 | 1 |
| Sep 29, 2026 | 1 |
| Sep 28, 2026 | 1 |
| Sep 27, 2026 | 1 |
| Sep 26, 2026 | 1 |
| Sep 25, 2026 | 1 |
| Sep 24, 2026 | 1 |
| Sep 23, 2026 | 1 |
| Sep 22, 2026 | 1 |
| Sep 21, 2026 | 1 |
| Sep 20, 2026 | 1 |
| Sep 19, 2026 | 1 |
| Sep 18, 2026 | 1 |
| Sep 17, 2026 | 1 |
| Sep 16, 2026 | 1 |
| Sep 15, 2026 | 1 |
| Sep 14, 2026 | 1 |
| Sep 13, 2026 | 1 |
| Sep 12, 2026 | 1 |
| Sep 11, 2026 | 1 |
| Sep 10, 2026 | 1 |
| Sep 9, 2026 | 1 |
| Sep 8, 2026 | 1 |
| Sep 7, 2026 | 1 |
156+143 since Sep 7
| Date | Weekly downloads |
|---|---|
| Dec 6, 2026 | 156 |
| Dec 5, 2026 | 90 |
| Dec 4, 2026 | 65 |
| Dec 3, 2026 | 38 |
| Dec 2, 2026 | 47 |
| Dec 1, 2026 | 47 |
| Nov 30, 2026 | 49 |
| Nov 29, 2026 | 59 |
| Nov 28, 2026 | 72 |
| Nov 27, 2026 | 79 |
| Nov 26, 2026 | 258 |
| Nov 25, 2026 | 252 |
| Nov 24, 2026 | 271 |
| Nov 23, 2026 | 271 |
| Nov 22, 2026 | 254 |
| Nov 21, 2026 | 236 |
| Nov 20, 2026 | 223 |
| Nov 19, 2026 | 38 |
| Nov 18, 2026 | 31 |
| Nov 17, 2026 | 10 |
| Nov 16, 2026 | 4 |
| Nov 15, 2026 | 3 |
| Nov 14, 2026 | 4 |
| Nov 13, 2026 | 4 |
| Nov 12, 2026 | 4 |
| Nov 11, 2026 | 4 |
| Nov 10, 2026 | 3 |
| Nov 9, 2026 | 9 |
| Nov 8, 2026 | 12 |
| Nov 7, 2026 | 12 |
| Nov 6, 2026 | 12 |
| Nov 5, 2026 | 14 |
| Nov 4, 2026 | 14 |
| Nov 3, 2026 | 13 |
| Nov 2, 2026 | 7 |
| Nov 1, 2026 | 4 |
| Oct 31, 2026 | 4 |
| Oct 30, 2026 | 4 |
| Oct 29, 2026 | 1 |
| Oct 28, 2026 | 1 |
| Oct 27, 2026 | 2 |
| Oct 26, 2026 | 3 |
| Oct 25, 2026 | 3 |
| Oct 24, 2026 | 2 |
| Oct 23, 2026 | 2 |
| Oct 22, 2026 | 4 |
| Oct 21, 2026 | 4 |
| Oct 20, 2026 | 3 |
| Oct 19, 2026 | 3 |
| Oct 18, 2026 | 3 |
| Oct 17, 2026 | 3 |
| Oct 16, 2026 | 3 |
| Oct 15, 2026 | 3 |
| Oct 14, 2026 | 3 |
| Oct 13, 2026 | 3 |
| Oct 12, 2026 | 2 |
| Oct 11, 2026 | 3 |
| Oct 10, 2026 | 3 |
| Oct 9, 2026 | 3 |
| Oct 8, 2026 | 3 |
| Oct 7, 2026 | 3 |
| Oct 6, 2026 | 4 |
| Oct 5, 2026 | 4 |
| Oct 4, 2026 | 5 |
| Oct 3, 2026 | 7 |
| Oct 2, 2026 | 9 |
| Oct 1, 2026 | 8 |
| Sep 30, 2026 | 9 |
| Sep 29, 2026 | 9 |
| Sep 28, 2026 | 9 |
| Sep 27, 2026 | 7 |
| Sep 26, 2026 | 8 |
| Sep 25, 2026 | 6 |
| Sep 24, 2026 | 5 |
| Sep 23, 2026 | 5 |
| Sep 22, 2026 | 5 |
| Sep 21, 2026 | 6 |
| Sep 20, 2026 | 9 |
| Sep 19, 2026 | 7 |
| Sep 18, 2026 | 7 |
| Sep 17, 2026 | 11 |
| Sep 16, 2026 | 16 |
| Sep 15, 2026 | 15 |
| Sep 14, 2026 | 17 |
| Sep 13, 2026 | 15 |
| Sep 12, 2026 | 14 |
| Sep 11, 2026 | 15 |
| Sep 10, 2026 | 11 |
| Sep 9, 2026 | 8 |
| Sep 8, 2026 | 10 |
| Sep 7, 2026 | 13 |
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/1340410744-root)<a href="https://payloaddirectory.dev/plugins/1340410744-root"><img src="https://payloaddirectory.dev/badge/1340410744-root.svg" alt="Payload Directory"></a>README
Validates the stock and the price of a variant before a Payload checkout reaches the payment provider, which @payloadcms/plugin-ecommerce never does, and holds what it validated until the payment settles or the hold expires.
@payloadcms/plugin-ecommerce, wrapping its checkout endpoints instead of replacing themRequires Payload 3.88 or newer and @payloadcms/plugin-ecommerce 3.88 or newer. Verified against Payload 3.88.0 with the official plugin installed.
pnpm add payload-stock-reservation
import { ecommercePlugin } from '@payloadcms/plugin-ecommerce'
import { stockReservationPlugin } from 'payload-stock-reservation'
export default buildConfig({
plugins: [
ecommercePlugin({ ... }),
stockReservationPlugin({
ttlSeconds: 900,
}),
],
})
stockReservationPlugin must come after ecommercePlugin, because it wraps the endpoints that plugin registers. If it comes first there is nothing to wrap, and it says so in the server log on startup rather than staying quiet.
Read in the published @payloadcms/[email protected], in the original TypeScript carried by its source maps.
In src/endpoints/initiatePayment.ts the variant check sits inside a branch that requires the item to have no variant:
201 if (item.product && !item.variant) {
253 if (item.variant) {
310 }
311 }
The inner condition can never be true. Reproduced on a live Payload 3.88 install: a product with inventory 2 and no variant is refused with HTTP 400 and cause.code: OutOfStock, while the same quantity of a variant with inventory 2 passes validation completely and reaches the payment provider.
confirmOrder then decrements that variant correctly, with payload.db.updateOne and $inc, so the stock goes negative. In the same handler productsValidation is destructured out of the arguments and never called.
What the official checkout actually checks, per cart line:
| Cart line | Price checked | Stock checked | Stock decremented after payment |
|---|---|---|---|
| product, no variant | yes | yes | yes |
| product with a variant | no | no | yes |
Availability is read in initiatePayment and the decrement happens in confirmOrder. Nothing is written between the two, so two shoppers can both pass validation holding the same last unit.
Three interception points were read before one was chosen:
| Point | Why it was rejected or chosen |
|---|---|
products.validation, the official override | Rejected. For a line with a variant the loop never reaches the call, so the override is never invoked |
| A hook on the transactions collection | Rejected. The Stripe adapter creates the payment intent first and the transaction record second, so a hook there fires after the provider has been contacted |
The post /payments/:adapter/initiate endpoint | Chosen. It is a root endpoint on the config, it runs before any adapter code, and wrapping it leaves the original handler in place |
Every Payload request runs in its own transaction, so a hold that is written inside the request transaction is invisible to a competing request until commit. Holds are therefore written outside it, which makes them visible immediately and carries the connection cost described under Honest limits.
A write alone still cannot serialise two checkouts, so the hold is written first and judged afterwards. All live holds on the same product or variant are read back, ranked oldest first, and their quantities accumulated. A hold keeps its stock only if the running total up to and including it fits inside the recorded stock. Of two checkouts racing for the last unit, the older keeps it and the newer deletes its own hold and is refused. Ties on the creation time are broken on the identifier, so the ranking is a total order and the two racers always agree on who won.
| Option | Default | Meaning |
|---|---|---|
cartsSlug | 'carts' | Slug of the carts collection |
defaultCurrency | '' | Currency for the price check when the cart carries none. Empty means the price check is skipped and only stock is validated |
disabled | false | Stops validating and holding but keeps the collection, so the database keeps its shape |
holdScanLimit | 500 | Highest number of holds read for one item when ranking a new hold |
holdsSlug | 'stock-holds' | Collection holding reserved stock |
inventoryFieldName | 'inventory' | Numeric field holding stock on products and variants |
productsSlug | 'products' | Slug of the products collection |
refuseNegativeInventory | true | Refuses an item already recorded below zero with the reason NegativeInventory |
releaseEndpointPath | '/stock-reservation/release-expired' | Path of the endpoint that deletes expired holds |
releaseSecret | '' | Secret accepted by that endpoint as Authorization: Bearer <secret>. Empty means only a signed in user may call it |
sweepOnCheckout | true | Deletes the expired holds of the items in the cart at the start of every checkout |
ttlSeconds | 900 | How long a hold survives, in seconds |
variantsSlug | 'variants' | Slug of the variants collection |
A value that cannot be used is replaced by its default rather than being applied. A ttlSeconds of 0 becomes 900, 90.9 becomes 90, and a slug given as an empty string becomes its default. A releaseEndpointPath without a leading slash gains one.
| Collection | Field | Type | Notes |
|---|---|---|---|
stock-holds | cart | text | indexed. The cart that owns the hold |
stock-holds | stockCollection | text | indexed. products or variants |
stock-holds | stockDocument | text | indexed. Identifier of the held product or variant |
stock-holds | quantity | number | minimum 1 |
stock-holds | expiresAt | date | indexed |
Nothing is added to your products, variants, carts or orders. The holds collection is closed to create, read, update and delete through the API and hidden in the admin panel. It is written only by the plugin.
| Endpoint | Method | Purpose |
|---|---|---|
/api/payments/:adapter/initiate | post | Wrapped. Validates and holds, then calls the original handler |
/api/payments/:adapter/confirm-order | post | Wrapped. Calls the original handler, then releases that cart's holds |
/api/stock-reservation/release-expired | post | Added. Deletes expired holds and returns { "released": n } |
A refused line returns HTTP 400 with a readable sentence and a machine readable reason:
{
"message": "Only 2 left of variants 41.",
"cause": { "code": "OutOfStock", "collection": "variants", "document": "41" }
}
The codes are OutOfStock, MissingPrice, NegativeInventory and NotFound, exported as refusalCodes.
Expired holds never count towards availability, so nothing breaks if they are left in place. Deleting them only keeps the collection small, and there are three ways to do it, none of which needs a scheduler in your dependencies:
Leave sweepOnCheckout on. Every checkout deletes the expired holds of the items in its own cart.
Point your existing scheduler at the endpoint.
curl -X POST https://example.com/api/stock-reservation/release-expired \
-H "Authorization: Bearer $STOCK_RELEASE_SECRET"
Call it yourself, from a route handler or a script.
import { releaseExpiredHolds } from 'payload-stock-reservation'
const released = await releaseExpiredHolds(payload)
import { availableStock } from 'payload-stock-reservation'
const left = await availableStock(payload, { collection: 'variants', document: variantID })
It returns the recorded stock minus every live hold, or null when the item does not exist or does not track stock.
PostgreSQL needs a large enough connection pool. A hold is written outside the request transaction, so that a competing checkout can see it. That opens a second connection while the request holds the first. The same mechanism was measured for payload-order-numbers on a live install: with the pg default pool of 10, ten simultaneous checkouts succeed and fifteen do not. Set the pool to at least twice the number of checkouts you expect in the same instant.
postgresAdapter({
pool: { connectionString: process.env.DATABASE_URI, max: 60 },
})
MongoDB is not affected and needs no change.
A hold is written before it is judged. The hold has to exist before it can be ranked against the others, and the cart is read with access checks overridden so that guest carts behave the same way. A checkout that is then refused, whether by this package or by the original handler, deletes its own holds before returning, so nothing is left behind, but the write happens.
It guards the checkout endpoints, nothing else. An order written straight through the local API, the admin panel or a payment webhook does not pass through this package and is not validated or held.
The decrement still belongs to the official plugin. This package stops a sale that cannot be fulfilled from starting; it does not change how confirmOrder decrements stock afterwards. Stock that is already negative stays negative until you correct it.
Both settings of refuseNegativeInventory refuse the sale. The option changes the reason, not the outcome: NegativeInventory when it is on, OutOfStock when it is off. It exists so a storefront can tell an oversold record apart from an ordinary sell out.
An item with no stock value is unlimited and is never held. That is what the official plugin does with an empty inventory field, and this package does not change it.
A price of zero is refused as a missing price. That is also what the official plugin does. Free items cannot go through this checkout, with or without this package.
Holds are deleted, not archived. There is no history of who held what. If you need an audit trail, that is a different guarantee and belongs in a different package.
Above holdScanLimit holds on one item the ranking window truncates. With more than 500 live holds on a single product or variant, the ranking reads the oldest 500 and a hold outside that window is refused rather than mis-sold.
The release endpoint path must not start with a collection slug. Payload resolves the first path segment to a collection before it looks at root endpoints, so /stock-holds/... would never reach the handler. The default avoids this, and a path that would be shadowed is reported in the server log on startup.
If the package cannot do its job, the sale goes through. A database error while validating or holding is logged as an error and the checkout is handed to the original handler unchanged, leaving you exactly where you were without this package. Only a real refusal, one this package decided on, stops a checkout. Losing a sale to a broken sweep is worse than the oversell it was meant to prevent.
MIT. Copyright George Vasiliades, https://github.com/Poseidonas
ecommerce
Ecommerce utilities for Payload with idempotent Stripe confirmation, orders, carts and inventory handling
10019.0k/wk46.1kv3 v4
payload ecommerce
Products, carts, orders and coupons for Payload 3 with server-side helpers and stock tracking
72537/wk2v3
payload stock alerts
Low stock threshold alerts by email and backorder handling for Payload ecommerce products and variants
7222/wk1v3
payload coupons
Coupon codes with percentage and fixed discounts for Payload ecommerce, calculated in integer minor units
76144/wk2v3
payload fulfillment
Extra order states, transition history, internal notes and tracking fields for Payload ecommerce orders
7332/wk1v3
payload shipping classes
Weight, dimensions and a shipping class on Payload products and variants, stored in one canonical unit
7210/wk2v3