OrderWise API Integration

  php

Want to send an order through to a customer’s OW backend via OrderWise API but only getting Order number SO-1485 cannot be imported as the imported order gross does not match the calculated order gross. I tried adding deliverycost to ordergross with no joy. Also tried adding matching values in itemGross and orderGross, no go.

    $lines = [];
    $date = date('Y-m-dTH:i:s');
    $orderwiseid = $user['orderwise_id'] != '' ? $user['orderwise_id'] : 0;
    $orderid = $_SESSION['ecommerce']['user']['checkout']['order_id'];
    $sub = $_SESSION['ecommerce']['user']['basket']['totals']['subtotal'];
    $grand = $_SESSION['ecommerce']['user']['basket']['totals']['grandtotal'];
    $delivercost = $_SESSION['ecommerce']['user']['checkout']['delivery_method']['cost'];
    $delivername = $_SESSION['ecommerce']['user']['checkout']['delivery']['first_name'] . " " . $_SESSION['ecommerce']['user']['checkout']['delivery']['surname'];
    $deliveraddr1 = $_SESSION['ecommerce']['user']['checkout']['delivery']['address_line_1'];
    $delivercity = $_SESSION['ecommerce']['user']['checkout']['delivery']['address_city'];
    $deliverpostcode = $_SESSION['ecommerce']['user']['checkout']['delivery']['address_postcode'];
    $get_order   = $db->shorthand("SELECT * FROM `ecommerce_orders` WHERE `id` = :id", ["id" => $_SESSION['ecommerce']['user']['checkout']['order_id']])->fetch(PDO::FETCH_ASSOC);
    $order_items = $db->shorthand("SELECT * FROM `ecommerce_order_items` WHERE `order_id` = :id", ["id" => $_SESSION['ecommerce']['user']['checkout']['order_id']])->fetchAll(PDO::FETCH_ASSOC);
    $orderdate = $get_order['added_date'];
    foreach($order_items as $o) {
        $product = $db->shorthand("SELECT * FROM `ecommerce_products` WHERE `id` = :id", ["id" => $o['product_id']])->fetch(PDO::FETCH_ASSOC);
        $totals = calculate_price($_SESSION['ecommerce']['user']['basket']['items'], $_SESSION['ecommerce']['user']['checkout']['delivery_method'], $_SESSION['ecommerce']['user']['basket']['discount'], $_SESSION['ecommerce']['user']['loggedin']['discount']);
        $discount = 0.2;
        $newgross = $totals['grandtotal']['value'] - ($totals['grandtotal']['value'] * $discount);
        $line = [
//            "variantCode"=> $product['SKU'],
            "variantCode" => 'MARLOW-J-7017',
            "itemNumber"=> '1',
            "quantity"=> $o['qty'],
            "itemGross"=> number_format($totals['grandtotal']['value'], 2),
            "itemNet"=> number_format($totals['subtotal']['value'], 2),
            "taxRateId"=> 2,
            "taxCodeId" => 1,
            "promisedDate"=> $orderdate,
            "requiredDate"=> $orderdate,
            "stockLocationId"=> 1
        ];
        array_push($lines, $line);
    }

    $data = [
        [
            "customer" => [
                "id" => $orderwiseid,
                "statementName" => $_SESSION['ecommerce']['user']['checkout']['delivery']['first_name'] . " " . $_SESSION['ecommerce']['user']['checkout']['delivery']['surname'],
                "statementTown" => ucfirst($delivercity),
            ],
            "orderHeader" => [
                "eCommerceOrderNumber"=> "SO-" . $orderid,
                "customerOrderReference"=> $orderid,
                "systemOrderType"=> 1,
                "deliveryMethodId"=> 1,
                "orderGross"=> number_format($grand, 2),
                "deliveryGross"=> number_format($delivercost ,2),
                "deliveryTaxCodeId" => 1,
                "orderDate"=> $orderdate,
                "requiredDate"=> $orderdate,
                "promisedDate"=> $orderdate,
            ],
            "customerDeliveryAddress"=> [
                "deliveryName"=> $delivername,
                "contactName"=> "Carl",
                "address1"=> $deliveraddr1,
                "town"=> $delivercity,
                "country"=> "United Kingdom",
                "countryCode"=> "GB",
                "postcode"=> $deliverpostcode,
                "deliveryMethodId"=> 1
            ],
            "orderLines"=> [
            ],
            "orderDissurs"=> [
                [
                    "description"=> "Standard Discount Value",
                    "amount"=> 20.00,
                    "taxCodeId" => 1
                ]
            ],
            "orderPayments"=> [
                [
                    "description"=> "Cash",
                    "amount"=>  number_format($grand, 2),
                    "comments"=> "Payment No. 1234",
                    "paymentMethodID" => 2
                ]
            ]
        ]
    ];
    
    foreach($lines as $l) {
        array_push($data[0]['orderLines'], $l);
    }
    echo "<pre>";
    var_dump($data);
    echo "</pre>";
    $ch = curl_init();
    $authorization = "Authorization: Bearer XXXXX";
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_URL, "http://77.68.127.101/OWAPITEST/sales/order?session_id=1");

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data[0]));

    $response = curl_exec($ch);
    $err = curl_error($ch);
    curl_close($ch);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }
    $responsenormal = json_decode($response);
    
    $newcustomerid = $responsenormal->customerId;
    if ($newcustomerid > 0)
        $user2 = $db->shorthand("UPDATE `ecommerce_customers` SET orderwise_id = :owid WHERE `id` = :id ", ["owid" => $newcustomerid, "id" => $_SESSION['ecommerce']['user']['loggedin']['id']])->fetch(PDO::FETCH_ASSOC);

Source: Ask PHP

LEAVE A COMMENT