ProductPromotion
Logo

PHP

made by https://0x3d.site

GitHub - square/connect-php-sdk: PHP client library for the Square Connect APIs
PHP client library for the Square Connect APIs. Contribute to square/connect-php-sdk development by creating an account on GitHub.
Visit Site

GitHub - square/connect-php-sdk: PHP client library for the Square Connect APIs

GitHub - square/connect-php-sdk: PHP client library for the Square Connect APIs

Square Connect PHP SDK - RETIRED replaced by square/square-php-sdk


Build Status PHP version Apache-2 license

NOTICE: Square Connect PHP SDK retired

The Square Connect PHP SDK is retired (EOL) as of 2020-06-10 and will no longer receive bug fixes or product updates. To continue receiving API and SDK improvements, please follow the instructions below to migrate to the new Square PHP SDK.

The old Connect SDK documentation is available under the /docs folder.



Migrate to the Square PHP SDK

Follow the instructions below to migrate your apps from the deprecated square/connect sdk to the new library.

You need to update your app to use the Square PHP SDK instead of the Connect PHP SDK The Square PHP SDK uses the square/square identifier.

  1. On the command line, run:
$ php composer.phar require square/square

-or-

  1. Update your composer.json:
"require": {
    ...
    "square/square": "^5.0.0",
    ...
}

Update your code

  1. Change all instances of use SquareConnect\... to use Square\....
  2. Replace SquareConnect models with the new Square equivalents
  3. Update client instantiation to follow the method outlined below.
  4. Update code for accessing response data to follow the method outlined below.
  5. Check $apiResponse->isSuccess() or $apiResponse->isError() to determine if the call was a success.

To simplify your code, we also recommend that you use method chaining to access APIs instead of explicitly instantiating multiple clients.

Client instantiation

Connect SDK

require 'vendor/autoload.php';

use SquareConnect\Configuration;
use SquareConnect\ApiClient;

$access_token = 'YOUR_ACCESS_TOKEN';
# setup authorization
$api_config = new Configuration();
$api_config->setHost("https://connect.squareup.com");
$api_config->setAccessToken($access_token);
$api_client = new ApiClient($api_config);

Square SDK

require 'vendor/autoload.php';

use Square\SquareClient;
use Square\Environment;

// Initialize the Square client.
$api_client = new SquareClient([
  'accessToken' => "YOUR_ACCESS_TOKEN",
  'environment' => Environment::SANDBOX
]); // In production, the environment arg is 'production'

Example code migration

As a specific example, consider the following code for creating a new payment from the following nonce:

# Fail if the card form didn't send a value for `nonce` to the server
$nonce = $_POST['nonce'];
if (is_null($nonce)) {
  echo "Invalid card data";
  http_response_code(422);
  return;
}

With the deprecated square/connect library, this is how you instantiate a client for the Payments API, format the request, and call the endpoint:

use SquareConnect\Api\PaymentsApi;
use SquareConnect\ApiException;

$payments_api = new PaymentsApi($api_client);
$request_body = array (
  "source_id" => $nonce,
  "amount_money" => array (
    "amount" => 100,
    "currency" => "USD"
  ),
  "idempotency_key" => uniqid()
);
try {
  $result = $payments_api->createPayment($request_body);
  echo "<pre>";
  print_r($result);
  echo "</pre>";
} catch (ApiException $e) {
  echo "Caught exception!<br/>";
  print_r("<strong>Response body:</strong><br/>");
  echo "<pre>"; var_dump($e->getResponseBody()); echo "</pre>";
  echo "<br/><strong>Response headers:</strong><br/>";
  echo "<pre>"; var_dump($e->getResponseHeaders()); echo "</pre>";
}

Now consider equivalent code using the new square/square library:

require 'vendor/autoload.php';

use Square\Environment;
use Square\Exceptions\ApiException;
use Square\SquareClient;
use Square\Models\CreatePaymentRequest;
use Square\Models\Money;

$payments_api = $api_client->getPaymentsApi();

$money = new Money();
$money->setAmount(100);
$money->setCurrency('USD');
$create_payment_request = new CreatePaymentRequest($nonce, uniqid(), $money);
try {
  $response = $payments_api->createPayment($create_payment_request);
  if ($response->isError()) {
    echo 'Api response has Errors';
    $errors = $response->getErrors();
    exit();
  }
  echo '<pre>';
  print_r($response);
  echo '</pre>';
} catch (ApiException $e) {
  echo 'Caught exception!<br/>';
  exit();
}

That's it!


Ask the community

Please join us in our Square developer community if you have any questions!

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory