Skip to content

fix(plugin-ecommerce): unnest variant validation during payment initiation (#18339) - #18347

Open
PINYOPATTANAWASANPORN wants to merge 1 commit into
payloadcms:mainfrom
PINYOPATTANAWASANPORN:fix/ecommerce-initiate-payment-validate-variants-18339
Open

PINYOPATTANAWASANPORN wants to merge 1 commit into
payloadcms:mainfrom
PINYOPATTANAWASANPORN:fix/ecommerce-initiate-payment-validate-variants-18339

Conversation

@PINYOPATTANAWASANPORN

Copy link
Copy Markdown
Contributor

Important

Fixes #18339

Summary of Changes

  • Unnests the variant validation block (if (item.variant)) from the product-only branch (if (item.product && !item.variant)).
  • Ensures that cart items with variants have their variant-specific price and inventory properly validated prior to payment gateway initiation.

Root Cause Analysis

In packages/plugin-ecommerce/src/endpoints/initiatePayment.ts, the variant validation block (if (item.variant) { ... }) was nested inside if (item.product && !item.variant) { ... }.
Because the outer branch checked !item.variant, the inner variant check was unreachable dead code. As a result, any cart item with a variant skipped both product and variant price/inventory checks, allowing out-of-stock items to pass payment initiation and decrement inventory below zero.

Restructuring to:

if (item.variant) {
  // Validate variant-specific pricing and inventory
} else if (item.product) {
  // Validate product-level pricing and inventory
}

restores proper validation order and prevents overselling.

Verification

  • Tested with cart items containing variants (validates variant price/inventory) and product-only items (validates product price/inventory).
  • Clean minimal diff strictly in packages/plugin-ecommerce/src/endpoints/initiatePayment.ts.
  • Strict LF line endings preserved.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant