简体   繁体   中英

Unable to Create invoice via rest api in magento 2.0

I have the below code to create invoice for order in magento 2.0

<?php
$adminUrl='http://xxxxx/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "xxxxx", "password" => "xxxxx");                                                                    
$data_string = json_encode($data);                       
$ch = curl_init($adminUrl); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
     'Content-Type: application/json',                                                                                
     'Content-Length: ' . strlen($data_string))                                                                       
      );       
$token = curl_exec($ch);
$token=  json_decode($token); 
$headers = array("Authorization: Bearer $token"); 
$requestUrl1='http://xxxxx/index.php/rest/V1/invoices';
$ch1 = curl_init();
$ch1 = curl_init($requestUrl1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
$result1 = curl_exec($ch1);
$result1=  json_decode($result1);
print_r($result1);
?>

It was giving me

{"message":"%fieldName is a required field.","parameters":{"fieldName":"entity"}} 

try to pass Invoice data like this

    $setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);

    $productData='{"entity":{"orderId":6,"items":[{"orderItemId":9,"qty":1},{"orderItemId":10,"qty":1}],"comments":[],"extensionAttributes":{}}}';

    $requestURL= "{magento-url}/rest//V1/invoices";

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $requestURL);

    curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM