getId() 方法在沙盒 Square Checkout API 脚本中未定义

getId() method is undefined in sandbox Square Checkout API script

我希望得到一些可能非常基本的帮助 -- 我正在尝试在我的网站上部署 Square Checkout API。我已经能够成功安装 SDK,并使用它成功提取了我的沙箱位置 ID,以测试它的功能。

我已经着手构建一个仅使用结帐 API 页面上的演示脚本的页面,如下所示:

<?php

#Set the required includes globally
require_once '../config.php';
require INC_PATH . '/squareup/autoload.php';

/* 
** Script for submitting payment information
** Utilizing Square API documentation at:
** https://docs.connect.squareup.com/payments/checkout/setup
*/

//Replace your access token and location ID
$accessToken = '<MY SANDBOX KEY>'; // Sandbox 
$locationId = '<MY SANDBOX LOCATION ID>'; // Sandbox

// Create and configure a new API client object
$defaultApiConfig = new \SquareConnect\Configuration();
$defaultApiConfig->setAccessToken($accessToken);
$defaultApiClient = new \SquareConnect\ApiClient($defaultApiConfig);
$checkoutClient = new SquareConnect\Api\CheckoutApi($defaultApiClient);

//Create a Money object to represent the price of the line item.
$price = new \SquareConnect\Model\Money;
$price->setAmount(600);
$price->setCurrency('USD');

//Create the line item and set details
$book = new \SquareConnect\Model\CreateOrderRequestLineItem;
$book->setName('The Shining');
$book->setQuantity('2');
$book->setBasePriceMoney($price);

//Puts our line item object in an array called lineItems.
$lineItems = array();
array_push($lineItems, $book);

// Create an Order object using line items from above
$order = new \SquareConnect\Model\CreateOrderRequest();

$order->setIdempotencyKey(uniqid()); //uniqid() generates a random string.

//sets the lineItems array in the order object
$order->setLineItems($lineItems);

## STEP 2: Create a checkout object
$checkout = new \SquareConnect\Model\CreateCheckoutRequest();
$checkout->setIdempotencyKey(uniqid()); //uniqid() generates a random string.
$checkout->setOrder($order); //this is the order we created in the previous step

try {
    $result = $checkoutClient->createCheckout(
      $locationId,
      $checkout
    );
    //Save the checkout ID for verifying transactions
    $checkoutId = $result->getId();
    //Get the checkout URL that opens the checkout page.
    $checkoutUrl = $result->getCheckoutPageUrl();
    print_r('Complete your transaction: ' + $checkoutUrl);
} 

catch (Exception $e) {
    echo 'Exception when calling CheckoutApi->createCheckout: ', $e->getMessage(), PHP_EOL;
}

当我尝试通过浏览器 运行 此脚本时,我的网络服务器出现 500 错误,在我的 httpd error_log 中,我收到以下错误消息:

PHP Fatal error: Uncaught Error: Call to undefined method SquareConnect\Model\CreateCheckoutResponse::getId() in <LOCATION>:62\nStack trace:\n#0 {main}\n thrown in <LOCATION> on line 62

有没有想过为什么 getId() 方法未定义?谢谢

更新

我在 try{} 块的 createCheckout() 部分之后注释掉了函数调用,然后 运行 在 $result 上添加了 var_dump() 以确保我实际上得到了某种回应。我得到了预期的结果!所以我知道 API/SDK 现在正在工作,我只是想不通为什么 $result 对象无法接受后续函数。

修改后的 try 块:

try {
    $result = $checkoutClient->createCheckout(
      $locationId,
      $checkout
    );

    /*
    //Save the checkout ID for verifying transactions
    $checkoutId = $result->getId();
    //Get the checkout URL that opens the checkout page.
    $checkoutUrl = $result->getCheckoutPageUrl();
    print_r('Complete your transaction: ' + $checkoutUrl);
    */
} 

catch (\Exception $e) {
    echo 'Exception when calling CheckoutApi->createCheckout: ', $e->getMessage(), PHP_EOL;
}

var_dump($result); //test to see if any non-zero response to createCheckout() function. 

基于此修订的任何想法? -A

当前解决方案:

function objectToArray ($object) {
    if(!is_object($object) && !is_array($object))
        return $object;

    return array_map('objectToArray', (array) $object);
}

$result_array = objectToArray($result);

echo '<pre>';
var_dump($result_array); //test to see if any non-zero response to createCheckout() function. 
echo '</pre>';

因为我得到了一个有效的对象,我需要做的就是从 $result 对象中提取 ID 和结帐 URL,我使用上面的函数将对象转换为数组,从这里我将通过键 => 值配对提取我需要的信息。这很丑陋,它并没有解决为什么 SDK 中包含的这些 post API 调用函数不起作用的问题,但它满足了我的即时解决方案。

如果有人能告诉我到底发生了什么阻止定义 SDK 函数调用,我将不胜感激。

CreateCheckoutResponse 没有 getId() 功能。它有 getCheckout()getErrors()。所以你需要:

$checkoutId = $result->getCheckout()->getId();

参考:https://github.com/square/connect-php-sdk/blob/master/docs/Model/CreateCheckoutResponse.md