Omnipay: Square
Square driver for the Omnipay PHP payment processing library
Omnipay is a framework agnostic, multi-gateway payment
processing library for PHP 5.3+. This package implements Square support for Omnipay, however it increases the minimum version of PHP to 7.1.
Installation
Omnipay is installed via Composer. To install, simply add it
to your composer.json
file:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/HTMLGuyLLC/omnipay-square"
}
],
"require": {
"transportersio/omnipay-square": "~1.0.0"
}
}
And run composer to update your dependencies:
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar update
Usage Example
Set two environment variables for the access token and location id from square.
SQUARE_ACCESS_TOKEN
and SQUARE_LOCATION_ID
.
The code to redirect a user to square checkout should look something like this:
//define order details
$total_price = 10.99;
$selected_items = [
'name'=>'Shoe',
'price'=>'10.99',
'quantity'=>1
];
//define a url that the user will be sent back to (with GET variables for transactionId, checkoutId, etc - see square API docs for details)
$return_url = 'http://example.com/order-complete';
//whether or not the user should be prompted for a shipping address during checkout
$_SERVER['square_ask_for_shipping_address'] = false;
//tell square about the order and then send the user to checkout
try
{
/** @var Omnipay\Square\Gateway $gateway */
$gateway = \Omnipay\Omnipay::create('Square');
$gateway->setAccessToken(getenv('SQUARE_ACCESS_TOKEN'));
$gateway->setLocationId(getenv('SQUARE_LOCATION_ID'));
$gateway->setCurrency('USD');
/** @var \Omnipay\Common\Message\AbstractRequest $request */
$request = $gateway->purchase([
'amount' => $total_price,
]);
$request->setItems($selected_items);
if( $return_url ) {
$request->setReturnUrl($return_url);
}
/** @var \Omnipay\Square\Message\WebPaymentResponse $response */
$response = $request->send();
if( !$response->isSuccessful() )
{
//handle error with $response->getMessage();
}
header('location: '.$response->getData()['checkout_url']);
}
catch (Throwable $e)
{
//handle exception
}
The following gateways are provided by this package:
- Square
For general usage instructions, please see the main Omnipay repository.
Support
If you are having general issues with Omnipay, we suggest posting on Stack Overflow. Be sure to add the omnipay tag so it can be easily found.
If you want to keep up to date with release anouncements, discuss ideas for the project, or ask more detailed questions, there is also a mailing list which you can subscribe to.
If you believe you have found a bug, please report it using the GitHub issue tracker, or better yet, fork the library and submit a pull request.