Skip to content

[plugin-ecommerce] initiatePayment never validates variants (variant check nested inside product-only branch) #18339

Open

Description

@yasnikoy

Describe the Bug

In @payloadcms/plugin-ecommerce, the payment-initiation endpoint never validates variants. The variant block (if (item.variant) { … }) is nested inside if (item.product && !item.variant) { … }, so it is unreachable:

As a result, for cart items with a variant neither the price check nor the inventory check (defaultProductsValidation / a custom productsValidation) runs. A cart with quantity 2 of a variant that has inventory 1 passes /api/payments/{provider}/initiate. The later decrement in confirmOrder is an unconditional $inc, so stock can go negative.

Still present on main. Fix: move the variant block out of the product-only branch (validate the variant when item.variant is set, otherwise the product).

Link to the code that reproduces this issue

https://github.com/payloadcms/payload/blob/v3.90.2/packages/plugin-ecommerce/src/endpoints/initiatePayment.ts#L201-L310

Reproduction Steps

  1. Ecommerce template (variants enabled). Create a product with a variant; set the variant inventory to 1.
  2. Add that variant to the cart with quantity 2.
  3. POST /api/payments/stripe/initiate (any adapter).

Expected: 400 (out of stock), as happens for products without variants.
Actual: validation is skipped and the adapter's initiatePayment is called; confirm-order then decrements inventory to -1.

Which area(s) are affected?

plugin: ecommerce

Environment Info

payload: 3.90.2
@payloadcms/plugin-ecommerce: 3.90.2
@payloadcms/db-postgres: 3.90.2
next: 16.3.3
Node: 22.23.3
pnpm: 11.27.1
OS: Linux x64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions