Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion quickstart/composer.json
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
{
"require":{
"vlucas/phpdotenv": "^5.6"
"vlucas/phpdotenv": "^5.6",
"square/square": ">=41.0.0"
}
}
47 changes: 47 additions & 0 deletions quickstart/legacy.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
<?php

require 'vendor/autoload.php'

use Square\Legacy\SquareClient as SquareClient;
use Square\Legacy\Environment as Environment;
use Square\Legacy\Exceptions\ApiException as ApiException;
use Dotenv\Dotenv;

// Load the .env file
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$square = new SquareClient([
'accessToken' => $_ENV['SQUARE_ACCESS_TOKEN'],
'environment' => Environment::SANDBOX
]);

try{
$apiResponse = $square->getLocationsApi()->listLocations();
if ($apiResponse->isSuccess()){
$result = $apiResponse->getResult();
foreach ($result->getLocations() as $location){
printf(
"%s: %s, %s, %s<p/>",
$location->getId(),
$location->getName(),
$location->getAddress()?->getAddressLine1(),
$location->getAddress()?->getLocality()
);
}
} else{
$errors = $apiResponse->getErrors();
foreach ($errors as $error){
printf(
"%s<br/> %s<br/> %s<p/>",
$error->getCategory(),
$error->getCode(),
$error->getDetail()
);
}
}
} catch (ApiException $e){
echo "ApiException occurred: <b/>"
echo $e->getMessage() . "<p/>"
}
?>
67 changes: 24 additions & 43 deletions quickstart/quickstart.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,53 +2,34 @@

require 'vendor/autoload.php'

use Square\SquareClientBuilder;
use Square\Authentication\BearerAuthCredentialsBuilder;
use Square\Environment;
use Square\Exceptions\ApiException;
use Square\SquareClient;
use Square\Environments;
use Square\Exceptions\SquareException;
use Dotenv\Dotenv;


$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
// Load the .env file
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$client = SquareClientBuilder::init()
->bearerAuthCredentials(
BearerAuthCredentialsBuilder::init(
$_ENV['SQUARE_ACCESS_TOKEN']
)
)
->environment(Environment::SANDBOX)
->build();
$square = new SquareClient(
token: $_ENV['SQUARE_ACCESS_TOKEN'],
options: ['baseUrl' => Environments::Sandbox->value // Used by default
]
);

try{

$apiResponse = $client->getLocationsApi()->listLocations();

if ($apiResponse->isSuccess()){
$result = $apiResponse->getResult();
foreach ($result->getLocations() as $location){
printf(
"%s: %s, %s, %s<p/>",
$location->getId(),
$location->getName(),
$location->getAddress()->getAddressLine1(),
$location->getAddress()->getLocality()
);
}

} else{
$errors = $apiResponse->getErrors();
foreach ($errors as $error){
printf(
"%s<br/> %s<br/> %s<p/>",
$error->getCategory(),
$error->getCode(),
$error->getDetail()
);
}
$response = $square->locations->list();
foreach ($response->getLocations() as $location){
printf(
"%s: %s, %s, %s<p/>",
$location->getId(),
$location->getName(),
$location->getAddress()?->getAddressLine1(),
$location->getAddress()?->getLocality()
);
}

} catch (ApiException $e){
echo "ApiException occurred: <b/>"
echo $e->getMessage() . "<p/>"
} catch (SquareException $e){
echo 'Square API Exception occurred: ' . $e->getMessage() . "\n"
echo 'Status Code: ' . $e->getCode() . "\n"
}
?>