# Close a pending invoice

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


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

Invoices for a subscription are created with a `pending` `status` when the subscription has `create_pending_invoices` attribute set to `true`. This API call finalizes a `pending` invoice. Any `refundable_credits` and `excess_payments` for the customer are applied to the invoice, and any payment due is collected automatically if `auto_collection` is `on` for the customer.

#### Automation[](#automation)

This operation can be automated by using a [site setting](https://www.chargebee.com/docs/billing/2.0/usage-based-billing/metered_billing#configuring-metered-billing). Moreover, the automation can be overridden at the [customer](/docs/api/customers/customer-object#auto_close_invoices) and [subscription](/docs/api/subscriptions/subscription-object#auto_close_invoices) level.

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/invoices/__demo_inv__8/close \
     -X POST  \
     -u {site_api_key}:
```

#### .NET

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

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Invoice.Close("__demo_inv__8").Request();

Invoice invoice = result.Invoice;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    invoiceAction "github.com/chargebee/chargebee-go/v3/actions/invoice"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := invoiceAction.Close("__demo_inv__8", nil).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        Invoice := res.Invoice
    }
}
```

#### 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.InvoiceCloseRequest{}
  res, err := client.Invoice.Close("__demo_inv__8", req)
      if err != nil {
        fmt.Println(err)
    } else {
        Invoice := res.Invoice
    }
}
```

#### 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.close("__demo_inv__8").request();

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

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.invoice.Invoice;
import com.chargebee.v4.models.invoice.params.InvoiceCloseParams;
import com.chargebee.v4.models.invoice.responses.InvoiceCloseResponse;

public class InvoiceClose {

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

        InvoiceCloseResponse response = client.invoices().close("__demo_inv__8");

        Invoice invoice = response.getInvoice();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

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

try {
    const result = await chargebee.invoice.close("__demo_inv__8");

    console.log(result);
    const invoice = result.invoice;
} 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()->close("__demo_inv__8");
$invoice = $result->invoice;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.Invoice.close("__demo_inv__8")
invoice = response.invoice
```

#### Ruby

```ruby
require 'chargebee'

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

result = ChargeBee::Invoice.close("__demo_inv__8")

invoice = result.invoice
```

## Sample Response

```json
{
  "invoice": {
    "adjustment_credit_notes": {},
    "amount_adjusted": 0,
    "amount_due": 500,
    "amount_paid": 0,
    "amount_to_collect": 500,
    "applied_credits": [
      {
        "applied_amount": 1000,
        "applied_at": 1517490267,
        "cn_create_reason_code": "Subscription Change",
        "cn_date": 1517490267,
        "cn_id": "__demo_cn__2",
        "cn_reason_code": "subscription_change",
        "cn_status": "refunded"
      },
      {..}
    ],
    "base_currency_code": "USD",
    "billing_address": {
      "first_name": "John",
      "last_name": "Mathew",
      "object": "billing_address",
      "validation_status": "not_validated"
    },
    "credits_applied": 1000,
    "currency_code": "USD",
    "customer_id": "__test__8asyKSOcTHxf1V",
    "date": 1517490267,
    "deleted": false,
    "due_date": 1517490268,
    "dunning_attempts": {},
    "exchange_rate": 1,
    "first_invoice": false,
    "has_advance_charges": false,
    "id": "__demo_inv__8",
    "is_gifted": false,
    "issued_credit_notes": {},
    "line_items": [
      {
        "amount": 1500,
        "customer_id": "__test__8asyKSOcTHxf1V",
        "date_from": 1517490267,
        "date_to": 1519909466,
        "description": "Standard USD - Prorated Charges",
        "discount_amount": 0,
        "entity_id": "standard-USD",
        "entity_type": "plan_item_price",
        "id": "li___test__8asyKSOcTIRM1m",
        "is_taxed": false,
        "item_level_discount_amount": 0,
        "object": "line_item",
        "pricing_model": "per_unit",
        "quantity": 1,
        "subscription_id": "__test__8asyKSOcTI3k1c",
        "tax_amount": 0,
        "tax_exempt_reason": "tax_not_configured",
        "unit_amount": 1500
      },
      {..}
    ],
    "linked_orders": {},
    "linked_payments": {},
    "net_term_days": 0,
    "next_retry_at": 1517490273,
    "object": "invoice",
    "price_type": "tax_exclusive",
    "recurring": true,
    "resource_version": 1517490267583,
    "round_off_amount": 0,
    "status": "payment_due",
    "sub_total": 1500,
    "subscription_id": "__test__8asyKSOcTI3k1c",
    "tax": 0,
    "term_finalized": true,
    "total": 1500,
    "updated_at": 1517490267,
    "write_off_amount": 0
  }
}
```

## URL Format

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

## 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) .

- `invoice_note` (optional, string, max chars=2000)
  A note for this particular invoice. This, and [all other notes](/docs/api/invoices/invoice-object#notes) for the invoice are displayed on the PDF invoice sent to the customer.

- `remove_general_note` (optional, boolean, default=false)
  Set as `true` to remove the [**general note**](https://www.chargebee.com/docs/invoice_notes.html#adding-general-notes) from this invoice.

- `invoice_date` (optional, timestamp(UTC) in seconds)
  Set the [invoice date](/docs/api/invoices/invoice-object#date). Must lie between the date when the invoice was generated and current date. Can only be passed when the site setting to allow overriding is enabled. If not passed, then the default value [set at the site level](https://www.chargebee.com/docs/metered_billing.html#overview) is used.

- `notes_to_remove` (optional, array)
  Parameters for notes\_to\_remove
  - `entity_type` (optional, enumerated string)
    Type of entity to which the [note](/docs/api/invoices/invoice-object#notes) belongs. To remove the general note, use the `remove_general_note` parameter.
    Possible enum values:
      - `customer`
        Entity that represents a customer.
      - `subscription`
        Entity that represents a subscription of customer.
      - `coupon`
        Entity that represents a coupon.
      - `plan_item_price`
        Indicates that this line item is based on plan Item Price
      - `addon_item_price`
        Indicates that this line item is based on addon Item Price
      - `charge_item_price`
        Indicates that this line item is based on charge Item Price
  - `entity_id` (optional, string, max chars=100)
    Unique identifier of the [note](/docs/api/invoices/invoice-object#notes) .

## Returns

- `invoice` (Invoice object)
  Resource object representing invoice
