Tag Archives: Shipment

Magento 2 API Issue Offline Refund

The following code will issue offline refund and will return creditmemo id.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "apiuser", "password" => "apiuserpwd");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 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);

$requestUrl =$magentoURL.'index.php/rest/V1/order/16/refund/';

$refundData = [
 'items' => array([ "order_item_id" =>28,"qty"=> 2],["order_item_id" =>29,"qty"=> 1]),
 "notify"=>true,
 "arguments"=>["shipping_amount" => 0,"adjustment_positive" => 0,"adjustment_negative" =>0,"extension_attributes" =>[ "return_to_stock_items" =>array(28,29)]]
 ];

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($refundData));
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 $result = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);
 ?>

Magento 2 API Create Shipment

The following code will create shipment for a given order id and return the shipment id.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "apiuser", "password" => "apiuserpwd");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 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);
 
 $requestUrl =$magentoURL.'index.php/rest/V1/order/16/ship/';
 
 $shipingData = [
 'items' => array([ "order_item_id" =>28,"qty"=> 2],["order_item_id" =>29,"qty"=> 2]),
 "notify"=>true,
 "appendComment"=>false
 ];
 
 $ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($shipingData));
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 $result = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);
?>