Magento 2 API: Create an Invoice

An invoice is created after you receive payment for a specific order. It’s like a confirmation that a purchase has been made and your customers also have all the information about what they’ve bought. In Magento 2, you can create an invoice via API easily. In this post, I will guide you on how to do that with a particular example. In the below example, the order was paid via a bank transfer. Because of this, you have to let Magento know that the order has been captured already.

In today’s article, I will show you how to call API to create a full invoice in Magento 2.

Create an Invoice in 3 steps:

Step 1: Capture payment

By adding an array of items to the payload to be invoiced, you will be able to create a partial invoice.

Endpoint

Here, the orderid is 3

POST <host>/rest/<store_code>/V1/order/3/invoice

Headers

Content-Type application/json

Authorization Bearer <administrator token>

Payload

{
  "capture": true,
  "notify": true
}

Response

The invoice id, for example, 3.

Step 2: View the invoice

In general, the structure of an invoice is similar to order. The only difference is that the order contains more details.

Endpoint

GET <host>/rest/<store_code>/V1/invoices/3

Headers

Content-Type application/json

Authorization Bearer <administrator token>

Payload

Irrelevant

Response

The order_item_id values will be used to create a shipment in the following step.

Code sample

{
    "base_currency_code": "USD",
    "base_discount_amount": 0,
    "base_grand_total": 165,
    "base_discount_tax_compensation_amount": 0,
    "base_shipping_amount": 5,
    "base_shipping_incl_tax": 5,
    "base_shipping_tax_amount": 0,
    "base_subtotal": 160,
    "base_subtotal_incl_tax": 160,
    "base_tax_amount": 0,
    "base_to_global_rate": 1,
    "base_to_order_rate": 1,
    "billing_address_id": 6,
    "can_void_flag": 0,
    "created_at": "2017-08-21 22:36:02",
    "discount_amount": 0,
    "email_sent": 1,
    "entity_id": 3,
    "global_currency_code": "USD",
    "grand_total": 165,
    "discount_tax_compensation_amount": 0,
    "increment_id": "000000003",
    "order_currency_code": "USD",
    "order_id": 3,
    "shipping_address_id": 5,
    "shipping_amount": 5,
    "shipping_discount_tax_compensation_amount": 0,
    "shipping_incl_tax": 5,
    "shipping_tax_amount": 0,
    "state": 2,
    "store_currency_code": "USD",
    "store_id": 1,
    "store_to_base_rate": 0,
    "store_to_order_rate": 0,
    "subtotal": 160,
    "subtotal_incl_tax": 160,
    "tax_amount": 0,
    "total_qty": 9,
    "updated_at": "2017-08-21 22:36:03",
    "items": [
        {
            "base_discount_tax_compensation_amount": 0,
            "base_price": 22,
            "base_price_incl_tax": 22,
            "base_row_total": 22,
            "base_row_total_incl_tax": 22,
            "base_tax_amount": 0,
            "entity_id": 3,
            "discount_tax_compensation_amount": 0,
            "name": "Radiant Tee-M-Orange",
            "parent_id": 3,
            "price": 22,
            "price_incl_tax": 22,
            "product_id": 1553,
            "row_total": 22,
            "row_total_incl_tax": 22,
            "sku": "WS12-M-Orange",
            "tax_amount": 0,
            "order_item_id": 3,
            "qty": 1
        },
        {
            "base_discount_tax_compensation_amount": 0,
            "base_price": 18,
            "base_price_incl_tax": 18,
            "base_row_total": 18,
            "base_row_total_incl_tax": 18,
            "base_tax_amount": 0,
            "entity_id": 4,
            "discount_tax_compensation_amount": 0,
            "name": "Advanced Pilates & Yoga (Strength)",
            "parent_id": 3,
            "price": 18,
            "price_incl_tax": 18,
            "product_id": 49,
            "row_total": 18,
            "row_total_incl_tax": 18,
            "sku": "240-LV08",
            "tax_amount": 0,
            "order_item_id": 4,
            "qty": 1
        },
        {
            "base_price": 68,
            "base_price_incl_tax": 68,
            "entity_id": 5,
            "name": "Sprite Yoga Companion Kit",
            "parent_id": 3,
            "price": 68,
            "price_incl_tax": 68,
            "product_id": 51,
            "sku": "24-WG080-24-WG084-24-WG088-24-WG082-blue-24-WG086",
            "order_item_id": 5,
            "qty": 1
        },
        {
            "base_discount_tax_compensation_amount": 0,
            "base_price": 27,
            "base_price_incl_tax": 27,
            "base_row_total": 27,
            "base_row_total_incl_tax": 27,
            "base_tax_amount": 0,
            "entity_id": 6,
            "discount_tax_compensation_amount": 0,
            "name": "Sprite Stasis Ball 65 cm",
            "parent_id": 3,
            "price": 27,
            "price_incl_tax": 27,
            "product_id": 29,
            "row_total": 27,
            "row_total_incl_tax": 27,
            "sku": "24-WG082-blue",
            "tax_amount": 0,
            "order_item_id": 6,
            "qty": 1
        },
        {
            "base_discount_tax_compensation_amount": 0,
            "base_price": 5,
            "base_price_incl_tax": 5,
            "base_row_total": 5,
            "base_row_total_incl_tax": 5,
            "base_tax_amount": 0,
            "entity_id": 7,
            "discount_tax_compensation_amount": 0,
            "name": "Sprite Foam Yoga Brick",
            "parent_id": 3,
            "price": 5,
            "price_incl_tax": 5,
            "product_id": 21,
            "row_total": 5,
            "row_total_incl_tax": 5,
            "sku": "24-WG084",
            "tax_amount": 0,
            "order_item_id": 7,
            "qty": 1
        },
        {
            "base_discount_tax_compensation_amount": 0,
            "base_price": 17,
            "base_price_incl_tax": 17,
            "base_row_total": 17,
            "base_row_total_incl_tax": 17,
            "base_tax_amount": 0,
            "entity_id": 8,
            "discount_tax_compensation_amount": 0,
            "name": "Sprite Yoga Strap 8 foot",
            "parent_id": 3,
            "price": 17,
            "price_incl_tax": 17,
            "product_id": 34,
            "row_total": 17,
            "row_total_incl_tax": 17,
            "sku": "24-WG086",
            "tax_amount": 0,
            "order_item_id": 8,
            "qty": 1
        },
        {
            "base_discount_tax_compensation_amount": 0,
            "base_price": 19,
            "base_price_incl_tax": 19,
            "base_row_total": 19,
            "base_row_total_incl_tax": 19,
            "base_tax_amount": 0,
            "entity_id": 9,
            "discount_tax_compensation_amount": 0,
            "name": "Sprite Foam Roller",
            "parent_id": 3,
            "price": 19,
            "price_incl_tax": 19,
            "product_id": 22,
            "row_total": 19,
            "row_total_incl_tax": 19,
            "sku": "24-WG088",
            "tax_amount": 0,
            "order_item_id": 9,
            "qty": 1
        },
        {
            "base_discount_tax_compensation_amount": 0,
            "base_price": 52,
            "base_price_incl_tax": 52,
            "base_row_total": 52,
            "base_row_total_incl_tax": 52,
            "base_tax_amount": 0,
            "entity_id": 10,
            "discount_tax_compensation_amount": 0,
            "name": "Chaz Kangeroo Hoodie",
            "parent_id": 3,
            "price": 52,
            "price_incl_tax": 52,
            "product_id": 67,
            "row_total": 52,
            "row_total_incl_tax": 52,
            "sku": "MH01-S-Gray",
            "tax_amount": 0,
            "order_item_id": 10,
            "qty": 1
        },
        {
            "base_price": 0,
            "entity_id": 11,
            "name": "Chaz Kangeroo Hoodie-S-Gray",
            "parent_id": 3,
            "price": 0,
            "product_id": 56,
            "sku": "MH01-S-Gray",
            "order_item_id": 11,
            "qty": 1
        }
    ],
    "comments": []
}

Step 3: Verify this step

In this step, you need to log in to Admin. Then go to Sales > Invoices. In the grid, you will see the invoice is displayed. The status should be Paid.

Next, click Sales > Orders, the status will be Processing.

Conclusion

In the above information, I have just shown you how to call API to create a full invoice in Magento 2. Hope it’s helpful for you when you need to create an invoice. If you have any questions or new ideas, feel free to leave a comment below.

Thanks for reading!

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.
x