【问题标题】:getId() method is undefined in sandbox Square Checkout API script沙盒 Square Checkout API 脚本中未定义 getId() 方法
【发布时间】:2018-08-15 23:20:58
【问题描述】:

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

我已经着手构建一个仅使用 Checkout 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 &lt;LOCATION&gt;:62\nStack trace:\n#0 {main}\n thrown in &lt;LOCATION&gt; 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

【问题讨论】:

    标签: php square-connect


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      当前解决方案:

      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 中包含的这些后 API 调用函数不起作用,但它符合我的直接解决方案。

      如果有人能告诉我阻止定义 SDK 函数调用的实际情况,我将不胜感激。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-08
        • 1970-01-01
        • 2017-06-19
        • 2013-05-09
        • 2011-05-31
        • 1970-01-01
        • 1970-01-01
        • 2015-10-26
        相关资源
        最近更新 更多