java2dev
java2dev

Reputation: 105

How to Post new order using shopify API

I've tried to create new order using a test from postman or ARC and it's work. But when I did from my php code I cannot Post neither Get orders (I can get products for example).

This is my code Class.Shopify.php

<?php 
     public function call($method, $path, $params=array())
{
    $baseurl = "https://{$this->shop_domain}/";
//var_dump($this->shop_domain);  

    $url = $baseurl.ltrim($path, '/');
    $query = in_array($method, array('GET','DELETE')) ? $params : array();
    $payload = in_array($method, array('POST','PUT')) ? json_encode($params) : array();
    $request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();

    // add auth headers
    $request_headers[] = 'X-Shopify-Access-Token: ' . $this->token;

    $response = $this->curlHttpApiRequest($method, $url, $query, $payload, $request_headers);
//var_dump($response);
        $response = json_decode($response, true);

    if (isset($response['errors']) or ($this->last_response_headers['http_status_code'] >= 400))
        throw new ShopifyApiException($method, $path, $params, $this->last_response_headers, $response);

    return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response;
}
private function curlHttpApiRequest($method, $url, $query='', $payload='', $request_headers=array())
{
    $url = $this->curlAppendQuery($url, $query);
    //echo $url.' '.$payload;
    $ch = curl_init($url);
/*var_dump($ch);
            var_dump($method);
            var_dump($payload);
            var_dump($request_headers);*/

            $this->curlSetopts($ch, $method, $payload, $request_headers);
    $response = curl_exec($ch);
    //var_dump($response);
            $errno = curl_errno($ch);
    $error = curl_error($ch);
    curl_close($ch);

    if ($errno) throw new ShopifyCurlException($error, $errno);
    list($message_headers, $message_body) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
    $this->last_response_headers = $this->curlParseHeaders($message_headers);

    return $message_body;
}

private function curlAppendQuery($url, $query)
{
    if (empty($query)) return $url;
    if (is_array($query)) return "$url?".http_build_query($query);
    else return "$url?$query";
}

private function curlSetopts($ch, $method, $payload, $request_headers)
{
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    /*WHEN CODE DEPLOY TO LIVE SERVER, CURLOPT_SSL_VERIFYPEER MUST BE TRUE*/
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_USERAGENT, 'ohShopify-php-api-client');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $method);
    if (!empty($request_headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);

    if ($method != 'GET' && !empty($payload))
    {
        if (is_array($payload)) $payload = http_build_query($payload);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $payload);
    }
}
?>

a part of my file to post order.php

$credential = $qdb->fetch();
//var_dump($credential);

//$id = $_REQUEST['id'];
$mail = $credential['mail'];
$url =  $credential['site'];
$key = $credential['key'];
$token = $credential['token'];
$country = $credential['name'];
$id_country = $credential['id_country'];
ob_start();
//where Shopify api key and shopify secret are the credentials for the orders private app
$shop = new  ShopifyClient($url, $credential['token'], SHOPIFY_API_KEY, SHOPIFY_SECRET);

$orderData = array('order' => array(
'line_items' => array(
    array(
        'variant_id' => 30781726924,
        'quantity' => 1
    )
)));
$product = $shop->call('POST', "/admin/orders.json", $orderData);
var_dump($product);
$content = ob_get_contents();
file_put_contents('update-pr.txt', $content);
?>

As I said I can get product but no orders and can't post orders. I have orders in private app in "read and write". Thanks in advance for your help.

Upvotes: 2

Views: 3269

Answers (1)

nibnut
nibnut

Reputation: 3127

Based on the message in your logs, it seems you do not have write access to the order entity.

When you ask users for permissions, during the app installation, you need to specify all the permissions you think your app will need.

The authorize URL will look something like:

https://{shop}.myshopify.com/admin/oauth/authorize?client_id={api_key}&scope={scopes}&redirect_uri={redirect_uri}&state={nonce}&grant_options[]={option}

where scope will be, in your case, "read_products,write_products, read_orders,write_orders"

Just defining the constant in your PHP file will not be enough - the merchant, on whose store your app is installed, needs to grant you permission to do what you want to do.

Note also that you can't change those permissions on an installed app, either. You will have to first uninstall the app, make sure your code asks for the right permissions, and then re-install the app with the new permissions.

You'll see which permissions your app is asking for on the app authorization screen.

Hope this helps!

Upvotes: 3

Related Questions