Prestashop webservice creating a new order

问题内容:

First of all yes i did try literally everyting stated on stack at other questions but sadly those didn’t work for me.

I’m using the prestashop webservice and i’m trying to create an order through the webservice but due to lacking information about debugging in the webservice i’m a bit stuck. I’ve tried many things others suggested as:

  1. Add customer, address then the cart and order (made no difference in errors)
  2. Use multiple code examples but those all stumbled to the same error.

This is the code i’ve currently come up with as the best working with the less bugs.

try {
            // Vars & includes required to use the lib
            define('DEBUG', true);
            require_once('../PSWebServiceLibrary.php');
            $webService = new PrestaShopWebservice($url, $key, DEBUG);

 ///*
            //* 4. Create the order
            //*
            //*/

            // Generate the order reference
            function generateRandomString($length = 9) {
                $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                $charactersLength = strlen($characters);
                $randomString = '';
                for ($i = 0; $i < $length; $i++) {
                    $randomString .= $characters[rand(0, $charactersLength - 1)];
                }
                return $randomString;
            }

            // CREATE Order

            $optOrder = $webService->get(array('url' => $url .'/api/orders/?schema=synopsis'));
            $resourceOrder = $optOrder->children()->children();
            $resourceOrder->id_shop_group = 1;
            $resourceOrder->reference = generateRandomString();
            $resourceOrder->id_shop = 1;
            $resourceOrder->id_carrier = 2; // Check your id_carrier
            $resourceOrder->id_lang = $id_lang;
            $resourceOrder->id_customer = $userId;
//            foreach ($cart_id1 as $key => $cartID) {
//                $resourceOrder->id_cart = $cartID->id;
//            }
            $resourceOrder->id_cart = 284;
            $resourceOrder->id_currency = $id_currency;
            $resourceOrder->id_address_delivery = 4;
            $resourceOrder->id_address_invoice = 4;
            $resourceOrder->current_state = 17; // order in winkel vanuit easycheckout.
            $resourceOrder->secure_key = "cc5a1cc34cb39c2625a66e9ba24fd5ea";
            $resourceOrder->payment = "TargetPay bank betalingen(Bancontact)";
            $resourceOrder->conversion_rate = 1.000000;
            $resourceOrder->module = 'ps_checkpayment';
            $resourceOrder->recyclable = 0;
            $resourceOrder->gift = 0;
            $resourceOrder->gift_message = null;
            $resourceOrder->mobile_theme = 0;
            $resourceOrder->shipping_number = 0;
            $resourceOrder->total_discounts = 0;
            $resourceOrder->total_discounts_tax_incl = 0;
            $resourceOrder->total_discounts_tax_excl = 0;
            $resourceOrder->total_paid = $total_price;
            $resourceOrder->total_paid_tax_incl = $total_price;
            $resourceOrder->total_paid_tax_excl = $price_excl_vat;
            $resourceOrder->total_paid_real = $total_price;
            $resourceOrder->total_products = $cart_items;
            $resourceOrder->total_products_wt = $cart_items;
            $resourceOrder->total_shipping = 2;
            $resourceOrder->total_shipping_tax_incl = 2;
            $resourceOrder->total_shipping_tax_excl = 2;
            $resourceOrder->carrier_tax_rate = 0;
            $resourceOrder->total_wrapping = 0;
            $resourceOrder->total_wrapping_tax_incl = 0;
            $resourceOrder->total_wrapping_tax_excl = 0;
            $resourceOrder->round_mode = 0;
            $resourceOrder->round_type = 0;
            $resourceOrder->invoice_number = $userId;
            $resourceOrder->delivery_number = $userId;
            $resourceOrder->invoice_date = $date;
            $resourceOrder->delivery_date = $date;
            $resourceOrder->valid = 1;
            $resourceOrder->date_add = $date;
            $resourceOrder->date_upd = $date;

            foreach ($cartDecode as $key => $cartItem) {
                $resourceOrder->associations->order_rows->order_row->id;
                $resourceOrder->associations->order_rows->order_row->product_id = $cartItem->id;
                $resourceOrder->associations->order_rows->order_row->product_attribute_id = 0;
                $resourceOrder->associations->order_rows->order_row->product_quantity = $cartItem->quantity;
            }

            $xmlOrder = $optOrder->asXML();
            $updateOrder = $webService->add(array(
                'resource' => 'orders',
                'postXml' => $xmlOrder
                )
            );



            $optOrderPayment = $webService->get(array('resource' => 'order_payments'));
            $resourceOrderPayment = $optOrderPayment->children()->children();
            $resourceOrderPayment->order_reference = generateRandomString();
            $resourceOrderPayment->id_currency = 1; // Check your id_carrier
            $resourceOrderPayment->amount = $total_price;
            $resourceOrderPayment->payment_method = "Actieve betaling";
            $resourceOrderPayment->conversion_rate = 1;
            $resourceOrderPayment->date_add = $date;

            $xmlOrderPayment = $optOrderPayment->asXML();
            $updateOrderPayment = $webService->add(array(
                    'resource' => 'order_payments',
                    'id_customer' => $userId,
                    'postXml' => $xmlOrderPayment
                )
            );
        }
        catch (PrestaShopWebserviceException $ex) {

            // Here we are dealing with errors

            $trace = $ex->getTrace();

            if ($trace[0]['args'][0] == 404) echo 'Bad ID';

            else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';

            else echo 'Other error<br />' . $ex->getMessage();
        }
    }
}

The error that is terurned is as follow:

Fatal error: Uncaught PrestaShopException: Fatal error in /home/sdweb/domains/sdwebdesign.nl/public_html/prestademo/classes/Tools.php:964
Stack trace:
#0 /home/sdweb/domains/sdwebdesign.nl/public_html/prestademo/classes/PaymentModule.php(236): ToolsCore::displayError()
#1 /home/sdweb/domains/sdwebdesign.nl/public_html/prestademo/classes/order/Order.php(1628): PaymentModuleCore-&gt;validateOrder('284', '12', '66.04', 'TargetPay bank ...', NULL, Array, NULL, false, 'cc5a1cc34cb39c2...')
#2 /home/sdweb/domains/sdwebdesign.nl/public_html/prestademo/classes/webservice/WebserviceRequest.php(1485): OrderCore-&gt;addWs()
#3 /home/sdweb/domains/sdwebdesign.nl/public_html/prestademo/classes/webservice/WebserviceRequest.php(1299): WebserviceRequestCore-&gt;saveEntityFromXml(201)
#4 /home/sdweb/domains/sdwebdesign.nl/public_html/prestademo/classes/webservice/WebserviceRequest.php(526): WebserviceRequestCore-&gt;executeEntityPost()
#5 /home/sdweb/domains/sdwebdesign.nl/public_html/prestademo/webservice/dispatcher.php(87): WebserviceRequestCore-&gt;f in /home/sdweb/domains/sdwebdesign.nl/public_html/prestademo/classes/Tools.php on line 964
</pre></div>Other error<br />HTTP XML response is not parsable: array (
  0 => 
  LibXMLError::__set_state(array(
     'level' => 3,
     'code' => 4,
     'column' => 1,
     'message' => 'Start tag expected, \'<\' not found
',
     'file' => '',
     'line' => 2,
  )),
)

So it has something to do with the ordervalidate() function. To be precise something with the module name. I’ve checked the names and active settings of my modules and they are all stated correctly.

By doing some cheating and editing the core of prestashop’s order.php class i came to the following error.

</pre></div><div style="display:table;background:#CCC;font-size:8pt;padding:7px"><h6 style="font-size:9pt;margin:0">RETURN HTTP BODY</h6><pre>The new order status is invalid</pre></div>Other error<br />HTTP XML response is not parsable: array (
  0 => 
  LibXMLError::__set_state(array(
     'level' => 3,
     'code' => 4,
     'column' => 1,
     'message' => 'Start tag expected, \'<\' not found
',
     'file' => '',
     'line' => 1,
  )),
)

So what i’ve changed in order.php to create this error is:

public function setWsCurrentState($state)
{
    if ($this->id) {
        $this->setCurrentState($state);
    }
    return true;
}

Into:

public function setWsCurrentState($state)
{
    $this->setCurrentState($state);
    return true;
}

On line: 2290 in order.php

The prestashop version is 1.7 ->

I hope someone has a good example or that someone could help me find the mistakes i’ve made.

Thanks in advance!

问题评论:

原文地址:

https://stackoverflow.com/questions/47755697/prestashop-webservice-creating-a-new-order

添加评论