# Void authorizations before capture

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


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

[Asynchronous](/docs/api/async_response/async-response-object)

Voids all outstanding scheduled-capture authorizations linked to an invoice before capture, then voids or writes off the invoice.

Use this operation when an invoice has an outstanding delayed-capture authorization that must be released before the invoice is either voided or written off. Use `invoice_action` to choose whether the invoice is voided or written off.

### Prerequisites & Constraints

-   The invoice must have at least one outstanding scheduled-capture authorization with a capturable amount greater than zero.
-   The invoice `status` must be `payment_due`, `posted`, or `not_paid`.
-   The invoice must not have successful payments, taxes withheld, applied credit notes, adjustment amounts, refundable credits, or refunds in progress.
-   The outstanding authorization must not already have a successful or in-progress capture.
-   This operation is not supported for 2Checkout.

### Impacts

**

Authorizations

**

-   Chargebee voids all eligible outstanding scheduled-capture authorizations linked to the invoice at the payment gateway.

**

Invoice

**

-   When `invoice_action` is `void`, Chargebee voids the invoice. The invoice `status` becomes `voided`.
-   When `invoice_action` is `write_off`, Chargebee writes off the invoice. The invoice `status` becomes `paid`, and `write_off_amount` is set to the invoice `amount_due`.

**

Credit Note

**

-   When `invoice_action` is `write_off`, Chargebee creates an adjustment credit note for the write-off. The response includes the `credit_note` resource when one is generated.

### Implementation Notes

Before calling this API, ensure that the scheduled-capture authorization is still outstanding. If it has already been captured, if no eligible authorization remains, or if another conflicting payment operation is in progress, the API returns HTTP `409` with `api_error_code` set to `invalid_state_for_request`.

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/invoices/__demo_inv__4/void_before_capture \
     -u {site_api_key}:\
     -d invoice_action="VOID" \
     -d comment="Void outstanding authorization before capture"
```

#### .NET

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

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Invoice.VoidBeforeCapture("__demo_inv__4")
		.InvoiceAction(InvoiceActionEnum.Void)
		.Comment("Void outstanding authorization before capture")
		.Request();

Invoice invoice = result.Invoice;
CreditNote creditNote = result.CreditNote;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    invoiceAction "github.com/chargebee/chargebee-go/v3/actions/invoice"
    "github.com/chargebee/chargebee-go/v3/models/invoice"
    enum "github.com/chargebee/chargebee-go/v3/enum"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := invoiceAction.VoidBeforeCapture("__demo_inv__4", &invoice.VoidBeforeCaptureRequestParams{
        InvoiceAction : enum.InvoiceActionVoid,
        Comment : "Void outstanding authorization before capture",
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        Invoice := res.Invoice
        CreditNote := res.CreditNote
    }
}
```

#### 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.InvoiceVoidBeforeCaptureRequest{
    InvoiceAction : chargebee.InvoiceActionVoid,
    Comment : "Void outstanding authorization before capture",
}
  res, err := client.Invoice.VoidBeforeCapture("__demo_inv__4", req)
      if err != nil {
        fmt.Println(err)
    } else {
        Invoice := res.Invoice
        CreditNote := res.CreditNote
    }
}
```

#### 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 = Invoice.voidBeforeCapture("__demo_inv__4")
            .invoiceAction(InvoiceAction.VOID)
            .comment("Void outstanding authorization before capture")
            .request();

        Invoice invoice = result.invoice();
        CreditNote creditNote = result.creditNote();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.creditNote.CreditNote;
import com.chargebee.v4.models.invoice.Invoice;
import com.chargebee.v4.models.invoice.params.InvoiceVoidBeforeCaptureParams;
import com.chargebee.v4.models.invoice.responses.InvoiceVoidBeforeCaptureResponse;

public class InvoiceVoidBeforeCapture {

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

        InvoiceVoidBeforeCaptureParams params = InvoiceVoidBeforeCaptureParams.builder()
            .invoiceAction(InvoiceVoidBeforeCaptureParams.InvoiceAction.VOID)
            .comment("Void outstanding authorization before capture")
            .build();

        InvoiceVoidBeforeCaptureResponse response = client
            .invoices()
            .voidBeforeCapture("__demo_inv__4", params);

        Invoice invoice = response.getInvoice();
        CreditNote creditNote = response.getCreditNote();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

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

try {
    const result = await chargebee.invoice.voidBeforeCapture("__demo_inv__4", {
        invoice_action: "void",
        comment: "Void outstanding authorization before capture"
    });

    console.log(result);
    const invoice = result.invoice;
    const creditNote = result.credit_note;
} 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->invoice()->voidBeforeCapture("__demo_inv__4", [
    "invoice_action" => "void",
    "comment" => "Void outstanding authorization before capture"
]);
$invoice = $result->invoice;
$creditNote = $result->credit_note;
```

#### Python

```python
import chargebee
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.Invoice.void_before_capture("__demo_inv__4",
    cb_client.Invoice.VoidBeforeCaptureParams(
        invoice_action=chargebee.InvoiceAction.VOID,
        comment="Void outstanding authorization before capture"
    )
)
invoice = response.invoice
credit_note = response.credit_note
```

#### Ruby

```ruby
require 'chargebee'

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

result = ChargeBee::Invoice.void_before_capture("__demo_inv__4",{
  :invoice_action => "VOID",
  :comment => "Void outstanding authorization before capture"
})

invoice = result.invoice
credit_note = result.credit_note
```

## Sample Response

```json
{
  "invoice": {
    "adjustment_credit_notes": {},
    "amount_adjusted": 0,
    "amount_due": 1000,
    "amount_paid": 0,
    "amount_to_collect": 1000,
    "applied_credits": {},
    "base_currency_code": "USD",
    "billing_address": {
      "first_name": "Rachel",
      "last_name": "Green",
      "object": "billing_address",
      "validation_status": "not_validated"
    },
    "credits_applied": 0,
    "currency_code": "USD",
    "customer_id": "__test__8at01SOcUsFk2s",
    "date": 1612797047,
    "deleted": false,
    "due_date": 1612797047,
    "dunning_attempts": [
      {
        "attempt": 0,
        "created_at": 1612797048,
        "dunning_type": "auto_collect",
        "retry_engine": "chargebee",
        "transaction_id": "txn___test__8at01SOcUuIQ3v",
        "txn_amount": 1000,
        "txn_status": "failure"
      },
      {..}
    ],
    "dunning_status": "stopped",
    "exchange_rate": 1,
    "first_invoice": false,
    "has_advance_charges": false,
    "id": "__demo_inv__4",
    "is_gifted": false,
    "issued_credit_notes": {},
    "line_items": [
      {
        "amount": 1000,
        "customer_id": "__test__8at01SOcUsFk2s",
        "date_from": 1612797047,
        "date_to": 1612883447,
        "description": "Basic USD 2",
        "discount_amount": 0,
        "entity_id": "basic-USD2",
        "entity_type": "plan_item_price",
        "id": "li___test__8at01SOcUuFN3u",
        "is_taxed": false,
        "item_level_discount_amount": 0,
        "object": "line_item",
        "pricing_model": "per_unit",
        "quantity": 1,
        "subscription_id": "__test__8at01SOcUsFk2s",
        "tax_amount": 0,
        "tax_exempt_reason": "tax_not_configured",
        "unit_amount": 1000
      },
      {..}
    ],
    "linked_orders": {},
    "linked_payments": [
      {
        "applied_amount": 1000,
        "applied_at": 1612797048,
        "txn_amount": 1000,
        "txn_date": 1612797048,
        "txn_id": "txn___test__8at01SOcUuIQ3v",
        "txn_status": "failure"
      },
      {..}
    ],
    "net_term_days": 0,
    "object": "invoice",
    "price_type": "tax_exclusive",
    "recurring": true,
    "resource_version": 1517490652136,
    "round_off_amount": 0,
    "status": "voided",
    "sub_total": 1000,
    "subscription_id": "__test__8at01SOcUsFk2s",
    "tax": 0,
    "term_finalized": true,
    "total": 1000,
    "updated_at": 1517490652,
    "voided_at": 1517490652,
    "write_off_amount": 0
  }
}
```

## URL Format

**POST** https://[site].chargebee.com/api/v2/invoices/{invoice-id}/void_before_capture

## Input Parameters

- `comment` (optional, string, max chars=300)
  An internal [comment](/docs/api/comments) to be added for this operation, to the invoice. This comment is displayed on the Chargebee UI. It is not displayed on any customer-facing [Hosted Page](/docs/api/hosted_pages) or any document such as the [Invoice PDF](/docs/api/invoices/retrieve-invoice-as-pdf) .

- `void_reason_code` (optional, string, max chars=100)
  Reason code for voiding the invoice. Applicable only when `invoice_action` is `void`. Select from the reason codes configured in **Settings > Configure Chargebee > Reason Codes > Invoices > Void invoice**. This parameter is required when a void reason code is configured as mandatory. The codes are case-sensitive.

- `invoice_action` (optional, enumerated string)
  Determines whether Chargebee voids or writes off the invoice after voiding all eligible outstanding scheduled-capture authorizations. Possible values are `void` and `write_off`. This is not related to [Close a pending invoice](/docs/api/invoices#close_a_pending_invoice).
  
  **Default value**
  
  `void`
  Possible enum values:
    - `void`
      Voids the invoice after all eligible outstanding scheduled-capture authorizations are voided.
    - `write_off`
      Writes off the invoice after all eligible outstanding scheduled-capture authorizations are voided. `void_reason_code` is not applicable for this value.

## Returns

- `invoice` (Invoice object)
  The updated invoice.

- `credit_note` (Credit note object)
  The adjustment credit note generated when the invoice is written off. This resource is returned only when a credit note is generated.
