> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.transxact.io/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.transxact.io/_mcp/server.

# Cancel a Checkout Session

POST https://api.transxact.io/v1/checkout-sessions/{id}/cancel

Withdraws a `pending` Checkout Session so it can't be paid, e.g. when the order is abandoned or changed. Fails with `payment_in_progress` once the Customer has started paying; wait for the outcome webhook instead. Unpaid sessions also cancel on their own after `expiresAt`, unless the Customer has started paying.

Reference: https://docs.transxact.io/api-reference/checkout-sessions/cancel

## Authentication

- `Authorization` header (bearer token, required) — Your secret key: sk_test_... in Test mode, sk_live_... in Live mode.

## Request

### Path parameters

- `id` (string, required) — Checkout Session identifier.

## Response

### 200

Cancelled; a `checkout_session.cancelled` webhook is sent. Safe to retry: a repeat returns 409 `invalid_state`.

- `id` (string, required) — Checkout Session identifier.
- `status` (enum, required) — `pending` until the Customer pays or the session ends. `succeeded`: paid, safe to fulfil. `failed`: the payment was declined or didn't go through. `cancelled`: you cancelled it or it expired unpaid. Only `pending` ever changes, and each change sends the matching `checkout_session.*` webhook.
  - Allowed values: `pending`, `succeeded`, `failed`, `cancelled`
- `mode` (enum, required) — `test` for a session made with an `sk_test_` key, `live` for an `sk_live_` key. Webhook endpoints each hear one mode, and the payload says which it came from.
  - Allowed values: `test`, `live`
- `amount` (integer, required) — Amount to charge, in FJD cents (minor units).
- `currency` (enum, required) — Always FJD in v1.
  - Allowed values: `FJD`
- `hostedUrl` (string, required) — Transxact-hosted URL to redirect the Customer to.
- `metadata` (map from string to string, required) — Your own key/value data from creation; empty object if none was set.
- `expiresAt` (integer, required) — Unix ms timestamp after which no payment can start; a still-pending session is then cancelled.
- `invoiceNumber` (string, optional) — The Invoice number, such as `INV-0007`, when the session pays one of your Invoices; absent otherwise. Also in the webhook payload.
- `quote` (CheckoutSessionQuote, optional) — Present when you created the session with a `quote`: the USD or AUD price you sent, the FJD rate it was converted at (a decimal string), and the Unix ms time the rate was taken. `amount` is the FJD that is charged. Never shown to the Customer.

## Errors

### 401 Unauthorized Error

Invalid or missing API key.

- `error` (ErrorError, required) — Branch on `code`; `message` is for logs.

### 404 Not Found Error

No such Checkout Session for this Merchant and mode.

- `error` (ErrorError, required) — Branch on `code`; `message` is for logs.

### 409 Conflict Error

`invalid_state` if the session isn't pending; `payment_in_progress` if the Customer has already started paying.

- `error` (ErrorError, required) — Branch on `code`; `message` is for logs.

## Types

### CheckoutSessionQuote

Present when you created the session with a `quote`: the USD or AUD price you sent, the FJD rate it was converted at (a decimal string), and the Unix ms time the rate was taken. `amount` is the FJD that is charged. Never shown to the Customer.

- `amount` (integer, required) — The price you sent, in cents of `currency`.
- `currency` (enum, required) — USD or AUD.
  - Allowed values: `USD`, `AUD`
- `rate` (string, required) — FJD per 1 unit of `currency`, a decimal string.
- `asOf` (integer, required) — Unix ms time the rate was taken.

### ErrorError

Branch on `code`; `message` is for logs.

- `code` (string, required) — Stable machine-readable error code.
- `message` (string, required) — Human-readable detail, not contractual.

## Examples

**Response**

```json
{
  "id": "cs_3f9c2b1a",
  "status": "pending",
  "mode": "test",
  "amount": 5000,
  "currency": "FJD",
  "hostedUrl": "https://transxact.io/c/cs_3f9c2b1a",
  "metadata": {
    "orderId": "9912"
  },
  "expiresAt": 1717086400000,
  "invoiceNumber": "string",
  "quote": {
    "amount": 1,
    "currency": "USD",
    "rate": "string",
    "asOf": 1
  }
}
```

**SDK Code**

```python
import requests

url = "https://api.transxact.io/v1/checkout-sessions/cs_3f9c2b1a/cancel"

headers = {"Authorization": "Bearer <token>"}

response = requests.post(url, headers=headers)

print(response.json())
```

```javascript
const url = 'https://api.transxact.io/v1/checkout-sessions/cs_3f9c2b1a/cancel';
const options = {method: 'POST', headers: {Authorization: 'Bearer <token>'}};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go
package main

import (
	"fmt"
	"net/http"
	"io"
)

func main() {

	url := "https://api.transxact.io/v1/checkout-sessions/cs_3f9c2b1a/cancel"

	req, _ := http.NewRequest("POST", url, nil)

	req.Header.Add("Authorization", "Bearer <token>")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://api.transxact.io/v1/checkout-sessions/cs_3f9c2b1a/cancel")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'

response = http.request(request)
puts response.read_body
```

```java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.transxact.io/v1/checkout-sessions/cs_3f9c2b1a/cancel")
  .header("Authorization", "Bearer <token>")
  .asString();
```

```php
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.transxact.io/v1/checkout-sessions/cs_3f9c2b1a/cancel', [
  'headers' => [
    'Authorization' => 'Bearer <token>',
  ],
]);

echo $response->getBody();
```

```csharp
using RestSharp;

var client = new RestClient("https://api.transxact.io/v1/checkout-sessions/cs_3f9c2b1a/cancel");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer <token>");
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = ["Authorization": "Bearer <token>"]

let request = NSMutableURLRequest(url: NSURL(string: "https://api.transxact.io/v1/checkout-sessions/cs_3f9c2b1a/cancel")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```