I am trying to use the Xsolla Payments System on my site... They have a PHP SDK but I have problems using it - so I am using it normally usisng cURL.
The cURL command in the Xsolla documentation is:
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"
},
},
}
}'
I converted it to PHP like that:
<?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);
?>
However I get: { "http_status_code": 422, "message": "The parameter settings.project_id is required.", "extended_message": { "global_errors": [], "property_errors": [] }, "request_id": "bfd6f72" }
I am sure the problem is in $data
but I don't know how to fix it.
Any help would be appreciated.
UPDATE after using the solution of @Magnus Eriksson (still not solved):
$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);
And now I am using curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
instead of $data.
The error I get now is: { "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" }
Thanks!