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

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago