Jump to content
renaud2263

Bug sur création de commande via WebService

Recommended Posts

Bonjour à tous,

Je tente, sur ma 1.7.6 de créer client + adresse + commande via le Web Service. Voici le script :

include('config/config.inc.php');
include('init.php');
DEFINE('PS_SHOP_PATH', 'https://www.monsite.com/');
DEFINE('PS_WS_AUTH_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
DEFINE('DEBUG', true);
	
$id_customer    = 0;
$id_address     = 0;
$id_cart        = 0;
 
try {
require_once('./PSWebServiceLibrary.php');
	
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$passwd = 'testeur2020';
$lastname = 'Letesteur';
$firstname = 'BobTest';
$email = 'testeur@testeur.com';
$id_gender = 1;
$date_now = date('Y-m-d H:i:s');

$id_country = 8;
$city = 'TestVille';
$ZIP = '38000';
$address1 = '12 rue du Test';
$phone_mobile = '0637560862';
	
$id_carrier = '38';
$id_status = '12';

$total_shipping = '13.00';
$total_shipping_tax_incl = '13.00';
$total_shipping_tax_excl = '10.83';

/*
         *  1. Create new customer
         */
        if(!$id_customer){
			
            // Getting the empty XML document to send back completed
            $xml = $webService->get( array( 'url' => PS_SHOP_PATH .'api/customers?schema=synopsis' ) );
            
            // Adding dinamic values
            // Required
            $xml->customer->passwd              = $passwd;
            $xml->customer->lastname            = $lastname;
            $xml->customer->firstname           = $firstname;
            $xml->customer->email               = $email;
            
            $xml->customer->id_default_group    = 3; // Customers    
            $xml->customer->active              = 1; 
            $xml->customer->newsletter          = 1;
            $xml->customer->newsletter_date_add = $date_now;
            $xml->customer->last_passwd_gen     = $date_now;
            $xml->customer->date_add            = $date_now;
            $xml->customer->date_upd            = $date_now;
            $xml->customer->id_gender           = $id_gender;
            $xml->customer->associations->groups->group[0]->id = 3; // customers
 
             // Adding the new customer
            $opt = array( 'resource' => 'customers' );
            $opt['postXml'] = $xml->asXML();
            $xml = $webService->add( $opt );
            $id_customer = $xml->customer->id;
			
        }
        
        /*
        * 2. Create an address
        */
        if(!$id_address){
            // Getting the empty XML document to send back completed
            $xml = $webService->get( array( 'url' => PS_SHOP_PATH .'api/addresses?schema=blank' ) );
 
            // Adding dinamic and mandatory fields
            // Required
            $xml->address->id_customer  = $id_customer;
            $xml->address->id_country   = $id_country;
            $xml->address->alias        = 'Principale';
            $xml->address->lastname     = $lastname;
            $xml->address->firstname    = $firstname;
            $xml->address->city         = $city;
            $xml->address->address1     = $address1;
            // Others
            $xml->address->phone_mobile = $phone_mobile;
            $xml->address->postcode     = $ZIP;
            $xml->address->date_add     = $date_now;
            $xml->address->date_upd     = $date_now;
 
            // Adding the new Customer's Addresss
            $opt = array( 'resource' => 'addresses' );
            $opt['postXml'] = $xml->asXML();
            $xml = $webService->add( $opt );
            $id_address = $xml->address->id;   
        }
 
        /*
         * 3. Create new cart
         * 
         */
		$products = [
			[
			'id_product' => 202,
			'reference' => 202,
			'id_product_attribute' => 0,
			'quantity' => 1,
			'product_price' => Product::getPriceStatic(202), //ttc
			'name' => Product::getProductName(202),
			],
			[
			'id_product' => 301,
			'reference' => 301,
			'id_product_attribute' => 0,
			'quantity' => 1,
			'product_price' => Product::getPriceStatic(301), //ttc
			'name' => Product::getProductName(301),
			]
		];
	

	
        if(!$id_cart){
            // Getting the empty XML document to send back completed
            $xml = $webService->get( array( 'url' => PS_SHOP_PATH .'api/carts?schema=blank' ) );
 
            $xml->cart->id_currency         = 1;
            $xml->cart->id_lang             = 1;
			$xml->cart->id_shop				= 1;
			$xml->cart->id_shop_group 		= 1;
			
			for($i=0; $i<count($products);$i++) {
			
				$xml->cart->associations->cart_rows->cart_row[$i]->id_product            = $products[$i]['id_product'];
				$xml->cart->associations->cart_rows->cart_row[$i]->id_product_attribute  = $products[$i]['id_product_attribute'];
				$xml->cart->associations->cart_rows->cart_row[$i]->id_address_delivery   = $id_address;
				$xml->cart->associations->cart_rows->cart_row[$i]->quantity              = $products[$i]['quantity'];
			}
            // Others
            $xml->cart->id_address_delivery = $id_address;
            $xml->cart->id_address_invoice  = $id_address;
            $xml->cart->id_customer         = $id_customer;
            $xml->cart->id_carrier          = $id_carrier;
            $xml->cart->date_add            = $date_now;
            $xml->cart->date_upd            = $date_now;
 
            // Adding the new customer's cart        
            $opt = array( 'resource' => 'carts' );
            $opt['postXml'] = $xml->asXML();
            $xml = $webService->add( $opt );
            $id_cart = $xml->cart->id;   
        }
       
        
        /*
        * 4. Create the order 
        * 
        */
        // Getting the structure of an order
		$xml = $webService->get(array('url' => PS_SHOP_PATH .'api/orders/?schema=blank'));
 
        // Adding dinamic and required fields
        // Required
		$xml->order->id_address_delivery    = $id_address; // Customer address
		$xml->order->id_address_invoice     = $id_address;        
		$xml->order->id_cart                = $id_cart; 
		$xml->order->id_currency            = 1;
		$xml->order->id_lang                = 1;
        $xml->order->id_customer            = $id_customer; 
		$xml->order->id_carrier             = $id_carrier;
        $xml->order->module                 = 'ps_wirepayment';
		$xml->order->payment                = 'Transfert bancaire';        
        // Others
		$xml->order->valid                      = 1; 
        $xml->order->current_state              = $id_status;        
        $xml->order->total_discounts            = 0;
        $xml->order->total_discounts_tax_incl   = 0;
        $xml->order->total_discounts_tax_excl   = 0;
        // Order Row. Required
		//$total_paid = 0;
		$total_products = 0;
		$total_products_wt = 0;
		$total_paid_tax_excl = 0;
		$total_paid_tax_incl = 0;
	
		for($i=0;$i<count($products);$i++){
			$Prod = new Product($products[$i]['id_product']);
			$price_ht =  $Prod->getPrice(false);
			$price_ttc = $Prod->getPrice(true);

			$xml->order->associations->order_rows->order_row[$i]->product_id             = $products[$i]['id_product'];
			$xml->order->associations->order_rows->order_row[$i]->product_attribute_id   = $products[$i]['id_product_attribute'];
			$xml->order->associations->order_rows->order_row[$i]->product_quantity       = $products[$i]['quantity'];
			// Order Row. Others
			$xml->order->associations->order_rows->order_row[$i]->product_name           = $products[$i]['name'];
			$xml->order->associations->order_rows->order_row[$i]->product_reference      = $products[$i]['reference'];
			$xml->order->associations->order_rows->order_row[$i]->product_price          = $products[$i]['product_price'];
			$xml->order->associations->order_rows->order_row[$i]->unit_price_tax_incl    = $products[$i]['product_price'];
			$xml->order->associations->order_rows->order_row[$i]->unit_price_tax_excl    = $price_ht;
			$total_paid_tax_excl += ($price_ht * $products[$i]['quantity']) + $total_shipping_tax_excl;
			$total_paid_tax_incl += ($price_ttc * $products[$i]['quantity']) + $total_shipping_tax_incl;
			$total_products += $price_ht * $products[$i]['quantity'];
			$total_products_wt += $price_ttc * $products[$i]['quantity'];
		}
		
        $xml->order->total_paid_tax_incl        = $total_paid_tax_incl;
        $xml->order->total_paid_tax_excl        = $total_paid_tax_excl;
        $xml->order->total_shipping             = $total_shipping;
        $xml->order->total_shipping_tax_incl    = $total_shipping_tax_incl;
        $xml->order->total_shipping_tax_excl    = $total_shipping_tax_excl;
	    $xml->order->total_paid             = $total_paid_tax_excl;
        $xml->order->total_paid_real        = $total_paid_tax_excl;
        $xml->order->total_products         = $total_products;
        $xml->order->total_products_wt      = $total_products_wt;
        $xml->order->conversion_rate        = 1;


	// Creating the order
        $opt = array( 'resource' => 'orders' );
        $opt['postXml'] = $xml->asXML();
        $xml = $webService->add( $opt );
        $id_order = $xml->order->id;   
	// Order History
		$xml = $webService->get(array('url' => PS_SHOP_PATH.'api/order_histories?schema=blank'));
		$xml->order_history->id_order = $id_order;
		$xml->order_history->id_employee  = '1';
		$xml->order_history->id_order_state = $id_status;
		$opt = array('resource' => 'order_histories');
		$opt['postXml'] = $xml->asXML();
		$xml = $webService->add($opt);
	
       
	echo "Customer: ".$id_customer." address: ".$id_address." cart: ".$id_cart." Order: .".$id_order;
        
        
}  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();
}

Le client se crée bien, avec l'adresse OK. Le panier se crée aussi avec les bons produits. 

Le problème se situe au niveau de la commande et principalement au niveau de current_state qui reste à 0 et surtout aucune entrée dans order_history ! 

Voici le débug :

Fatal error: Uncaught Error: Call to a member function get() on null in /homepages/21/d473697229/htdocs/monsite/classes/Tools.php:801
Stack trace:
#0 /homepages/21/d473697229/htdocs/monsite/classes/Tools.php(773): ToolsCore::getContextLocale(Object(Context))
#1 /homepages/21/d473697229/htdocs/monsite/classes/PaymentModule.php(421): ToolsCore::displayPrice(349, Object(Currency), false)
#2 /homepages/21/d473697229/htdocs/monsite/classes/order/Order.php(1692): PaymentModuleCore->validateOrder('53675', '11', '535.993333', 'Virement bancai...', NULL, Array, NULL, false, 'a89735207537017...')
#3 /homepages/21/d473697229/htdocs/monsite/classes/webservice/WebserviceRequest.php(1623): OrderCore->addWs()
#4 /homepages/21/d473697229/htdocs/monsite/classes/webservice/WebserviceRequest.php(1409): WebserviceRequestCore->saveEntityFromXml(201)
#5 /homepages/21/d473697229/htdocs/monsite/classes/webservice/WebserviceRequest.php(584): WebserviceRequestCore->execut in /homepages/21/d473697229/htdocs/monsite/classes/Tools.php on line 801

J'ai fait énormément de recherches...sans succès. Merci d'avance à ceux qui voudront bien m'aider.

Share this post


Link to post
Share on other sites

En fait, tout se résume à la ligne d'erreur 

Fatal error: Uncaught Error: Call to a member function get() on null in /homepages/21/d473697229/htdocs/monsite/classes/Tools.php:801

J'ai vu plusieurs posts relatifs à ce sujet pour des versions de PS 1.7.6 qui avaient été upgradées depuis une antérieure. Un problème avec les modules de paiement. Un bug dans la table ps_currency, je crois...mais dans mon cas il s'agit d'une installation nouvelle et les paiements standards par les clients fonctionnent très bien.

Ce bug se produit uniquement à la création de commande par le WebService. Help ! 
Merci beaucoup.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More