Xsolla付款POSTFIELDS错误

时间:2018-05-09 12:24:10

标签: php json curl

我正在尝试在我的网站上使用Xsolla支付系统......他们有一个PHP SDK,但我在使用它时遇到了问题 - 所以我通常使用它来使用cURL。

Xsolla文档中的cURL命令是:

curl -v https://api.xsolla.com/merchant/merchants/{merchant_id}/token \
-X POST \
-u your_merchant_id:merchant_api_key \
-H 'Content-Type:application/json' \
-H 'Accept: application/json' \
-d '
{
    "user": {
        "id": {
            "value": "1234567"
        },
    },
    "settings": {
        "project_id": 14004
    },
    "purchase": {
        "virtual_items": {
            "items": {
              "sku": "item"
            },
      },
  }
}'

我将它转换为PHP:

<?php

$ch = curl_init();

$data = array(
    "user" => array(
        'id' => array('value' => '1234567')
    ),
    "settings" => array(
        'project_id' => 29039
    ),
    "purchase" => array(
        'virtual_items' => array(
            'items' => array(
                'sku' => '1'
            )
        )
    )
);

curl_setopt($ch, CURLOPT_URL, "https://api.xsolla.com/merchant/merchants/{My merchant id}/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "{My merchant id}" . ":" . "{My api key}");

$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Accept: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

echo $result;
curl_close ($ch);
?>

但是我得到:{ "http_status_code": 422, "message": "The parameter settings.project_id is required.", "extended_message": { "global_errors": [], "property_errors": [] }, "request_id": "bfd6f72" }

我确定问题出在$data,但我不知道如何修复它。

任何帮助都将不胜感激。

使用@Magnus Eriksson解决方案后更新(仍未解决):

$data = array(
    "user" => array(
        'id' => array('value' => '1234567')
    ),
    "settings" => array(
        'project_id' => 29039
    ),
    "purchase" => array(
        'virtual_items' => array(
            'items' => array(
                'sku' => '1'
            )
        )
    )
);
$data_json = json_encode($data);

现在我正在使用curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);而不是$ data。

我现在得到的错误是:{ "http_status_code": 422, "message": "JSON is not valid against json schema, please check documentation http:\/\/developers.xsolla.com\/api.html#payment-ui", "extended_message": { "global_errors": [], "property_errors": { "purchase.virtual_items.items": [ "Object value found, but an array is required" ] } }, "request_id": "34dc63e" }

谢谢!

1 个答案:

答案 0 :(得分:1)

documentation表示项目应该是一个数组。你可以尝试这样的事情:

$data = array(
    "user" => array(
        'id' => array('value' => '1234567')
    ),
    "settings" => array(
        'project_id' => 29039
    ),
    "purchase" => array(
        'virtual_items' => array(
            'items' => array(
                0 => array(
                'sku' => '1',
                'amount' => 1
               )
            )
        )
    )
);

服务的回复中也解释了这一点:

  

“purchase.virtual_items.items”:[“找到对象值,但数组是   要求“]

确保对数组进行JSON编码并使用“POSTFIELDS”中的JSON编码数据,如下所示:

$json_data = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);