# Cancel an order

> For the complete machine-readable documentation index, see [llms.txt](https://apidocs.chargebee.com/llms.txt).


[Idempotency Supported](/docs/api/idempotency)

Cancel order and create a refundable credit note for the order

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/orders/1/cancel \
     -u {site_api_key}:\
     -d cancellation_reason="PRODUCT_UNSATISFACTORY" \
     -d customer_notes="Products were defective"
```

#### .NET

```dotnet
using ChargeBee.Api;
using ChargeBee.Models;

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Order.Cancel("1")
		.CancellationReason(Order.CancellationReasonEnum.ProductUnsatisfactory)
		.CustomerNotes("Products were defective")
		.Request();

Order order = result.Order;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    orderAction "github.com/chargebee/chargebee-go/v3/actions/order"
    "github.com/chargebee/chargebee-go/v3/models/order"
    orderEnum "github.com/chargebee/chargebee-go/v3/models/order/enum"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := orderAction.Cancel("1", &order.CancelRequestParams{
        CancellationReason : orderEnum.CancellationReasonProductUnsatisfactory,
        CustomerNotes : "Products were defective",
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        Order := res.Order
    }
}
```

#### Go

```go
package main

import (
  "fmt"
  "github.com/chargebee/chargebee-go/v4"
)

func main() {
  config := &chargebee.ClientConfig{
    SiteName: "{site}",
    ApiKey: "{site_api_key}",
  }    
  client := chargebee.NewClient(config)
  req := &chargebee.OrderCancelRequest{
    CancellationReason : chargebee.OrderCancellationReasonProductUnsatisfactory,
    CustomerNotes : "Products were defective",
}
  res, err := client.Order.Cancel("1", req)
      if err != nil {
        fmt.Println(err)
    } else {
        Order := res.Order
    }
}
```

#### Java

```java
import com.chargebee.*;
import com.chargebee.ListResult;
import com.chargebee.models.*;
import com.chargebee.models.enums.*;
import java.io.IOException;

public class Sample {

    public static void main(String args[]) throws IOException, Exception {
        Environment.configure("{site}", "{site_api_key}");
        Result result = Order.cancel("1")
            .cancellationReason(Order.CancellationReason.PRODUCT_UNSATISFACTORY)
            .customerNotes("Products were defective")
            .request();

        Order order = result.order();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.order.Order;
import com.chargebee.v4.models.order.params.OrderCancelParams;
import com.chargebee.v4.models.order.responses.OrderCancelResponse;

public class OrderCancel {

    public static void main(String[] args) {
        ChargebeeClient client = ChargebeeClient.builder()
            .apiKey("{site_api_key}")
            .siteName("{site}")
            .build();

        OrderCancelParams params = OrderCancelParams.builder()
            .cancellationReason(OrderCancelParams.CancellationReason.PRODUCT_UNSATISFACTORY)
            .customerNotes("Products were defective")
            .build();

        OrderCancelResponse response = client
            .orders()
            .cancel("1", params);

        Order order = response.getOrder();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

const chargebee = new Chargebee({
    site: "{site}",
    apiKey: "{site_api_key}",
});

try {
    const result = await chargebee.order.cancel("1", {
        cancellation_reason: "product_unsatisfactory",
        customer_notes: "Products were defective"
    });

    console.log(result);
    const order = result.order;
} catch (err) {
    console.log(err);
}
```

#### PHP

```php
<?php

require __DIR__ . '/vendor/autoload.php';

use Chargebee\ChargebeeClient;

$chargebee = new ChargebeeClient(options: [
    "site" => "{site}",
    "apiKey" => "{site_api_key}",
]);
$result = $chargebee->order()->cancel("1", [
    "cancellation_reason" => "product_unsatisfactory",
    "customer_notes" => "Products were defective"
]);
$order = $result->order;
```

#### Python

```python
import chargebee
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.Order.cancel("1",
    cb_client.Order.CancelParams(
        cancellation_reason=chargebee.Order.CancellationReason.PRODUCT_UNSATISFACTORY,
        customer_notes="Products were defective"
    )
)
order = response.order
```

#### Ruby

```ruby
require 'chargebee'

ChargeBee.configure(:site => "{site}",
  :api_key => "{site_api_key}")

result = ChargeBee::Order.cancel("1",{
  :cancellation_reason => "PRODUCT_UNSATISFACTORY",
  :customer_notes => "Products were defective"
})

order = result.order
```

## Sample Response

```json
{
  "order": {
    "amount_adjusted": 0,
    "amount_paid": 40000,
    "base_currency_code": "USD",
    "cancellation_reason": "product_unsatisfactory",
    "cancelled_at": 1517678683,
    "created_at": 1517505885,
    "created_by": "Auto generated by system",
    "currency_code": "USD",
    "customer_id": "__test__KyVnHhSBWll5K2lX",
    "deleted": false,
    "discount": 0,
    "document_number": "1",
    "exchange_rate": 1,
    "id": "1",
    "invoice_id": "__demo_inv__1",
    "is_gifted": false,
    "linked_credit_notes": {},
    "object": "order",
    "order_date": 1517505884,
    "order_line_items": [
      {
        "amount": 40000,
        "amount_adjusted": 0,
        "amount_paid": 40000,
        "description": "MB1S1P",
        "discount_amount": 0,
        "entity_id": "MB1S1P",
        "entity_type": "plan",
        "fulfillment_amount": 40000,
        "fulfillment_quantity": 1,
        "id": "o_li__test__KyVnGlSBWllJc264",
        "invoice_id": "__demo_inv__1",
        "invoice_line_item_id": "li___test__KyVnHhSBWll852lZ",
        "is_shippable": true,
        "item_level_discount_amount": 0,
        "object": "order_line_item",
        "refundable_credits": 40000,
        "refundable_credits_issued": 0,
        "status": "queued",
        "tax_amount": 0,
        "unit_price": 40000
      },
      {..}
    ],
    "order_type": "system_generated",
    "paid_on": 1517505884,
    "payment_status": "paid",
    "price_type": "tax_exclusive",
    "refundable_credits": 40000,
    "refundable_credits_issued": 0,
    "resource_version": 1517678683000,
    "rounding_adjustement": 0,
    "shipping_date": 1517505884,
    "status": "cancelled",
    "status_update_at": 1517678683,
    "sub_total": 40000,
    "subscription_id": "__test__KyVnHhSBWll5K2lX",
    "tax": 0,
    "total": 40000,
    "updated_at": 1517678683
  }
}
```

## URL Format

**POST** https://[site].chargebee.com/api/v2/orders/{order-id}/cancel

## Input Parameters

- `cancellation_reason` (required, enumerated string)
  Cancellation reason.
  Possible enum values:
    - `shipping_cut_off_passed`
      The invoice has been paid late and Chargebee cancel's the first order for the invoice.
    - `product_unsatisfactory`
      Product unsatisfactory.
    - `third_party_cancellation`
      Third party cancellation.
    - `product_not_required`
      Product not required.
    - `delivery_date_missed`
      Delivery date missed.
    - `alternative_found`
      Alternative found.
    - `invoice_written_off`
      The invoice has been completely written off. Orders are generated by Chargebee in cancelled state.
    - `invoice_voided`
      The invoice for which the order was createed has been voided.
    - `fraudulent_transaction`
      Fraudulent transaction.
    - `payment_declined`
      Payment declined.
    - `subscription_cancelled`
      The subsctiption for which the order was created has been cancelled.
    - `product_not_available`
      Product not available.
    - `others`
      Other reason
    - `order_resent`
      Order resent

- `customer_notes` (optional, string, max chars=2000)
  The Customer Notes to be filled in the Credit Notes created to capture this refund detail.

- `comment` (optional, string, max chars=300)
  Comment, if any, on the refund.

- `cancelled_at` (optional, timestamp(UTC) in seconds)
  The time at which the order was cancelled.

- `credit_note` (optional, in cents)
  Parameters for credit\_note
  - `total` (optional, in cents, default=0, min=0)
    Credit Note amount in cents.

## Returns

- `order` (Order object)
  Resource object representing order
