<?php

$serviceUrl = "https://mws.amazonservices." . CODICE . "/Orders/2013-09-01";
$config = array(
    'ServiceURL' => $serviceUrl,
    'ProxyHost' => null,
    'ProxyPort' => -1,
    'ProxyUsername' => null,
    'ProxyPassword' => null,
    'MaxErrorRetry' => 3,
);

$service = new MarketplaceWebServiceOrders_Client(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, APPLICATION_NAME, APPLICATION_VERSION, $config);

function getOrderStatus($order_id) {
    global $service, $MERCHANT_ID, $MARKETPLACE_ID;
    $request = new MarketplaceWebServiceOrders_Model_GetOrderRequest();
    $request->setSellerId(MERCHANT_ID);
    $request->setAmazonOrderId($order_id);
    $response = invokeGetOrder($service, $request);
    return $response;
}

function invokeGetOrder(MarketplaceWebServiceOrders_Interface $service, $request) {
    try {
        $response = $service->GetOrder($request);
        $dom = new DOMDocument();
        $dom->loadXML($response->toXML());
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $responseXML = $dom->saveXML();
        $response = new SimpleXMLElement($responseXML);
        return $response;
    } catch (MarketplaceWebServiceOrders_Exception $ex) {
        
    }
}

function listOrder($noOfdays) {
    global $service, $MERCHANT_ID, $MARKETPLACE_ID;
    $request = new MarketplaceWebServiceOrders_Model_ListOrdersRequest();
    $request->setSellerId(MERCHANT_ID);
    // List all orders udpated after a certain date
    $today = date('Y-m-d H:i:s');
    if ($noOfdays == 1) {
        $last15 = date('Y-m-d H:i:s', strtotime($today . ' - ' . $noOfdays . 'day'));
    } else {
        $last15 = date('Y-m-d H:i:s', strtotime($today . ' - ' . $noOfdays . 'days'));
    }
//    $last15 = '2016-08-01 09:20:07';
//    die;
    $createdAfter = new DateTime($last15, new DateTimeZone('UTC'));
    $request->setCreatedAfter($createdAfter->format('Y-m-d'));
    //$request->setCreatedAfter($startDate . "T00:00:00Z");
    //$request->setCreatedBefore($endDate . "T23:59:59Z");
    // Set the marketplaces queried in this ListOrdersRequest
    $request->setMarketplaceId(array(MARKETPLACE_ID));
    // Set the order statuses for this ListOrdersRequest (optional)
    //$request->setOrderStatus(array("Unshipped", "PartiallyShipped", "Shipped"));
    $order = invokeListOrders($service, $request);
    return $order;
}

/**
 * Get List Orders Action Sample
 * Gets competitive pricing and related information for a product identified by
 * the MarketplaceId and ASIN.
 *
 * @param MarketplaceWebServiceOrders_Interface $service instance of MarketplaceWebServiceOrders_Interface
 * @param mixed $request MarketplaceWebServiceOrders_Model_ListOrders or array of parameters
 */
function invokeListOrders(MarketplaceWebServiceOrders_Interface $service, $request) {
    try {
        $response = $service->ListOrders($request);
//        echo ("Service Response\n");
//        echo ("=============================================================================\n");
//        print_r($response);
//        echo '-----------------' . "<br>";
        $dom = new DOMDocument();
        $dom->loadXML($response->toXML());
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $responseXML = $dom->saveXML();
        $response = new SimpleXMLElement($responseXML);
        return $response;
        //echo("ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");
    } catch (MarketplaceWebServiceOrders_Exception $ex) {
//        print_r($ex);
        //echo("Caught Exception: " . $ex->getMessage() . "\n");
        //echo("Response Status Code: " . $ex->getStatusCode() . "\n");
        //echo("Error Code: " . $ex->getErrorCode() . "\n");
        //echo("Error Type: " . $ex->getErrorType() . "\n");
        //echo("Request ID: " . $ex->getRequestId() . "\n");
        //echo("XML: " . $ex->getXML() . "\n");
        //echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
        return array('response' => $ex->getMessage());
    }
}
