From 2010c33c25cd3b116d09cd2cbff32e662b17e6b7 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Thu, 30 May 2019 11:18:51 +1200 Subject: [PATCH 01/37] Add reference ID customer parameter --- src/Message/CreateCustomerRequest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Message/CreateCustomerRequest.php b/src/Message/CreateCustomerRequest.php index 4105ebf..a309c75 100644 --- a/src/Message/CreateCustomerRequest.php +++ b/src/Message/CreateCustomerRequest.php @@ -60,6 +60,16 @@ public function setEmail($value) return $this->setParameter('email', $value); } + public function getReferenceId() + { + return $this->getParameter('referenceId'); + } + + public function setReferenceId($value) + { + return $this->setParameter('referenceId', $value); + } + public function getData() { $data = []; @@ -68,6 +78,7 @@ public function getData() $data['family_name'] = $this->getLastName(); $data['company_name'] = $this->getCompanyName(); $data['email_address'] = $this->getEmail(); + $data['reference_id'] = $this->getReferenceId(); return $data; } From 3475de996aa1c32b0c70a1c4567d1059d6e35a8a Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Mon, 10 Jun 2019 16:51:24 +1200 Subject: [PATCH 02/37] Fix error message not being returned as part of response detail --- src/Message/DeleteCardRequest.php | 2 +- src/Message/DeleteCustomerRequest.php | 2 +- src/Message/FetchCardRequest.php | 2 +- src/Message/FetchCustomerRequest.php | 2 +- src/Message/ListRefundsRequest.php | 2 +- src/Message/ListTransactionsRequest.php | 2 +- src/Message/TransactionRequest.php | 2 +- src/Message/UpdateCustomerRequest.php | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Message/DeleteCardRequest.php b/src/Message/DeleteCardRequest.php index 9afabe7..af14f73 100644 --- a/src/Message/DeleteCardRequest.php +++ b/src/Message/DeleteCardRequest.php @@ -91,7 +91,7 @@ public function sendData($data) } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when creating card: ', $e->getMessage() + 'detail' => 'Exception when creating card: ' . $e->getMessage() ]; } diff --git a/src/Message/DeleteCustomerRequest.php b/src/Message/DeleteCustomerRequest.php index 30fbbda..a76ddc2 100644 --- a/src/Message/DeleteCustomerRequest.php +++ b/src/Message/DeleteCustomerRequest.php @@ -78,7 +78,7 @@ public function sendData($data) } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when creating customer: ', $e->getMessage() + 'detail' => 'Exception when creating customer: ' . $e->getMessage() ]; } diff --git a/src/Message/FetchCardRequest.php b/src/Message/FetchCardRequest.php index 64bae60..f829b29 100644 --- a/src/Message/FetchCardRequest.php +++ b/src/Message/FetchCardRequest.php @@ -97,7 +97,7 @@ public function sendData($data) } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when creating customer: ', $e->getMessage() + 'detail' => 'Exception when creating customer: ' . $e->getMessage() ]; } diff --git a/src/Message/FetchCustomerRequest.php b/src/Message/FetchCustomerRequest.php index 29a6385..3e450a0 100644 --- a/src/Message/FetchCustomerRequest.php +++ b/src/Message/FetchCustomerRequest.php @@ -83,7 +83,7 @@ public function sendData($data) } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when creating customer: ', $e->getMessage() + 'detail' => 'Exception when creating customer: ' . $e->getMessage() ]; } diff --git a/src/Message/ListRefundsRequest.php b/src/Message/ListRefundsRequest.php index 1a689cc..62d11ca 100644 --- a/src/Message/ListRefundsRequest.php +++ b/src/Message/ListRefundsRequest.php @@ -137,7 +137,7 @@ public function sendData() } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when calling TransactionsApi->listRefunds: ', $e->getMessage() + 'detail' => 'Exception when calling TransactionsApi->listRefunds: ' . $e->getMessage() ]; } diff --git a/src/Message/ListTransactionsRequest.php b/src/Message/ListTransactionsRequest.php index 16d07d6..e6823fd 100644 --- a/src/Message/ListTransactionsRequest.php +++ b/src/Message/ListTransactionsRequest.php @@ -187,7 +187,7 @@ public function sendData() } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when calling TransactionsApi->listTransactions: ', $e->getMessage() + 'detail' => 'Exception when calling TransactionsApi->listTransactions: ' . $e->getMessage() ]; } diff --git a/src/Message/TransactionRequest.php b/src/Message/TransactionRequest.php index 81caa2d..4045acd 100644 --- a/src/Message/TransactionRequest.php +++ b/src/Message/TransactionRequest.php @@ -100,7 +100,7 @@ public function sendData($data) } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when calling LocationsApi->listLocations: ', $e->getMessage() + 'detail' => 'Exception when calling LocationsApi->listLocations: ' . $e->getMessage() ]; } diff --git a/src/Message/UpdateCustomerRequest.php b/src/Message/UpdateCustomerRequest.php index 162f7c6..766f58e 100644 --- a/src/Message/UpdateCustomerRequest.php +++ b/src/Message/UpdateCustomerRequest.php @@ -179,7 +179,7 @@ public function sendData($data) } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when creating customer: ', $e->getMessage() + 'detail' => 'Exception when creating customer: ' . $e->getMessage() ]; } From cea33a4d5d02128e9967c7ed126a7307ec21cfe9 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Tue, 27 Aug 2019 14:55:03 +1200 Subject: [PATCH 03/37] wip - migrating to new endpoints --- src/Message/ChargeRequest.php | 46 +++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index d7772e2..a94c054 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -123,19 +123,31 @@ public function setNote($value) public function getData() { - $data = []; - - $data['idempotency_key'] = $this->getIdempotencyKey(); - $data['amount_money'] = [ - 'amount' => $this->getAmountInteger(), - 'currency' => $this->getCurrency() - ]; - $data['card_nonce'] = $this->getNonce(); - $data['customer_id'] = $this->getCustomerReference(); - $data['customer_card_id'] = $this->getCustomerCardId(); - $data['reference_id'] = $this->getReferenceId(); - $data['order_id'] = $this->getOrderId(); - $data['note'] = $this->getNote(); + $data = new SquareConnect\Model\CreatePaymentRequest(); + $amountMoney = new \SquareConnect\Model\Money(); + + $amountMoney->setAmount($this->getAmountInteger()); + $amountMoney->setCurrency($this->getCurrency()); + + dump($this->getNonce()); + $data->setSourceId('zzzzz'); //$this->getNonce() is null; this needs to be fixed + $data->setIdempotencyKey($this->getIdempotencyKey()); + $data->setAmountMoney($amountMoney); + +// $data->setLocationId($this->getLocationId()); + +// $data['idempotency_key'] = $this->getIdempotencyKey(); +// $data['amount_money'] = [ +// 'amount' => $this->getAmountInteger(), +// 'currency' => $this->getCurrency() +// ]; +// $data['card_nonce'] = $this->getNonce(); +//// $data['source_id'] = $this->getNonce(); +// $data['customer_id'] = $this->getCustomerReference(); +// $data['customer_card_id'] = $this->getCustomerCardId(); +// $data['reference_id'] = $this->getReferenceId(); +// $data['order_id'] = $this->getOrderId(); +// $data['note'] = $this->getNote(); return $data; } @@ -144,12 +156,16 @@ public function sendData($data) { SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); - $api_instance = new SquareConnect\Api\TransactionsApi(); +// $api_client = new \SquareConnect\ApiClient(); + $api_instance = new SquareConnect\Api\PaymentsApi(); $tenders = []; try { - $result = $api_instance->charge($this->getLocationId(), $data); + + $result = $api_instance->createPayment($data); + + dd($result); if ($error = $result->getErrors()) { $response = [ From 4975804ed845a5ab2228ef2fcde0c4b0e35da25b Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Wed, 28 Aug 2019 10:35:56 +1200 Subject: [PATCH 04/37] wip - still migrating to new endpoints --- src/Message/ChargeRequest.php | 109 ++++++++++++++++++++++++++-------- 1 file changed, 83 insertions(+), 26 deletions(-) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index a94c054..01466d0 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -10,6 +10,9 @@ */ class ChargeRequest extends AbstractRequest { + protected $liveEndpoint = 'https://connect.squareup.com'; + protected $testEndpoint = 'https://connect.squareupsandbox.com'; + public function getAccessToken() { return $this->getParameter('accessToken'); @@ -121,49 +124,103 @@ public function setNote($value) return $this->setParameter('note', $value); } - public function getData() + public function getEndpoint() { - $data = new SquareConnect\Model\CreatePaymentRequest(); - $amountMoney = new \SquareConnect\Model\Money(); + return $this->getTestMode() === true ? $this->testEndpoint : $this->liveEndpoint; + } - $amountMoney->setAmount($this->getAmountInteger()); - $amountMoney->setCurrency($this->getCurrency()); + public function getData() + { + $api_config = new \SquareConnect\Configuration(); +// $api_config->setHost("https://connect.squareup.com"); + $api_config->setHost($this->getEndpoint()); + $api_config->setAccessToken($this->getAccessToken()); + $api_client = new \SquareConnect\ApiClient($api_config); + # create an instance of the Location API + $locations_api = new \SquareConnect\Api\LocationsApi($api_client); - dump($this->getNonce()); - $data->setSourceId('zzzzz'); //$this->getNonce() is null; this needs to be fixed - $data->setIdempotencyKey($this->getIdempotencyKey()); - $data->setAmountMoney($amountMoney); + try { + $locations = $locations_api->listLocations(); + print_r($locations->getLocations()); + } catch (\SquareConnect\ApiException $e) { + echo "Caught exception!
"; + print_r("Response body:
"); + echo "
"; var_dump($e->getResponseBody()); echo "
"; + echo "
Response headers:
"; + echo "
"; var_dump($e->getResponseHeaders()); echo "
"; + exit(1); + } +// $data = new SquareConnect\Model\CreatePaymentRequest(); +// $amountMoney = new \SquareConnect\Model\Money(); +// +// $amountMoney->setAmount($this->getAmountInteger()); +// $amountMoney->setCurrency($this->getCurrency()); +// +// dump($this->getNonce()); +//// dd($this->getAccessToken()); +// $data->setSourceId('ccof:58131831-1198-54a9-8883-b5f7a64a5a30'); //$this->getNonce() is null; this needs to be fixed +// $data->setIdempotencyKey($this->getIdempotencyKey()); +// $data->setAmountMoney($amountMoney); // $data->setLocationId($this->getLocationId()); -// $data['idempotency_key'] = $this->getIdempotencyKey(); -// $data['amount_money'] = [ -// 'amount' => $this->getAmountInteger(), -// 'currency' => $this->getCurrency() -// ]; -// $data['card_nonce'] = $this->getNonce(); -//// $data['source_id'] = $this->getNonce(); -// $data['customer_id'] = $this->getCustomerReference(); -// $data['customer_card_id'] = $this->getCustomerCardId(); -// $data['reference_id'] = $this->getReferenceId(); -// $data['order_id'] = $this->getOrderId(); -// $data['note'] = $this->getNote(); - - return $data; +// $data->setLocationId($this->getLocationId()); + +// $data['idempotency_key'] = $this->getIdempotencyKey(); +// $data['amount_money'] = [ +// 'amount' => $this->getAmountInteger(), +// 'currency' => $this->getCurrency() +// ]; +// $data['card_nonce'] = $this->getNonce(); +// // $data['source_id'] = $this->getNonce(); +// $data['customer_id'] = $this->getCustomerReference(); +// $data['customer_card_id'] = $this->getCustomerCardId(); +// $data['reference_id'] = $this->getReferenceId(); +// $data['order_id'] = $this->getOrderId(); +// $data['note'] = $this->getNote(); + +// return $data; } public function sendData($data) { - SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); +// SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); +// SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken(env('SQUARE_TOKEN_SANDBOX')); + + # setup authorization + $api_config = new \SquareConnect\Configuration(); + $api_config->setHost("https://connect.squareupsandbox.com"); + $api_config->setAccessToken($this->getAccessToken()); + $api_client = new \SquareConnect\ApiClient($api_config); + + $api_instance = new \SquareConnect\Api\PaymentsApi($api_client); +// $api_instance = new \SquareConnect\Api\PaymentsApi(); + // $api_instance = new SquareConnect\Api\TransactionsApi(); + + dump($api_config); + dump($api_instance); + + $data = new SquareConnect\Model\CreatePaymentRequest(); + $amountMoney = new \SquareConnect\Model\Money(); -// $api_client = new \SquareConnect\ApiClient(); - $api_instance = new SquareConnect\Api\PaymentsApi(); + $amountMoney->setAmount($this->getAmountInteger()); + $amountMoney->setCurrency($this->getCurrency()); + + dump($this->getNonce()); + // dd($this->getAccessToken()); + $data->setSourceId('ccof:58131831-1198-54a9-8883-b5f7a64a5a30'); //$this->getNonce() is null; this needs to be fixed + $data->setSourceId('cnon:CBASECZfEf2qjf3mGaCk1tmsiGsgAQ'); //$this->getNonce() is null; this needs to be fixed + $data->setIdempotencyKey($this->getIdempotencyKey()); + $data->setAmountMoney($amountMoney); + $data->setLocationId($this->getLocationId()); $tenders = []; + try { $result = $api_instance->createPayment($data); +// $result = $api_instance->charge($this->getLocationId(), $data); dd($result); From 0bf24395e5ca0cb45b9cae2fcfc5ceb4dd15f1bf Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Wed, 28 Aug 2019 12:58:18 +1200 Subject: [PATCH 05/37] wip --- src/Message/ChargeRequest.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index 01466d0..a9ba666 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -132,7 +132,6 @@ public function getEndpoint() public function getData() { $api_config = new \SquareConnect\Configuration(); -// $api_config->setHost("https://connect.squareup.com"); $api_config->setHost($this->getEndpoint()); $api_config->setAccessToken($this->getAccessToken()); $api_client = new \SquareConnect\ApiClient($api_config); @@ -207,9 +206,9 @@ public function sendData($data) $amountMoney->setCurrency($this->getCurrency()); dump($this->getNonce()); - // dd($this->getAccessToken()); - $data->setSourceId('ccof:58131831-1198-54a9-8883-b5f7a64a5a30'); //$this->getNonce() is null; this needs to be fixed - $data->setSourceId('cnon:CBASECZfEf2qjf3mGaCk1tmsiGsgAQ'); //$this->getNonce() is null; this needs to be fixed + + $data->setSourceId($this->getNonce() ?? $this->getCustomerCardId()); //$this->getNonce() is null; this needs to be fixed + $data->setCustomerId($this->getCustomerReference()); //$this->getNonce() is null; this needs to be fixed $data->setIdempotencyKey($this->getIdempotencyKey()); $data->setAmountMoney($amountMoney); $data->setLocationId($this->getLocationId()); From e0b91e5683a70a9bbcc057a27552b58d58701c92 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Wed, 28 Aug 2019 14:57:08 +1200 Subject: [PATCH 06/37] wip --- src/Message/ChargeRequest.php | 76 ++++++++++++++++------------------- 1 file changed, 34 insertions(+), 42 deletions(-) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index a9ba666..5ca847a 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -131,24 +131,24 @@ public function getEndpoint() public function getData() { - $api_config = new \SquareConnect\Configuration(); - $api_config->setHost($this->getEndpoint()); - $api_config->setAccessToken($this->getAccessToken()); - $api_client = new \SquareConnect\ApiClient($api_config); - # create an instance of the Location API - $locations_api = new \SquareConnect\Api\LocationsApi($api_client); - - try { - $locations = $locations_api->listLocations(); - print_r($locations->getLocations()); - } catch (\SquareConnect\ApiException $e) { - echo "Caught exception!
"; - print_r("Response body:
"); - echo "
"; var_dump($e->getResponseBody()); echo "
"; - echo "
Response headers:
"; - echo "
"; var_dump($e->getResponseHeaders()); echo "
"; - exit(1); - } +// $api_config = new \SquareConnect\Configuration(); +// $api_config->setHost($this->getEndpoint()); +// $api_config->setAccessToken($this->getAccessToken()); +// $api_client = new \SquareConnect\ApiClient($api_config); +// # create an instance of the Location API +// $locations_api = new \SquareConnect\Api\LocationsApi($api_client); +// +// try { +// $locations = $locations_api->listLocations(); +// print_r($locations->getLocations()); +// } catch (\SquareConnect\ApiException $e) { +// echo "Caught exception!
"; +// print_r("Response body:
"); +// echo "
"; var_dump($e->getResponseBody()); echo "
"; +// echo "
Response headers:
"; +// echo "
"; var_dump($e->getResponseHeaders()); echo "
"; +// exit(1); +// } // $data = new SquareConnect\Model\CreatePaymentRequest(); // $amountMoney = new \SquareConnect\Model\Money(); @@ -179,26 +179,6 @@ public function getData() // $data['note'] = $this->getNote(); // return $data; - } - - public function sendData($data) - { -// SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); -// SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken(env('SQUARE_TOKEN_SANDBOX')); - - # setup authorization - $api_config = new \SquareConnect\Configuration(); - $api_config->setHost("https://connect.squareupsandbox.com"); - $api_config->setAccessToken($this->getAccessToken()); - $api_client = new \SquareConnect\ApiClient($api_config); - - $api_instance = new \SquareConnect\Api\PaymentsApi($api_client); -// $api_instance = new \SquareConnect\Api\PaymentsApi(); - // $api_instance = new SquareConnect\Api\TransactionsApi(); - - dump($api_config); - dump($api_instance); - $data = new SquareConnect\Model\CreatePaymentRequest(); $amountMoney = new \SquareConnect\Model\Money(); @@ -213,15 +193,27 @@ public function sendData($data) $data->setAmountMoney($amountMoney); $data->setLocationId($this->getLocationId()); - $tenders = []; + return $data; + } + + 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\PaymentsApi($api_client); + } + public function sendData($data) + { + $tenders = []; try { + $api_instance = $this->getApiInstance(); $result = $api_instance->createPayment($data); -// $result = $api_instance->charge($this->getLocationId(), $data); - - dd($result); if ($error = $result->getErrors()) { $response = [ From 5118710cd5a89d818ed4a95a354764537a03e383 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Wed, 28 Aug 2019 15:18:00 +1200 Subject: [PATCH 07/37] Finish updating ChargeRequest class --- src/Message/ChargeRequest.php | 78 ++++------------------------------- 1 file changed, 7 insertions(+), 71 deletions(-) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index 5ca847a..13ee6de 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -131,64 +131,13 @@ public function getEndpoint() public function getData() { -// $api_config = new \SquareConnect\Configuration(); -// $api_config->setHost($this->getEndpoint()); -// $api_config->setAccessToken($this->getAccessToken()); -// $api_client = new \SquareConnect\ApiClient($api_config); -// # create an instance of the Location API -// $locations_api = new \SquareConnect\Api\LocationsApi($api_client); -// -// try { -// $locations = $locations_api->listLocations(); -// print_r($locations->getLocations()); -// } catch (\SquareConnect\ApiException $e) { -// echo "Caught exception!
"; -// print_r("Response body:
"); -// echo "
"; var_dump($e->getResponseBody()); echo "
"; -// echo "
Response headers:
"; -// echo "
"; var_dump($e->getResponseHeaders()); echo "
"; -// exit(1); -// } - -// $data = new SquareConnect\Model\CreatePaymentRequest(); -// $amountMoney = new \SquareConnect\Model\Money(); -// -// $amountMoney->setAmount($this->getAmountInteger()); -// $amountMoney->setCurrency($this->getCurrency()); -// -// dump($this->getNonce()); -//// dd($this->getAccessToken()); -// $data->setSourceId('ccof:58131831-1198-54a9-8883-b5f7a64a5a30'); //$this->getNonce() is null; this needs to be fixed -// $data->setIdempotencyKey($this->getIdempotencyKey()); -// $data->setAmountMoney($amountMoney); -// $data->setLocationId($this->getLocationId()); - -// $data->setLocationId($this->getLocationId()); - -// $data['idempotency_key'] = $this->getIdempotencyKey(); -// $data['amount_money'] = [ -// 'amount' => $this->getAmountInteger(), -// 'currency' => $this->getCurrency() -// ]; -// $data['card_nonce'] = $this->getNonce(); -// // $data['source_id'] = $this->getNonce(); -// $data['customer_id'] = $this->getCustomerReference(); -// $data['customer_card_id'] = $this->getCustomerCardId(); -// $data['reference_id'] = $this->getReferenceId(); -// $data['order_id'] = $this->getOrderId(); -// $data['note'] = $this->getNote(); - -// return $data; - $data = new SquareConnect\Model\CreatePaymentRequest(); $amountMoney = new \SquareConnect\Model\Money(); - $amountMoney->setAmount($this->getAmountInteger()); $amountMoney->setCurrency($this->getCurrency()); - dump($this->getNonce()); - - $data->setSourceId($this->getNonce() ?? $this->getCustomerCardId()); //$this->getNonce() is null; this needs to be fixed - $data->setCustomerId($this->getCustomerReference()); //$this->getNonce() is null; this needs to be fixed + $data = new SquareConnect\Model\CreatePaymentRequest(); + $data->setSourceId($this->getNonce() ?? $this->getCustomerCardId()); + $data->setCustomerId($this->getCustomerReference()); $data->setIdempotencyKey($this->getIdempotencyKey()); $data->setAmountMoney($amountMoney); $data->setLocationId($this->getLocationId()); @@ -222,25 +171,12 @@ public function sendData($data) 'detail' => $error['detail'] ]; } else { - $lineItems = $result->getTransaction()->getTenders(); - if (count($lineItems) > 0) { - foreach ($lineItems as $key => $value) { - $tender = []; - $tender['id'] = $value->getId(); - $tender['quantity'] = 1; - $tender['amount'] = $value->getAmountMoney()->getAmount() / 100; - $tender['currency'] = $value->getAmountMoney()->getCurrency(); - $item['note'] = $value->getNote(); - $tenders[] = $tender; - } - } $response = [ 'status' => 'success', - 'transactionId' => $result->getTransaction()->getId(), - 'referenceId' => $result->getTransaction()->getReferenceId(), - 'created_at' => $result->getTransaction()->getCreatedAt(), - 'orderId' => $result->getTransaction()->getOrderId(), - 'tenders' => $tenders + 'transactionId' => $result->getPayment()->getId(), + 'referenceId' => $result->getPayment()->getReferenceId(), + 'created_at' => $result->getPayment()->getCreatedAt(), + 'orderId' => $result->getPayment()->getOrderId() ]; } } catch (\Exception $e) { From 258771230ac0aacdab20acbb81593c26a06296c9 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Wed, 28 Aug 2019 15:25:03 +1200 Subject: [PATCH 08/37] Remove unused variable, rearrange function order --- src/Message/ChargeRequest.php | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index 13ee6de..252c683 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -129,6 +129,16 @@ public function getEndpoint() return $this->getTestMode() === true ? $this->testEndpoint : $this->liveEndpoint; } + 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\PaymentsApi($api_client); + } + public function getData() { $amountMoney = new \SquareConnect\Model\Money(); @@ -145,20 +155,8 @@ public function getData() return $data; } - 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\PaymentsApi($api_client); - } - public function sendData($data) { - $tenders = []; - try { $api_instance = $this->getApiInstance(); From 5b09b94c7ffb32ba23927c0fbceda24b60cc1fde Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Wed, 28 Aug 2019 16:09:44 +1200 Subject: [PATCH 09/37] Update CreateCustomerRequest file --- src/Message/CreateCustomerRequest.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Message/CreateCustomerRequest.php b/src/Message/CreateCustomerRequest.php index a309c75..aaf2d28 100644 --- a/src/Message/CreateCustomerRequest.php +++ b/src/Message/CreateCustomerRequest.php @@ -10,6 +10,9 @@ */ class CreateCustomerRequest extends AbstractRequest { + protected $liveEndpoint = 'https://connect.squareup.com'; + protected $testEndpoint = 'https://connect.squareupsandbox.com'; + public function getAccessToken() { return $this->getParameter('accessToken'); @@ -70,6 +73,21 @@ public function setReferenceId($value) return $this->setParameter('referenceId', $value); } + public function getEndpoint() + { + return $this->getTestMode() === true ? $this->testEndpoint : $this->liveEndpoint; + } + + 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\CustomersApi($api_client); + } + public function getData() { $data = []; @@ -85,9 +103,7 @@ 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->createCustomer($data); From 6a352113747256fddfa68b4831adaa140230c732 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Wed, 28 Aug 2019 16:10:06 +1200 Subject: [PATCH 10/37] wip - updating CreateCardRequest file --- src/Message/CreateCardRequest.php | 43 +++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Message/CreateCardRequest.php b/src/Message/CreateCardRequest.php index 12d577b..709af52 100644 --- a/src/Message/CreateCardRequest.php +++ b/src/Message/CreateCardRequest.php @@ -10,6 +10,9 @@ */ class CreateCardRequest extends AbstractRequest { + protected $liveEndpoint = 'https://connect.squareup.com'; + protected $testEndpoint = 'https://connect.squareupsandbox.com'; + public function getAccessToken() { return $this->getParameter('accessToken'); @@ -50,25 +53,49 @@ public function setCardholderName($value) return $this->setParameter('cardholderName', $value); } + public function getEndpoint() + { + return $this->getTestMode() === true ? $this->testEndpoint : $this->liveEndpoint; + } + + 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\CustomersApi($api_client); + } + public function getData() { - $data = []; + $data = new SquareConnect\Model\CreateCustomerCardRequest(); +// $data->setCustomerId($this->getCustomerReference()); + $data->setCardNonce('cnon:CBASEB2noQwHztYV2s_buuQn9Us'); - $data['customer_id'] = $this->getCustomerReference(); - $data['card_nonce'] = $this->getCard(); - $data['cardholder_name'] = $this->getCardholderName(); + dump($this->getCustomerReference()); + dump($this->getCard()); return $data; +// +// $data = []; +// +// $data['customer_id'] = $this->getCustomerReference(); +// $data['card_nonce'] = $this->getCard(); +// $data['cardholder_name'] = $this->getCardholderName(); +// +// return $data; } public function sendData($data) { - SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); - - $api_instance = new SquareConnect\Api\CustomersApi(); + $api_instance = $this->getApiInstance(); try { - $result = $api_instance->createCustomerCard($data['customer_id'], $data); + $result = $api_instance->createCustomerCard('FJDYW3ZVKCXRKAKYRG9R6918KM', $data); + + dd($result); if ($error = $result->getErrors()) { $response = [ From df28cb34eab8ef344595c9837eb8effc166b73b9 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Wed, 28 Aug 2019 16:44:42 +1200 Subject: [PATCH 11/37] Finish updating CreateCardRequest file --- src/Message/CreateCardRequest.php | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/src/Message/CreateCardRequest.php b/src/Message/CreateCardRequest.php index 709af52..49dd0d6 100644 --- a/src/Message/CreateCardRequest.php +++ b/src/Message/CreateCardRequest.php @@ -71,21 +71,10 @@ private function getApiInstance() public function getData() { $data = new SquareConnect\Model\CreateCustomerCardRequest(); -// $data->setCustomerId($this->getCustomerReference()); - $data->setCardNonce('cnon:CBASEB2noQwHztYV2s_buuQn9Us'); - - dump($this->getCustomerReference()); - dump($this->getCard()); + $data->setCardNonce($this->getCard()); + $data->setCardholderName($this->getCardholderName()); return $data; -// -// $data = []; -// -// $data['customer_id'] = $this->getCustomerReference(); -// $data['card_nonce'] = $this->getCard(); -// $data['cardholder_name'] = $this->getCardholderName(); -// -// return $data; } public function sendData($data) @@ -93,9 +82,7 @@ public function sendData($data) $api_instance = $this->getApiInstance(); try { - $result = $api_instance->createCustomerCard('FJDYW3ZVKCXRKAKYRG9R6918KM', $data); - - dd($result); + $result = $api_instance->createCustomerCard($this->getCustomerReference(), $data); if ($error = $result->getErrors()) { $response = [ @@ -107,7 +94,7 @@ public function sendData($data) $response = [ 'status' => 'success', 'card' => $result->getCard(), - 'customerId' => $data['customer_id'] + 'customerId' => $this->getCustomerReference() ]; } } catch (\Exception $e) { From 5f33520a55a9e5e69c6bc69d347467a4d342ad98 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Thu, 29 Aug 2019 10:23:56 +1200 Subject: [PATCH 12/37] Update FetchCustomerRequest file --- src/Message/FetchCustomerRequest.php | 50 ++++++++++++---------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/src/Message/FetchCustomerRequest.php b/src/Message/FetchCustomerRequest.php index 3e450a0..2001e7a 100644 --- a/src/Message/FetchCustomerRequest.php +++ b/src/Message/FetchCustomerRequest.php @@ -1,22 +1,15 @@ 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 getEndpoint() + { + return $this->getTestMode() === true ? $this->testEndpoint : $this->liveEndpoint; + } + + 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\CustomersApi($api_client); + } + public function getData() { $data = []; @@ -53,19 +55,11 @@ 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(); - try { + $api_instance = $this->getApiInstance(); + $result = $api_instance->retrieveCustomer($data['customer_id']); if ($error = $result->getErrors()) { @@ -83,7 +77,7 @@ public function sendData($data) } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when creating customer: ' . $e->getMessage() + 'detail' => 'Exception when retrieving customer: ' . $e->getMessage() ]; } @@ -95,4 +89,4 @@ public function createResponse($response) return $this->response = new CustomerResponse($this, $response); } -} \ No newline at end of file +} From f1817febb034424b831df1dbb75c36dd1ba001fc Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Thu, 29 Aug 2019 13:21:24 +1200 Subject: [PATCH 13/37] Update RefundRequest --- src/Message/RefundRequest.php | 51 ++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/src/Message/RefundRequest.php b/src/Message/RefundRequest.php index abf0d9a..b73cfa7 100755 --- a/src/Message/RefundRequest.php +++ b/src/Message/RefundRequest.php @@ -10,6 +10,9 @@ */ class RefundRequest extends AbstractRequest { + protected $liveEndpoint = 'https://connect.squareup.com'; + protected $testEndpoint = 'https://connect.squareupsandbox.com'; + public function getAccessToken() { return $this->getParameter('accessToken'); @@ -70,32 +73,42 @@ public function setReason($value) return $this->setParameter('reason', $value); } + public function getEndpoint() + { + return $this->getTestMode() === true ? $this->testEndpoint : $this->liveEndpoint; + } + + 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\RefundsApi($api_client); + } + public function getData() { - $data = []; + $amountMoney = new \SquareConnect\Model\Money(); + $amountMoney->setAmount($this->getAmountInteger()); + $amountMoney->setCurrency($this->getCurrency()); - $data['location_id'] = $this->getLocationId(); - $data['transaction_id'] = $this->getTransactionId(); - $data['body'] = new \SquareConnect\Model\CreateRefundRequest(); - $data['body']->setIdempotencyKey($this->getIdempotencyKey()); - $data['body']->setTenderId($this->getTenderId()); - $data['body']->setReason($this->getReason()); - $money = new \SquareConnect\Model\Money(); - $money->setAmount($this->getAmountInteger()); - $money->setCurrency($this->getCurrency()); - $data['body']->setAmountMoney($money); + $data = new \SquareConnect\Model\RefundPaymentRequest(); + $data->setPaymentId($this->getTransactionId()); + $data->setIdempotencyKey($this->getIdempotencyKey()); + $data->setReason($this->getReason()); + $data->setAmountMoney($amountMoney); return $data; } public function sendData($data) { - SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); - - $api_instance = new SquareConnect\Api\TransactionsApi(); - try { - $result = $api_instance->createRefund($data['location_id'], $data['transaction_id'], $data['body']); + $api_instance = $this->getApiInstance(); + + $result = $api_instance->refundPayment($data); if ($error = $result->getErrors()) { $response = [ @@ -108,14 +121,14 @@ public function sendData($data) 'status' => $result->getRefund()->getStatus(), 'id' => $result->getRefund()->getId(), 'location_id' => $result->getRefund()->getLocationId(), - 'transaction_id' => $result->getRefund()->getTransactionId(), - 'tender_id' => $result->getRefund()->getTenderId(), + '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(), ]; - $processing_fee = $result->getRefund()->getProcessingFeeMoney(); + $processing_fee = $result->getRefund()->getProcessingFee(); if (!empty($processing_fee)) { $response['processing_fee'] = $processing_fee->getAmount(); } From 83b642fbae86675266f7ea148495355a61b6b2a3 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Thu, 29 Aug 2019 13:22:55 +1200 Subject: [PATCH 14/37] Update RefundResponse Add 'refund pending' message to message text if status is pending. Add any other error messages to message text. --- src/Message/RefundRequest.php | 3 ++- src/Message/RefundResponse.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Message/RefundRequest.php b/src/Message/RefundRequest.php index b73cfa7..4e77a63 100755 --- a/src/Message/RefundRequest.php +++ b/src/Message/RefundRequest.php @@ -136,7 +136,8 @@ public function sendData($data) } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when creating refund: ' . $e->getMessage() + 'detail' => 'Exception when creating refund: ' . $e->getMessage(), + 'errors' => method_exists($e, 'getResponseBody') ? $e->getResponseBody()->errors : [] ]; } diff --git a/src/Message/RefundResponse.php b/src/Message/RefundResponse.php index 3839d3c..fc53fbd 100644 --- a/src/Message/RefundResponse.php +++ b/src/Message/RefundResponse.php @@ -15,6 +15,11 @@ public function isSuccessful() return $this->data['status'] === 'APPROVED'; } + public function isPending() + { + return $this->data['status'] === 'PENDING'; + } + public function getMessage() { $message = ''; @@ -24,6 +29,17 @@ public function getMessage() if (array_key_exists('error', $this->data) && strlen($this->data['error'])) { $message .= $this->data['error']; } + + if (!empty($this->data['errors'])) { + foreach ($this->data['errors'] as $error) { + $message .= $error->detail . ' '; + } + } + + if ($this->isPending()) { + return $message .= 'Refund pending'; + } + return $message; } } From e0d3908f6b382946cd18ee2ab44be63ef5fc2d0e Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 27 Sep 2019 17:34:18 -0300 Subject: [PATCH 15/37] Added address bag into create customer request --- src/Message/CreateCustomerRequest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Message/CreateCustomerRequest.php b/src/Message/CreateCustomerRequest.php index aaf2d28..02ca3ae 100644 --- a/src/Message/CreateCustomerRequest.php +++ b/src/Message/CreateCustomerRequest.php @@ -63,6 +63,16 @@ public function setEmail($value) return $this->setParameter('email', $value); } + public function setAddress(SquareConnect\Model\Address $value) + { + return $this->setParameter('address', $value); + } + + public function getAddress() + { + return $this->getParameter('address'); + } + public function getReferenceId() { return $this->getParameter('referenceId'); @@ -98,6 +108,8 @@ public function getData() $data['email_address'] = $this->getEmail(); $data['reference_id'] = $this->getReferenceId(); + $data['address'] = $this->getAddress(); + return $data; } From 8f221e4e7b4debf30d091da800e1e842341b9fab Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Mon, 14 Oct 2019 15:28:31 +1300 Subject: [PATCH 16/37] Update UpdateCustomerRequest class to work with updated Square API --- src/Message/UpdateCustomerRequest.php | 38 ++++++++++++++++++++------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/Message/UpdateCustomerRequest.php b/src/Message/UpdateCustomerRequest.php index 766f58e..1444792 100644 --- a/src/Message/UpdateCustomerRequest.php +++ b/src/Message/UpdateCustomerRequest.php @@ -8,13 +8,15 @@ namespace Omnipay\Square\Message; - use Omnipay\Common\Message\AbstractRequest; use Omnipay\Common\Message\ResponseInterface; use SquareConnect; class UpdateCustomerRequest extends AbstractRequest { + protected $liveEndpoint = 'https://connect.squareup.com'; + protected $testEndpoint = 'https://connect.squareupsandbox.com'; + public function getAccessToken() { return $this->getParameter('accessToken'); @@ -107,8 +109,8 @@ public function setPhoneNumber($value) } - - public function getNote(){ + public function getNote() + { return $this->getParameter('note'); } @@ -117,7 +119,8 @@ public function setNote($value) return $this->setParameter('note', $value); } - public function getReferenceId(){ + public function getReferenceId() + { return $this->getParameter('referenceId'); } @@ -125,6 +128,22 @@ public function setReferenceId($value) { return $this->setParameter('referenceId', $value); } + + public function getEndpoint() + { + return $this->getTestMode() === true ? $this->testEndpoint : $this->liveEndpoint; + } + + 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\CustomersApi($api_client); + } + /** * 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. @@ -148,18 +167,17 @@ public function getData() return $data; } + /** * Send the request with specified data * - * @param mixed $data The data to send + * @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->updateCustomer($this->getCustomerReference(), $data); @@ -179,7 +197,7 @@ public function sendData($data) } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when creating customer: ' . $e->getMessage() + 'detail' => 'Exception when updating customer: ' . $e->getMessage() ]; } @@ -190,4 +208,4 @@ public function createResponse($response) { return $this->response = new CustomerResponse($this, $response); } -} \ No newline at end of file +} From a443b8bad2292431f885883f5799b6f68c782b06 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Wed, 8 Apr 2020 13:52:06 +1200 Subject: [PATCH 17/37] Add note data to charge request --- src/Message/ChargeRequest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index 252c683..1a55f7b 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -151,6 +151,7 @@ public function getData() $data->setIdempotencyKey($this->getIdempotencyKey()); $data->setAmountMoney($amountMoney); $data->setLocationId($this->getLocationId()); + $data->setNote($this->getNote()); return $data; } From 55a7da6ddf05943b2fb02afadc4700bf5f84c4a4 Mon Sep 17 00:00:00 2001 From: deckbooks Date: Mon, 28 Jun 2021 17:19:22 +0100 Subject: [PATCH 18/37] Use Square PHP SDK --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" From 5a84ffc7c34a796fca04962c954d16c30d1faeae Mon Sep 17 00:00:00 2001 From: deckbooks Date: Mon, 28 Jun 2021 17:19:52 +0100 Subject: [PATCH 19/37] Update ChargeRequest to new Square API --- src/Message/ChargeRequest.php | 38 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index 1a55f7b..7eb4f44 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -3,16 +3,16 @@ namespace Omnipay\Square\Message; use Omnipay\Common\Message\AbstractRequest; -use SquareConnect; +use Square\Environment; +use Square\SquareClient; +use Square\Models\CreatePaymentRequest; +use Square\Models\Money; /** * 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'); @@ -124,32 +124,30 @@ public function setNote($value) return $this->setParameter('note', $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\PaymentsApi($api_client); + return $api_client->getPaymentsApi(); } public function getData() { - $amountMoney = new \SquareConnect\Model\Money(); + $amountMoney = new 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 CreatePaymentRequest($sourceId, $this->getIdempotencyKey(), $amountMoney); $data->setCustomerId($this->getCustomerReference()); - $data->setIdempotencyKey($this->getIdempotencyKey()); - $data->setAmountMoney($amountMoney); $data->setLocationId($this->getLocationId()); $data->setNote($this->getNote()); @@ -172,10 +170,10 @@ public function sendData($data) } 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) { From 6dea04519da92df89d1e448bee43d6a479b01c16 Mon Sep 17 00:00:00 2001 From: deckbooks Date: Mon, 28 Jun 2021 18:20:16 +0100 Subject: [PATCH 20/37] Update ChargeRequest to support Verification Token --- src/Message/ChargeRequest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index 7eb4f44..d095f87 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -124,6 +124,16 @@ public function setNote($value) return $this->setParameter('note', $value); } + public function getVerificationToken() + { + return $this->getParameter('verification_token'); + } + + public function setVerificationToken($verificationToken) + { + return $this->setParameter('verification_token', $verificationToken); + } + public function getEnvironment() { return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION; @@ -151,6 +161,10 @@ public function getData() $data->setLocationId($this->getLocationId()); $data->setNote($this->getNote()); + if ($this->getVerificationToken()) { + $data->setVerificationToken($this->getVerificationToken()); + } + return $data; } From 3b845579e58672f6516f5a746fc25f7952ea9dbe Mon Sep 17 00:00:00 2001 From: deckbooks Date: Mon, 28 Jun 2021 18:20:59 +0100 Subject: [PATCH 21/37] Change case of verification token --- src/Message/ChargeRequest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index d095f87..458b482 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -126,12 +126,12 @@ public function setNote($value) public function getVerificationToken() { - return $this->getParameter('verification_token'); + return $this->getParameter('verificationToken'); } public function setVerificationToken($verificationToken) { - return $this->setParameter('verification_token', $verificationToken); + return $this->setParameter('verificationToken', $verificationToken); } public function getEnvironment() From 7b08873011af4dfae71f91b6f8af0d75cd1337c6 Mon Sep 17 00:00:00 2001 From: deckbooks Date: Mon, 28 Jun 2021 19:47:59 +0100 Subject: [PATCH 22/37] Update all requests to use Square PHP SDK --- src/Message/ChargeRequest.php | 7 +-- src/Message/CreateCardRequest.php | 25 +++++----- src/Message/CreateCustomerRequest.php | 40 +++++++--------- src/Message/DeleteCardRequest.php | 29 +++++++----- src/Message/DeleteCustomerRequest.php | 43 ++++++++--------- src/Message/FetchCardRequest.php | 37 ++++++++------- src/Message/FetchCustomerRequest.php | 22 ++++----- src/Message/ListRefundsRequest.php | 26 ++++++----- src/Message/ListTransactionsRequest.php | 26 ++++++----- src/Message/RefundRequest.php | 47 +++++++++---------- src/Message/TransactionRequest.php | 28 ++++++++--- src/Message/UpdateCustomerRequest.php | 53 +++++++++------------ src/Message/WebPaymentRequest.php | 62 ++++++++++++++----------- 13 files changed, 223 insertions(+), 222 deletions(-) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index 458b482..b5065a9 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -5,8 +5,6 @@ use Omnipay\Common\Message\AbstractRequest; use Square\Environment; use Square\SquareClient; -use Square\Models\CreatePaymentRequest; -use Square\Models\Money; /** * Square Purchase Request @@ -78,7 +76,6 @@ public function setCustomerReference($value) return $this->setParameter('customerReference', $value); } - public function getCustomerReference() { return $this->getParameter('customerReference'); @@ -151,12 +148,12 @@ private function getApiInstance() public function getData() { - $amountMoney = new Money(); + $amountMoney = new \Square\Models\Money(); $amountMoney->setAmount($this->getAmountInteger()); $amountMoney->setCurrency($this->getCurrency()); $sourceId = $this->getNonce() ?? $this->getCustomerCardId(); - $data = new CreatePaymentRequest($sourceId, $this->getIdempotencyKey(), $amountMoney); + $data = new \Square\Models\CreatePaymentRequest($sourceId, $this->getIdempotencyKey(), $amountMoney); $data->setCustomerId($this->getCustomerReference()); $data->setLocationId($this->getLocationId()); $data->setNote($this->getNote()); diff --git a/src/Message/CreateCardRequest.php b/src/Message/CreateCardRequest.php index 49dd0d6..1efcbaa 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; @@ -93,7 +90,7 @@ public function sendData($data) } 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..076424b 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; } @@ -129,7 +125,7 @@ public function sendData($data) } else { $response = [ 'status' => 'success', - 'customer' => $result->getCustomer() + 'customer' => $result->getResult()->getCustomer() ]; } } catch (\Exception $e) { diff --git a/src/Message/DeleteCardRequest.php b/src/Message/DeleteCardRequest.php index af14f73..5198aed 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,9 +77,7 @@ 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']); diff --git a/src/Message/DeleteCustomerRequest.php b/src/Message/DeleteCustomerRequest.php index a76ddc2..5421617 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,17 +52,9 @@ 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']); diff --git a/src/Message/FetchCardRequest.php b/src/Message/FetchCardRequest.php index f829b29..26da982 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,9 +72,7 @@ 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']); @@ -82,7 +85,7 @@ public function sendData($data) ]; } 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..f5f1d71 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() @@ -71,7 +69,7 @@ public function sendData($data) } else { $response = [ 'status' => 'success', - 'customer' => $result->getCustomer() + 'customer' => $result->getResult()->getCustomer() ]; } } catch (\Exception $e) { diff --git a/src/Message/ListRefundsRequest.php b/src/Message/ListRefundsRequest.php index 62d11ca..42ffa97 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( @@ -110,7 +112,7 @@ public function sendData() ]; } else { $refunds = []; - $refundItems = $result->getRefunds(); + $refundItems = $result->getResult()->getRefunds(); if ($refundItems === null) { $refundItems = []; } diff --git a/src/Message/ListTransactionsRequest.php b/src/Message/ListTransactionsRequest.php index e6823fd..245b80f 100644 --- a/src/Message/ListTransactionsRequest.php +++ b/src/Message/ListTransactionsRequest.php @@ -3,7 +3,8 @@ namespace Omnipay\Square\Message; use Omnipay\Common\Message\AbstractRequest; -use SquareConnect; +use Square\Environment; +use Square\SquareClient; /** * Square List Transactions 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->listTransactions( @@ -110,7 +112,7 @@ public function sendData() ]; } else { $transactions = []; - $transactionList = $result->getTransactions(); + $transactionList = $result->getResult()->getTransactions(); if ($transactionList === null) { $transactionList = []; } diff --git a/src/Message/RefundRequest.php b/src/Message/RefundRequest.php index 4e77a63..35d63bd 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; } @@ -118,17 +113,17 @@ public function sendData($data) ]; } 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/TransactionRequest.php b/src/Message/TransactionRequest.php index 4045acd..54ad463 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 = []; @@ -92,8 +106,8 @@ public function sendData($data) } 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..9983d73 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; } @@ -191,7 +180,7 @@ public function sendData($data) } 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() From 3b368d18260ea9fb31d57ddf2e6aeb89a41c269b Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Fri, 13 Aug 2021 12:20:11 +1200 Subject: [PATCH 23/37] fix: Get correct ID in getTransactionReference function --- src/Message/ChargeResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Message/ChargeResponse.php b/src/Message/ChargeResponse.php index 8d2f14d..ebc1272 100755 --- a/src/Message/ChargeResponse.php +++ b/src/Message/ChargeResponse.php @@ -78,7 +78,7 @@ public function getMessage() */ public function getTransactionReference() { - return $this->getTransactionId(); + return $this->getOrderId(); } /** From c277a53d879993748b5ab7189914fac758fbfc57 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Fri, 13 Aug 2021 12:27:25 +1200 Subject: [PATCH 24/37] fix: Update ListTransactionsRequest class to match other request classes --- src/Message/ListTransactionsRequest.php | 27 ++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Message/ListTransactionsRequest.php b/src/Message/ListTransactionsRequest.php index e6823fd..886db46 100644 --- a/src/Message/ListTransactionsRequest.php +++ b/src/Message/ListTransactionsRequest.php @@ -10,6 +10,9 @@ */ class ListTransactionsRequest extends AbstractRequest { + protected $liveEndpoint = 'https://connect.squareup.com'; + protected $testEndpoint = 'https://connect.squareupsandbox.com'; + public function getAccessToken() { return $this->getParameter('accessToken'); @@ -70,6 +73,11 @@ public function setCursor($value) return $this->setParameter('cursor', $value); } + public function getEndpoint() + { + return $this->getTestMode() === true ? $this->testEndpoint : $this->liveEndpoint; + } + /* public function getCheckoutId() { @@ -87,20 +95,29 @@ public function getData() return []; } - public function sendData() + private function getApiInstance() { - SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($this->getAccessToken()); + $api_config = new \SquareConnect\Configuration(); + $api_config->setHost($this->getEndpoint()); + $api_config->setAccessToken($this->getAccessToken()); + $api_client = new \SquareConnect\ApiClient($api_config); - $api_instance = new SquareConnect\Api\TransactionsApi(); + return new \SquareConnect\Api\TransactionsApi($api_client); + } + + public function sendData($data) + { + /** @var SquareConnect\Api\TransactionsApi $api_instance */ + $api_instance = $this->getApiInstance(); try { - $result = $api_instance->listTransactions( + $result = $api_instance->listTransactionsWithHttpInfo( $this->getLocationId(), $this->getBeginTime(), $this->getEndTime(), $this->getSortOrder(), $this->getCursor() - ); + )[0]; if ($error = $result->getErrors()) { $response = [ From 338407f25803ffd772e3680b6158c9fdb3a34663 Mon Sep 17 00:00:00 2001 From: deckbooks Date: Sun, 22 Aug 2021 10:36:59 +0100 Subject: [PATCH 25/37] Remove $liveEndpoint and $testEndpoint variables as these are not needed and are handled by getEnvironment() --- src/Message/ListTransactionsRequest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Message/ListTransactionsRequest.php b/src/Message/ListTransactionsRequest.php index 8d40da6..245b80f 100644 --- a/src/Message/ListTransactionsRequest.php +++ b/src/Message/ListTransactionsRequest.php @@ -11,9 +11,6 @@ */ class ListTransactionsRequest extends AbstractRequest { - protected $liveEndpoint = 'https://connect.squareup.com'; - protected $testEndpoint = 'https://connect.squareupsandbox.com'; - public function getAccessToken() { return $this->getParameter('accessToken'); From 8c134c0660c1e0c5a1cbeda26385c3cc1c4e019c Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Mon, 23 Aug 2021 11:35:20 +1200 Subject: [PATCH 26/37] fix: Update listTransactions endpoint to no longer use deprecated API The new Square API deprecates the Transactions API, so use the Payments API instead. --- src/Message/ListTransactionsRequest.php | 69 ++++--------------------- 1 file changed, 11 insertions(+), 58 deletions(-) diff --git a/src/Message/ListTransactionsRequest.php b/src/Message/ListTransactionsRequest.php index 245b80f..d8c1062 100644 --- a/src/Message/ListTransactionsRequest.php +++ b/src/Message/ListTransactionsRequest.php @@ -4,6 +4,7 @@ use Omnipay\Common\Message\AbstractRequest; use Square\Environment; +use Square\Models\ListPaymentsResponse; use Square\SquareClient; /** @@ -83,7 +84,7 @@ private function getApiInstance() 'environment' => $this->getEnvironment() ]); - return $api_client->getTransactionsApi(); + return $api_client->getPaymentsApi(); } public function getData() @@ -96,12 +97,12 @@ public function sendData($data = '') $api_instance = $this->getApiInstance(); try { - $result = $api_instance->listTransactions( - $this->getLocationId(), + $result = $api_instance->listPayments( $this->getBeginTime(), $this->getEndTime(), $this->getSortOrder(), - $this->getCursor() + $this->getCursor(), + $this->getLocationId() ); if ($error = $result->getErrors()) { @@ -112,72 +113,24 @@ public function sendData($data = '') ]; } else { $transactions = []; - $transactionList = $result->getResult()->getTransactions(); + $transactionList = $result->getResult()->getPayments(); if ($transactionList === null) { $transactionList = []; } + /** @var \Square\Models\Payment $transaction */ foreach ($transactionList as $transaction) { $trans = new \stdClass(); $trans->id = $transaction->getID(); $trans->orderId = $transaction->getOrderId(); - $trans->clientId = $transaction->getClientId(); + $trans->clientId = $transaction->getCustomerId(); $trans->referenceId = $transaction->getReferenceId(); $trans->locationId = $transaction->getLocationId(); $trans->createdAt = $transaction->getCreatedAt(); $trans->shippingAddress = $transaction->getShippingAddress(); - $trans->product = $transaction->getProduct(); + $trans->amount = $transaction->getAmountMoney()->getAmount(); + $trans->status = $transaction->getStatus(); $trans->items = []; - $tenderList = $transaction->getTenders(); - if ($tenderList === null) { - $tenderList = []; - } - foreach ($tenderList as $tender) { - $item = new \stdClass(); - $item->id = $tender->getId(); - $item->quantity = 1; - $item->amount = $tender->getAmountMoney()->getAmount(); - $item->currency = $tender->getAmountMoney()->getCurrency(); - if ($tender->getTipMoney() !== null) { - $item->tipAmount = $tender->getTipMoney()->getAmount(); - } - $item->processingFee = $tender->getProcessingFeeMoney()->getAmount(); - $item->note = $tender->getNote(); - $item->type = $tender->getType(); - $item->customerId = $tender->getCustomerId(); - $item->cardDetails = new \stdClass(); - $cardDetails = $tender->getCardDetails(); - if (!empty($cardDetails)) { - $item->cardDetails->status = $cardDetails->getStatus(); - $item->cardDetails->card = $cardDetails->getCard(); - $item->cardDetails->entryMethod = $cardDetails->getEntryMethod(); - } - $item->cashDetails = new \stdClass(); - $cashDetails = $tender->getcashDetails(); - if (!empty($cashDetails)) { - $item->cashDetails->buyerTenderedMoney = $cashDetails->getBuyerTenderedMoney()->getAmount(); - $item->cashDetails->chargeBackMoney = $cashDetails->getChangeBackMoney()->getAmount(); - } - $trans->items[] = $item; - } $trans->refunds = []; - $refundList = $transaction->getRefunds(); - if ($refundList === null) { - $refundList = []; - } - foreach ($refundList as $refund) { - $item = new \stdClass(); - $item->id = $refund->getId(); - $item->tenderId = $refund->getTenderId(); - $item->locationId = $refund->getLocationId(); - $item->transactionId = $refund->getTransactionId(); - $item->createdAt = $refund->getCreatedAt(); - $item->reason = $refund->getReason(); - $item->status = $refund->getStatus(); - $item->amount = $refund->getAmountMoney()->getAmount(); - $item->processingFee = $refund->getProcessingFeeMoney(); - $item->currency = $refund->getAmountMoney()->getCurrency(); - $trans->items[] = $item; - } $transactions[] = $trans; } $response = [ @@ -189,7 +142,7 @@ public function sendData($data = '') } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when calling TransactionsApi->listTransactions: ' . $e->getMessage() + 'detail' => 'Exception when calling PaymentsApi->listPayments: ' . $e->getMessage() ]; } From a7fd9db54698cab6e28b42b646e8bc0a5e6beaf7 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Mon, 23 Aug 2021 16:33:57 +1200 Subject: [PATCH 27/37] fix: Update new card endpoints to be compatible with Square SDK --- src/Message/CardResponse.php | 19 +++++++++---------- src/Message/CreateCardRequest.php | 16 ++++++++++++---- src/Message/CustomerResponse.php | 10 +++++----- src/Message/UpdateCustomerRequest.php | 1 + 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/Message/CardResponse.php b/src/Message/CardResponse.php index bbbc682..32fea3a 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 @@ -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; } } diff --git a/src/Message/CreateCardRequest.php b/src/Message/CreateCardRequest.php index 1efcbaa..3bda348 100644 --- a/src/Message/CreateCardRequest.php +++ b/src/Message/CreateCardRequest.php @@ -3,7 +3,10 @@ namespace Omnipay\Square\Message; use Omnipay\Common\Message\AbstractRequest; +use Square\Apis\CardsApi; use Square\Environment; +use Square\Models\Card; +use Square\Models\CreateCardRequest as CreateSquareCardRequest; use Square\SquareClient; /** @@ -63,23 +66,28 @@ private function getApiInstance() 'environment' => $this->getEnvironment() ]); - return $api_client->getCustomersApi(); + return $api_client->getCardsApi(); } public function getData() { - $data = new \Square\Models\CreateCustomerCardRequest($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()) { $response = [ diff --git a/src/Message/CustomerResponse.php b/src/Message/CustomerResponse.php index 964cd7c..ec92f2c 100644 --- a/src/Message/CustomerResponse.php +++ b/src/Message/CustomerResponse.php @@ -4,6 +4,7 @@ use Omnipay\Common\Message\AbstractResponse; use Omnipay\Common\Message\RedirectResponseInterface; +use Square\Models\Customer; /** * Square Purchase Response @@ -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; } diff --git a/src/Message/UpdateCustomerRequest.php b/src/Message/UpdateCustomerRequest.php index 9983d73..2538144 100644 --- a/src/Message/UpdateCustomerRequest.php +++ b/src/Message/UpdateCustomerRequest.php @@ -5,6 +5,7 @@ use Omnipay\Common\Message\AbstractRequest; use Omnipay\Common\Message\ResponseInterface; use Square\Environment; +use Square\Models\Address; use Square\SquareClient; class UpdateCustomerRequest extends AbstractRequest From 55106f984936d1a96e107ab4bbf1a863301e0ac1 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Mon, 23 Aug 2021 16:45:03 +1200 Subject: [PATCH 28/37] fix: Fix undefined index exception when handling error The `getErrors` function returns an array of errors; this commit gets the details for the first error in the array. --- src/Message/ChargeRequest.php | 8 +++++--- src/Message/CreateCardRequest.php | 8 +++++--- src/Message/CreateCustomerRequest.php | 8 +++++--- src/Message/DeleteCardRequest.php | 8 +++++--- src/Message/DeleteCustomerRequest.php | 8 +++++--- src/Message/FetchCardRequest.php | 8 +++++--- src/Message/FetchCustomerRequest.php | 8 +++++--- src/Message/ListRefundsRequest.php | 8 +++++--- src/Message/ListTransactionsRequest.php | 8 +++++--- src/Message/RefundRequest.php | 8 +++++--- src/Message/TransactionRequest.php | 8 +++++--- src/Message/UpdateCustomerRequest.php | 8 +++++--- 12 files changed, 60 insertions(+), 36 deletions(-) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php index b5065a9..40cc9e5 100755 --- a/src/Message/ChargeRequest.php +++ b/src/Message/ChargeRequest.php @@ -172,11 +172,13 @@ 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 = [ diff --git a/src/Message/CreateCardRequest.php b/src/Message/CreateCardRequest.php index 3bda348..747c4bd 100644 --- a/src/Message/CreateCardRequest.php +++ b/src/Message/CreateCardRequest.php @@ -89,11 +89,13 @@ public function sendData($data) try { $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 = [ diff --git a/src/Message/CreateCustomerRequest.php b/src/Message/CreateCustomerRequest.php index 076424b..fbe9ca8 100644 --- a/src/Message/CreateCustomerRequest.php +++ b/src/Message/CreateCustomerRequest.php @@ -116,11 +116,13 @@ 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 = [ diff --git a/src/Message/DeleteCardRequest.php b/src/Message/DeleteCardRequest.php index 5198aed..207b151 100644 --- a/src/Message/DeleteCardRequest.php +++ b/src/Message/DeleteCardRequest.php @@ -82,11 +82,13 @@ public function sendData($data) 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 5421617..8ff19c6 100644 --- a/src/Message/DeleteCustomerRequest.php +++ b/src/Message/DeleteCustomerRequest.php @@ -59,11 +59,13 @@ public function sendData($data) 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 26da982..ef768fa 100644 --- a/src/Message/FetchCardRequest.php +++ b/src/Message/FetchCardRequest.php @@ -77,11 +77,13 @@ public function sendData($data) 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(); diff --git a/src/Message/FetchCustomerRequest.php b/src/Message/FetchCustomerRequest.php index f5f1d71..f020de7 100644 --- a/src/Message/FetchCustomerRequest.php +++ b/src/Message/FetchCustomerRequest.php @@ -60,11 +60,13 @@ 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 = [ diff --git a/src/Message/ListRefundsRequest.php b/src/Message/ListRefundsRequest.php index 42ffa97..66e7e36 100644 --- a/src/Message/ListRefundsRequest.php +++ b/src/Message/ListRefundsRequest.php @@ -104,11 +104,13 @@ public function sendData($data = '') $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 = []; diff --git a/src/Message/ListTransactionsRequest.php b/src/Message/ListTransactionsRequest.php index d8c1062..77b7280 100644 --- a/src/Message/ListTransactionsRequest.php +++ b/src/Message/ListTransactionsRequest.php @@ -105,11 +105,13 @@ public function sendData($data = '') $this->getLocationId() ); - 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 { $transactions = []; diff --git a/src/Message/RefundRequest.php b/src/Message/RefundRequest.php index 35d63bd..35af415 100755 --- a/src/Message/RefundRequest.php +++ b/src/Message/RefundRequest.php @@ -105,11 +105,13 @@ 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 = [ diff --git a/src/Message/TransactionRequest.php b/src/Message/TransactionRequest.php index 54ad463..c613eff 100644 --- a/src/Message/TransactionRequest.php +++ b/src/Message/TransactionRequest.php @@ -97,11 +97,13 @@ 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 = [ diff --git a/src/Message/UpdateCustomerRequest.php b/src/Message/UpdateCustomerRequest.php index 2538144..b92b043 100644 --- a/src/Message/UpdateCustomerRequest.php +++ b/src/Message/UpdateCustomerRequest.php @@ -172,11 +172,13 @@ 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 = [ From 3d9bdb95fc8e52c2937a6fd61cade949b7a408f4 Mon Sep 17 00:00:00 2001 From: deckbooks Date: Wed, 6 Oct 2021 20:06:12 +0100 Subject: [PATCH 29/37] Updated to latest square sdk --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0ffe8ca..bc77ebd 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ }, "require": { "omnipay/common": "~3.0", - "square/square": "12.0.0.20210616" + "square/square": "14.1.0.20210915" }, "require-dev": { "omnipay/tests": "~3.0" From 6b4451576c86fceffaf5ab8bb6ad3fc6c103b95a Mon Sep 17 00:00:00 2001 From: deckbooks Date: Wed, 6 Oct 2021 20:06:35 +0100 Subject: [PATCH 30/37] Updated DeleteCardRequest.php to use new DisableCard API --- src/Message/DeleteCardRequest.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/Message/DeleteCardRequest.php b/src/Message/DeleteCardRequest.php index 207b151..a76114f 100644 --- a/src/Message/DeleteCardRequest.php +++ b/src/Message/DeleteCardRequest.php @@ -20,16 +20,6 @@ public function setAccessToken($value) return $this->setParameter('accessToken', $value); } - public function setCustomerReference($value) - { - return $this->setParameter('customerReference', $value); - } - - public function getCustomerReference() - { - return $this->getParameter('customerReference'); - } - public function getLocationId() { return $this->getParameter('locationId'); @@ -62,14 +52,13 @@ private function getApiInstance() 'environment' => $this->getEnvironment() ]); - return $api_client->getCustomersApi(); + return $api_client->getCardsApi(); } public function getData() { $data = []; - $data['customer_id'] = $this->getCustomerReference(); $data['card_id'] = $this->getCardReference(); return $data; @@ -80,7 +69,7 @@ public function sendData($data) $api_instance = $this->getApiInstance(); try { - $result = $api_instance->deleteCustomerCard($data['customer_id'], $data['card_id']); + $result = $api_instance->disableCard($data['card_id']); if ($errors = $result->getErrors()) { $response = [ @@ -98,7 +87,7 @@ public function sendData($data) } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when creating card: ' . $e->getMessage() + 'detail' => 'Exception when disabling card: ' . $e->getMessage() ]; } From 580316a044ff10807a87e6c10857fa307641efa5 Mon Sep 17 00:00:00 2001 From: deckbooks Date: Wed, 6 Oct 2021 20:06:52 +0100 Subject: [PATCH 31/37] Updated ListRefundsRequest.php.php to use new Refunds API --- src/Message/ListRefundsRequest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Message/ListRefundsRequest.php b/src/Message/ListRefundsRequest.php index 66e7e36..a9a1217 100644 --- a/src/Message/ListRefundsRequest.php +++ b/src/Message/ListRefundsRequest.php @@ -83,7 +83,7 @@ private function getApiInstance() 'environment' => $this->getEnvironment() ]); - return $api_client->getTransactionsApi(); + return $api_client->getRefundsApi(); } public function getData() @@ -96,12 +96,12 @@ public function sendData($data = '') $api_instance = $this->getApiInstance(); try { - $result = $api_instance->listRefunds( - $this->getLocationId(), + $result = $api_instance->listPaymentRefunds( $this->getBeginTime(), $this->getEndTime(), $this->getSortOrder(), - $this->getCursor() + $this->getCursor(), + $this->getLocationId() ); if ($errors = $result->getErrors()) { @@ -141,7 +141,7 @@ public function sendData($data = '') } catch (\Exception $e) { $response = [ 'status' => 'error', - 'detail' => 'Exception when calling TransactionsApi->listRefunds: ' . $e->getMessage() + 'detail' => 'Exception when calling RefundsApi->listPaymentRefunds: ' . $e->getMessage() ]; } From 25954654c645f22e12c26618c9317f0b71191d57 Mon Sep 17 00:00:00 2001 From: Alofoxx Date: Tue, 6 Dec 2022 15:18:45 -0800 Subject: [PATCH 32/37] Move purchase to /v2/payments API and implement create payment --- composer.json | 7 +- src/Gateway.php | 2 +- src/Message/ChargeRequest.php | 208 ----------- src/Message/ChargeResponse.php | 93 ----- src/Message/CreatePaymentRequest.php | 508 ++++++++++++++++++++++++++ src/Message/CreatePaymentResponse.php | 138 +++++++ 6 files changed, 651 insertions(+), 305 deletions(-) delete mode 100755 src/Message/ChargeRequest.php delete mode 100755 src/Message/ChargeResponse.php create mode 100755 src/Message/CreatePaymentRequest.php create mode 100755 src/Message/CreatePaymentResponse.php diff --git a/composer.json b/composer.json index bc77ebd..0f6c09b 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "blasttech/omnipay-square", + "name": "alofoxx/omnipay-square", "type": "library", "description": "Square driver for the Omnipay payment processing library", "keywords": [ @@ -27,14 +27,15 @@ }, "require": { "omnipay/common": "~3.0", - "square/square": "14.1.0.20210915" + "square/square": "*" }, "require-dev": { "omnipay/tests": "~3.0" }, "extra": { "branch-alias": { - "dev-master": "2.0.x-dev" + "dev-master": "2.0.x-dev", + "dev-dev-3.x": "3.0.x-dev" } } } diff --git a/src/Gateway.php b/src/Gateway.php index feda4aa..8436a64 100755 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -111,7 +111,7 @@ public function setIdempotencyKey($value) public function purchase(array $parameters = []) { - return $this->createRequest('\Omnipay\Square\Message\ChargeRequest', $parameters); + return $this->createRequest('\Omnipay\Square\Message\CreatePaymentRequest', $parameters); } public function completePurchase(array $parameters = []) diff --git a/src/Message/ChargeRequest.php b/src/Message/ChargeRequest.php deleted file mode 100755 index b4cbdc3..0000000 --- a/src/Message/ChargeRequest.php +++ /dev/null @@ -1,208 +0,0 @@ -getParameter('accessToken'); - } - - public function setAccessToken($value) - { - return $this->setParameter('accessToken', $value); - } - - public function getLocationId() - { - return $this->getParameter('locationId'); - } - - public function setLocationId($value) - { - return $this->setParameter('locationId', $value); - } - - public function getCheckoutId() - { - return $this->getParameter('checkoutId'); - } - - public function setCheckoutId($value) - { - return $this->setParameter('ReceiptId', $value); - } - - public function getTransactionId() - { - return $this->getParameter('transactionId'); - } - - public function setTransactionId($value) - { - return $this->setParameter('transactionId', $value); - } - - public function getIdempotencyKey() - { - return $this->getParameter('idempotencyKey'); - } - - public function setIdempotencyKey($value) - { - return $this->setParameter('idempotencyKey', $value); - } - - public function getNonce() - { - return $this->getParameter('nonce'); - } - - public function setNonce($value) - { - return $this->setParameter('nonce', $value); - } - - public function setCustomerReference($value) - { - return $this->setParameter('customerReference', $value); - } - - public function getCustomerReference() - { - return $this->getParameter('customerReference'); - } - - public function getCustomerCardId() - { - return $this->getParameter('customerCardId'); - } - - public function setCustomerCardId($value) - { - return $this->setParameter('customerCardId', $value); - } - - public function getReferenceId() - { - return $this->getParameter('referenceId'); - } - - public function setReferenceId($value) - { - return $this->setParameter('referenceId', $value); - } - - public function getOrderId() - { - return $this->getParameter('orderId'); - } - - public function setOrderId($value) - { - return $this->setParameter('orderId', $value); - } - - public function getNote() - { - return $this->getParameter('note'); - } - - public function setNote($value) - { - return $this->setParameter('note', $value); - } - - public function getVerificationToken() - { - return $this->getParameter('verificationToken'); - } - - public function setVerificationToken($verificationToken) - { - return $this->setParameter('verificationToken', $verificationToken); - } - - 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->getPaymentsApi(); - } - - public function getData() - { - $amountMoney = new \Square\Models\Money(); - $amountMoney->setAmount($this->getAmountInteger()); - $amountMoney->setCurrency($this->getCurrency()); - - $sourceId = $this->getNonce() ?? $this->getCustomerCardId(); - $data = new \Square\Models\CreatePaymentRequest($sourceId, $this->getIdempotencyKey(), $amountMoney); - $data->setCustomerId($this->getCustomerReference()); - $data->setLocationId($this->getLocationId()); - $data->setNote($this->getNote()); - - if ($this->getVerificationToken()) { - $data->setVerificationToken($this->getVerificationToken()); - } - - return $data; - } - - public function sendData($data) - { - try { - $api_instance = $this->getApiInstance(); - - $result = $api_instance->createPayment($data); - - 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', - 'transactionId' => $result->getResult()->getPayment()->getId(), - 'referenceId' => $result->getResult()->getPayment()->getReferenceId(), - 'created_at' => $result->getResult()->getPayment()->getCreatedAt(), - 'orderId' => $result->getResult()->getPayment()->getOrderId() - ]; - } - } catch (\Exception $e) { - $error = $e->getResponseBody()->errors[0]->detail ?? $e->getMessage(); - - $response = [ - 'status' => 'error', - 'detail' => 'Exception when creating transaction: ' . $error - ]; - } - - return $this->createResponse($response); - } - - public function createResponse($response) - { - return $this->response = new ChargeResponse($this, $response); - } -} diff --git a/src/Message/ChargeResponse.php b/src/Message/ChargeResponse.php deleted file mode 100755 index ebc1272..0000000 --- a/src/Message/ChargeResponse.php +++ /dev/null @@ -1,93 +0,0 @@ -data['status'] === 'success'; - } - - public function isRedirect() - { - return false; - } - - public function getRedirectUrl() - { - return ""; - } - - public function getRedirectMethod() - { - return ""; - } - - public function getRedirectData() - { - return []; - } - - public function getTransactionId() - { - return $this->data['transactionId'] ?? null; - } - - public function getTenders() - { - return $this->data['tenders'] ?? null; - } - - public function getOrderId() - { - return $this->data['orderId'] ?? null; - } - - public function getCreatedAt() - { - return $this->data['created_at'] ?? null; - } - - public function getReferenceId() - { - return $this->data['referenceId'] ?? null; - } - - public function getMessage() - { - $message = ''; - if (isset($this->data['code'])) { - $message .= $this->data['code'] . ': '; - } - - return $message . ($this->data['detail'] ?? ''); - } - - /** - * Gateway Reference - * - * @return null|string A reference provided by the gateway to represent this transaction - */ - public function getTransactionReference() - { - return $this->getOrderId(); - } - - /** - * Get the tender id that is used for processing refunds - * - * @return null|string - */ - public function getBillingId() - { - return $this->getTenders()[0]['id'] ?? null; - } -} diff --git a/src/Message/CreatePaymentRequest.php b/src/Message/CreatePaymentRequest.php new file mode 100755 index 0000000..135ff06 --- /dev/null +++ b/src/Message/CreatePaymentRequest.php @@ -0,0 +1,508 @@ +getToken(); + } + + /** + * Set the source_id for the payment request + * Mapped to Omnipay's `token` per the Omnipay standard + */ + public function setSourceId(string $value) + { + return $this->setToken($value); + } + + public function getIdempotencyKey() + { + return $this->getParameter('idempotencyKey'); + } + + public function setIdempotencyKey($value) + { + return $this->setParameter('idempotencyKey', $value); + } + + public function getDelayDuration() + { + return $this->getParameter('delayDuration') ?? null; + } + + public function setDelayDuration($value) + { + return $this->setParameter('delayDuration', $value); + } + + public function getAutocomplete() + { + return $this->getParameter('autocomplete') ?? true; + } + + public function setAutocomplete($autocomplete) + { + return $this->setParameter('autocomplete', $autocomplete); + } + + public function getOrderId() + { + return $this->getParameter('orderId') ?? null; + } + + public function setOrderId($value) + { + return $this->setParameter('orderId', $value); + } + + public function getCustomerId() + { + return $this->getParameter('customerId') ?? null; + } + + public function setCustomerId($value) + { + return $this->setParameter('customerId', $value); + } + + public function getLocationId() + { + return $this->getParameter('locationId') ?? null; + } + + public function setLocationId($value) + { + return $this->setParameter('locationId', $value); + } + + public function getTeamMemberId() + { + return $this->getParameter('teamMemberId') ?? null; + } + + public function setTeamMemberId($value) + { + return $this->setParameter('teamMemberId', $value); + } + + public function getAcceptPartialAuthorization() + { + return $this->getParameter('acceptPartialAuthorization') ?? false; + } + + public function setAcceptPartialAuthorization($value) + { + return $this->setParameter('acceptPartialAuthorization', $value); + } + + public function getBuyerEmailAddress() + { + return $this->getParameter('buyerEmailAddress') ?? null; + } + + public function setBuyerEmailAddress($value) + { + return $this->setParameter('buyerEmailAddress', $value); + } + + public function getBillingAddress() + { + return $this->getParameter('billingAddress') ?? null; + } + + public function setBillingAddress(?Address $value) + { + return $this->setParameter('billingAddress', $value); + } + + public function getShippingAddress() + { + return $this->getParameter('shippingAddress') ?? null; + } + + public function setShippingAddress(?Address $value) + { + return $this->setParameter('shippingAddress', $value); + } + + + + /** + * Get the reference_id for the payment request + * Mapped to Omnipay's `transactionId` per the Omnipay standard + */ + public function getReferenceId() + { + return $this->getTransactionId(); + } + + /** + * Set the reference_id for the payment request + * Mapped to Omnipay's `transactionId` per the Omnipay standard + */ + public function setReferenceId($value) + { + return $this->setTransactionId($value); + } + + /** + * Get the note for the payment request + * Mapped to Omnipay's `description` per the Omnipay standard + */ + public function getNote() + { + return $this->getDescription(); + } + + /** + * Set the note for the payment request + * Mapped to Omnipay's `description` per the Omnipay standard + */ + public function setNote($value) + { + return $this->setDescription($value); + } + + public function getVerificationToken() + { + return $this->getParameter('verificationToken') ?? null; + } + + public function setVerificationToken($value) + { + return $this->setParameter('verificationToken', $value); + } + + public function getStatementDescriptionIdentifier() + { + return $this->getParameter('statementDescriptionIdentifier') ?? null; + } + + public function setStatementDescriptionIdentifier($value) + { + return $this->setParameter('statementDescriptionIdentifier', $value); + } + + /** cash details */ + public function getCashAmountInteger() + { + return $this->getParameter('cashAmountInteger') ?? null; + } + + public function setCashAmountInteger($value) + { + return $this->setParameter('cashAmountInteger', (int) $value); + } + + public function getCashCurrency() + { + return $this->getParameter('cashCurrency') ?? null; + } + + public function setCashCurrency(?string $currency) + { + return $this->setParameter('cashCurrency', $currency); + } + + /** tip details */ + public function getTipAmountInteger() + { + return $this->getParameter('tipAmountInteger') ?? null; + } + + public function setTipAmountInteger($value) + { + return $this->setParameter('tipAmountInteger', (int) $value); + } + + public function getTipCurrency() + { + return $this->getParameter('tipCurrency') ?? null; + } + + public function setTipCurrency(?string $currency) + { + return $this->setParameter('tipCurrency', $currency); + } + + /** app fee details */ + + public function getAppFeeAmountInteger() + { + return $this->getParameter('appFeeAmountInteger') ?? null; + } + + public function setAppFeeAmountInteger($value) + { + return $this->setParameter('appFeeAmountInteger', (int) $value); + } + + public function getAppFeeCurrency() + { + return $this->getParameter('appFeeCurrency') ?? null; + } + + public function setAppFeeCurrency(?string $currency) + { + return $this->setParameter('appFeeCurrency', $currency); + } + + /** external payment details */ + public function getExternalPaymentType() + { + return $this->getParameter('externalPaymentType') ?? null; + } + + public function setExternalPaymentType($value) + { + return $this->setParameter('externalPaymentType', $value); + } + + public function getExternalPaymentSource() + { + return $this->getParameter('externalPaymentSource') ?? null; + } + + public function setExternalPaymentSource($value) + { + return $this->setParameter('externalPaymentSource', $value); + } + + public function getExternalPaymentSourceId() + { + return $this->getParameter('externalPaymentSourceId') ?? null; + } + + public function setExternalPaymentSourceId($value) + { + return $this->setParameter('externalPaymentSourceId', $value); + } + + public function getExternalPaymentSourceFeeAmountInteger() + { + return $this->getParameter('externalPaymentSourceFeeAmountInteger') ?? null; + } + + public function setExternalPaymentSourceFeeAmountInteger($value) + { + return $this->setParameter('externalPaymentSourceFeeAmountInteger', (int) $value); + } + + public function getExternalPaymentSourceFeeCurrency() + { + return $this->getParameter('externalPaymentSourceFeeCurrency') ?? null; + } + + public function setExternalPaymentSourceFeeCurrency(?string $currency) + { + return $this->setParameter('externalPaymentSourceFeeCurrency', $currency); + } + + public function getAccessToken() + { + return $this->getParameter('accessToken') ?? null; + } + + public function setAccessToken($value) + { + return $this->setParameter('accessToken', $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->getPaymentsApi(); + } + + public function getData() + { + $this->validate('amount', 'token', 'idempotencyKey'); + + // handle addresses and email via omnipay card object per omnipay standard + $card = $this->getCard(); + if ($card) { + $cardData['billingAddress'] = array(); + $cardData['billingAddress']['lastName'] = $card->getBillingLastName(); + $cardData['billingAddress']['firstName'] = $card->getBillingFirstName(); + $cardData['billingAddress']['address1'] = $card->getBillingAddress1(); + $cardData['billingAddress']['address2'] = $card->getBillingAddress2(); + $cardData['billingAddress']['city'] = $card->getBillingCity(); + $cardData['billingAddress']['state'] = $card->getBillingState(); + $cardData['billingAddress']['countryCode'] = $card->getBillingCountry(); + $cardData['billingAddress']['postalCode'] = $card->getBillingPostcode(); + + $cardData['deliveryAddress'] = array(); + $cardData['deliveryAddress']['lastName'] = $card->getShippingLastName(); + $cardData['deliveryAddress']['firstName'] = $card->getShippingFirstName(); + $cardData['deliveryAddress']['address1'] = $card->getShippingAddress1(); + $cardData['deliveryAddress']['address2'] = $card->getShippingAddress2(); + $cardData['deliveryAddress']['city'] = $card->getShippingCity(); + $cardData['deliveryAddress']['state'] = $card->getShippingState(); + $cardData['deliveryAddress']['countryCode'] = $card->getShippingCountry(); + $cardData['deliveryAddress']['postalCode'] = $card->getShippingPostcode(); + + $cardData['buyerEmailAddress'] = $card->getEmail(); + if (!empty($cardData['buyerEmailAddress'])) { + $this->setBuyerEmailAddress($cardData['buyerEmailAddress']); + } + + if (!empty($cardData['billingAddress'])) { + $billingAddress = new Address(); + $billingAddress->setAddressLine1($cardData['billingAddress']['address1']); + $billingAddress->setAddressLine2($cardData['billingAddress']['address2']); + $billingAddress->setLocality($cardData['billingAddress']['city']); + $billingAddress->setAdministrativeDistrictLevel1($cardData['billingAddress']['state']); + $billingAddress->setPostalCode($cardData['billingAddress']['postalCode']); + $billingAddress->setCountry($cardData['billingAddress']['countryCode']); + $billingAddress->setFirstName($cardData['billingAddress']['firstName']); + $billingAddress->setLastName($cardData['billingAddress']['lastName']); + $this->setBillingAddress($billingAddress); + } + if (!empty($cardData['deliveryAddress'])) { + $deliveryAddress = new Address(); + $deliveryAddress->setAddressLine1($cardData['deliveryAddress']['address1']); + $deliveryAddress->setAddressLine2($cardData['deliveryAddress']['address2']); + $deliveryAddress->setLocality($cardData['deliveryAddress']['city']); + $deliveryAddress->setAdministrativeDistrictLevel1($cardData['deliveryAddress']['state']); + $deliveryAddress->setPostalCode($cardData['deliveryAddress']['postalCode']); + $deliveryAddress->setCountry($cardData['deliveryAddress']['countryCode']); + $deliveryAddress->setFirstName($cardData['deliveryAddress']['firstName']); + $deliveryAddress->setLastName($cardData['deliveryAddress']['lastName']); + $this->setShippingAddress($deliveryAddress); + } + unset($cardData); + } + + // set up amount of money to accept for this payment, not including tip money. + $amountMoney = new Money(); + $amountMoney->setAmount($this->getAmountInteger()); + $amountMoney->setCurrency($this->getCurrency()); + + // set up Payment Request body + $data = new \Square\Models\CreatePaymentRequest($this->getSourceId(), $this->getIdempotencyKey(), $amountMoney); + + // tip money + if ($this->getTipAmountInteger()) { + $tipMoney = new Money(); + $tipMoney->setAmount($this->getTipAmountInteger()); + $tipMoney->setCurrency($this->getTipCurrency()); + $data->setTipMoney($tipMoney); + } + + // app fee money + // To set this field, PAYMENTS_WRITE_ADDITIONAL_RECIPIENTS OAuth permission is required + if ($this->getAppFeeAmountInteger()) { + $appFeeMoney = new Money(); + $appFeeMoney->setAmount($this->getAppFeeAmountInteger()); + $appFeeMoney->setCurrency($this->getAppFeeCurrency()); + $data->setAppFeeMoney($appFeeMoney); + } + + $data->setDelayDuration($this->getDelayDuration()); + $data->setAutocomplete($this->getAutocomplete()); + $data->setOrderId($this->getOrderId()); + $data->setCustomerId($this->getCustomerId()); + $data->setLocationId($this->getLocationId()); + $data->setTeamMemberId($this->getTeamMemberId()); + $data->setReferenceId($this->getReferenceId()); + $data->setVerificationToken($this->getVerificationToken()); + $data->setAcceptPartialAuthorization($this->getAcceptPartialAuthorization()); + $data->setBuyerEmailAddress($this->getBuyerEmailAddress()); + $data->setBillingAddress($this->getBillingAddress()); + $data->setShippingAddress($this->getShippingAddress()); + $data->setNote($this->getNote()); + $data->setStatementDescriptionIdentifier($this->getStatementDescriptionIdentifier()); + + // set up details for cash payment (sourceId is CASH) + if ($this->getCashAmountInteger() !== null) { + $buyerSuppliedMoney = new Money(); + $buyerSuppliedMoney->setAmount($this->getCashAmountInteger()); + $buyerSuppliedMoney->setCurrency($this->getCashCurrency()); + $cashDetails = new \Square\Models\CashPaymentDetails($buyerSuppliedMoney); + $data->setCashDetails($cashDetails); + } + + // set up details for external payment (payment->sourceId is EXTERNAL) + if ($this->getExternalPaymentType() !== null) { + $externalDetails = new ExternalPaymentDetails($this->getExternalPaymentType(), $this->getExternalPaymentSource()); + // note this is a separate sourceId from the payment sourceId + $externalDetails->setSourceId($this->getExternalPaymentSourceId()); + if ($this->getExternalPaymentSourceFeeAmountInteger() !== null) { + $sourceFeeMoney = new Money(); + $sourceFeeMoney->setAmount($this->getExternalPaymentSourceFeeAmountInteger()); + $sourceFeeMoney->setCurrency($this->getExternalPaymentSourceFeeCurrency()); + $externalDetails->setSourceFeeMoney($sourceFeeMoney); + } + $data->setExternalDetails($externalDetails); + } + + return $data; + } + + public function sendData($data) + { + + try { + $api_instance = $this->getApiInstance(); + $result = $api_instance->createPayment($data); + if ($result->isSuccess()) { + $response = [ + 'successful' => true, + 'result' => $result->getResult() + ]; + } else { + $errors = $result->getErrors(); + $response = [ + 'successful' => false, + 'error' => [ + 'code' => ($errors[0]->getCode() ?? ''), + 'detail' => ($errors[0]->getDetail() ?? ''), + 'field' => ($errors[0]->getField() ?? ''), + 'category' => ($errors[0]->getCategory() ?? '') + ] + ]; + } + } catch (\Exception $e) { + $error = $e->getResponseBody()->errors[0]->detail ?? $e->getMessage(); + $response = [ + 'successful' => false, + 'error' => [ + 'detail' => 'Exception when creating payment: ' . $error, + ] + ]; + } + + return $this->createResponse($response); + } + + public function createResponse($response) + { + return $this->response = new CreatePaymentResponse($this, $response); + } +} diff --git a/src/Message/CreatePaymentResponse.php b/src/Message/CreatePaymentResponse.php new file mode 100755 index 0000000..c3034f0 --- /dev/null +++ b/src/Message/CreatePaymentResponse.php @@ -0,0 +1,138 @@ +data['successful'] && in_array($this->getPayment()->getStatus(), array('COMPLETED', 'APPROVED'), true)); + } + + /** + * Does the response require a redirect? + * + * @return boolean + */ + public function isRedirect() + { + return false; + } + + /** + * Is the transaction cancelled by the user? + * + * @return boolean + */ + public function isCancelled() + { + return (in_array($this->getPayment()->getStatus(), array('FAILED', 'CANCELED'), true)); + } + + /** + * Response Message + * + * @return null|string A response message from the payment gateway + */ + public function getMessage() + { + $message = '' . ($this->data['result']->getReasonPhrase() ?? ''); + if (isset($this->data['error'])) { + $message .= ' ' . $this->data['error']['code'] . ': '; + $message .= $this->data['error']['detail']; + $message = trim($message); + } + return $message; + } + + /** + * Response code + * + * @return null|string A response code from the payment gateway + */ + public function getCode() + { + return $this->data['result']->getStatusCode() ?? null; + } + + /** + * Gateway Reference + * Get Square's unique ID for the payment. + * + * @return null|string A reference provided by the gateway to represent this transaction + */ + public function getTransactionReference() + { + return $this->data['result']->getPayment()->getId() ?? null; + } + + /** + * Merchant’s Reference + * Get the merchant’s reference to the transaction + * + * @return null|string A reference provided by the merchant to represent this transaction + */ + public function getTransactionId() + { + return $this->data['result']->getPayment()->getReferenceId() ?? null; + } + + /** + * Get the Square payment object from the response. + * The Payment object contains all the information about the payment via "getPayment()->get{PaymentAttribute}()". + * + * @example getPayment()->getAmountMoney()->getAmount() will return the amount of the payment in cents (for USD). + * @example getPayment()->getSourceType() will return the source type for the payment. + * @see https://developer.squareup.com/reference/square/payments-api/create-payment#response__property-payment + * @return Payment|null + */ + public function getPayment() :?Payment + { + return $this->data['result']->getPayment() ?? null; + } + + // The following methods are provided for convenience, but all can be done with getPayment()->get{PaymentAttribute}() + + public function getOrderId() + { + return $this->data['result']->getPayment()->getOrderId() ?? null; + } + + public function getCreatedAt() + { + return $this->data['result']->getPayment()->getCreatedAt() ?? null; + } + + /** + * Caution: Square payment object uses `reference_id` for the merchant's assigned reference ID. + * For the Square generated payment ID, use getTransactionReference() or payment object `id`. + * + * @return string|null + */ + public function getReferenceId() + { + return $this->getTransactionId(); + } + + /** + * Indicates whether the payment is APPROVED, PENDING, COMPLETED, CANCELED, or FAILED. + * @see https://developer.squareup.com/docs/payments-api/take-payments#payment-status + * @return null|string + */ + public function getPaymentStatus() + { + return $this->data['result']->getPayment()->getStatus() ?? null; + } +} From 927ff058c81d0f4e5e17879964e2f001bd345625 Mon Sep 17 00:00:00 2001 From: Alofoxx Date: Tue, 6 Dec 2022 23:08:49 -0800 Subject: [PATCH 33/37] add complete payment for completePurchase method --- src/Gateway.php | 2 +- src/Message/CompletePaymentRequest.php | 106 +++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/Message/CompletePaymentRequest.php diff --git a/src/Gateway.php b/src/Gateway.php index 8436a64..8981269 100755 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -116,7 +116,7 @@ public function purchase(array $parameters = []) public function completePurchase(array $parameters = []) { - return $this->createRequest('\Omnipay\Square\Message\TransactionRequest', $parameters); + return $this->createRequest('\Omnipay\Square\Message\CompletePaymentRequest', $parameters); } /** diff --git a/src/Message/CompletePaymentRequest.php b/src/Message/CompletePaymentRequest.php new file mode 100644 index 0000000..dd9584f --- /dev/null +++ b/src/Message/CompletePaymentRequest.php @@ -0,0 +1,106 @@ +getToken(); + } + + /** + * Set the payment_id for the payment to be completed. + * Mapped to Omnipay's `token` per the Omnipay standard + */ + public function setPaymentId(string $value) + { + return $this->setToken($value); + } + + public function getAccessToken() + { + return $this->getParameter('accessToken') ?? null; + } + + public function setAccessToken($value) + { + return $this->setParameter('accessToken', $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->getPaymentsApi(); + } + + public function getData() + { + $this->validate('token'); + + // set up Payment Request body + return new \Square\Models\CompletePaymentRequest(); + } + + public function sendData($data) + { + + try { + $api_instance = $this->getApiInstance(); + $result = $api_instance->completePayment($this->getToken(), $data); + if ($result->isSuccess()) { + $response = [ + 'successful' => true, + 'result' => $result->getResult() + ]; + } else { + $errors = $result->getErrors(); + $response = [ + 'successful' => false, + 'error' => [ + 'code' => ($errors[0]->getCode() ?? ''), + 'detail' => ($errors[0]->getDetail() ?? ''), + 'field' => ($errors[0]->getField() ?? ''), + 'category' => ($errors[0]->getCategory() ?? '') + ] + ]; + } + } catch (\Exception $e) { + $error = $e->getResponseBody()->errors[0]->detail ?? $e->getMessage(); + $response = [ + 'successful' => false, + 'error' => [ + 'detail' => 'Exception when creating payment: ' . $error, + ] + ]; + } + + return $this->createResponse($response); + } + + public function createResponse($response) + { + return $this->response = new CreatePaymentResponse($this, $response); + } +} \ No newline at end of file From 4fae96d783e4fea04c59a721473418b3ddca1eb1 Mon Sep 17 00:00:00 2001 From: Alofoxx Date: Tue, 10 Jan 2023 23:39:27 -0800 Subject: [PATCH 34/37] update branch-alias for 3.0.x --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 0f6c09b..a1752d3 100644 --- a/composer.json +++ b/composer.json @@ -34,8 +34,7 @@ }, "extra": { "branch-alias": { - "dev-master": "2.0.x-dev", - "dev-dev-3.x": "3.0.x-dev" + "dev-master": "3.0.x-dev" } } } From 3aa0e5619b11b3b01f336b3c0bd2f811f0ef376e Mon Sep 17 00:00:00 2001 From: Alofoxx Date: Wed, 11 Jan 2023 01:55:40 -0800 Subject: [PATCH 35/37] fix: correctly form response message for getMessage() --- src/Message/CreatePaymentResponse.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Message/CreatePaymentResponse.php b/src/Message/CreatePaymentResponse.php index c3034f0..6997d1b 100755 --- a/src/Message/CreatePaymentResponse.php +++ b/src/Message/CreatePaymentResponse.php @@ -48,11 +48,16 @@ public function isCancelled() */ public function getMessage() { - $message = '' . ($this->data['result']->getReasonPhrase() ?? ''); + if ($this->data['successful']) { + return $this->getPayment()->getStatus(); + } + if (isset($this->data['error'])) { - $message .= ' ' . $this->data['error']['code'] . ': '; + $message = $this->data['error']['code'] . ': '; $message .= $this->data['error']['detail']; $message = trim($message); + } else { + $message = 'Unknown error'; } return $message; } From 78604eb6b5aa508770f3fdf12478231c8a52189c Mon Sep 17 00:00:00 2001 From: Alofoxx Date: Thu, 9 Mar 2023 11:58:01 -0800 Subject: [PATCH 36/37] feat: Add ability to get receipt number --- src/Message/CreatePaymentResponse.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Message/CreatePaymentResponse.php b/src/Message/CreatePaymentResponse.php index 6997d1b..a99a411 100755 --- a/src/Message/CreatePaymentResponse.php +++ b/src/Message/CreatePaymentResponse.php @@ -94,6 +94,17 @@ public function getTransactionId() return $this->data['result']->getPayment()->getReferenceId() ?? null; } + /** + * The payment's receipt number generated by Square. The field is missing if a payment is canceled. + * This is the easiest way to find a payment in Square's UI + * + * @return null|string A shorter payment reference provided by Square + */ + public function getReceiptNumber() + { + return $this->data['result']->getPayment()->getReceiptNumber() ?? null; + } + /** * Get the Square payment object from the response. * The Payment object contains all the information about the payment via "getPayment()->get{PaymentAttribute}()". From 24f6332a73ba0c93a9972a82ea64a2406ac82756 Mon Sep 17 00:00:00 2001 From: Alofoxx Date: Sat, 2 Mar 2024 22:34:53 -0800 Subject: [PATCH 37/37] Release 3.0.3 as stable (#6) Bring Square API Support to: 35.0.0.20240222 Square Payment API Updates * Support beta field Version Token * Update support for all fields on create payment request * Add location id methods Maintenance Updates * Get tests working for v3 * PSR2 code style cleanup * Update travis * Update readme for php8 --- .travis.yml | 25 +++++++-- README.md | 25 ++++++--- composer.json | 20 +++++-- phpunit.xml.dist | 37 +++++------- src/.env.testing | 3 + src/Message/CardResponse.php | 4 +- src/Message/CompletePaymentRequest.php | 32 ++++++++++- src/Message/CreateCardRequest.php | 10 ++++ src/Message/CreatePaymentRequest.php | 78 +++++++++++++++++++++++--- src/Message/CreatePaymentResponse.php | 9 ++- src/Message/CustomerResponse.php | 16 +++--- src/Message/DeleteCardRequest.php | 2 +- src/Message/DeleteCustomerRequest.php | 8 ++- src/Message/FetchCardRequest.php | 6 +- src/Message/FetchCustomerRequest.php | 1 - src/Message/RefundRequest.php | 7 ++- tests/GatewayTest.php | 39 +++++++------ tests/Message/PurchaseRequestTest.php | 5 +- tests/bootstrap.php | 9 +++ 19 files changed, 244 insertions(+), 92 deletions(-) create mode 100644 src/.env.testing create mode 100644 tests/bootstrap.php diff --git a/.travis.yml b/.travis.yml index 60a7524..0b82018 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,26 @@ language: php php: - - 5.4 - - 5.5 - - 5.6 + - 8.1 + - 8.2 -before_script: - - composer install -n --dev --prefer-source +dist: focal + +addons: + apt: + packages: + - libonig5 + +# This triggers builds to run on the new TravisCI infrastructure. +# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/ +sudo: false + +# Cache composer +cache: + directories: + - $HOME/.composer/cache + +install: + - travis_retry composer install --prefer-dist --no-interaction script: vendor/bin/phpcs --standard=PSR2 src && vendor/bin/phpunit --coverage-text diff --git a/README.md b/README.md index f645ff7..5f27d54 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,15 @@ **Square driver for the Omnipay PHP payment processing library** -[![Build Status](https://travis-ci.org/Transportersio/omnipay-square.png?branch=master)](https://travis-ci.org/Transportersio/omnipay-square) -[![Latest Stable Version](https://poser.pugx.org/transportersio/omnipay-square/version.png)](https://packagist.org/packages/transportersio/omnipay-square) -[![Total Downloads](https://poser.pugx.org/transportersio/omnipay-square/d/total.png)](https://packagist.org/packages/transportersio/omnipay-square) -[![License](https://poser.pugx.org/transportersio/omnipay-square/license)](https://packagist.org/packages/transportersio/omnipay-square) +[![Build Status](https://app.travis-ci.com/Alofoxx/omnipay-square.svg?branch=master)](https://app.travis-ci.com/Alofoxx/omnipay-square) +[![Latest Stable Version](https://poser.pugx.org/alofoxx/omnipay-square/version.png)](https://packagist.org/packages/alofoxx/omnipay-square) +[![Total Downloads](https://poser.pugx.org/alofoxx/omnipay-square/d/total.png)](https://packagist.org/packages/alofoxx/omnipay-square) +[![License](https://poser.pugx.org/alofoxx/omnipay-square/license)](https://packagist.org/packages/alofoxx/omnipay-square) [Omnipay](https://github.com/thephpleague/omnipay) is a framework agnostic, multi-gateway payment -processing library for PHP 5.3+. This package implements Square support for Omnipay. +processing library for PHP 7.2+. This package implements Square support for Omnipay. +_Note that this package is only tested with PHP 8.0 and above._ ## Installation Omnipay is installed via [Composer](http://getcomposer.org/). To install, simply add it @@ -18,13 +19,11 @@ to your `composer.json` file: ```json { "require": { - "transportersio/omnipay-square": "~2.1" + "alofoxx/omnipay-square": "~3.0" } } ``` -For omnipay v2 use tag 1.0.5 instead. - And run composer to update your dependencies: $ curl -s http://getcomposer.org/installer | php @@ -49,5 +48,13 @@ If you want to keep up to date with release anouncements, discuss ideas for the or ask more detailed questions, there is also a [mailing list](https://groups.google.com/forum/#!forum/omnipay) which you can subscribe to. -If you believe you have found a bug, please report it using the [GitHub issue tracker](https://github.com/Transportersio/omnipay-square/issues), +If you believe you have found a bug, please report it using the [GitHub issue tracker](https://github.com/alofoxx/omnipay-square/issues), or better yet, fork the library and submit a pull request. + +## Older Versions +This package is a fork of the original [omnipay-square](https://github.com/transportersio/omnipay-square) package. +The original package is no longer maintained and this repo has merged in the [community made](https://github.com/alofoxx/omnipay-square/graphs/contributors) PRs and updates. +This package is a continuation of the original package with the following changes: +- Updated to work with the latest version of Omnipay +- Updated to work with the latest version of Square +- Updated to work with the latest version of PHP \ No newline at end of file diff --git a/composer.json b/composer.json index a1752d3..2cc703e 100644 --- a/composer.json +++ b/composer.json @@ -10,16 +10,16 @@ "payment", "square" ], - "homepage": "https://github.com/transportersio/omnipay-square", + "homepage": "https://github.com/alofoxx/omnipay-square", "license": "MIT", "authors": [ { - "name": "Siriphan Boonmee", - "email": "ms@transporters.io" + "name": "Alofoxx", + "email": "alo@alofoxx.com" }, { "name": "Omnipay Contributors", - "homepage": "https://github.com/transportersio/omnipay-square/contributors" + "homepage": "https://github.com/alofoxx/omnipay-square/graphs/contributors" } ], "autoload": { @@ -30,11 +30,21 @@ "square/square": "*" }, "require-dev": { - "omnipay/tests": "~3.0" + "omnipay/tests": "4.1.2", + "symfony/http-client": "^6.2", + "php-http/message-factory": "^1.1", + "http-interop/http-factory-guzzle": "^1.2", + "vlucas/phpdotenv": "^5.6", + "squizlabs/php_codesniffer": "*" }, "extra": { "branch-alias": { "dev-master": "3.0.x-dev" } + }, + "config": { + "allow-plugins": { + "php-http/discovery": true + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a35b736..3f5837b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,25 +1,16 @@ - - - - ./tests/ - - - - - - - - ./src - - + + + + ./src + + + + + ./tests/ + + + + + diff --git a/src/.env.testing b/src/.env.testing new file mode 100644 index 0000000..5e48e1b --- /dev/null +++ b/src/.env.testing @@ -0,0 +1,3 @@ +SQUARE_APP_ID=sandbox-xxxxxx-xxxx-xxxxxxxxxxxxxxxxx +SQUARE_ACCESS_TOKEN=xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +SQUARE_LOCATION_ID=xxxxxxxxxxxxx diff --git a/src/Message/CardResponse.php b/src/Message/CardResponse.php index 32fea3a..6c69dd3 100644 --- a/src/Message/CardResponse.php +++ b/src/Message/CardResponse.php @@ -29,7 +29,7 @@ public function getErrorCode() public function getCard(): ?Card { - if(!empty($this->data['card'])) { + if (!empty($this->data['card'])) { return $this->data['card']; } @@ -38,7 +38,7 @@ public function getCard(): ?Card public function getCardReference(): ?string { - if(!empty($this->data['card'])) { + if (!empty($this->data['card'])) { return $this->data['card']->getId(); } diff --git a/src/Message/CompletePaymentRequest.php b/src/Message/CompletePaymentRequest.php index dd9584f..6fd7740 100644 --- a/src/Message/CompletePaymentRequest.php +++ b/src/Message/CompletePaymentRequest.php @@ -1,12 +1,12 @@ setParameter('accessToken', $value); } + public function getVersionToken() + { + return $this->getParameter('versionToken') ?? null; + } + + /** + * BETA FEATURE: Used for optimistic concurrency. This opaque token identifies the current `Payment` + * version that the caller expects. If the server has a different version of the Payment, the update fails and + * a response with a VERSION_MISMATCH error is returned. + * + * https://developer.squareup.com/reference/square/payments-api/complete-payment#request__property-version_token + */ + public function setVersionToken(string $value) + { + return $this->setParameter('versionToken', $value); + } + public function getEnvironment() { return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION; @@ -55,6 +72,17 @@ private function getApiInstance() return $api_client->getPaymentsApi(); } + public function getLocationId() + { + return $this->getParameter('locationId') ?? null; + } + + public function setLocationId(string $value) + { + return $this->setParameter('locationId', $value); + } + + public function getData() { $this->validate('token'); @@ -103,4 +131,4 @@ public function createResponse($response) { return $this->response = new CreatePaymentResponse($this, $response); } -} \ No newline at end of file +} diff --git a/src/Message/CreateCardRequest.php b/src/Message/CreateCardRequest.php index 747c4bd..f46b07d 100644 --- a/src/Message/CreateCardRequest.php +++ b/src/Message/CreateCardRequest.php @@ -69,6 +69,16 @@ private function getApiInstance() return $api_client->getCardsApi(); } + public function getLocationId() + { + return $this->getParameter('locationId') ?? null; + } + + public function setLocationId(string $value) + { + return $this->setParameter('locationId', $value); + } + public function getData() { $idempotencyKey = uniqid(); diff --git a/src/Message/CreatePaymentRequest.php b/src/Message/CreatePaymentRequest.php index 135ff06..bf89e43 100755 --- a/src/Message/CreatePaymentRequest.php +++ b/src/Message/CreatePaymentRequest.php @@ -5,6 +5,7 @@ use Omnipay\Common\Message\AbstractRequest; use Square\Environment; use Square\Models\Address; +use Square\Models\CustomerDetails; use Square\Models\ExternalPaymentDetails; use Square\Models\Money; use Square\SquareClient; @@ -44,20 +45,45 @@ public function setIdempotencyKey($value) public function getDelayDuration() { - return $this->getParameter('delayDuration') ?? null; + return $this->getParameter('delayDuration') ?? 'P7D'; } - public function setDelayDuration($value) + /** + * Set the delay time duration for the payment request in RFC 3339 format + * Only supported for card payments + * Default: + * Card-present payments: "PT36H" (36 hours) from the creation time. + * Card-not-present payments: "P7D" (7 days) from the creation time. + * Example for 2 days, 12 hours, 30 minutes, and 15 seconds: P2DT12H30M15S + * https://developer.squareup.com/docs/payments-api/take-payments/card-payments/delayed-capture#time-threshold + */ + public function setDelayDuration(string $value) { return $this->setParameter('delayDuration', $value); } + public function getDelayAction() + { + return $this->getParameter('delayAction') ?? 'CANCEL'; + } + + /** + * Set the delay action for the payment request + * The action must be CANCEL or COMPLETE + * Default: CANCEL + * https://developer.squareup.com/docs/payments-api/take-payments/card-payments/delayed-capture#time-threshold + */ + public function setDelayAction(string $value) + { + return $this->setParameter('delayAction', $value); + } + public function getAutocomplete() { return $this->getParameter('autocomplete') ?? true; } - public function setAutocomplete($autocomplete) + public function setAutocomplete(bool $autocomplete) { return $this->setParameter('autocomplete', $autocomplete); } @@ -325,6 +351,26 @@ public function setAccessToken($value) return $this->setParameter('accessToken', $value); } + public function getCustomerInitiated() + { + return $this->getParameter('customerInitiated') ?? null; + } + + public function setCustomerInitiated(bool $value) + { + return $this->setParameter('customerInitiated', $value); + } + + public function getSellerKeyedIn() + { + return $this->getParameter('sellerKeyedIn') ?? null; + } + + public function setSellerKeyedIn(bool $value) + { + return $this->setParameter('sellerKeyedIn', $value); + } + public function getEnvironment() { return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION; @@ -398,14 +444,14 @@ public function getData() } unset($cardData); } + // set up Payment Request body + $data = new \Square\Models\CreatePaymentRequest($this->getSourceId(), $this->getIdempotencyKey()); // set up amount of money to accept for this payment, not including tip money. $amountMoney = new Money(); $amountMoney->setAmount($this->getAmountInteger()); $amountMoney->setCurrency($this->getCurrency()); - - // set up Payment Request body - $data = new \Square\Models\CreatePaymentRequest($this->getSourceId(), $this->getIdempotencyKey(), $amountMoney); + $data->setAmountMoney($amountMoney); // tip money if ($this->getTipAmountInteger()) { @@ -424,7 +470,12 @@ public function getData() $data->setAppFeeMoney($appFeeMoney); } - $data->setDelayDuration($this->getDelayDuration()); + // set up a delayed capture if needed + if ($this->getAutocomplete() === false) { + $data->setDelayDuration($this->getDelayDuration()); + $data->setDelayAction($this->getDelayAction()); + } + $data->setAutocomplete($this->getAutocomplete()); $data->setOrderId($this->getOrderId()); $data->setCustomerId($this->getCustomerId()); @@ -450,7 +501,10 @@ public function getData() // set up details for external payment (payment->sourceId is EXTERNAL) if ($this->getExternalPaymentType() !== null) { - $externalDetails = new ExternalPaymentDetails($this->getExternalPaymentType(), $this->getExternalPaymentSource()); + $externalDetails = new ExternalPaymentDetails( + $this->getExternalPaymentType(), + $this->getExternalPaymentSource() + ); // note this is a separate sourceId from the payment sourceId $externalDetails->setSourceId($this->getExternalPaymentSourceId()); if ($this->getExternalPaymentSourceFeeAmountInteger() !== null) { @@ -462,6 +516,14 @@ public function getData() $data->setExternalDetails($externalDetails); } + // set up customer details + if ($this->getCustomerInitiated() !== null || $this->getSellerKeyedIn() !== null) { + $customerDetails = new CustomerDetails(); + $customerDetails->setCustomerInitiated($this->getCustomerInitiated()); + $customerDetails->setSellerKeyedIn($this->getSellerKeyedIn()); + $data->setCustomerDetails($customerDetails); + } + return $data; } diff --git a/src/Message/CreatePaymentResponse.php b/src/Message/CreatePaymentResponse.php index a99a411..db9b819 100755 --- a/src/Message/CreatePaymentResponse.php +++ b/src/Message/CreatePaymentResponse.php @@ -18,7 +18,12 @@ class CreatePaymentResponse extends AbstractResponse implements ResponseInterfac */ public function isSuccessful() { - return ($this->data['successful'] && in_array($this->getPayment()->getStatus(), array('COMPLETED', 'APPROVED'), true)); + return ( + $this->data['successful'] && in_array( + $this->getPayment()->getStatus(), + array('COMPLETED', 'APPROVED'), + true + )); } /** @@ -119,7 +124,7 @@ public function getPayment() :?Payment return $this->data['result']->getPayment() ?? null; } - // The following methods are provided for convenience, but all can be done with getPayment()->get{PaymentAttribute}() + //The following methods are provided for convenience, but all can be done with getPayment()->get{PaymentAttribute}() public function getOrderId() { diff --git a/src/Message/CustomerResponse.php b/src/Message/CustomerResponse.php index ec92f2c..b93cf63 100644 --- a/src/Message/CustomerResponse.php +++ b/src/Message/CustomerResponse.php @@ -29,25 +29,27 @@ public function getErrorCode() public function getCustomer(): ?Customer { - if(!empty($this->data['customer'])){ + if (!empty($this->data['customer'])) { return $this->data['customer']; } return null; } - public function getCustomerReference(){ - if(isset($this->data['customer'])){ - if(!empty($this->data['customer'])){ + public function getCustomerReference() + { + if (isset($this->data['customer'])) { + if (!empty($this->data['customer'])) { return $this->data['customer']['id']; } } return null; } - public function getCustomerCards(){ - if(isset($this->data['customer'])){ - if(!empty($this->data['customer'])){ + public function getCustomerCards() + { + if (isset($this->data['customer'])) { + if (!empty($this->data['customer'])) { return $this->data['customer']['cards']; } } diff --git a/src/Message/DeleteCardRequest.php b/src/Message/DeleteCardRequest.php index a76114f..c844634 100644 --- a/src/Message/DeleteCardRequest.php +++ b/src/Message/DeleteCardRequest.php @@ -98,4 +98,4 @@ public function createResponse($response) { return $this->response = new CardResponse($this, $response); } -} \ No newline at end of file +} diff --git a/src/Message/DeleteCustomerRequest.php b/src/Message/DeleteCustomerRequest.php index 8ff19c6..f890598 100644 --- a/src/Message/DeleteCustomerRequest.php +++ b/src/Message/DeleteCustomerRequest.php @@ -20,11 +20,13 @@ public function setAccessToken($value) return $this->setParameter('accessToken', $value); } - public function setCustomerReference($value){ + public function setCustomerReference($value) + { return $this->setParameter('customerReference', $value); } - public function getCustomerReference(){ + public function getCustomerReference() + { return $this->getParameter('customerReference'); } @@ -86,4 +88,4 @@ public function createResponse($response) { return $this->response = new CustomerResponse($this, $response); } -} \ No newline at end of file +} diff --git a/src/Message/FetchCardRequest.php b/src/Message/FetchCardRequest.php index ef768fa..e88a20f 100644 --- a/src/Message/FetchCardRequest.php +++ b/src/Message/FetchCardRequest.php @@ -87,11 +87,11 @@ public function sendData($data) ]; } else { $cardId = $this->getCard(); - $cards = array_filter($result->getResult()->getCustomer()->getCards(), function ($cur) use ($cardId){ + $cards = array_filter($result->getResult()->getCustomer()->getCards(), function ($cur) use ($cardId) { return $cur->getId() == $cardId; }); - if($cards === null || count($cards) == 0){ + if ($cards === null || count($cards) == 0) { throw new \Exception('Card not found!'); } $response = [ @@ -113,4 +113,4 @@ public function createResponse($response) { return $this->response = new CardResponse($this, $response); } -} \ No newline at end of file +} diff --git a/src/Message/FetchCustomerRequest.php b/src/Message/FetchCustomerRequest.php index f020de7..14bd515 100644 --- a/src/Message/FetchCustomerRequest.php +++ b/src/Message/FetchCustomerRequest.php @@ -88,5 +88,4 @@ public function createResponse($response) { return $this->response = new CustomerResponse($this, $response); } - } diff --git a/src/Message/RefundRequest.php b/src/Message/RefundRequest.php index 123eaeb..522a4d4 100755 --- a/src/Message/RefundRequest.php +++ b/src/Message/RefundRequest.php @@ -92,7 +92,11 @@ public function getData() $amountMoney->setAmount($this->getAmountInteger()); $amountMoney->setCurrency($this->getCurrency()); - $data = new \Square\Models\RefundPaymentRequest($this->getIdempotencyKey(), $amountMoney, $this->getTransactionId()); + $data = new \Square\Models\RefundPaymentRequest( + $this->getIdempotencyKey(), + $amountMoney, + $this->getTransactionId() + ); $data->setReason($this->getReason()); return $data; @@ -129,7 +133,6 @@ public function sendData($data) if (!empty($processing_fee)) { $response['processing_fee'] = $processing_fee->getAmount(); } - } } catch (\Exception $e) { $response = [ diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index a288c5e..cd0a2e6 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -7,30 +7,35 @@ class GatewayTest extends GatewayTestCase { public $options; - - public function setUp() + + public function setUp(): void { parent::setUp(); $this->gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest()); - $this->gateway->setAccessToken('sandbox-sq0atb-ULI2NEKmXpkABJb4G17e6A'); - $this->gateway->setLocationId('CBASEDHRl0qakIMd91_K52yx7XcgAQ'); + $this->gateway->setAppId(getenv('SQUARE_APP_ID')); + $this->gateway->setAccessToken(getenv('SQUARE_ACCESS_TOKEN')); + $this->gateway->setTestMode(true); + $this->gateway->setLocationId(getenv('SQUARE_LOCATION_ID')); $this->gateway->setIdempotencyKey(uniqid()); $this->options = [ - 'transactionReference'=> 'REF01', - 'customer_id' => uniqid(), - 'card_nonce' => 'fake-card-nonce-ok', - 'customer_card_id' => 'fake-customer-card-id-ok', - 'currency' => 'USD', - 'amount' => '620.00', - 'items' => [ + 'token' => 'CASH', + 'transactionReference' => 'REF01', +// 'customer_id' => uniqid(), + 'card_nonce' => 'fake-card-nonce-ok', + 'customer_card_id' => 'fake-customer-card-id-ok', + 'currency' => 'USD', + 'amount' => '620.00', + 'items' => [ [ - 'name' => 'Name', - 'price' => '620.00', - 'quantity'=> 1 - ] - ] + 'name' => 'Name', + 'price' => '620.00', + 'quantity' => 1, + ], + ], + 'cashAmountInteger' => '62000', + 'cashCurrency' => 'USD', ]; } @@ -40,4 +45,4 @@ public function testPurchase() $this->assertTrue($response->isSuccessful()); } -} +} \ No newline at end of file diff --git a/tests/Message/PurchaseRequestTest.php b/tests/Message/PurchaseRequestTest.php index fecedcb..1121003 100644 --- a/tests/Message/PurchaseRequestTest.php +++ b/tests/Message/PurchaseRequestTest.php @@ -6,12 +6,12 @@ class WebPaymentRequestTest extends TestCase { - protected function setUp() - { + protected function setUp(): void { $this->request = new WebPaymentRequest($this->getHttpClient(), $this->getHttpRequest()); $this->request->initialize( array( + 'locationId' => getenv('SQUARE_LOCATION_ID'), 'transactionReference' => 'REF01', 'currency' => 'USD', 'items' => array( @@ -29,6 +29,7 @@ public function testGetData() { $this->request->initialize( array( + 'locationId' => getenv('SQUARE_LOCATION_ID'), 'transactionReference' => 'REF01', 'currency' => 'USD', 'items' => array( diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..798730f --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,9 @@ +load(); +}