Skip to content

Commit 938b751

Browse files
authored
Merge pull request #19 from blasttech/add-fetch-refund-request
Add fetch refund request
2 parents 02f19ee + 38fc188 commit 938b751

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

src/Gateway.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,13 @@ public function refund(array $parameters = [])
178178
{
179179
return $this->createRequest('\Omnipay\Square\Message\RefundRequest', $parameters);
180180
}
181+
182+
/**
183+
* @param array $parameters
184+
* @return \Omnipay\Common\Message\AbstractRequest|\Omnipay\Square\Message\FetchRefundResponse
185+
*/
186+
public function fetchRefund(array $parameters = [])
187+
{
188+
return $this->createRequest('\Omnipay\Square\Message\FetchRefundRequest', $parameters);
189+
}
181190
}

src/Message/FetchRefundRequest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Omnipay\Square\Message;
4+
5+
use Omnipay\Common\Message\AbstractRequest;
6+
use Omnipay\Common\Message\ResponseInterface;
7+
use Square\Environment;
8+
use Square\SquareClient;
9+
10+
class FetchRefundRequest extends AbstractRequest
11+
{
12+
public function getAccessToken()
13+
{
14+
return $this->getParameter('accessToken');
15+
}
16+
17+
public function setAccessToken($value)
18+
{
19+
return $this->setParameter('accessToken', $value);
20+
}
21+
22+
public function setRefundId($value)
23+
{
24+
return $this->setParameter('refundId', $value);
25+
}
26+
27+
public function getRefundId()
28+
{
29+
return $this->getParameter('refundId');
30+
}
31+
32+
public function getEnvironment()
33+
{
34+
return $this->getTestMode() === true ? Environment::SANDBOX : Environment::PRODUCTION;
35+
}
36+
37+
private function getApiInstance()
38+
{
39+
$api_client = new SquareClient([
40+
'accessToken' => $this->getAccessToken(),
41+
'environment' => $this->getEnvironment()
42+
]);
43+
44+
return $api_client->getRefundsApi();
45+
}
46+
47+
public function getData()
48+
{
49+
$data = [];
50+
51+
$data['refund_id'] = $this->getRefundId();
52+
53+
return $data;
54+
}
55+
56+
public function sendData($data)
57+
{
58+
try {
59+
$api_instance = $this->getApiInstance();
60+
61+
$result = $api_instance->getPaymentRefund($data['refund_id']);
62+
63+
if ($errors = $result->getErrors()) {
64+
$response = [
65+
'status' => 'error',
66+
'code' => $errors[0]->getCode(),
67+
'detail' => $errors[0]->getDetail(),
68+
'field' => $errors[0]->getField(),
69+
'category' => $errors[0]->getCategory()
70+
];
71+
} else {
72+
$response = [
73+
'status' => 'success',
74+
'refund' => $result->getResult()->getRefund()
75+
];
76+
}
77+
} catch (\Exception $e) {
78+
$response = [
79+
'status' => 'error',
80+
'detail' => 'Exception when retrieving refund: ' . $e->getMessage()
81+
];
82+
}
83+
84+
return $this->createResponse($response);
85+
}
86+
87+
public function createResponse($response)
88+
{
89+
return $this->response = new FetchRefundResponse($this, $response);
90+
}
91+
92+
}

src/Message/FetchRefundResponse.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Omnipay\Square\Message;
4+
5+
use Omnipay\Common\Message\AbstractResponse;
6+
use Square\Models\PaymentRefund;
7+
8+
class FetchRefundResponse extends AbstractResponse
9+
{
10+
public function isSuccessful()
11+
{
12+
return $this->data['status'] === 'success';
13+
}
14+
15+
public function getErrorDetail()
16+
{
17+
return $this->data['detail'];
18+
}
19+
20+
public function getErrorCode()
21+
{
22+
return $this->data['code'];
23+
}
24+
25+
public function getRefund(): ?PaymentRefund
26+
{
27+
if (!empty($this->data['refund'])) {
28+
return $this->data['refund'];
29+
}
30+
31+
return null;
32+
}
33+
}

src/Message/RefundResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public function isPending()
1919
{
2020
return $this->data['status'] === 'PENDING';
2121
}
22+
23+
public function getTransactionReference()
24+
{
25+
return $this->isSuccessful() || $this->isPending() ? $this->data['id'] : null;
26+
}
2227

2328
public function getMessage()
2429
{

0 commit comments

Comments
 (0)