2

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!

Mario
  • 1,374
  • 6
  • 22
  • 48
  • It seems like the service is expecting JSON, not ordinary POST fields. – M. Eriksson May 09 '18 at 12:26
  • Possible duplicate of [Curl and PHP - how can I pass a json through curl by PUT,POST,GET](https://stackoverflow.com/questions/21271140/curl-and-php-how-can-i-pass-a-json-through-curl-by-put-post-get) – M. Eriksson May 09 '18 at 12:27
  • 1
    I tried to add `$data_json = json_encode($data);` and use the encoded JSON in the POSTFIELDS instead of the array but I get another error. The new error is `JSON is not valid against json schema` @MagnusEriksson – Mario May 09 '18 at 12:31
  • That sounds strange. Please show us your attempt. – M. Eriksson May 09 '18 at 12:33
  • 1
    Updated with the new code. @MagnusEriksson – Mario May 09 '18 at 12:35
  • That's odd. Especially since their example doesn't even follow the JSON specification (trailing commas aren't allowed according to the JSON Spec). Try their example from the command line but with your JSON instead. If that doesn't work, then you should contact them. – M. Eriksson May 09 '18 at 12:42
  • 1
    So documentation error? – Mario May 09 '18 at 12:43

1 Answers1

1

The documentation says items should be an array. You can try something like this:

$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
               )
            )
        )
    )
);

That is also explained in the service's response:

"purchase.virtual_items.items": [ "Object value found, but an array is required" ]

Make sure to JSON-encode the array and use the JSON-encoded data in "POSTFIELDS", like that:

$json_data = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
Mario
  • 1,374
  • 6
  • 22
  • 48
alpadev
  • 470
  • 9
  • 23
  • 1
    I see you missed a comma after `'sku' => '1'` - I fixed it and still get `{ "http_status_code": 422, "message": "The parameter settings.project_id is required.", "extended_message": { "global_errors": [], "property_errors": [] }, "request_id": "6f097be" }` - @alpadev – Mario May 09 '18 at 12:54
  • @MatrixCow08, keep sending it as JSON like Magnus Eriksson suggested – alpadev May 09 '18 at 12:55