Example #1
0
 function invokeGetReportList(MarketplaceWebService_Interface $service, $request)
 {
     try {
         $response = $service->getReportList($request);
         if ($response->isSetGetReportListResult()) {
             $getReportListResult = $response->getGetReportListResult();
             $reportInfoList = $getReportListResult->getReportInfoList();
             foreach ($reportInfoList as $reportInfo) {
                 if ($reportInfo->isSetReportType() && $reportInfo->getReportType() == '_GET_ORDERS_DATA_' && $reportInfo->isSetReportId()) {
                     return $reportInfo->getReportId();
                 }
             }
         } else {
             return false;
         }
     } catch (MarketplaceWebService_Exception $ex) {
         $message = 'MWS Report API : Caught Exception : ' . $ex->getMessage() . "\n";
         $message .= "Response Status Code: " . $ex->getStatusCode() . "\n";
         $message .= "Error Code: " . $ex->getErrorCode() . "\n";
         $message .= "Error Type: " . $ex->getErrorType() . "\n";
         $param['message'] = $message;
         $obj = new Pwapresta();
         $obj->generate_log($param);
     }
 }
Example #2
0
 function invokeGetReport(MarketplaceWebService_Interface $service, $request)
 {
     try {
         $response = $service->getReport($request);
         return stream_get_contents($request->getReport());
     } catch (MarketplaceWebService_Exception $ex) {
         $message = 'MWS Report API : Caught Exception : ' . $ex->getMessage() . "\n";
         $message .= "Response Status Code: " . $ex->getStatusCode() . "\n";
         $message .= "Error Code: " . $ex->getErrorCode() . "\n";
         $message .= "Error Type: " . $ex->getErrorType() . "\n";
         $param['message'] = $message;
         $obj = new Pwapresta();
         $obj->generate_log($param);
     }
 }
Example #3
0
 function invokeListOrders(MarketplaceWebServiceOrders_Interface $service, $request)
 {
     try {
         $response = $service->ListOrders($request);
         $dom = new DOMDocument();
         $dom->loadXML($response->toXML());
         $dom->preserveWhiteSpace = false;
         $dom->formatOutput = true;
         $xml = $dom->saveXML();
         $this->update_order($xml);
     } catch (MarketplaceWebServiceOrders_Exception $ex) {
         $message = 'MWS Order API : Caught Exception : ' . $ex->getMessage() . "\n";
         $message .= "Response Status Code: " . $ex->getStatusCode() . "\n";
         $message .= "Error Code: " . $ex->getErrorCode() . "\n";
         $message .= "Error Type: " . $ex->getErrorType() . "\n";
         $param['message'] = $message;
         $obj = new Pwapresta();
         $obj->generate_log($param);
     }
 }
 function invokeManageReportSchedule(MarketplaceWebService_Interface $service, $request)
 {
     try {
         $response = $service->manageReportSchedule($request);
         echo "        ManageReportScheduleResponse\n";
         if ($response->isSetManageReportScheduleResult()) {
             echo "            ManageReportScheduleResult\n";
             $manageReportScheduleResult = $response->getManageReportScheduleResult();
             if ($manageReportScheduleResult->isSetCount()) {
                 echo "                Count\n";
                 echo "                    " . $manageReportScheduleResult->getCount() . "\n";
             }
             $reportScheduleList = $manageReportScheduleResult->getReportScheduleList();
             foreach ($reportScheduleList as $reportSchedule) {
                 echo "                ReportSchedule\n";
                 if ($reportSchedule->isSetReportType()) {
                     echo "                    ReportType\n";
                     echo "                        " . $reportSchedule->getReportType() . "\n";
                 }
                 if ($reportSchedule->isSetSchedule()) {
                     echo "                    Schedule\n";
                     echo "                        " . $reportSchedule->getSchedule() . "\n";
                 }
                 if ($reportSchedule->isSetScheduledDate()) {
                     echo "                    ScheduledDate\n";
                     echo "                        " . $reportSchedule->getScheduledDate()->format(DATE_FORMAT) . "\n";
                 }
             }
         }
     } catch (MarketplaceWebService_Exception $ex) {
         $message = 'MWS Report API : Caught Exception : ' . $ex->getMessage() . "\n";
         $message .= "Response Status Code: " . $ex->getStatusCode() . "\n";
         $message .= "Error Code: " . $ex->getErrorCode() . "\n";
         $message .= "Error Type: " . $ex->getErrorType() . "\n";
         $param['message'] = $message;
         $obj = new Pwapresta();
         $obj->generate_log($param);
         echo $message;
     }
 }
Example #5
0
 public function pwa_iopn($data)
 {
     include_once 'iopn/pwa-iopn.php';
     if (Configuration::get('PWAPRESTA_PWAPRESTA_ORDER_UPDATE_API') == 'IOPN') {
         if (!empty($data)) {
             $param['message'] = 'IOPN Notifications : IOPN function called with some POST data.';
             $obj = new Pwapresta();
             $obj->generate_log($param);
             $data1 = json_encode($data);
             $filename = '1_iopn_non';
             $myfile = fopen($filename, "w");
             fwrite($myfile, $data1);
             fclose($myfile);
             $iopn = new PWA_Iopn();
             $iopn->notifications($data);
         } else {
             $param['message'] = 'IOPN Notifications : IOPN function called without POST data.';
             $obj = new Pwapresta();
             $obj->generate_log($param);
         }
     }
     exit;
 }
Example #6
0
 public function notifications($param)
 {
     $prefix = _DB_PREFIX_;
     try {
         if (isset($param['UUID']) && $param['UUID'] != '') {
             $uuid = urldecode($param['UUID']);
         } else {
             $uuid = '';
         }
         if (isset($param['Timestamp']) && $param['Timestamp'] != '') {
             $timestamp = urldecode($param['Timestamp']);
         } else {
             $timestamp = '';
         }
         if (isset($param['Signature']) && $param['Signature'] != '') {
             $Signature = str_replace(' ', '+', urldecode($param['Signature']));
         } else {
             $Signature = '';
         }
         if (isset($param['AWSAccessKeyId']) && $param['AWSAccessKeyId'] != '') {
             $AWSAccessKeyId = urldecode($param['AWSAccessKeyId']);
         } else {
             $AWSAccessKeyId = '';
         }
         $NotificationType = urldecode($param['NotificationType']);
         $NotificationData = stripslashes(urldecode($param['NotificationData']));
         if ($uuid != '') {
             $sql = 'INSERT into `' . $prefix . 'pwa_iopn_records` (`uuid`,`timestamp`,`notification_type`) VALUES("' . $uuid . '" , "' . $timestamp . '" , "' . $NotificationType . '") ';
             Db::getInstance()->Execute($sql);
             $iopn_record_id = Db::getInstance()->Insert_ID();
         }
         // Verify that the notification request is valid by verifying the Signature
         $concatenate = $uuid . $timestamp;
         $secretKeyID = Configuration::get('PWAPRESTA_PWAPRESTA_SECRET_KEY');
         $calculator = new SignatureCalculator();
         $generatedSignature = $calculator->calculateRFC2104HMAC($concatenate, $secretKeyID);
         if ($Signature != '' && $Signature == $generatedSignature || $Signature == '') {
             // Verify the Timestamp
             //$this->time_difference($timestamp) > 15
             if (1) {
                 if ($NotificationType == 'NewOrderNotification') {
                     $new_order = new NewOrderNotification();
                     $new_order->update_order($NotificationData, $iopn_record_id);
                 }
                 if ($NotificationType == 'OrderReadyToShipNotification') {
                     if ($Signature == '') {
                         $xml = simplexml_load_string($NotificationData);
                         $AmazonOrderID = (string) $xml->ProcessedOrder->AmazonOrderID;
                         $obj = new Pwapresta();
                         if ($obj->pwa_order_exist($AmazonOrderID)) {
                             $confirm_order = new OrderReadyToShipNotification();
                             $confirm_order->update_order_status($NotificationData, $iopn_record_id);
                             header('HTTP/1.1 200 OK');
                         } else {
                             echo 'Sorry! it seems that this order is a fake order.';
                         }
                     } else {
                         $confirm_order = new OrderReadyToShipNotification();
                         $confirm_order->update_order_status($NotificationData, $iopn_record_id);
                         header('HTTP/1.1 200 OK');
                     }
                 }
                 if ($NotificationType == 'OrderCancelledNotification') {
                     $cancel_order = new OrderCancelledNotification();
                     $cancel_order->cancel_order($NotificationData, $iopn_record_id);
                     header('HTTP/1.1 200 OK');
                 }
             } else {
                 $param['message'] = 'IOPN Notifications : ' . $NotificationType . ' : IOPN function called and with wrong timestamp.';
                 $obj = new Pwapresta();
                 $obj->generate_log($param);
                 // Respond to the Request
                 header('HTTP/1.1 403 PERMISSION_DENIED');
             }
         } else {
             $param['message'] = 'IOPN Notifications : ' . $NotificationType . ' : IOPN function called and with wrong signature.';
             $obj = new Pwapresta();
             $obj->generate_log($param);
             // Respond to the Request
             header('HTTP/1.1 403 PERMISSION_DENIED');
         }
     } catch (Exception $e) {
         $param['message'] = 'IOPN Notifications : Caught exception : ' . $e->getMessage() . '.';
         $obj = new Pwapresta();
         $obj->generate_log($param);
     }
 }
Example #7
0
 function invokeSubmitFeed(MarketplaceWebService_Interface $service, $request)
 {
     try {
         $response = $service->submitFeed($request);
         if ($response->isSetSubmitFeedResult()) {
             $submitFeedResult = $response->getSubmitFeedResult();
             if ($submitFeedResult->isSetFeedSubmissionInfo()) {
                 $feedSubmissionInfo = $submitFeedResult->getFeedSubmissionInfo();
                 if ($feedSubmissionInfo->isSetFeedSubmissionId()) {
                     $feedSubmissionId = $feedSubmissionInfo->getFeedSubmissionId();
                     $param['message'] = 'Order Acknowledged : Feed Submission Id - ' . $feedSubmissionId;
                     $obj = new Pwapresta();
                     $obj->generate_log($param);
                 }
             }
         }
     } catch (MarketplaceWebService_Exception $ex) {
         $message = 'MWS Feed API : Caught Exception : ' . $ex->getMessage() . "\n";
         $message .= "Response Status Code: " . $ex->getStatusCode() . "\n";
         $message .= "Error Code: " . $ex->getErrorCode() . "\n";
         $message .= "Error Type: " . $ex->getErrorType() . "\n";
         $param['message'] = $message;
         $obj = new Pwapresta();
         $obj->generate_log($param);
     }
 }
Example #8
0
    public static function create(Order $order, $product_list, $shipping_cost = false, $amount = 0, $amount_choosen = false, $add_tax = true)
    {
        $currency = new Currency((int) $order->id_currency);
        $order_slip = new OrderSlip();
        $order_slip->id_customer = (int) $order->id_customer;
        $order_slip->id_order = (int) $order->id;
        $order_slip->conversion_rate = $currency->conversion_rate;
        if ($add_tax) {
            $add_or_remove = 'add';
            $inc_or_ex_1 = 'excl';
            $inc_or_ex_2 = 'incl';
        } else {
            $add_or_remove = 'remove';
            $inc_or_ex_1 = 'incl';
            $inc_or_ex_2 = 'excl';
        }
        $order_slip->{'total_shipping_tax_' . $inc_or_ex_1} = 0;
        $order_slip->{'total_shipping_tax_' . $inc_or_ex_2} = 0;
        $order_slip->partial = 0;
        if ($shipping_cost !== false) {
            $order_slip->shipping_cost = true;
            $carrier = new Carrier((int) $order->id_carrier);
            $address = Address::initialize($order->id_address_delivery, false);
            $tax_calculator = $carrier->getTaxCalculator($address);
            $order_slip->{'total_shipping_tax_' . $inc_or_ex_1} = $shipping_cost === null ? $order->{'total_shipping_tax_' . $inc_or_ex_1} : (double) $shipping_cost;
            if ($tax_calculator instanceof TaxCalculator) {
                $order_slip->{'total_shipping_tax_' . $inc_or_ex_2} = Tools::ps_round($tax_calculator->{$add_or_remove . 'Taxes'}($order_slip->{'total_shipping_tax_' . $inc_or_ex_1}), _PS_PRICE_COMPUTE_PRECISION_);
            } else {
                $order_slip->{'total_shipping_tax_' . $inc_or_ex_2} = $order_slip->{'total_shipping_tax_' . $inc_or_ex_1};
            }
        } else {
            $order_slip->shipping_cost = false;
        }
        $order_slip->amount = 0;
        $order_slip->{'total_products_tax_' . $inc_or_ex_1} = 0;
        $order_slip->{'total_products_tax_' . $inc_or_ex_2} = 0;
        foreach ($product_list as &$product) {
            $order_detail = new OrderDetail((int) $product['id_order_detail']);
            $price = (double) $product['unit_price'];
            $quantity = (int) $product['quantity'];
            $order_slip_resume = OrderSlip::getProductSlipResume((int) $order_detail->id);
            if ($quantity + $order_slip_resume['product_quantity'] > $order_detail->product_quantity) {
                $quantity = $order_detail->product_quantity - $order_slip_resume['product_quantity'];
            }
            if ($quantity == 0) {
                continue;
            }
            $order_detail->product_quantity_refunded += $quantity;
            $order_detail->save();
            $address = Address::initialize($order->id_address_invoice, false);
            $id_tax_rules_group = Product::getIdTaxRulesGroupByIdProduct((int) $order_detail->product_id);
            $tax_calculator = TaxManagerFactory::getManager($address, $id_tax_rules_group)->getTaxCalculator();
            $order_slip->{'total_products_tax_' . $inc_or_ex_1} += $price * $quantity;
            if (in_array(Configuration::get('PS_ROUND_TYPE'), array(Order::ROUND_ITEM, Order::ROUND_LINE))) {
                if (!isset($total_products[$id_tax_rules_group])) {
                    $total_products[$id_tax_rules_group] = 0;
                } else {
                    if (!isset($total_products[$id_tax_rules_group . '_' . $id_address])) {
                        $total_products[$id_tax_rules_group . '_' . $id_address] = 0;
                    }
                }
            }
            $product_tax_incl_line = Tools::ps_round($tax_calculator->{$add_or_remove . 'Taxes'}($price) * $quantity, _PS_PRICE_COMPUTE_PRECISION_);
            switch (Configuration::get('PS_ROUND_TYPE')) {
                case Order::ROUND_ITEM:
                    $product_tax_incl = Tools::ps_round($tax_calculator->{$add_or_remove . 'Taxes'}($price), _PS_PRICE_COMPUTE_PRECISION_) * $quantity;
                    $total_products[$id_tax_rules_group] += $product_tax_incl;
                    break;
                case Order::ROUND_LINE:
                    $product_tax_incl = $product_tax_incl_line;
                    $total_products[$id_tax_rules_group] += $product_tax_incl;
                    break;
                case Order::ROUND_TOTAL:
                    $product_tax_incl = $product_tax_incl_line;
                    $total_products[$id_tax_rules_group . '_' . $id_address] += $price * $quantity;
                    break;
            }
            $product['unit_price_tax_' . $inc_or_ex_1] = $price;
            $product['unit_price_tax_' . $inc_or_ex_2] = Tools::ps_round($tax_calculator->{$add_or_remove . 'Taxes'}($price), _PS_PRICE_COMPUTE_PRECISION_);
            $product['total_price_tax_' . $inc_or_ex_1] = Tools::ps_round($price * $quantity, _PS_PRICE_COMPUTE_PRECISION_);
            $product['total_price_tax_' . $inc_or_ex_2] = Tools::ps_round($product_tax_incl, _PS_PRICE_COMPUTE_PRECISION_);
            $product['product_id'] = $order_detail->product_id;
        }
        unset($product);
        foreach ($total_products as $key => $price) {
            if (Configuration::get('PS_ROUND_TYPE') == Order::ROUND_TOTAL) {
                $tmp = explode('_', $key);
                $address = Address::initialize((int) $tmp[1], true);
                $tax_calculator = TaxManagerFactory::getManager($address, $tmp[0])->getTaxCalculator();
                $order_slip->{'total_products_tax_' . $inc_or_ex_2} += Tools::ps_round($tax_calculator->{$add_or_remove . 'Taxes'}($price), _PS_PRICE_COMPUTE_PRECISION_);
            } else {
                $order_slip->{'total_products_tax_' . $inc_or_ex_2} += $price;
            }
        }
        $order_slip->{'total_products_tax_' . $inc_or_ex_2} -= (double) $amount && !$amount_choosen ? (double) $amount : 0;
        $order_slip->amount = $amount_choosen ? (double) $amount : $order_slip->{'total_products_tax_' . $inc_or_ex_1};
        $order_slip->shipping_cost_amount = $order_slip->{'total_shipping_tax_' . $inc_or_ex_1};
        if ((double) $amount && !$amount_choosen) {
            $order_slip->order_slip_type = 1;
        }
        if ((double) $amount && $amount_choosen || $order_slip->shipping_cost_amount > 0) {
            $order_slip->order_slip_type = 2;
        }
        if (!$order_slip->add()) {
            return false;
        }
        $res = true;
        $param = array();
        $param['MerchantOrderID'] = $order->id;
        $prefix = _DB_PREFIX_;
        $i = 0;
        foreach ($product_list as $product) {
            $res &= $order_slip->addProductOrderSlip($product);
            $order_item_detail = new OrderDetail((int) $product['id_order_detail']);
            $sql = 'UPDATE `' . $prefix . 'stock_available` set
					`quantity` = `quantity` + ' . $product['quantity'] . '
					where `id_product` = ' . $product['product_id'] . ' and
					`id_product_attribute` = 0
					';
            //Db::getInstance()->Execute($sql);
            if ($order_item_detail->product_attribute_id > 0) {
                $sql = 'UPDATE `' . $prefix . 'stock_available` set
						`quantity` = `quantity` + ' . $product['quantity'] . '
						where `id_product` = ' . $product['product_id'] . ' and
						`id_product_attribute` = ' . $order_item_detail->product_attribute_id . '
						';
                //Db::getInstance()->Execute($sql);
            }
            $date = date('Y-m-d');
            $sql = 'UPDATE `' . $prefix . 'product_sale` set
					`quantity` = `quantity` - ' . $product['quantity'] . ',
					`sale_nbr` = `sale_nbr` - ' . $product['quantity'] . ',
					`date_upd` = ' . $date . '
					where `id_product` = ' . $product['product_id'] . '
					';
            //Db::getInstance()->Execute($sql);
            $param['items'][$i]['MerchantOrderItemID'] = $product['product_id'];
            $param['items'][$i]['Principal'] = $product['total_price_tax_incl'];
            $param['items'][$i]['Shipping'] = 0;
            $param['items'][$i]['Tax'] = 0;
            $param['items'][$i]['ShippingTax'] = 0;
            $param['items'][$i]['quantity'] = $product['quantity'];
            $i++;
        }
        $refund = new Pwapresta();
        $refund->pwa_refund_feed($param);
        return $res;
    }
 public function acknowledge_order($order_id, $data)
 {
     $xml = simplexml_load_string($data);
     $acknowledge_arr = array();
     $i = 0;
     foreach ($xml->ProcessedOrder->ProcessedOrderItems->ProcessedOrderItem as $item) {
         $AmazonOrderItemCode = (string) $item->AmazonOrderItemCode;
         $product_id = $item->ItemCustomData->Item_product_id;
         $acknowledge_arr['items'][$i]['AmazonOrderItemCode'] = $AmazonOrderItemCode;
         $acknowledge_arr['items'][$i]['product_id'] = $product_id;
         $i++;
     }
     // Acknowledge the order in seller central using MWS FEED API
     $acknowledge_arr['MerchantOrderID'] = $order_id;
     $obj = new Pwapresta();
     $obj->pwa_acknowledge_feed($acknowledge_arr);
 }
    public function update_cart_by_junglee_xml($order_id, $orderdetail)
    {
        $prefix = _DB_PREFIX_;
        $tablename = $prefix . 'orders';
        $total_amount = 0;
        $total_principal = 0;
        $shipping_amount = 0;
        $total_promo = 0;
        $ClientRequestId = 0;
        $AmazonOrderID = (string) $orderdetail->OrderReport->AmazonOrderID;
        foreach ($orderdetail->OrderReport->Item as $item) {
            $SKU = (string) $item->SKU;
            $Title = (string) $item->Title;
            $Quantity = (int) $item->Quantity;
            $Principal_Promotions = 0;
            $Shipping_Promotions = 0;
            foreach ($item->ItemPrice->Component as $amount_type) {
                $item_charge_type = (string) $amount_type->Type;
                if ($item_charge_type == 'Principal') {
                    $Principal = abs((double) $amount_type->Amount);
                }
                if ($item_charge_type == 'Shipping') {
                    $Shipping = abs((double) $amount_type->Amount);
                }
                if ($item_charge_type == 'Tax') {
                    $Tax = abs((double) $amount_type->Amount);
                }
                if ($item_charge_type == 'ShippingTax') {
                    $ShippingTax = abs((double) $amount_type->Amount);
                }
            }
            if (!empty($item->Promotion)) {
                foreach ($item->Promotion as $promotions) {
                    foreach ($promotions->Component as $promotion_amount_type) {
                        $promotion_type = (string) $promotion_amount_type->Type;
                        if ($promotion_type == 'Shipping') {
                            $Shipping_Promotions += abs((double) $promotion_amount_type->Amount);
                        }
                        if ($promotion_type == 'Principal') {
                            $Principal_Promotions += abs((double) $promotion_amount_type->Amount);
                        }
                    }
                }
            }
            $total_principal += $Principal;
            $total_amount += $Principal - $Principal_Promotions + ($Shipping - $Shipping_Promotions);
            $shipping_amount += $Shipping + $Shipping_Promotions;
            $total_promo += $Principal_Promotions + $Shipping_Promotions;
            foreach ($item->CustomizationInfo as $info) {
                $info_type = (string) $info->Type;
                if ($info_type == 'url') {
                    $info_array = explode(',', $info->Data);
                    $customerId_array = explode('=', $info_array[0]);
                    $ClientRequestId = $customerId_array[1];
                }
            }
        }
        $ShippingServiceLevel = (string) $orderdetail->OrderReport->FulfillmentData->FulfillmentServiceLevel;
        $sql = 'UPDATE `' . $prefix . 'pwa_orders` set `shipping_service` = "' . $ShippingServiceLevel . '" , `order_type` = "junglee" where `prestashop_order_id` = "' . $order_id . '" ';
        Db::getInstance()->Execute($sql);
        $cust_name = (string) $orderdetail->OrderReport->BillingData->BuyerName;
        $name_arr = explode(' ', $cust_name);
        if (count($name_arr) > 1) {
            $firstname = '';
            for ($i = 0; $i < count($name_arr) - 2; $i++) {
                $firstname = $firstname . ' ' . $name_arr[$i];
            }
            $lastname = $name_arr[count($name_arr) - 1];
        } else {
            $firstname = $cust_name;
            $lastname = ' ';
        }
        $email = (string) $orderdetail->OrderReport->BillingData->BuyerEmailAddress;
        $sql = 'SELECT * from `' . $prefix . 'customer` where email = "' . $email . '" ';
        $results = Db::getInstance()->ExecuteS($sql);
        if (empty($results)) {
            $password = Tools::passwdGen();
            $customer = new Customer();
            $customer->firstname = trim($firstname);
            $customer->lastname = $lastname;
            $customer->email = (string) $xml->ProcessedOrder->BuyerInfo->BuyerEmailAddress;
            $customer->passwd = md5($password);
            $customer->active = 1;
            if (Configuration::get('PS_GUEST_CHECKOUT_ENABLED')) {
                $customer->is_guest = 1;
            } else {
                $customer->is_guest = 0;
            }
            $customer->add();
            $customer_id = $customer->id;
            if (Configuration::get('PS_CUSTOMER_CREATION_EMAIL') && !Configuration::get('PS_GUEST_CHECKOUT_ENABLED')) {
                Mail::Send($this->context->language->id, 'account', Mail::l('Welcome!'), array('{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{email}' => $customer->email, '{passwd}' => $password), $customer->email, $customer->firstname . ' ' . $customer->lastname);
            }
        } else {
            $customer_id = $results[0]['id_customer'];
        }
        $id_country = Country::getByIso((string) $orderdetail->OrderReport->FulfillmentData->Address->CountryCode);
        if ($id_country == 0 || $id_country == '') {
            $id_country = 110;
        }
        $address = new Address();
        $address->id_country = $id_country;
        $address->id_state = 0;
        $address->id_customer = $customer_id;
        $address->alias = 'My Address';
        $address->firstname = trim($firstname);
        $address->lastname = $lastname;
        $address->address1 = (string) $orderdetail->OrderReport->FulfillmentData->Address->AddressFieldOne;
        $address->address2 = (string) $orderdetail->OrderReport->FulfillmentData->Address->AddressFieldTwo;
        $address->postcode = (string) $orderdetail->OrderReport->FulfillmentData->Address->PostalCode;
        $address->phone_mobile = (string) $orderdetail->OrderReport->FulfillmentData->Address->PhoneNumber;
        $address->city = (string) $orderdetail->OrderReport->FulfillmentData->Address->City . ' ' . (string) $orderdetail->OrderReport->FulfillmentData->Address->StateOrRegion;
        $address->active = 1;
        $address->add();
        $address_id = $address->id;
        $id_order_state = 2;
        $reference = Order::generateReference();
        $order = new Order();
        $order->id = $order_id;
        $order->id_customer = (int) $customer_id;
        $order->id_address_invoice = (int) $address_id;
        $carrier = null;
        $sql = 'SELECT id_carrier from  `' . $prefix . 'carrier` where `active` = 1 and `deleted` = 0 limit 0,1';
        $result = Db::getInstance()->ExecuteS($sql);
        $id_carrier = $result[0]['id_carrier'];
        $sql = 'SELECT id_currency from  `' . $prefix . 'currency` where `active` = 1 and `deleted` = 0 and `iso_code` = "INR" limit 0,1';
        $result = Db::getInstance()->ExecuteS($sql);
        $currency_id = $result[0]['id_currency'];
        $sql = 'UPDATE `' . $tablename . '` set 
			  `id_customer` = ' . (int) $customer_id . ',
			  `id_carrier` = ' . $id_carrier . ',
			  `id_address_invoice` = ' . (int) $address_id . ',
			  `id_address_delivery` = ' . (int) $address_id . ',
			  `id_currency` = ' . $currency_id . ',
			  `reference` = "' . $reference . '",
			  `secure_key` = "' . md5(uniqid()) . '",
			  
			  `total_paid` = ' . $total_amount . ',
			  `total_paid_tax_incl` = ' . $total_amount . ',
			  `total_paid_tax_excl` = ' . $total_amount . ',
			  `total_paid_real` = 0,
			 
			  `total_shipping` = ' . $shipping_amount . ',
			  `total_shipping_tax_incl` = ' . $shipping_amount . ',
			  `total_shipping_tax_excl` = ' . $shipping_amount . ',
			  
			  `total_discounts` = ' . (double) $total_promo . ',
			  `total_discounts_tax_incl` = ' . (double) $total_promo . ',
			  `total_discounts_tax_excl` = ' . (double) $total_promo . ',
			  
			  `total_products` = ' . $total_principal . ',
			  `total_products_wt` = ' . $total_principal . ',
			 
			  `invoice_date` = "0000-00-00 00:00:00",
			  `delivery_date` = "0000-00-00 00:00:00"
			  where `id_order` = ' . $order_id . '';
        // `round_mode` = '.Configuration::get('PS_PRICE_ROUND_MODE').',
        Db::getInstance()->Execute($sql);
        $i = 0;
        foreach ($orderdetail->OrderReport->Item as $item) {
            $id_product = (string) $item->SKU;
            $product = new Product((int) $product_id);
            $SKU = $product->reference;
            $AmazonOrderItemCode = (string) $item->AmazonOrderItemCode;
            $Title = (string) $item->Title;
            $Quantity = (int) $item->Quantity;
            foreach ($item->ItemPrice->Component as $amount_type) {
                $item_charge_type = (string) $amount_type->Type;
                if ($item_charge_type == 'Principal') {
                    $Amount = (double) $amount_type->Amount;
                }
            }
            $Amount = $Amount / $Quantity;
            $Amount = round($Amount, 3);
            $acknowledge_arr['items'][$i]['AmazonOrderItemCode'] = $AmazonOrderItemCode;
            $acknowledge_arr['items'][$i]['product_id'] = $id_product;
            $i++;
            $sql = 'INSERT into `' . $prefix . 'order_detail` set
							`id_order` = ' . $order_id . ',
							`product_id` = ' . $id_product . ',
							`product_name` = "' . $Title . '",
							`product_quantity` = ' . $Quantity . ',
							`product_quantity_in_stock` = ' . $Quantity . ',
							`product_price` = ' . $Amount . ',
							`product_reference` = "' . $SKU . '",
							`total_price_tax_incl` = ' . $Amount * $Quantity . ',
							`total_price_tax_excl` = ' . $Amount * $Quantity . ',
							`unit_price_tax_incl` = ' . $Amount . ',
							`unit_price_tax_excl` = ' . $Amount . ',
							`original_product_price` = ' . $Amount . '
							';
            Db::getInstance()->Execute($sql);
            $sql = 'UPDATE `' . $prefix . 'stock_available` set
						`quantity` = `quantity` - ' . $Quantity . '
						where `id_product` = ' . $id_product . ' and
						`id_product_attribute` = 0
						';
            Db::getInstance()->Execute($sql);
            /*$sql = 'UPDATE `'.$prefix.'stock_available` set
            				`quantity` = `quantity` - '.$Quantity.'
            				where `id_product` = '.$product_id.' and
            				`id_product_attribute` = '.$product_attribute_id.'
            				';
            		Db::getInstance()->Execute($sql);*/
            $date = date('Y-m-d');
            $sql = 'UPDATE `' . $prefix . 'product_sale` set
						`quantity` = `quantity` + ' . $Quantity . ',
						`sale_nbr` = `sale_nbr` + ' . $Quantity . ',
						`date_upd` = ' . $date . '
						where `id_product` = ' . $id_product . '
						';
            Db::getInstance()->Execute($sql);
        }
        // Adding an entry in order_carrier table
        if (!is_null($carrier)) {
            $order_carrier = new OrderCarrier();
            $order_carrier->id_order = (int) $order->id;
            $order_carrier->id_carrier = (int) $id_carrier;
            $order_carrier->weight = '0';
            $order_carrier->shipping_cost_tax_excl = (double) $shipping_amount;
            $order_carrier->shipping_cost_tax_incl = (double) $shipping_amount;
            $order_carrier->add();
        } else {
            $order_carrier = new OrderCarrier();
            $order_carrier->id_order = (int) $order->id;
            $order_carrier->id_carrier = (int) $id_carrier;
            $order_carrier->weight = '0';
            $order_carrier->shipping_cost_tax_excl = (double) $shipping_amount;
            $order_carrier->shipping_cost_tax_incl = (double) $shipping_amount;
            $order_carrier->add();
        }
        // Acknowledge the order in seller central using MWS FEED API
        $acknowledge_arr['MerchantOrderID'] = (int) $order->id;
        $obj = new Pwapresta();
        $obj->pwa_acknowledge_feed($acknowledge_arr);
        $history = new OrderHistory();
        $history->id_order = $order->id;
        $history->changeIdOrderState((int) $id_order_state, $order->id, true);
        $history->addWithemail(true, array());
    }