From 38fc188a9c2af87f197f03fa90cfeb4efa382662 Mon Sep 17 00:00:00 2001 From: Mike Beck Date: Tue, 19 Oct 2021 11:56:25 +1300 Subject: [PATCH] feat: Add 'fetch refund' request --- src/Gateway.php | 9 +++ src/Message/FetchRefundRequest.php | 92 +++++++++++++++++++++++++++++ src/Message/FetchRefundResponse.php | 33 +++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/Message/FetchRefundRequest.php create mode 100644 src/Message/FetchRefundResponse.php diff --git a/src/Gateway.php b/src/Gateway.php index cca64fe..4ca471d 100755 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -178,4 +178,13 @@ public function refund(array $parameters = []) { return $this->createRequest('\Omnipay\Square\Message\RefundRequest', $parameters); } + + /** + * @param array $parameters + * @return \Omnipay\Common\Message\AbstractRequest|\Omnipay\Square\Message\FetchRefundResponse + */ + public function fetchRefund(array $parameters = []) + { + return $this->createRequest('\Omnipay\Square\Message\FetchRefundRequest', $parameters); + } } diff --git a/src/Message/FetchRefundRequest.php b/src/Message/FetchRefundRequest.php new file mode 100644 index 0000000..e735b19 --- /dev/null +++ b/src/Message/FetchRefundRequest.php @@ -0,0 +1,92 @@ +getParameter('accessToken'); + } + + public function setAccessToken($value) + { + return $this->setParameter('accessToken', $value); + } + + public function setRefundId($value) + { + return $this->setParameter('refundId', $value); + } + + public function getRefundId() + { + return $this->getParameter('refundId'); + } + + public function getEnvironment() + { + return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION; + } + + private function getApiInstance() + { + $api_client = new SquareClient([ + 'accessToken' => $this->getAccessToken(), + 'environment' => $this->getEnvironment() + ]); + + return $api_client->getRefundsApi(); + } + + public function getData() + { + $data = []; + + $data['refund_id'] = $this->getRefundId(); + + return $data; + } + + public function sendData($data) + { + try { + $api_instance = $this->getApiInstance(); + + $result = $api_instance->getPaymentRefund($data['refund_id']); + + if ($errors = $result->getErrors()) { + $response = [ + 'status' => 'error', + 'code' => $errors[0]->getCode(), + 'detail' => $errors[0]->getDetail(), + 'field' => $errors[0]->getField(), + 'category' => $errors[0]->getCategory() + ]; + } else { + $response = [ + 'status' => 'success', + 'refund' => $result->getResult()->getRefund() + ]; + } + } catch (\Exception $e) { + $response = [ + 'status' => 'error', + 'detail' => 'Exception when retrieving refund: ' . $e->getMessage() + ]; + } + + return $this->createResponse($response); + } + + public function createResponse($response) + { + return $this->response = new FetchRefundResponse($this, $response); + } + +} diff --git a/src/Message/FetchRefundResponse.php b/src/Message/FetchRefundResponse.php new file mode 100644 index 0000000..7d08125 --- /dev/null +++ b/src/Message/FetchRefundResponse.php @@ -0,0 +1,33 @@ +data['status'] === 'success'; + } + + public function getErrorDetail() + { + return $this->data['detail']; + } + + public function getErrorCode() + { + return $this->data['code']; + } + + public function getRefund(): ?PaymentRefund + { + if (!empty($this->data['refund'])) { + return $this->data['refund']; + } + + return null; + } +}