How to Call API to Create a Full Invoice in Magento 2

Hello Magento Friends,

Today I am going to explain How to Call API to Create a Full Invoice in Magento 2.

When the order is placed and the payment is received, the store owner can generate an invoice for the order. In Magento 2 you can create an invoice of an order using the third-party platform by calling an API.

Steps to Call API to Create a Full Invoice in Magento 2:

To create a partial Invoice, specify only those order_item_ids that are to be Invoice now.

If the call is successful on a full Invoice, Magento changes the status of an order to Processing.

Endpoint:

POST https://domain.com/rest/<store_code>/V1/order/1/invoice

// here 1 is Your Order Id and store_code (optional)

Headers:

Content-Type: application/json

Authorization: Bearer <administrator token>

Payload:

{
  "items": [
    {
      "order_item_id": 1,
      "qty": 1
    }
  ],
  "notify": true,
  "appendComment": true,
  "comment": {
    "comment": "Order Invoice Generated"
  }
}

Response:

The Invoice ID, such as 1.

Verify this step

From Magento Admin, click Sales > Invoices. Invoices for this order are shown in the grid.

Create Invoice Example for Invoices using  REST API

Create invoice.php in your Magento root path after that add the below code.

<?php
$admindata = array("username" => "admin", "password" => "admin@123");
$ch = curl_init("https://domain.com/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($admindata));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen(json_encode($admindata))));
$token = curl_exec($ch);
curl_close($ch);
echo "Token :".$token;

$ch = curl_init();
if (!$ch)
{
    return "Couldn't initialize a cURL handle";
}
$data = '{
  "items": [
    {
      "order_item_id": 1,
      "qty": 1
    }
  ]
}';

$host= "https://domain.com/rest/V1/order/1/invoice";

curl_setopt($ch, CURLOPT_URL,$host);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$curlresponse = curl_exec($ch);
print_r(curl_error($curlresponse));
$responseData=json_decode($curlresponse);
echo "<pre>";
print_r($responseData);
echo "</pre>";

After adding the above code run the below URL to check invoice created or not.

https://domain.com/invoice.php

Conclusion:

Hence, this way you can Call API to Create a Full Invoice in Magento 2.

Check out other related blogs – 

If you have any questions let me know in the comment box. I will be prompt to answer. Share the article with your friends and colleagues. See you in the next article.

Happy Reading!

Previous Article

Big News: Get ready for Magento 2.4.4 Beta Releases [Updated]

Next Article

How to Add Infinite Scroll and “Back to Top” Button in Magento 2 

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨