Skip to content

Update to new Square PHP SDK #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 7, 2021
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
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
19 changes: 9 additions & 10 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 @@ -26,23 +27,21 @@ public function getErrorCode()
return $this->data['code'];
}

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
45 changes: 26 additions & 19 deletions src/Message/CreateCardRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
namespace Omnipay\Square\Message;

use Omnipay\Common\Message\AbstractRequest;
use SquareConnect;
use Square\Apis\CardsApi;
use Square\Environment;
use Square\Models\Card;
use Square\Models\CreateCardRequest as CreateSquareCardRequest;
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,47 +54,53 @@ 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->getCardsApi();
}

public function getData()
{
$data = new SquareConnect\Model\CreateCustomerCardRequest();
$data->setCardNonce($this->getCard());
$data->setCardholderName($this->getCardholderName());
$idempotencyKey = uniqid();
$sourceId = $this->getCard(); // Card nonce
$card = new Card;
$card->setCustomerId($this->getCustomerReference());

$data = new CreateSquareCardRequest($idempotencyKey, $sourceId, $card);

return $data;
}

public function sendData($data)
{
/** @var CardsApi $api_instance */
$api_instance = $this->getApiInstance();

try {
$result = $api_instance->createCustomerCard($this->getCustomerReference(), $data);
$result = $api_instance->createCard($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
10 changes: 5 additions & 5 deletions src/Message/CustomerResponse.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\Customer;

/**
* Square Purchase Response
Expand All @@ -26,13 +27,12 @@ public function getErrorCode()
return $this->data['code'];
}

public function getCustomer()
public function getCustomer(): ?Customer
{
if(isset($this->data['customer'])){
if(!empty($this->data['customer'])){
return $this->data['customer'];
}
if(!empty($this->data['customer'])){
return $this->data['customer'];
}

return null;
}

Expand Down
Loading