diff --git a/composer.json b/composer.json index f05a701..0ffe8ca 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ }, "require": { "omnipay/common": "~3.0", - "square/connect": "*" + "square/square": "12.0.0.20210616" }, "require-dev": { "omnipay/tests": "~3.0" diff --git a/src/Gateway.php b/src/Gateway.php index cca64fe..4ca471d 100755 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -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); + } } diff --git a/src/Message/CardResponse.php b/src/Message/CardResponse.php index bbbc682..d9a4ca8 100644 --- a/src/Message/CardResponse.php +++ b/src/Message/CardResponse.php @@ -4,6 +4,7 @@ use Omnipay\Common\Message\AbstractResponse; use Omnipay\Common\Message\RedirectResponseInterface; +use Square\Models\Card; /** * Square Purchase Response @@ -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; } } diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index 1a55f7b..40cc9e5 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -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'); @@ -78,7 +76,6 @@ public function setCustomerReference($value) return $this->setParameter('customerReference', $value); } - public function getCustomerReference() { return $this->getParameter('customerReference'); @@ -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; } @@ -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) { diff --git a/src/Message/ChargeResponse.php b/src/Message/ChargeResponse.php index 8d2f14d..4f937b0 100755 --- a/src/Message/ChargeResponse.php +++ b/src/Message/ChargeResponse.php @@ -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; } /** diff --git a/src/Message/CreateCardRequest.php b/src/Message/CreateCardRequest.php index 49dd0d6..217010f 100644 --- a/src/Message/CreateCardRequest.php +++ b/src/Message/CreateCardRequest.php @@ -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'); @@ -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; @@ -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() ]; } diff --git a/src/Message/CreateCustomerRequest.php b/src/Message/CreateCustomerRequest.php index 02ca3ae..fbe9ca8 100644 --- a/src/Message/CreateCustomerRequest.php +++ b/src/Message/CreateCustomerRequest.php @@ -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'); @@ -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); } @@ -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; } @@ -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) { diff --git a/src/Message/CustomerResponse.php b/src/Message/CustomerResponse.php index 964cd7c..d7e69f6 100644 --- a/src/Message/CustomerResponse.php +++ b/src/Message/CustomerResponse.php @@ -11,46 +11,34 @@ class CustomerResponse extends AbstractResponse implements RedirectResponseInterface { + 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 getCustomer() { - if(isset($this->data['customer'])){ - if(!empty($this->data['customer'])){ - return $this->data['customer']; - } - } - return null; + return $this->data['customer'] ?? null; } public function getCustomerReference(){ - if(isset($this->data['customer'])){ - if(!empty($this->data['customer'])){ - return $this->data['customer']['id']; - } - } - return null; + return $this->data['customer'] ? $this->data['customer']->getId() : null; } - + /* + * Depreciated + * */ public function getCustomerCards(){ - if(isset($this->data['customer'])){ - if(!empty($this->data['customer'])){ - return $this->data['customer']['cards']; - } - } return null; } } diff --git a/src/Message/DeleteCardRequest.php b/src/Message/DeleteCardRequest.php index af14f73..207b151 100644 --- a/src/Message/DeleteCardRequest.php +++ b/src/Message/DeleteCardRequest.php @@ -1,17 +1,11 @@ setParameter('customerReference', $value); } - public function getCustomerReference() { return $this->getParameter('customerReference'); @@ -57,6 +50,20 @@ public function setCardReference($value) return $this->setParameter('cardReference', $value); } + public function getEnvironment() + { + return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION; + } + + private function getApiInstance() + { + $api_client = new SquareClient([ + 'accessToken' => $this->getAccessToken(), + 'environment' => $this->getEnvironment() + ]); + + return $api_client->getCustomersApi(); + } public function getData() { @@ -70,18 +77,18 @@ public function getData() public function sendData($data) { - SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); - - $api_instance = new SquareConnect\Api\CustomersApi(); + $api_instance = $this->getApiInstance(); try { $result = $api_instance->deleteCustomerCard($data['customer_id'], $data['card_id']); - 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 = [ diff --git a/src/Message/DeleteCustomerRequest.php b/src/Message/DeleteCustomerRequest.php index a76ddc2..8ff19c6 100644 --- a/src/Message/DeleteCustomerRequest.php +++ b/src/Message/DeleteCustomerRequest.php @@ -1,17 +1,11 @@ setParameter('customerReference', $value); } - public function getCustomerReference(){ return $this->getParameter('customerReference'); } - /** - * Get the raw data array for this message. The format of this varies from gateway to - * gateway, but will usually be either an associative array, or a SimpleXMLElement. - * - * @return mixed - */ + + public function getEnvironment() + { + return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION; + } + + private function getApiInstance() + { + $api_client = new SquareClient([ + 'accessToken' => $this->getAccessToken(), + 'environment' => $this->getEnvironment() + ]); + + return $api_client->getCustomersApi(); + } + public function getData() { $data = []; @@ -49,26 +52,20 @@ public function getData() return $data; } - /** - * Send the request with specified data - * - * @param mixed $data The data to send - * @return ResponseInterface - */ public function sendData($data) { - SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); - - $api_instance = new SquareConnect\Api\CustomersApi(); + $api_instance = $this->getApiInstance(); try { $result = $api_instance->deleteCustomer($data['customer_id']); - 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 = [ diff --git a/src/Message/FetchCardRequest.php b/src/Message/FetchCardRequest.php index f829b29..ef768fa 100644 --- a/src/Message/FetchCardRequest.php +++ b/src/Message/FetchCardRequest.php @@ -1,16 +1,11 @@ setParameter('card', $value); } - /** - * Get the raw data array for this message. The format of this varies from gateway to - * gateway, but will usually be either an associative array, or a SimpleXMLElement. - * - * @return mixed - */ + + public function getEnvironment() + { + return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION; + } + + private function getApiInstance() + { + $api_client = new SquareClient([ + 'accessToken' => $this->getAccessToken(), + 'environment' => $this->getEnvironment() + ]); + + return $api_client->getCustomersApi(); + } + public function getData() { $data = []; @@ -67,22 +72,22 @@ public function getData() */ public function sendData($data) { - SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); - - $api_instance = new SquareConnect\Api\CustomersApi(); + $api_instance = $this->getApiInstance(); try { $result = $api_instance->retrieveCustomer($data['customer_id']); - 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 { $cardId = $this->getCard(); - $cards = array_filter($result->getCustomer()->getCards(), function ($cur) use ($cardId){ + $cards = array_filter($result->getResult()->getCustomer()->getCards(), function ($cur) use ($cardId){ return $cur->getId() == $cardId; }); diff --git a/src/Message/FetchCustomerRequest.php b/src/Message/FetchCustomerRequest.php index 2001e7a..f020de7 100644 --- a/src/Message/FetchCustomerRequest.php +++ b/src/Message/FetchCustomerRequest.php @@ -4,13 +4,11 @@ use Omnipay\Common\Message\AbstractRequest; use Omnipay\Common\Message\ResponseInterface; -use SquareConnect; +use Square\Environment; +use Square\SquareClient; class FetchCustomerRequest extends AbstractRequest { - protected $liveEndpoint = 'https://connect.squareup.com'; - protected $testEndpoint = 'https://connect.squareupsandbox.com'; - public function getAccessToken() { return $this->getParameter('accessToken'); @@ -31,19 +29,19 @@ public function getCustomerReference() return $this->getParameter('customerReference'); } - 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() @@ -62,16 +60,18 @@ public function sendData($data) $result = $api_instance->retrieveCustomer($data['customer_id']); - 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) { diff --git a/src/Message/FetchRefundRequest.php b/src/Message/FetchRefundRequest.php new file mode 100644 index 0000000..e735b19 --- /dev/null +++ b/src/Message/FetchRefundRequest.php @@ -0,0 +1,92 @@ +getParameter('accessToken'); + } + + public function setAccessToken($value) + { + return $this->setParameter('accessToken', $value); + } + + public function setRefundId($value) + { + return $this->setParameter('refundId', $value); + } + + public function getRefundId() + { + return $this->getParameter('refundId'); + } + + public function getEnvironment() + { + return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION; + } + + private function getApiInstance() + { + $api_client = new SquareClient([ + 'accessToken' => $this->getAccessToken(), + 'environment' => $this->getEnvironment() + ]); + + return $api_client->getRefundsApi(); + } + + public function getData() + { + $data = []; + + $data['refund_id'] = $this->getRefundId(); + + return $data; + } + + public function sendData($data) + { + try { + $api_instance = $this->getApiInstance(); + + $result = $api_instance->getPaymentRefund($data['refund_id']); + + if ($errors = $result->getErrors()) { + $response = [ + 'status' => 'error', + 'code' => $errors[0]->getCode(), + 'detail' => $errors[0]->getDetail(), + 'field' => $errors[0]->getField(), + 'category' => $errors[0]->getCategory() + ]; + } else { + $response = [ + 'status' => 'success', + 'refund' => $result->getResult()->getRefund() + ]; + } + } catch (\Exception $e) { + $response = [ + 'status' => 'error', + 'detail' => 'Exception when retrieving refund: ' . $e->getMessage() + ]; + } + + return $this->createResponse($response); + } + + public function createResponse($response) + { + return $this->response = new FetchRefundResponse($this, $response); + } + +} diff --git a/src/Message/FetchRefundResponse.php b/src/Message/FetchRefundResponse.php new file mode 100644 index 0000000..6655168 --- /dev/null +++ b/src/Message/FetchRefundResponse.php @@ -0,0 +1,33 @@ +data['status'] === 'success'; + } + + public function getCode() + { + return $this->data['code'] ?? null; + } + + public function getMessage() + { + return $this->data['detail'] ?? $this->data['error'] ?? null; + } + + public function getRefund(): ?PaymentRefund + { + if (!empty($this->data['refund'])) { + return $this->data['refund']; + } + + return null; + } +} diff --git a/src/Message/ListRefundsRequest.php b/src/Message/ListRefundsRequest.php index 62d11ca..66e7e36 100644 --- a/src/Message/ListRefundsRequest.php +++ b/src/Message/ListRefundsRequest.php @@ -3,7 +3,8 @@ namespace Omnipay\Square\Message; use Omnipay\Common\Message\AbstractRequest; -use SquareConnect; +use Square\Environment; +use Square\SquareClient; /** * Square List Refunds Request @@ -70,28 +71,29 @@ public function setCursor($value) return $this->setParameter('cursor', $value); } - /* - public function getCheckoutId() + public function getEnvironment() { - return $this->getParameter('checkOutId'); + return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION; } - public function setCheckoutId($value) + private function getApiInstance() { - return $this->setParameter('checkOutId', $value); + $api_client = new SquareClient([ + 'accessToken' => $this->getAccessToken(), + 'environment' => $this->getEnvironment() + ]); + + return $api_client->getTransactionsApi(); } - */ public function getData() { return []; } - public function sendData() + public function sendData($data = '') { - SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); - - $api_instance = new SquareConnect\Api\TransactionsApi(); + $api_instance = $this->getApiInstance(); try { $result = $api_instance->listRefunds( @@ -102,15 +104,17 @@ public function sendData() $this->getCursor() ); - 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 { $refunds = []; - $refundItems = $result->getRefunds(); + $refundItems = $result->getResult()->getRefunds(); if ($refundItems === null) { $refundItems = []; } diff --git a/src/Message/ListRefundsResponse.php b/src/Message/ListRefundsResponse.php index 2448caa..4b59f3a 100644 --- a/src/Message/ListRefundsResponse.php +++ b/src/Message/ListRefundsResponse.php @@ -15,15 +15,13 @@ public function isSuccessful() return $this->data['status'] === 'success'; } + public function getCode() + { + return $this->data['code'] ?? null; + } + public function getMessage() { - $message = ''; - if (array_key_exists('code', $this->data) && strlen($this->data['code'])) { - $message .= $this->data['code'] . ': '; - } - if (array_key_exists('error', $this->data) && strlen($this->data['error'])) { - $message .= $this->data['error']; - } - return $message; + return $this->data['detail'] ?? $this->data['error'] ?? null; } } diff --git a/src/Message/ListTransactionsRequest.php b/src/Message/ListTransactionsRequest.php index 886db46..245b80f 100644 --- a/src/Message/ListTransactionsRequest.php +++ b/src/Message/ListTransactionsRequest.php @@ -3,16 +3,14 @@ namespace Omnipay\Square\Message; use Omnipay\Common\Message\AbstractRequest; -use SquareConnect; +use Square\Environment; +use Square\SquareClient; /** * Square List Transactions Request */ class ListTransactionsRequest extends AbstractRequest { - protected $liveEndpoint = 'https://connect.squareup.com'; - protected $testEndpoint = 'https://connect.squareupsandbox.com'; - public function getAccessToken() { return $this->getParameter('accessToken'); @@ -73,51 +71,38 @@ public function setCursor($value) return $this->setParameter('cursor', $value); } - public function getEndpoint() + public function getEnvironment() { - return $this->getTestMode() === true ? $this->testEndpoint : $this->liveEndpoint; + return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION; } - /* - public function getCheckoutId() + private function getApiInstance() { - return $this->getParameter('checkOutId'); - } + $api_client = new SquareClient([ + 'accessToken' => $this->getAccessToken(), + 'environment' => $this->getEnvironment() + ]); - public function setCheckoutId($value) - { - return $this->setParameter('checkOutId', $value); + return $api_client->getTransactionsApi(); } - */ public function getData() { return []; } - 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); - - return new \SquareConnect\Api\TransactionsApi($api_client); - } - - public function sendData($data) + public function sendData($data = '') { - /** @var SquareConnect\Api\TransactionsApi $api_instance */ $api_instance = $this->getApiInstance(); try { - $result = $api_instance->listTransactionsWithHttpInfo( + $result = $api_instance->listTransactions( $this->getLocationId(), $this->getBeginTime(), $this->getEndTime(), $this->getSortOrder(), $this->getCursor() - )[0]; + ); if ($error = $result->getErrors()) { $response = [ @@ -127,7 +112,7 @@ public function sendData($data) ]; } else { $transactions = []; - $transactionList = $result->getTransactions(); + $transactionList = $result->getResult()->getTransactions(); if ($transactionList === null) { $transactionList = []; } diff --git a/src/Message/ListTransactionsResponse.php b/src/Message/ListTransactionsResponse.php index 783aaa4..ed5a6bf 100644 --- a/src/Message/ListTransactionsResponse.php +++ b/src/Message/ListTransactionsResponse.php @@ -15,15 +15,13 @@ public function isSuccessful() return $this->data['status'] === 'success'; } + public function getCode() + { + return $this->data['code'] ?? null; + } + public function getMessage() { - $message = ''; - if (array_key_exists('code', $this->data) && strlen($this->data['code'])) { - $message .= $this->data['code'] . ': '; - } - if (array_key_exists('error', $this->data) && strlen($this->data['error'])) { - $message .= $this->data['error']; - } - return $message; + return $this->data['detail'] ?? $this->data['error'] ?? null; } } diff --git a/src/Message/RefundRequest.php b/src/Message/RefundRequest.php index 4e77a63..35af415 100755 --- a/src/Message/RefundRequest.php +++ b/src/Message/RefundRequest.php @@ -3,16 +3,14 @@ namespace Omnipay\Square\Message; use Omnipay\Common\Message\AbstractRequest; -use SquareConnect; +use Square\Environment; +use Square\SquareClient; /** * Square Refund Request */ class RefundRequest extends AbstractRequest { - protected $liveEndpoint = 'https://connect.squareup.com'; - protected $testEndpoint = 'https://connect.squareupsandbox.com'; - public function getAccessToken() { return $this->getParameter('accessToken'); @@ -73,32 +71,29 @@ public function setReason($value) return $this->setParameter('reason', $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\RefundsApi($api_client); + return $api_client->getRefundsApi(); } 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\RefundPaymentRequest(); - $data->setPaymentId($this->getTransactionId()); - $data->setIdempotencyKey($this->getIdempotencyKey()); + $data = new \Square\Models\RefundPaymentRequest($this->getIdempotencyKey(), $amountMoney, $this->getTransactionId()); $data->setReason($this->getReason()); - $data->setAmountMoney($amountMoney); return $data; } @@ -110,25 +105,27 @@ public function sendData($data) $result = $api_instance->refundPayment($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' => $result->getRefund()->getStatus(), - 'id' => $result->getRefund()->getId(), - 'location_id' => $result->getRefund()->getLocationId(), - 'transaction_id' => $result->getRefund()->getPaymentId(), - 'tender_id' => $result->getRefund()->getOrderid(), - 'created_at' => $result->getRefund()->getCreatedAt(), - 'reason' => $result->getRefund()->getReason(), - 'amount' => $result->getRefund()->getAmountMoney()->getAmount(), - 'currency' => $result->getRefund()->getAmountMoney()->getCurrency(), + 'status' => $result->getResult()->getRefund()->getStatus(), + 'id' => $result->getResult()->getRefund()->getId(), + 'location_id' => $result->getResult()->getRefund()->getLocationId(), + 'transaction_id' => $result->getResult()->getRefund()->getPaymentId(), + 'tender_id' => $result->getResult()->getRefund()->getOrderid(), + 'created_at' => $result->getResult()->getRefund()->getCreatedAt(), + 'reason' => $result->getResult()->getRefund()->getReason(), + 'amount' => $result->getResult()->getRefund()->getAmountMoney()->getAmount(), + 'currency' => $result->getResult()->getRefund()->getAmountMoney()->getCurrency(), ]; - $processing_fee = $result->getRefund()->getProcessingFee(); + $processing_fee = $result->getResult()->getRefund()->getProcessingFee(); if (!empty($processing_fee)) { $response['processing_fee'] = $processing_fee->getAmount(); } diff --git a/src/Message/RefundResponse.php b/src/Message/RefundResponse.php index 0cd6098..a0f3412 100644 --- a/src/Message/RefundResponse.php +++ b/src/Message/RefundResponse.php @@ -19,21 +19,20 @@ public function isPending() { return $this->data['status'] === 'PENDING'; } - + public function getTransactionReference() { return $this->isSuccessful() || $this->isPending() ? $this->data['id'] : null; } + public function getCode() + { + return $this->data['code'] ?? null; + } + public function getMessage() { - $message = ''; - if (array_key_exists('code', $this->data) && strlen($this->data['code'])) { - $message .= $this->data['code'] . ': '; - } - if (array_key_exists('error', $this->data) && strlen($this->data['error'])) { - $message .= $this->data['error']; - } + $message = $this->data['error'] ?? ''; if (!empty($this->data['errors'])) { foreach ($this->data['errors'] as $error) { diff --git a/src/Message/TransactionRequest.php b/src/Message/TransactionRequest.php index 4045acd..c613eff 100644 --- a/src/Message/TransactionRequest.php +++ b/src/Message/TransactionRequest.php @@ -3,7 +3,8 @@ namespace Omnipay\Square\Message; use Omnipay\Common\Message\AbstractRequest; -use SquareConnect; +use Square\Environment; +use Square\SquareClient; /** * Square Purchase Request @@ -51,6 +52,21 @@ public function setTransactionId($value) return $this->setParameter('transactionId', $value); } + public function getEnvironment() + { + return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION; + } + + private function getApiInstance() + { + $api_client = new SquareClient([ + 'accessToken' => $this->getAccessToken(), + 'environment' => $this->getEnvironment() + ]); + + return $api_client->getTransactionsApi(); + } + public function getData() { $data = []; @@ -63,16 +79,14 @@ public function getData() public function sendData($data) { - SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); - - $api_instance = new SquareConnect\Api\TransactionsApi(); + $api_instance = $this->getApiInstance(); try { $result = $api_instance->retrieveTransaction($this->getLocationId(), $data['transactionId']); $orders = []; - $lineItems = $result->getTransaction()->getTenders(); + $lineItems = $result->getResult()->getTransaction()->getTenders(); if (count($lineItems) > 0) { foreach ($lineItems as $key => $value) { $data = []; @@ -83,17 +97,19 @@ public function sendData($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->getTransaction()->getId(), - 'referenceId' => $result->getTransaction()->getReferenceId(), + 'transactionId' => $result->getResult()->getTransaction()->getId(), + 'referenceId' => $result->getResult()->getTransaction()->getReferenceId(), 'orders' => $orders ]; } diff --git a/src/Message/UpdateCustomerRequest.php b/src/Message/UpdateCustomerRequest.php index 1444792..9ada22c 100644 --- a/src/Message/UpdateCustomerRequest.php +++ b/src/Message/UpdateCustomerRequest.php @@ -1,22 +1,14 @@ getParameter('accessToken'); @@ -32,7 +24,6 @@ public function setCustomerReference($value) return $this->setParameter('customerReference', $value); } - public function getCustomerReference() { return $this->getParameter('customerReference'); @@ -78,7 +69,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); } @@ -108,7 +99,6 @@ public function setPhoneNumber($value) return $this->setParameter('phoneNumber', $value); } - public function getNote() { return $this->getParameter('note'); @@ -129,19 +119,19 @@ 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(); } /** @@ -152,18 +142,17 @@ private function getApiInstance() */ 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 = new \Square\Models\UpdateCustomerRequest(); + $data->setGivenName($this->getFirstName()); + $data->setFamilyName($this->getLastName()); + $data->setCompanyName($this->getCompanyName()); + $data->setEmailAddress($this->getEmail()); - $data['address'] = $this->getAddress(); - $data['nickname'] = $this->getEmail(); - $data['phone_number'] = $this->getPhoneNumber(); - $data['reference_id'] = $this->getReferenceId(); - $data['note'] = $this->getNote(); + $data->setAddress($this->getAddress()); + $data->setNickname($this->getEmail()); + $data->setPhoneNumber($this->getPhoneNumber()); + $data->setReferenceId($this->getReferenceId()); + $data->setNote($this->getNote()); return $data; } @@ -182,16 +171,18 @@ public function sendData($data) try { $result = $api_instance->updateCustomer($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', - 'customer' => $result->getCustomer() + 'customer' => $result->getResult()->getCustomer() ]; } } catch (\Exception $e) { diff --git a/src/Message/WebPaymentRequest.php b/src/Message/WebPaymentRequest.php index 32fbf09..77462ac 100644 --- a/src/Message/WebPaymentRequest.php +++ b/src/Message/WebPaymentRequest.php @@ -3,7 +3,8 @@ namespace Omnipay\Square\Message; use Omnipay\Common\Message\AbstractRequest; -use SquareConnect; +use Square\Environment; +use Square\SquareClient; /** * Square Purchase Request @@ -30,6 +31,21 @@ public function setLocationId($value) return $this->setParameter('locationId', $value); } + public function getEnvironment() + { + return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION; + } + + private function getApiInstance() + { + $api_client = new SquareClient([ + 'accessToken' => $this->getAccessToken(), + 'environment' => $this->getEnvironment() + ]); + + return $api_client->getCheckoutApi(); + } + public function getData() { $items = $this->getItems(); @@ -38,45 +54,37 @@ public function getData() if (!empty($items) && count($items) > 0) { foreach ($items as $index => $item) { - $items_list[$index] = new SquareConnect\Model\OrderLineItem( - [ - 'name' => $item->getName(), - 'quantity' => (string) $item->getQuantity(), - 'base_price_money' => new SquareConnect\Model\Money( - [ - 'amount' => $item->getPrice() * 100, - 'currency' => $this->getCurrency() - ] - ) - ] - ); + $base_price_money = new \Square\Models\Money(); + $base_price_money->setAmount($item->getPrice() * 100); + $base_price_money->setCurrency($this->getCurrency()); + + $items_list[$index] = new \Square\Models\OrderLineItem((string) $item->getQuantity()); + $items_list[$index]->setName($item->getName()); + $items_list[$index]->setBasePriceMoney($base_price_money); } } - $data_array = [ - 'idempotency_key' => uniqid(), - 'order' => new SquareConnect\Model\Order([ - 'reference_id' => $this->getTransactionReference(), - 'line_items' => $items_list - ]), - 'ask_for_shipping_address' => false, - 'redirect_url' => $this->getReturnUrl() - ]; + $order = new \Square\Models\Order($this->getLocationId()); + $order->setReferenceId($this->getTransactionReference()); + $order->setLineItems($items_list); - $data = new \SquareConnect\Model\CreateCheckoutRequest($data_array); + $order_request = new \Square\Models\CreateOrderRequest(); + $order_request->setOrder($order); + + $data = new \Square\Models\CreateCheckoutRequest(uniqid(), $order_request); + $data->setAskForShippingAddress(false); + $data->setRedirectUrl($this->getReturnUrl()); return $data; } public function sendData($data) { - SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); - - $api_instance = new SquareConnect\Api\CheckoutApi(); + $api_instance = $this->getApiInstance(); try { $result = $api_instance->createCheckout($this->getLocationId(), $data); - $result = $result->getCheckout(); + $result = $result->getResult()->getCheckout(); $response = [ 'id' => $result->getId(), 'checkout_url' => $result->getCheckoutPageUrl()