cancelOrder
Cancel an order: release an uncaptured payment authorization, update the payment and shipment status, and restock inventory. It is the service behind POST /api/orders/:id/cancel.
Import
import { cancelOrder } from "@evershop/evershop/oms/services";
Syntax
cancelOrder(uuid: string, reason: string | undefined): Promise<void>
Parameters
uuid
Type: string
The order uuid (order.uuid), not the numeric order_id.
reason
Type: string | undefined
The cancellation reason. It is written to the order activity log. Pass undefined for none; the argument itself cannot be left out in TypeScript.
Return Value
Returns Promise<void>.
Behavior
Everything runs in one database transaction, in this order:
- Loads the order and validates it. The payment status must not be flagged
isCancelable: false, andoms.order.shipmentRollupCancelablemust allow the order's shipment rollup (adeliveredorder cannot be canceled by default). - If the payment status is flagged
isVoidableand the payment method has avoidhandler, calls the handler to release the authorization. If it throws, the whole cancellation rolls back. - Sets the payment status to
canceled. - Cancels the shipments that are not final.
- Adds the activity
Order canceled (<reason>). - Returns the stock of the canceled items.
- Emits
order_canceledwith{ orderId, reason }. The customer receives the cancellation email, which can be switched off withsystem.notification_emails.order_canceled.enabled.
cancelOrder does not refund money that was already captured. A captured order is not cancelable: refund it first with refundOrder.
Errors
Order not foundOrder is not cancelable at this status— the payment status or the shipment rollup does not allow it.- Any error thrown by the payment method's
voidhandler. Invalid status— thecanceledpayment status is not registered.
Hooks
cancelOrder is hookable. Hook cancelOrder, or one of its steps: validateStatus, updatePaymentStatusToCancel, updateShipmentStatusToCancel and reStockAfterCancel, with hookBefore and hookAfter from @evershop/evershop/lib/util/hookable.
Examples
import { cancelOrder } from "@evershop/evershop/oms/services";
await cancelOrder('order-uuid-123', 'Customer requested cancellation');
See Also
- updatePaymentStatus - Update payment status
- refundOrder - Refund a captured payment
- Order API: Cancel An Order - The REST endpoint
- updateShipmentStatus - Update shipment status