Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fa6f1fa
modelled around stripe and has backwards compatibility
May 12, 2019
62077bf
Merge pull request #1 from blasttech/master
Dolaned Jul 8, 2019
d5e0e34
Merge pull request #3 from blasttech/master
Dolaned Sep 16, 2019
7a659bb
Merge branch 'blasttech-master'
Sep 16, 2019
55a7da6
Use Square PHP SDK
millnut Jun 28, 2021
5a84ffc
Update ChargeRequest to new Square API
millnut Jun 28, 2021
6dea045
Update ChargeRequest to support Verification Token
millnut Jun 28, 2021
3b84557
Change case of verification token
millnut Jun 28, 2021
7b08873
Update all requests to use Square PHP SDK
millnut Jun 28, 2021
9d38b46
Merge branch 'feature/square-php-sdk' of https://github.com/deckbooks…
Jul 30, 2021
b11bcd6
update charge request to return first error
Dolaned Aug 4, 2021
f5893de
fix grabbing private var
Dolaned Aug 4, 2021
3304ab0
fixes
Dolaned Aug 4, 2021
f93f21a
Merge branch 'master' into feature/square-php-sdk
mikeybeck Aug 19, 2021
338407f
Remove $liveEndpoint and $testEndpoint variables as these are not nee…
millnut Aug 22, 2021
397026f
Merge pull request #13 from deckbooks/feature/square-php-sdk
mikeybeck Aug 22, 2021
8c134c0
fix: Update listTransactions endpoint to no longer use deprecated API
mikeybeck Aug 22, 2021
a7fd9db
fix: Update new card endpoints to be compatible with Square SDK
mikeybeck Aug 23, 2021
55106f9
fix: Fix undefined index exception when handling error
mikeybeck Aug 23, 2021
02f19ee
Merge pull request #15 from blasttech/deckbooks-square-php-sdk
mikeybeck Sep 7, 2021
663957e
fix charge request and fix CustomerResponse.php using customer object…
Oct 4, 2021
38fc188
feat: Add 'fetch refund' request
mikeybeck Oct 18, 2021
938b751
Merge pull request #19 from blasttech/add-fetch-refund-request
mikeybeck Oct 18, 2021
05a4ef4
Merge branch 'blasttech:blasttech-master' into blasttech-master
nicholaszuccarelli Sep 18, 2023
0f79088
Merge branch 'blasttech-master'
nicholaszuccarelli Sep 18, 2023
4f4ec49
Consolidate Square code to use proper getCode and getMessage for errors
nicholaszuccarelli Sep 18, 2023
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"require": {
"omnipay/common": "~3.0",
"square/connect": "*"
"square/square": "12.0.0.20210616"
},
"require-dev": {
"omnipay/tests": "~3.0"
Expand Down
9 changes: 9 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,13 @@ public function refund(array $parameters = [])
{
return $this->createRequest('\Omnipay\Square\Message\RefundRequest', $parameters);
}

/**
* @param array $parameters
* @return \Omnipay\Common\Message\AbstractRequest|\Omnipay\Square\Message\FetchRefundResponse
*/
public function fetchRefund(array $parameters = [])
{
return $this->createRequest('\Omnipay\Square\Message\FetchRefundRequest', $parameters);
}
}
27 changes: 13 additions & 14 deletions src/Message/CardResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Omnipay\Common\Message\AbstractResponse;
use Omnipay\Common\Message\RedirectResponseInterface;
use Square\Models\Card;

/**
* Square Purchase Response
Expand All @@ -16,33 +17,31 @@ public function isSuccessful()
return $this->data['status'] === 'success';
}

public function getErrorDetail()
public function getCode()
{
return $this->data['detail'];
return $this->data['code'] ?? null;
}

public function getErrorCode()
public function getMessage()
{
return $this->data['code'];
return $this->data['detail'] ?? $this->data['error'] ?? null;
}

public function getCard()
public function getCard(): ?Card
{
if(isset($this->data['card'])){
if(!empty($this->data['card'])){
return $this->data['card'];
}
if(!empty($this->data['card'])) {
return $this->data['card'];
}

return null;
}

public function getCardReference()
public function getCardReference(): ?string
{
if(isset($this->data['card'])){
if(!empty($this->data['card'])){
return $this->data['card']['id'];
}
if(!empty($this->data['card'])) {
return $this->data['card']->getId();
}

return null;
}
}
59 changes: 35 additions & 24 deletions src/Message/ChargeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
namespace Omnipay\Square\Message;

use Omnipay\Common\Message\AbstractRequest;
use SquareConnect;
use Square\Environment;
use Square\SquareClient;

/**
* Square Purchase Request
*/
class ChargeRequest extends AbstractRequest
{
protected $liveEndpoint = 'https://connect.squareup.com';
protected $testEndpoint = 'https://connect.squareupsandbox.com';

public function getAccessToken()
{
return $this->getParameter('accessToken');
Expand Down Expand Up @@ -78,7 +76,6 @@ public function setCustomerReference($value)
return $this->setParameter('customerReference', $value);
}


public function getCustomerReference()
{
return $this->getParameter('customerReference');
Expand Down Expand Up @@ -124,35 +121,47 @@ public function setNote($value)
return $this->setParameter('note', $value);
}

public function getEndpoint()
public function getVerificationToken()
{
return $this->getParameter('verificationToken');
}

public function setVerificationToken($verificationToken)
{
return $this->getTestMode() === true ? $this->testEndpoint : $this->liveEndpoint;
return $this->setParameter('verificationToken', $verificationToken);
}

public function getEnvironment()
{
return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION;
}

private function getApiInstance()
{
$api_config = new \SquareConnect\Configuration();
$api_config->setHost($this->getEndpoint());
$api_config->setAccessToken($this->getAccessToken());
$api_client = new \SquareConnect\ApiClient($api_config);
$api_client = new SquareClient([
'accessToken' => $this->getAccessToken(),
'environment' => $this->getEnvironment()
]);

return new \SquareConnect\Api\PaymentsApi($api_client);
return $api_client->getPaymentsApi();
}

public function getData()
{
$amountMoney = new \SquareConnect\Model\Money();
$amountMoney = new \Square\Models\Money();
$amountMoney->setAmount($this->getAmountInteger());
$amountMoney->setCurrency($this->getCurrency());

$data = new SquareConnect\Model\CreatePaymentRequest();
$data->setSourceId($this->getNonce() ?? $this->getCustomerCardId());
$sourceId = $this->getNonce() ?? $this->getCustomerCardId();
$data = new \Square\Models\CreatePaymentRequest($sourceId, $this->getIdempotencyKey(), $amountMoney);
$data->setCustomerId($this->getCustomerReference());
$data->setIdempotencyKey($this->getIdempotencyKey());
$data->setAmountMoney($amountMoney);
$data->setLocationId($this->getLocationId());
$data->setNote($this->getNote());

if ($this->getVerificationToken()) {
$data->setVerificationToken($this->getVerificationToken());
}

return $data;
}

Expand All @@ -163,19 +172,21 @@ public function sendData($data)

$result = $api_instance->createPayment($data);

if ($error = $result->getErrors()) {
if ($errors = $result->getErrors()) {
$response = [
'status' => 'error',
'code' => $error['code'],
'detail' => $error['detail']
'code' => $errors[0]->getCode(),
'detail' => $errors[0]->getDetail(),
'field' => $errors[0]->getField(),
'category' => $errors[0]->getCategory()
];
} else {
$response = [
'status' => 'success',
'transactionId' => $result->getPayment()->getId(),
'referenceId' => $result->getPayment()->getReferenceId(),
'created_at' => $result->getPayment()->getCreatedAt(),
'orderId' => $result->getPayment()->getOrderId()
'transactionId' => $result->getResult()->getPayment()->getId(),
'referenceId' => $result->getResult()->getPayment()->getReferenceId(),
'created_at' => $result->getResult()->getPayment()->getCreatedAt(),
'orderId' => $result->getResult()->getPayment()->getOrderId()
];
}
} catch (\Exception $e) {
Expand Down
12 changes: 6 additions & 6 deletions src/Message/ChargeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ public function getReferenceId()
return $this->data['referenceId'] ?? null;
}

public function getMessage()
public function getCode()
{
$message = '';
if (isset($this->data['code'])) {
$message .= $this->data['code'] . ': ';
}
return $this->data['code'] ?? null;
}

return $message . ($this->data['detail'] ?? '');
public function getMessage()
{
return $this->data['detail'] ?? $this->data['error'] ?? null;
}

/**
Expand Down
33 changes: 16 additions & 17 deletions src/Message/CreateCardRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
namespace Omnipay\Square\Message;

use Omnipay\Common\Message\AbstractRequest;
use SquareConnect;
use Square\Environment;
use Square\SquareClient;

/**
* Square Create Credit Card Request
*/
class CreateCardRequest extends AbstractRequest
{
protected $liveEndpoint = 'https://connect.squareup.com';
protected $testEndpoint = 'https://connect.squareupsandbox.com';

public function getAccessToken()
{
return $this->getParameter('accessToken');
Expand Down Expand Up @@ -53,25 +51,24 @@ public function setCardholderName($value)
return $this->setParameter('cardholderName', $value);
}

public function getEndpoint()
public function getEnvironment()
{
return $this->getTestMode() === true ? $this->testEndpoint : $this->liveEndpoint;
return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION;
}

private function getApiInstance()
{
$api_config = new \SquareConnect\Configuration();
$api_config->setHost($this->getEndpoint());
$api_config->setAccessToken($this->getAccessToken());
$api_client = new \SquareConnect\ApiClient($api_config);
$api_client = new SquareClient([
'accessToken' => $this->getAccessToken(),
'environment' => $this->getEnvironment()
]);

return new \SquareConnect\Api\CustomersApi($api_client);
return $api_client->getCustomersApi();
}

public function getData()
{
$data = new SquareConnect\Model\CreateCustomerCardRequest();
$data->setCardNonce($this->getCard());
$data = new \Square\Models\CreateCustomerCardRequest($this->getCard());
$data->setCardholderName($this->getCardholderName());

return $data;
Expand All @@ -84,16 +81,18 @@ public function sendData($data)
try {
$result = $api_instance->createCustomerCard($this->getCustomerReference(), $data);

if ($error = $result->getErrors()) {
if ($errors = $result->getErrors()) {
$response = [
'status' => 'error',
'code' => $error['code'],
'detail' => $error['detail']
'code' => $errors[0]->getCode(),
'detail' => $errors[0]->getDetail(),
'field' => $errors[0]->getField(),
'category' => $errors[0]->getCategory()
];
} else {
$response = [
'status' => 'success',
'card' => $result->getCard(),
'card' => $result->getResult()->getCard(),
'customerId' => $this->getCustomerReference()
];
}
Expand Down
48 changes: 23 additions & 25 deletions src/Message/CreateCustomerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
namespace Omnipay\Square\Message;

use Omnipay\Common\Message\AbstractRequest;
use SquareConnect;
use Square\Environment;
use Square\SquareClient;

/**
* Square Create Customer Request
*/
class CreateCustomerRequest extends AbstractRequest
{
protected $liveEndpoint = 'https://connect.squareup.com';
protected $testEndpoint = 'https://connect.squareupsandbox.com';

public function getAccessToken()
{
return $this->getParameter('accessToken');
Expand Down Expand Up @@ -63,7 +61,7 @@ public function setEmail($value)
return $this->setParameter('email', $value);
}

public function setAddress(SquareConnect\Model\Address $value)
public function setAddress(\Square\Models\Address $value)
{
return $this->setParameter('address', $value);
}
Expand All @@ -83,32 +81,30 @@ public function setReferenceId($value)
return $this->setParameter('referenceId', $value);
}

public function getEndpoint()
public function getEnvironment()
{
return $this->getTestMode() === true ? $this->testEndpoint : $this->liveEndpoint;
return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION;
}

private function getApiInstance()
{
$api_config = new \SquareConnect\Configuration();
$api_config->setHost($this->getEndpoint());
$api_config->setAccessToken($this->getAccessToken());
$api_client = new \SquareConnect\ApiClient($api_config);
$api_client = new SquareClient([
'accessToken' => $this->getAccessToken(),
'environment' => $this->getEnvironment()
]);

return new \SquareConnect\Api\CustomersApi($api_client);
return $api_client->getCustomersApi();
}

public function getData()
{
$data = [];

$data['given_name'] = $this->getFirstName();
$data['family_name'] = $this->getLastName();
$data['company_name'] = $this->getCompanyName();
$data['email_address'] = $this->getEmail();
$data['reference_id'] = $this->getReferenceId();

$data['address'] = $this->getAddress();
$data = new \Square\Models\CreateCustomerRequest();
$data->setGivenName($this->getFirstName());
$data->setFamilyName($this->getLastName());
$data->setCompanyName($this->getCompanyName());
$data->setEmailAddress($this->getEmail());
$data->setReferenceId($this->getReferenceId());
$data->setAddress($this->getAddress());

return $data;
}
Expand All @@ -120,16 +116,18 @@ public function sendData($data)
try {
$result = $api_instance->createCustomer($data);

if ($error = $result->getErrors()) {
if ($errors = $result->getErrors()) {
$response = [
'status' => 'error',
'code' => $error['code'],
'detail' => $error['detail']
'code' => $errors[0]->getCode(),
'detail' => $errors[0]->getDetail(),
'field' => $errors[0]->getField(),
'category' => $errors[0]->getCategory()
];
} else {
$response = [
'status' => 'success',
'customer' => $result->getCustomer()
'customer' => $result->getResult()->getCustomer()
];
}
} catch (\Exception $e) {
Expand Down
Loading