【发布时间】: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 <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
【问题讨论】:
标签: php square-connect