Reputation: 73
I'm trying to create API connection to prestashop via webservices
I can create Custommers, Addresses, Carts succesfully (there is available well known pure documentation about customers CRUD, it works as expected)
I'm not able to complete new Order:
There is source code fragment
require_once('config.php'); // PS_SHOP_PATH etc.
require_once('PSWebServiceLibrary.php');
try
{
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$opt = array('resource' => 'orders');
$xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/orders?schema=blank'));
$xml->children()->children()->id_address_delivery = 1;
$xml->children()->children()->id_address_invoice = 1;
$xml->children()->children()->id_cart = 25;
$xml->children()->children()->id_currency = 1;
$xml->children()->children()->current_state = 1;
$xml->children()->children()->id_lang = 1;
$xml->children()->children()->id_customer = 1;
$xml->children()->children()->id_carrier = 0;
$xml->children()->children()->total_paid = '56973';
$xml->children()->children()->total_paid_real = '56973';
$xml->children()->children()->total_paid_tax_excl = '56973';
$xml->children()->children()->total_paid_tax_incl = '56977';
$xml->children()->children()->total_products = 1338;
$xml->children()->children()->total_products_wt = 1337;
$xml->children()->children()->conversion_rate = '1';
$xml->children()->children()->secure_key = md5('-1');;
$xml->children()->children()->valid = 1;
$xml->children()->children()->module = 'Bankwire';
$xml->children()->children()->payment = 'bankwire';
$xml->children()->children()->current_state = 1;
unset($xml->children()->children()->id);
unset($xml->children()->children()->date_add);
unset($xml->children()->children()->date_upd);
unset($xml->children()->children()->associations);
unset($xml->children()->children()->delivery_date);
unset($xml->children()->children()->invoice_date);
unset($xml->children()->children()->total_discounts_tax_incl);
unset($xml->children()->children()->total_discounts_tax_excl);
$xml = $webService->add(array('resource' => 'orders',
'postXml' => $xml->asXML()
));
$resources = $xml->children()->children();
echo "Successfully added order.".$resources->id;
}
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();
}
And there is part. webview log from Fiddler
XML SENT
xml=<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<order>
<id_address_delivery>1</id_address_delivery>
<id_address_invoice>1</id_address_invoice>
<id_cart>25</id_cart>
<id_currency>1</id_currency>
<id_lang>1</id_lang>
<id_customer>1</id_customer>
<id_carrier>0</id_carrier>
<current_state>1</current_state>
<module>Bankwire</module>
<invoice_number/>
<delivery_number/>
<valid>1</valid>
<id_shop_group/>
<id_shop/>
<secure_key>6bb61e3b7bce0931da574d19d1d82c88</secure_key>
<payment>bankwire</payment>
<recyclable/>
<gift/>
<gift_message/>
<mobile_theme/>
<total_discounts/>
<total_paid>56973</total_paid>
<total_paid_tax_incl>56977</total_paid_tax_incl>
<total_paid_tax_excl>56973</total_paid_tax_excl>
<total_paid_real>56973</total_paid_real>
<total_products>1338</total_products>
<total_products_wt>1337</total_products_wt>
<total_shipping/>
<total_shipping_tax_incl/>
<total_shipping_tax_excl/>
<carrier_tax_rate/>
<total_wrapping/>
<total_wrapping_tax_incl/>
<total_wrapping_tax_excl/>
<shipping_number/>
<conversion_rate>1</conversion_rate>
<reference/>
</order>
</prestashop>
RETURN HTTP BODY Fatal error Other error
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, )), )
"Other error" comes from this:
else echo 'Other error<br />'.$ex->getMessage();
PSWebServiceLibrary contains
/** @var array compatible versions of PrestaShop Webservice */
const psCompatibleVersionsMin = '1.4.0.17';
const psCompatibleVersionsMax = '1.5.4.1';
PrestaShop is 1.5.4.0
Any advice?
Upvotes: 1
Views: 12318
Reputation: 9
define('DEBUG', true);
define('PS_SHOP_PATH', 'http://.....');
define('PS_WS_AUTH_KEY', '...');
define('FILE_NAME', 'file.xlsx');
require_once('./PSWebServiceLibrary.php');
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
require_once __DIR__.'/src/SimpleXLSX.php';
$newArray = array(); // here we will accumulate grouped data
$dataArray=[];
$product_count=0;
if ( $xlsx = SimpleXLSX::parse(FILE_NAME) ) {
for($i=1; $i < count($xlsx->rows()); $i++){
$id=$xlsx->rows()[$i][0];
$product_id=$xlsx->rows()[$i][96];
if($product_id)
{
if(!isset($dataArray[$id]))
{
$dataArray[$id]["products"][$product_id]= $xlsx->rows()[$i][103];
$dataArray[$id]["price"]=$xlsx->rows()[$i][38];
$dataArray[$id]["product_weight"]=$xlsx->rows()[$i][24];
$dataArray[$id]["id_address_delivery"]=$xlsx->rows()[$i][170];
$dataArray[$id]["id_carrier"]=$xlsx->rows()[$i][172];
$dataArray[$id]["payment_mode"]=$xlsx->rows()[$i][173];
$dataArray[$id]["product_count"]=$product_count;
$dataArray[$id]["currency_code"] = $xlsx->rows()[$i][17];
}
else
{
if(!isset($dataArray[$id]["products"][$product_id]))
{
$dataArray[$id]["products"][$product_id]+= $xlsx->rows()[$i][103];
}
else
{
$dataArray[$id]["products"][$product_id]+= $xlsx->rows()[$i][103];
}
}
}
}
} else {
echo SimpleXLSX::parseError();
}
try{
foreach($dataArray as $key => $value){
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/carts?schema=blank'));
$xml_currency = $webService->get(array('url' => PS_SHOP_PATH.'/api/currencies/?display=full'));
$xml_language = $webService->get(array('url' => PS_SHOP_PATH.'/api/languages?display=full'));
$value['id_language'] = (int)$xml_language->languages->language->id;
$xml->cart->id_customer = $key;
$count = 0;
$value['total_products'] = 0;
foreach($value['products'] as $product_key => $product_value){
$xml->cart->associations->cart_rows->cart_row[$count]->id_product = $product_key;
$xml->cart->associations->cart_rows->cart_row[$count]->quantity = $product_value;
$value['total_products']+= $product_value;
$count++;
}
for($i=0; $i<count($xml_currency->currencies->currency);$i++){
if($xml_currency->currencies->currency[$i]->iso_code == $value['currency_code'])
$value['id_currency'] =(int) $xml_currency->currencies->currency[$i]->id;
}
$xml->cart->id_address_delivery = $value['id_address_delivery'];
$xml->cart->id_address_invoice = $value['id_address_delivery'];
$xml->cart->id_currency = $value['id_currency'];
$xml->cart->id_lang = $value['id_language'];
$xml->cart->id_carrier = $value['id_carrier'];
$opt = array('resource' => 'carts');
$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);
$cart_id = $xml->cart->id;
$xml = $webService->get(array('url' => PS_SHOP_PATH .'/api/orders/?schema=blank'));
$xml->order->id_address_delivery = $value['id_address_delivery'];
$xml->order->id_address_invoice = $value['id_address_delivery'];
$xml->order->id_cart = $cart_id;
$xml->order->id_currency = $value['id_currency'];
$xml->order->id_lang = $value['id_language'];
$xml->order->id_customer = $key;
$xml->order->id_carrier = $value['id_carrier'];
if($value['payment_mode'] == 'Cash on delivery (COD)')
$xml->order->module = 'ps_cashondelivery';
if($value['payment_mode'] == 'Payment by check')
$xml->order->module = 'ps_checkpayment';
$xml->order->payment = $value['payment_mode'];
$xml->order->total_paid = $value['price'];
$xml->order->total_paid_real = $value['price'];
$xml->order->total_products = $value['total_products'];
$xml->order->total_products_wt = $value['product_weight'];
$xml->order->conversion_rate = '1'; // conversion rate is fixed here
$opt = array('resource' => 'orders');
$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);
}
}catch (PrestaShopWebserviceException $e) {
// Here we are dealing with errors
$trace = $e->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 />'.$e->getMessage();
} catch (PrestaShopWebserviceException $e) {
// Here we are dealing with errors
$trace = $e->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 />'.$e->getMessage();
}
Upvotes: 0
Reputation: 41
If you are getting this Start tag expected, \'<\' not found ',
error try to check response which your sending to parse.
This is showing due to improper data that you sending in order or due to xml validation problem.
I also gone through same problem and I solved it by checking what response I am sending to parse method in Prestashop library.
Upvotes: 0
Reputation: 129
For placing order through webservice you need to make some entries directly to db with respect to cart like delivery option in cart table and for discount add entries in ps_cart_cart_rule table then place and order as you were placing. 1. add cart; 2. add discount options with respect to cart. 3. add delivery option with respect to cart. 4. Place order With same cart id. Code as followed:
foreach ($raw_data['discounts'] as $discount) {
foreach ($raw_data['discounts'] as $discount) {
$sql = "insert into ps_cart_cart_rule values('" . $id['cart'] . "','" . $discount['id_cart_rule'] . "')";
$r = mysql_query($sql, $conn)or die(mysql_error($conn));
}
$sql = "update ps_cart set `delivery_option`='a:1:{i:" . $id['address'] . ";s:3:\"" . $id['carrier'] . ",\";}', `id_carrier`='" . $id['carrier'] . "' where id_cart='" . $id['cart'] . "'";
$r = mysql_query($sql, $conn)or die(mysql_error($conn));
$xml = $webService->get(array('url' => PS_SHOP_PATH . '/api/orders?schema=blank'));
$xml->order->id_customer = $id['customer'];
$xml->order->id_address_delivery = $id['address'];
$xml->order->id_address_invoice = $id['address'];
$xml->order->id_cart = $id['cart'];
$xml->order->id_currency = $id['currency'];
$xml->order->id_lang = $id['lang'];
$xml->order->id_carrier = $id['carrier'];
$xml->order->current_state = "3";
$xml->order->valid = 0;
$xml->order->total_shipping = $raw_data['total_shipping'];
$xml->order->total_shipping_tax_incl = $raw_data['total_shipping'];
$xml->order->total_shipping_tax_excl = $raw_data['total_shipping'];
$xml->order->total_discounts = $raw_data['total_discounts'];
$xml->order->total_discounts_tax_incl = $raw_data['total_discounts'];
$xml->order->total_discounts_tax_excl = $raw_data['total_discounts'];
$xml->order->payment = 'Cash on delivery';
$xml->order->module = 'cashondelivery';
$xml->order->total_products = $raw_data['total_products'];
$xml->order->total_products_wt = $raw_data['total_products'];
$xml->order->total_paid = $raw_data['total_paid'];
$xml->order->total_paid_tax_incl = $raw_data['total_paid'];
$xml->order->total_paid_tax_excl = $raw_data['total_paid'];
$xml->order->total_paid_real = "0";
$xml->order->conversion_rate = '1';
$opt = array('resource' => 'orders');
$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);
$id['order'] = $xml->order->id;
$id['secure_key'] = $xml->order->secure_key;
Upvotes: 1
Reputation: 4252
Following error is because of missing required value in Cart or Order
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, )), )
In my case id_address_delivery & id_address_invoice where missing in Cart, that's why i was facing this error.
Read more at PrestaShop web service create order errors
Upvotes: 0
Reputation: 73
Problem solved.
1) create Customer (opt.)
2) create Address (opt.)
3) check Products availibility (this is really better...)
4) create Cart with order_rows and with product id's and quantities
5) create Order with this
in all cases required tags should by filled- see /api/object?schema=synopsis
Simply cart should not be empty before create order, its sounds logic in step by step intearctive process and the same sequence should be done in "batch" processing via webservices.
Upvotes: 5