ajd2598
ajd2598

Reputation: 25

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

I'm hoping to get a little assistance with something that is probably pretty basic -- I'm attempting to deploy the Square Checkout API with my website. I've been able to successfully install the SDK, and I've used it to successfully pull my sandbox location ID, to test it's function.

I've proceeded to build a page employing only the demo script on the Checkout API page, as seen below:

<?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;
}

I get a 500 error from my webserver when attempting to run this script through my browser, in my httpd error_log I get the following error message:

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

Any thoughts on why the getId() method is undefined? Thanks.

UPDATE

I commented out the function calls after the createCheckout() portion of the try{} block, and then ran a var_dump() on $result to make sure I was in fact getting some sort of response. I am getting back the expected result! So I KNOW the API/SDK is working now, I just can't figure out why the $result object is unable to accept the follow-up functions.

Revised try block:

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. 

Any thoughts based on this revision? -A

Upvotes: 0

Views: 1104

Answers (2)

sjosey
sjosey

Reputation: 1348

The CreateCheckoutResponse doesn't have the getId() function. It has getCheckout() and getErrors(). So you need to:

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

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

Upvotes: 1

ajd2598
ajd2598

Reputation: 25

Current Solution:

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>';

Since I am getting a valid object back, and all I need to do is extract the ID and checkout URL's from the $result object, I used the function above to convert the object to an array, and from here I'll extract the information I need by key => Value pairing. It's ugly, and it doesn't solve why these post API-call functions included with the SDK aren't working, but it's meeting my immediate solution.

If anyone can tell me what actually happened that prevented the SDK function calls from being defined, I'd appreciate it.

Upvotes: 0

Related Questions