Magento Tutorials

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!

Click to rate this post!
[Total: 8 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

23 hours ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

23 hours ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 days ago

How to Choose the Best Shopify Development Company?

In today’s digital landscape, e-commerce has become a cornerstone for businesses looking to thrive. Among…

2 days ago

Flutter | Card Widget

Flutter is a popular UI toolkit that allows developers to create beautiful, natively compiled applications…

3 days ago

Resolving 403 Forbidden Errors in Chrome

The 403 Forbidden error is an everyday issue users may face while browsing the internet.…

3 days ago