Php 必须使用平方API设置幂等键

Php 必须使用平方API设置幂等键,php,api,square-connect,idempotent,Php,Api,Square Connect,Idempotent,尝试使用Square电子商务API。运行测试充电时,我收到以下响应: object(stdClass)#14 (1) { ["errors"]=> array(1) { [0]=> object(stdClass)#13 (4) { ["category"]=> string(21) "INVALID_REQUEST_ERROR" ["code"]=> string(26) "MISSING_REQ

尝试使用Square电子商务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"
    }
  }
}
我不太明白为什么它不能设置幂等键。我以为这是由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>";
}

?>

看起来您正在使用一些过时的示例代码。请在此处尝试代码:


主要区别在于请求中缺少
授权
参数。

这很奇怪,您能告诉我您使用的是哪个版本的PHP SDK吗?我相信它是5.4.0whoa,这似乎不对,可能是您正在运行的PHP版本。您有composer.json文件吗?你能告诉我“square/connect”旁边写的是什么吗?是的,我知道。它只是说“*”下面是我在该文件中的所有内容:{“require”:{“square/connect”:“*”}}很好,谢谢。我使用的是沙盒细节,所以目前没有真正的充电,但它显示了成功。只是想一想,当我在文档中苦苦寻找任何东西时,当我向一张卡收费时,付款在哪里存入?我看不到代码中有任何定义。它被存入你的square帐户。我不确定我是否理解了您的问题thoughTristan,根据您的建议,我已经成功地使用了使用沙箱细节的API。然而,当测试我的真实细节时,它说卡被拒绝了。我知道这不是我的账户的错,因为其他网站上的其他购买都很好,我只是在尝试处理一小部分。我想知道使用上面提供的代码设置它的方式是否有任何问题?i、 是否需要更多信息?就如我在你的个人资料中看到的那样,你是一个开发人员。希望你能帮忙。你能用那张卡和虚拟终端进行交易吗?其他卡片有用吗?您从API得到的确切响应是什么?如果您所说的虚拟终端是指使用沙盒凭据充电时,那么是的,它正在工作。没有,我试过几张牌,没有乐趣。答复如下: