Skip to content

Validate cancel/refund reasons against marketplace valid values before sending order actions - #144

Draft
michaelmaslengow with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-wrong-cancel-reason-sent
Draft

Validate cancel/refund reasons against marketplace valid values before sending order actions#144
michaelmaslengow with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-wrong-cancel-reason-sent

Conversation

Copilot AI commented Sep 3, 2026

Copy link
Copy Markdown

Since v4.2.2, cancelling an order after a refund reason had been selected sends that refund reason to the marketplace as the cancel reason. Amazon rejects this ("ProductOutOfStock" is a valid REFUND reason but not a valid CANCEL reason) because lengow_orders stores both under a single refund_reason column, and the code reused it as-is for both action types with no validation.

Marketplace action payload (root cause)

  • LengowMarketplace::getAllParams() and getAllParamsForPartialRefund() pulled the saved reason via getRefundReasonByPrestashopId() and passed it straight through, regardless of whether it was a valid value for the action actually being sent (cancel, refund, ship).
  • Added LengowMarketplace::isValidReasonForAction($reason, $action) and getValidatedReason($action, $reason), which check the reason against that action's valid_values and fall back to the marketplace's default value when it doesn't match.
  • Both param builders now route the saved reason through getValidatedReason() before including it in the payload.

Admin UI dropdown pre-selection

  • LengowOrder::getRefundDataFromLengowOrder() previously returned the same refund_reason value for both refund_reason and cancel_reason.
  • It now resolves the marketplace and independently validates the saved value against cancel valid values and refund valid values, so each dropdown only pre-selects a reason that is actually valid for that action.
// before
'cancel_reason' => $result['refund_reason'] ?? '',

// after
'cancel_reason' => $marketplace->isValidReasonForAction($savedReason, LengowAction::TYPE_CANCEL)
    ? $savedReason
    : '',

@michaelmaslengow

Copy link
Copy Markdown
Contributor

@copilot
To help, the marketplace.json file lists valid reasons for cancellations:
"cancel": {
"args": [],
"status": [
"new",
"waiting_shipment"
],
"optional_args": [
"line",
"reason"
],
"args_description": {
"line": {
"type": "line_number",
"depends_on": null,
"valid_values": {},
"default_value": null,
"accept_free_values": true
},
"reason": {
"type": "list",
"depends_on": null,
"valid_values": {
"NoInventory": {
"label": "No inventory"
},
"BuyerCanceled": {
"label": "Buyer canceled"
},
"CustomerReturn": {
"label": "Customer return"
},
"CustomerExchange": {
"label": "Customer exchange"
},
"GeneralAdjustment": {
"label": "General adjustment"
},
"CarrierCreditDecision": {
"label": "Carrier credit decision"
},
"CarrierCoverageFailure": {
"label": "Carrier coverage failure"
},
"MerchandiseNotReceived": {
"label": "Merchandise not received"
},
"ShippingAddressUndeliverable": {
"label": "Shipping address undeliverable"
},
"RiskAssessmentInformationNotValid": {
"label": "Risk assessment information not valid"
}
},
"default_value": null,
"accept_free_values": false
}
}
},

…e sending actions

Co-authored-by: michaelmaslengow <147600733+michaelmaslengow@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix wrong cancel reason sent to Amazon Validate cancel/refund reasons against marketplace valid values before sending order actions Sep 3, 2026

Copilot AI commented Sep 3, 2026

Copy link
Copy Markdown
Author

@copilot
To help, the marketplace.json file lists valid reasons for cancellations:
"cancel": {
...

Thanks, this matches how the fix is implemented. LengowMarketplace::getValidReasonCodes() reads argValues[TYPE_CANCEL][ARG_REASON]['valid_values'], which is populated from exactly this cancel.args_description.reason.valid_values structure (keyed by code, e.g. NoInventory, BuyerCanceled, etc.). I tested against this exact JSON snippet: ProductOutOfStock correctly resolves as invalid for cancel and valid for refund, so it now falls back to the marketplace's cancel default instead of being sent to Amazon.

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.

2 participants