Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
92 changes: 92 additions & 0 deletions src/Message/FetchRefundRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Omnipay\Square\Message;

use Omnipay\Common\Message\AbstractRequest;
use Omnipay\Common\Message\ResponseInterface;
use Square\Environment;
use Square\SquareClient;

class FetchRefundRequest extends AbstractRequest
{
public function getAccessToken()
{
return $this->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);
}

}
33 changes: 33 additions & 0 deletions src/Message/FetchRefundResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Omnipay\Square\Message;

use Omnipay\Common\Message\AbstractResponse;
use Square\Models\PaymentRefund;

class FetchRefundResponse extends AbstractResponse
{
public function isSuccessful()
{
return $this->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;
}
}