Create payment error message

In order to create a payment I’m using this code:
require(“vendor/autoload.php”);
use Square\Exceptions\ApiException;
use Square\Exceptions;
use Square\ApiHelper;
use Square\Apis\PaymentsApi;
use Square\Models\CancelPaymentByIdempotencyKeyRequest;
use Square\Models\CompletePaymentResponse;
use Square\Models\CreatePaymentRequest;
use Square\Models\CreatePaymentResponse;
use Square\Models\Currency;
use Square\Models\GetPaymentResponse;
use Square\Models\ListPaymentsResponse;
use Square\Models\Payment;
use Square\Models\Money;

use PHPUnit\Framework\TestCase;

$amount_money = new \Square\Models\Money();
$amount_money->setAmount(2000);
$amount_money->setCurrency(‘USD’);

$body = new \Square\Models\CreatePaymentRequest(
‘cnon:CBASEEjQm2rSKpGpWP_8c-T9eWM’,
‘437861’,
$amount_money
);

$api_response = $client->getPaymentsApi()->createPayment($body);

if ($api_response->isSuccess()) {
$result = $api_response->getResult();
} else {
$errors = $api_response->getErrors();
}

I get this error Undefined variable: client Where do I get that variable? thank you.

Did you correctly configure your client:

$client = new SquareClient([
    'accessToken' => 'YOUR SANDBOX ACCESS TOKEN HERE',
    'environment' => Environment::SANDBOX,
]);

Now my problem is this: class SquareClient don’t found

:wave: To use the Square API, you import the Client class, instantiate a Client object, and initialize it with the appropriate access token. Here’s how:

Instantiate a SquareClient object with the access token for the Square account whose resources you want to manage. To access sandbox resources, initialize the SquareClient with environment set to sandbox:

use Square\SquareClient;
use Square\Environment;

$client = new SquareClient([
    'accessToken' => 'YOUR SANDBOX ACCESS TOKEN HERE',
    'environment' => Environment::SANDBOX
]);

To access production resources, set environment to production:

use Square\SquareClient;
use Square\Environment;

$client = new SquareClient([
    'accessToken' => 'YOUR PRODUCTION ACCESS TOKEN HERE',
    'environment' => Environment::PRODUCTION
]);

To set a custom environment provide a customUrl, and set the environment to

Environment::CUSTOM:
use Square\SquareClient;
use Square\Environment;

$client = new SquareClient([
    'accessToken' => 'YOUR ACCESS TOKEN HERE',
    'environment' => Environment::CUSTOM,
    'customUrl' => 'https://your.customdomain.com'
]);

For more please see the README.md of the SDK. :slightly_smiling_face:

Thank you very much for your help

I’m having another problem, I try to set amount in this way: $amount_money->setAmount($precio); but it doesn’t work, how do I have to do it?

And besides that, in this part $body = new \Square\Models\CreatePaymentRequest(
$token,
‘437861’,
$amount_money
);
if I try to use a variable $token and try to show on screen the result of the payment I get an error saying undefined variable result

Is the variable that you are passing to the $amount_money an int?

At first I didn’t but I changed it so now it is working, I can do now all the process, thank you.

Good day. Hope you’re doing good.

I’m having similar issues concerning SDK implementation. So far, I’m running it all on localhost host. Everything is setup and running, but here is the error:

Notice : Undefined offset: 0 in C:\xampp\htdocs\perzsi.com\dashboard\payment\vendor\square\square\src\SquareClient.php on line 176

Notice : Trying to access array offset on value of type null in C:\xampp\htdocs\perzsi.com\dashboard\payment\vendor\square\square\src\SquareClient.php on line 176

Fatal error : Uncaught TypeError: Argument 1 passed to Square\ApiHelper::appendUrlWithTemplateParameters() must be of the type string, null given, called in C:\xampp\htdocs\perzsi.com\dashboard\payment\vendor\square\square\src\SquareClient.php on line 180 and defined in C:\xampp\htdocs\perzsi.com\dashboard\payment\vendor\square\square\src\ApiHelper.php:25 Stack trace: #0 C:\xampp\htdocs\perzsi.com\dashboard\payment\vendor\square\square\src\SquareClient.php(180): Square\ApiHelper::appendUrlWithTemplateParameters(NULL, Array, false) #1 C:\xampp\htdocs\perzsi.com\dashboard\payment\vendor\square\square\src\Apis\LocationsApi.php(184): Square\SquareClient->getBaseUri() #2 C:\xampp\htdocs\perzsi.com\dashboard\payment\utils\location-info.php(31): Square\Apis\LocationsApi->retrieveLocation(‘’) #3 C:\xampp\htdocs\perzsi.com\dashboard\payment\utils\location-info.php(50): LocationInfo->__construct() #4 C:\xampp\htdocs\perzsi.com\dashboard\payment\process-card.php(6): include(‘C:\xampp\htdocs…’) #5 {main} thrown in C:\xampp\htdocs\perzsi.com\dashboard\payment\vendor\square\square\src\ApiHelper.php on line 25

Any idea please? Been stuck for over a week…

Cheers :heart: