fatal-erroramazon-selling-partner-api

ObjectSerializer.php Error when using confirmShipment() from jlevers/selling-partner-api


I am trying to confirm Amazon's orders using confirmShipment() from from jlevers/selling-partner-api as described in the GitHub below but I am getting Fatal error: Uncaught Error: Class 'null' not found /vendor/jlevers/selling-partner-api/lib/ObjectSerializer.php:366

https://github.com/jlevers/selling-partner-api/

I successfully create the $apiInstance and the $payload, but when I execute it I get this error from the ObjectSerializer:

Fatal error: Uncaught Error: Class 'null' not found in /vendor/jlevers/selling-partner-api/lib/ObjectSerializer.php:366 Stack trace: #0 /vendor/jlevers/selling-partner-api/lib/Api/OrdersV0Api.php(92):
 SellingPartnerApi\ObjectSerializer::deserialize(Object(stdClass), 'null', Array) #1 /vendor/jlevers/selling-partner-api/lib/Api/OrdersV0Api.php(55):
 SellingPartnerApi\Api\OrdersV0Api->confirmShipmentWithHttpInfo('114-0297756-279...', Object(SellingPartnerApi\Model\OrdersV0\ConfirmShipmentRequest)) #2 /amazon_settings.php(457):
 SellingPartnerApi\Api\OrdersV0Api->confirmShipment('114-0297756-279...', Object(SellingPartnerApi\Model\OrdersV0\ConfirmShipmentRequest)) #3 /scripts/test.php(65):
 Amazon->fn_confirmShipment('/home/scripts...') #4 {main} thrown in /vendor/jlevers/selling-partner-api/lib/ObjectSerializer.php on line 366

This is what I am using:

$apiInstance = new SellingPartnerApi\Api\OrdersV0Api($config);
$order_id = 'order_id_example'; // string | An Amazon-defined order identifier, in 3-7-7 format (correct order number being passed).
$payload = new \SellingPartnerApi\Model\OrdersV0\ConfirmShipmentRequest(); // \SellingPartnerApi\Model\OrdersV0\ConfirmShipmentRequest | Request body of confirmShipment.

try {
    $apiInstance->confirmShipment($order_id, $payload);
} catch (Exception $e) {
    echo 'Exception when calling OrdersV0Api->confirmShipment: ', $e->getMessage(), PHP_EOL;
}

This is the $payload that ConfirmShipmentRequest generates:

SellingPartnerApi\Model\OrdersV0\ConfirmShipmentRequest Object
(
    [container:protected] => Array
        (
            [package_detail] => Array
                (
                    [package_reference_id] => 2790602
                    [carrier_code] => FedEx
                    [shipping_method] => Ground
                    [tracking_number] => 63828440XXX
                    [ship_date] => 2023-11-07T07:17:07-00:00
                    [order_items] => Array
                        (
                            [order_item_id] => 87699213804801
                            [quantity] => 4
                        )

                )

            [cod_collection_method] => 
            [marketplace_id] => ATVPDKIKX0DER
        )

)

Am I passing the information in the payload incorrectly? The error says Class 'null' not found, but I am passing all the parameters inside $apiInstance->confirmShipment, any help is appreciated

I tried omitting the order_items, also passing them as a nested array


Solution

  • After updating to version 5.10.2 as suggested by Filip I was getting an PackageReferenceId error which later on was addressed by creating new protected containers first then adding the data instead of just passing arrays to them. The code below is a little more detailed than what is available in github:

    require_once(__DIR__ . '/vendor/autoload.php');
        
    use SellingPartnerApi\Api\OrdersV0Api;
    use SellingPartnerApi\Model\OrdersV0\ConfirmShipmentRequest;
    use SellingPartnerApi\Model\OrdersV0\PackageDetail;
    use SellingPartnerApi\Model\OrdersV0\ConfirmShipmentOrderItem;
        
    $order_id = 'your_order_id_here';
    $order_items_list = $this->fn_GetOrderItems($order_id);
    $order_item = $order_items_list->getPayload()->getOrderItems();
                
    $shipment_items_array = array(); 
        
    foreach ($order_item as $item) {
        if( $item['quantity_shipped']> 0) {
            echo "Items in order " . $order_id . " already marked shipped";
            return;
        } else {
            $shipment_items = new ConfirmShipmentOrderItem();
            $shipment_items->setOrderItemId($item->getOrderItemId());
            $shipment_items->setQuantity($item->getQuantityOrdered());
            $shipment_items_array[] = $shipment_items;
        }
    }
        
    $package_details = new PackageDetail();
    $package_details->setCarrierCode('your_carrier_code');       
    $package_details->setCarrierName('your_shipping_carrier');
    $package_details->setTrackingNumber('your_tracking_number');
    $package_details->setShippingMethod('your_shipping_method');
            
    $package_details->setPackageReferenceId('1');
    $package_details->setShipDate(gmdate('Y-m-d\TH:i:s\Z'),strtotime('your_ship_date')); 
    $package_details->setOrderItems($shipment_items_array);
        
    $payload = new ConfirmShipmentRequest();
    $payload ->setPackageDetail($package_details);
    $payload ->setMarketplaceId('your_market_placeid');
        
    $apiInstance = new OrdersV0Api($this->config); //your config settings
        
    try {
        $apiInstance->confirmShipment($order_id, $payload);
        echo "Order " . $order_id . " Fulfilled";
        return; 
    } catch (Exception $e) {
        echo $order_id . ' Exception when calling OrdersV0Api->confirmShipment: ', $e->getMessage(), PHP_EOL;
        return; 
    }