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.
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
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!
As the world of eCommerce continues to thrive, Shopify has become one of the most…
Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…
Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…
In today’s digital landscape, e-commerce has become a cornerstone for businesses looking to thrive. Among…
Flutter is a popular UI toolkit that allows developers to create beautiful, natively compiled applications…
The 403 Forbidden error is an everyday issue users may face while browsing the internet.…