# Collect payment for an invoice

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


[Idempotency Supported](/docs/api/v2/pcv-1/idempotency)

**Important:**

-   Storing card after successful 3DS completion is not supported in this API. Use [create using Payment Intent API](/docs/api/payment_sources/create-using-payment-intent) under Payment source to store the card after successful 3DS flow completion.
-   This endpoint returns an error if a payment is initiated for an invoice with a [status](/docs/api/invoices/invoice-object#status) of `payment_due` associated with the `app_store` and `play_store` [channels](/docs/api/invoices/invoice-object#channel).

This API is used to collect payments for `payment_due` and `not_paid invoices`. If no payment methods are present for the customer or if the payment is unsuccessful, the corresponding error will be thrown. Pass `authorization_transaction_id` to capture the already blocked funds to collect payments. Note that if the invoice due amount is greater than the authorized amount, the invoice status is returned as `payment_due`.

The authorization transaction will not be captured if the fraud status is found as suspicious. This api will result in invalid\_state\_for\_request error. Read more on [fraud management using Stripe Radar](https://www.chargebee.com/docs/stripe-radar.html) .

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/invoices/__demo_inv__12/collect_payment \
     -u {site_api_key}:\
     -d payment_source_id="pm___test__XpbBxQiS4HD14TWv"
```

#### .NET

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

ApiConfig.Configure("{site}","{site_api_key}");
EntityResult result = Invoice.CollectPayment("__demo_inv__12")
		.PaymentSourceId("pm___test__XpbBxQiS4HD14TWv")
		.Request();

Invoice invoice = result.Invoice;
Transaction transaction = result.Transaction;
```

#### 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"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := invoiceAction.CollectPayment("__demo_inv__12", &invoice.CollectPaymentRequestParams{
        PaymentSourceId : "pm___test__XpbBxQiS4HD14TWv",
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        Invoice := res.Invoice
        Transaction := res.Transaction
    }
}
```

#### 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.InvoiceCollectPaymentRequest{
    PaymentSourceId : "pm___test__XpbBxQiS4HD14TWv",
}
  res, err := client.Invoice.CollectPayment("__demo_inv__12", req)
      if err != nil {
        fmt.Println(err)
    } else {
        Invoice := res.Invoice
        Transaction := res.Transaction
    }
}
```

#### 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.collectPayment("__demo_inv__12")
            .paymentSourceId("pm___test__XpbBxQiS4HD14TWv")
            .request();

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

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.invoice.Invoice;
import com.chargebee.v4.models.invoice.params.InvoiceCollectPaymentParams;
import com.chargebee.v4.models.invoice.responses.InvoiceCollectPaymentResponse;
import com.chargebee.v4.models.transaction.Transaction;

public class InvoiceCollectPayment {

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

        InvoiceCollectPaymentParams params = InvoiceCollectPaymentParams.builder()
            .paymentSourceId("pm___test__XpbBxQiS4HD14TWv")
            .build();

        InvoiceCollectPaymentResponse response = client
            .invoices()
            .collectPayment("__demo_inv__12", params);

        Invoice invoice = response.getInvoice();
        Transaction transaction = response.getTransaction();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

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

try {
    const result = await chargebee.invoice.collectPayment("__demo_inv__12", {
        payment_source_id: "pm___test__XpbBxQiS4HD14TWv"
    });

    console.log(result);
    const invoice = result.invoice;
    const transaction = result.transaction;
} 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()->collectPayment("__demo_inv__12", [
    "payment_source_id" => "pm___test__XpbBxQiS4HD14TWv"
]);
$invoice = $result->invoice;
$transaction = $result->transaction;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.Invoice.collect_payment("__demo_inv__12",
    cb_client.Invoice.CollectPaymentParams(
        payment_source_id="pm___test__XpbBxQiS4HD14TWv"
    )
)
invoice = response.invoice
transaction = response.transaction
```

#### Ruby

```ruby
require 'chargebee'

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

result = ChargeBee::Invoice.collect_payment("__demo_inv__12",{
  :payment_source_id => "pm___test__XpbBxQiS4HD14TWv"
})

invoice = result.invoice
transaction = result.transaction
```

## Sample Response

```json
{
  "invoice": {
    "adjustment_credit_notes": {},
    "amount_adjusted": 0,
    "amount_due": 0,
    "amount_paid": 895,
    "amount_to_collect": 0,
    "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__XpbBxViS4HD0mk3u",
    "date": 1594073426,
    "deleted": false,
    "due_date": 1594073426,
    "dunning_attempts": [
      {
        "attempt": 0,
        "created_at": 1594073427,
        "dunning_type": "auto_collect",
        "retry_engine": "chargebee",
        "transaction_id": "txn___test__XpbBxViS4HD0xF4S",
        "txn_amount": 895,
        "txn_status": "failure"
      },
      {..}
    ],
    "dunning_status": "stopped",
    "exchange_rate": 1,
    "first_invoice": false,
    "has_advance_charges": false,
    "id": "__demo_inv__12",
    "is_gifted": false,
    "issued_credit_notes": {},
    "line_items": [
      {
        "amount": 895,
        "customer_id": "__test__XpbBxViS4HD0mk3u",
        "date_from": 1594073426,
        "date_to": 1596751826,
        "description": "No Trial",
        "discount_amount": 0,
        "entity_id": "no_trial",
        "entity_type": "plan",
        "id": "li___test__XpbBxViS4HD0wq4R",
        "is_taxed": false,
        "item_level_discount_amount": 0,
        "object": "line_item",
        "pricing_model": "per_unit",
        "quantity": 1,
        "subscription_id": "__test__XpbBxViS4HD0mk3u",
        "tax_amount": 0,
        "tax_exempt_reason": "tax_not_configured",
        "unit_amount": 895
      },
      {..}
    ],
    "linked_orders": {},
    "linked_payments": [
      {
        "applied_amount": 895,
        "applied_at": 1517429427,
        "txn_amount": 895,
        "txn_date": 1517429427,
        "txn_id": "txn___test__XpbBxQiS4HD15WX1",
        "txn_status": "success"
      },
      {..}
    ],
    "net_term_days": 0,
    "object": "invoice",
    "paid_at": 1517429427,
    "price_type": "tax_exclusive",
    "recurring": true,
    "resource_version": 1517429427000,
    "round_off_amount": 0,
    "status": "paid",
    "sub_total": 895,
    "subscription_id": "__test__XpbBxViS4HD0mk3u",
    "tax": 0,
    "term_finalized": true,
    "total": 895,
    "updated_at": 1517429427,
    "write_off_amount": 0
  },
  "transaction": {
    "amount": 895,
    "amount_unused": 0,
    "base_currency_code": "USD",
    "currency_code": "USD",
    "customer_id": "__test__XpbBxViS4HD0mk3u",
    "date": 1517429427,
    "deleted": false,
    "exchange_rate": 1,
    "gateway": "chargebee",
    "gateway_account_id": "gw___test__XpbBxViS4HCz991g",
    "id": "txn___test__XpbBxQiS4HD15WX1",
    "id_at_gateway": "cb___test__XpbBxQiS4HD15YX2",
    "linked_invoices": [
      {
        "applied_amount": 895,
        "applied_at": 1517429427,
        "invoice_date": 1594073426,
        "invoice_id": "__demo_inv__12",
        "invoice_status": "paid",
        "invoice_total": 895
      },
      {..}
    ],
    "linked_refunds": {},
    "masked_card_number": "***********0005",
    "object": "transaction",
    "payment_method": "card",
    "payment_source_id": "pm___test__XpbBxQiS4HD14TWv",
    "resource_version": 1517429427000,
    "status": "success",
    "subscription_id": "__test__XpbBxViS4HD0mk3u",
    "type": "payment",
    "updated_at": 1517429427
  }
}
```

## URL Format

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

## Input Parameters

- `amount` (optional, in cents, min=1)
  Amount to be collected. If this parameter is not passed then the entire amount due will be collected.

- `authorization_transaction_id` (optional, string, max chars=40)
  Authorization transaction to be captured.

- `payment_source_id` (optional, string, max chars=40)
  Payment source to be used for this payment.

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

- `payment_initiator` (optional, enumerated string)
  The type of initiator to be used for the payment request triggered by this operation.
  Possible enum values:
    - `customer`
      Pass this value to indicate that the request is initiated by the customer
    - `merchant`
      Pass this value to indicate that the request is initiated by the merchant

## Returns

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

- `transaction` (Transaction object)
  Resource object representing transaction
