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