Vipul jain
Vipul jain

Reputation: 129

Place Complete order in prestashop using webservice

I am trying to add an order in prestashop through webservices. But my order is getting payment error every time. The problem is my discount and shipping is not reflecting in the order panel in admin dashboard.

Here is my code:

$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);


$customer = array();
$product = array();


$id['country'] = '165';
$id['lang'] = $raw_data['id_lang'];
$id['currency'] = $raw_data['id_currency'];
$id['carrier'] = $raw_data['id_carrier'];

$product['order_rows'] = $raw_data['order_rows'];
//            $product['id'] = $id_product;
//            $product['price'] = Product::getPriceStatic($product['id']);
//            $product['name'] = Product::getProductName($product['id']);
//$product['total'] = $product['price'] * $product['quantity'];

$customerid = $raw_data['id_customer'];
// ID of created customer

$id['customer'] = $customerid;

// CREATE Address

$addresid = $raw_data['id_address_delivery'];
// ID of created address

$id['address'] = $addresid;

// CREATE Cart
$cartid = $raw_data['id_cart'];
// ID of created cart

$id['cart'] = $cartid;





//$sql = "select  id_order from ps_orders order by id_order desc limit 0,1";
//$r = mysql_query($sql, $conn)or die(mysql_error($conn));
//$row = mysql_fetch_assoc($r);
//// CREATE Order
//$id['order'] = $row['id_order'] + 1;


$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 = "0";
$xml->order->total_paid_tax_incl = "0";
$xml->order->total_paid_tax_excl ="0";
$xml->order->total_paid_real = "0";



$xml->order->conversion_rate = '1';

//            foreach ($products as $product){
//            $xml->order->associations->order_rows->order_row->product_id = $product['id'];
//            $xml->order->associations->order_rows->order_row->product_quantity = $product['quantity'];
//            }

$opt = array('resource' => 'orders');
$opt['postXml'] = $xml->asXML();
 $xml = $webService->add($opt);
//$xml=$webService->parseXML($xml);
$id['order'] = $xml->order->id;
//var_dump($xml->order);

$id['secure_key'] = $xml->order->secure_key;
$xml = $webService->get(array('url' => PS_SHOP_PATH . '/api/order_carriers?schema=blank'));

$xml->order_carrier->id_carrier = $id['carrier'];
$xml->order_carrier->id_order = $id['order'];
$xml->order_carrier->shipping_cost_tax_excl = $raw_data['total_shipping'];
$xml->order_carrier->shipping_cost_tax_incl = $raw_data['total_shipping'];
$opt = array('resource' => 'order_carriers');

$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);


foreach ($raw_data['discounts'] as $discount) {
    $xml = $webService->get(array('url' => PS_SHOP_PATH . '/api/order_discounts?schema=blank'));

    $xml->order_cart_rule->id_cart_rule = $discount['id_cart_rule'];
    $xml->order_cart_rule->id_order = $id['order'];
    $xml->order_cart_rule->value = $discount['value'];
    $xml->order_cart_rule->name = $discount['name'];
    $xml->order_cart_rule->value_tax_excl = $discount['value'];

    $opt = array('resource' => 'order_discounts');

    $opt['postXml'] = $xml->asXML();
    $xml = $webService->add($opt);
}
$sql = "update ps_order_history set id_order_state=3 where id_order=" . $id['order'];
$r = mysql_query($sql, $conn)or die(mysql_error($conn));

$xml = $webService->get(array('url' => PS_SHOP_PATH . '/api/orders/'.$id['order']));

$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',"id"=>$id['order']);
$opt['putXml'] = $xml->asXML();
$xml = $webService->edit($opt);
$id['orderrefer'] = $xml->order->reference;

The problem is with admin dashboard. Coupons and shipping is not getting applied. so it is changing my payment status to payment error

Upvotes: 2

Views: 2596

Answers (2)

Pallavi Jain
Pallavi Jain

Reputation: 5667

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.

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: 3

Vipul jain
Vipul jain

Reputation: 129

I have solved my problem by direct entries to db tables for shipping and discounts.

Upvotes: 2

Related Questions