簡體   English   中英

如何使用 Shopify API 取消訂單和退款?

[英]How can orders be cancelled and refunded using the Shopify API?

我一直在嘗試使用 php 中的 shopify API 取消和退款我的一個訂單。 當我向 Shopify API 發送 curl 請求時,其響應為空白。

這是我的代碼:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://3de0c1f89e936d4f52b29aXXXXXXX:8ae242d39b8fde6b0f8fe04fXXXXXXXX@ABC-XYZ.myshopify.com/admin/orders/145785434/cancel.json?amount=45.5");
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_HTTPGET, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$response = curl_exec($ch);
$result = json_decode($response);
curl_close($ch);
echo '<pre>';
print_r($result);
echo '</pre>';
?> 

我錯過了什么。 請幫忙。 提前致謝。

這不是代碼的問題。 這是您嘗試訪問的地址(或介於兩者之間的任何地址)的問題。 您需要自己調試問題。 首先,啟用標頭輸出並查看您收到的響應。

curl_setopt($ch, CURLOPT_HEADER, true); 

在將接收到的數據傳遞給 JSON 解碼器之前,回顯它以查看它是否已損壞或空白。

echo $response;

取消Shopify API 函數應通過 POST 方法發送。 在此處輸入圖片說明

我分享了我用來取消訂單的代碼:

    $url = "https://API_KEY:API_PASS@YOUR_SHOP.myshopify.com/admin/api/2021-01/orders/$orderId/cancel.json";
            
    $curl = curl_init();
    
    $data = new stdClass();
    $data->reason = 'declined';

    $jsonData = json_encode($data);
    
    //Set the URL that you want to GET by using the CURLOPT_URL option.
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-type: application/json'
    ));
    //curl_setopt($curl, CURLOPT_HEADER, true);
    
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);

    //Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable.
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    //additional options
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    //Set CURLOPT_FOLLOWLOCATION to true to follow redirects.
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    //Execute the request.
    $result = curl_exec($curl);

如果您想發送更多參數,請將它們添加到$data變量中。 例子:

$data->amount = '45.5';
$data->currency = 'USD';

在發送金額時檢查貨幣是否為必填字段。 Shopify 訂單 API 文檔中查看更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM