idempotency_key должен быть установлен с ошибкой при помощи Square API

Попытка использовать Square e-commerce API. При проведении пробной зарядки получаю следующий ответ:

object(stdClass)#14 (1) {
["errors"]=>
array(1) {
[0]=>
object(stdClass)#13 (4) {
["category"]=>
string(21) "INVALID_REQUEST_ERROR"["code"]=>
string(26) "MISSING_REQUIRED_PARAMETER"["detail"]=>
string(17) "Field must be set"["field"]=>
string(15) "idempotency_key"}
}
}

Я не могу понять, почему он не смог установить idempotenct_key. Я думал, что это решается с помощью функции uniqid (). Пожалуйста, порекомендуйте. Мой код выглядит следующим образом:

<?php
require 'vendor/autoload.php';
# Replace these values. You probably want to start with your Sandbox credentials
# to start: https://docs.connect.squareup.com/articles/using-sandbox/
# The ID of the business location to associate processed payments with.
# If you're testing things out, use a sandbox location ID.
#
# See [Retrieve your business's locations](https://docs.connect.squareup.com/articles/getting-started/#retrievemerchantprofile)
# for an easy way to get your business's location IDs.

# The access token to use in all Connect API requests. Use your *sandbox* access
# token if you're just testing things out.
$access_token = 'REPLACE_ME';

SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($access_token);

$locations_api = new \SquareConnect\Api\LocationsApi();

$location_id = $locations_api->listLocations($access_token);
# Helps ensure this code has been reached via form submission
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
error_log("Received a non-POST request");
echo "Request not allowed";
http_response_code(405);
return;
}
# Fail if the card form didn't send a value for `nonce` to the server
$nonce = $_POST['nonce'];
if (is_null($nonce)) {
echo "Invalid card data";
http_response_code(422);
return;
}
$transactions_api = new \SquareConnect\Api\TransactionsApi();
$request_body = array (
"card_nonce" => $nonce,
# Monetary amounts are specified in the smallest unit of the applicable currency.
# This amount is in cents. It's also hard-coded for $1.00, which isn't very useful.
"amount_money" => array (
"amount" => 100,
"currency" => "USD"),
# Every payment you process with the SDK must have a unique idempotency key.
# If you're unsure whether a particular payment succeeded, you can reattempt
# it with the same idempotency key without worrying about double charging
# the buyer.
"idempotency_key" => uniqid()
);
# The SDK throws an exception if a Connect endpoint responds with anything besides
# a 200-level HTTP code. This block catches any exceptions that occur from the request.
try {
$result = $transactions_api->charge($access_token, $location_id, $request_body);
echo "<pre>";
print_r($result);
echo "</pre>";
} catch (\SquareConnect\ApiException $e) {
echo "Caught exception!<br/>";
print_r("<strong>Response body:</strong><br/>");
echo "<pre>"; var_dump($e->getResponseBody()); echo "</pre>";
echo "<br/><strong>Response headers:</strong><br/>";
echo "<pre>"; var_dump($e->getResponseHeaders()); echo "</pre>";
}

?>

0

Решение

Похоже, вы используете устаревший пример кода. Попробуйте код здесь: https://github.com/square/connect-api-examples/blob/master/connect-examples/v2/php_payment/process-card.php

Основным отличием является отсутствие authorization параметр в запросе.

0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]