diff --git a/src/Gateway.php b/src/Gateway.php index dff28b8..7a995f1 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -5,7 +5,7 @@ use Omnipay\Common\AbstractGateway; use Omnipay\Paystack\Message\CompletePurchaseRequest; use Omnipay\Paystack\Message\PurchaseRequest; -use Omnipay\Paystack\Message\PurchaseResponse; +use Omnipay\Paystack\Message\RefundRequest; /** * PayStack Gateway @@ -81,4 +81,12 @@ public function completePurchase(array $parameters = []) { return $this->createRequest(CompletePurchaseRequest::class, $parameters); } + + /** + * @inheritDoc + */ + public function refund(array $parameters = []) + { + return $this->createRequest(RefundRequest::class, $parameters); + } } diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index f3f14fb..7f7ceec 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -37,4 +37,13 @@ public function setSecretKey($value) { $this->setParameter('secret_key', $value); } + + public function getRequestHeaders() + { + return [ + 'Authorization' => 'Bearer ' . $this->getSecretKey(), + 'Content-Type' => 'application/json', + 'Cache-Control' => 'no-cache' + ]; + } } diff --git a/src/Message/CompletePurchaseRequest.php b/src/Message/CompletePurchaseRequest.php index 30e43b5..d6d5bf9 100644 --- a/src/Message/CompletePurchaseRequest.php +++ b/src/Message/CompletePurchaseRequest.php @@ -28,13 +28,7 @@ public function getData() public function sendData($data) { try { - $headers = [ - 'Authorization' => 'Bearer ' . $this->getSecretKey(), - 'Content-Type' => 'application/json', - 'Cache-Control' => 'no-cache' - ]; - - $response = $this->httpClient->request('GET', $this->getApiEndpoint(), $headers, json_encode($data)); + $response = $this->httpClient->request('GET', $this->getApiEndpoint(), $this->getRequestHeaders(), json_encode($data)); $responseData = json_decode((string)$response->getBody(), true); } catch (\Exception $e) { throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e); diff --git a/src/Message/PurchaseRequest.php b/src/Message/PurchaseRequest.php index 9a2d765..367c909 100644 --- a/src/Message/PurchaseRequest.php +++ b/src/Message/PurchaseRequest.php @@ -23,14 +23,19 @@ public function getData() $amount = $this->getAmountInteger(); $email = $this->getParameter('email'); - $reference = $this->getTransactionReference(); - return [ + $data = [ 'amount' => $amount, 'email' => $email, 'callback_url' => $this->getReturnUrl(), - 'reference' => $reference + 'metadata' => $this->getParameter('metadata') ]; + + if ($reference = $this->getTransactionReference()) { + $data['reference'] = $reference; + } + + return $data; } /** @@ -39,13 +44,7 @@ public function getData() public function sendData($data) { try { - $headers = [ - 'Authorization' => 'Bearer ' . $this->getSecretKey(), - 'Content-Type' => 'application/json', - 'Cache-Control' => 'no-cache' - ]; - - $response = $this->httpClient->request('POST', $this->getApiEndpoint(), $headers, json_encode($data)); + $response = $this->httpClient->request('POST', $this->getApiEndpoint(), $this->getRequestHeaders(), json_encode($data)); $responseData = json_decode((string)$response->getBody(), true); } catch (\Exception $e) { throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e); diff --git a/src/Message/PurchaseResponse.php b/src/Message/PurchaseResponse.php index d89a052..6ff11b3 100644 --- a/src/Message/PurchaseResponse.php +++ b/src/Message/PurchaseResponse.php @@ -7,7 +7,6 @@ class PurchaseResponse extends AbstractResponse implements RedirectResponseInterface { - public function isSuccessful() { return false; diff --git a/src/Message/RefundRequest.php b/src/Message/RefundRequest.php new file mode 100644 index 0000000..e48604d --- /dev/null +++ b/src/Message/RefundRequest.php @@ -0,0 +1,48 @@ +baseApiEndpoint . '/refund'; + } + + /** + * @inheritdoc + */ + public function getData() + { + $amount = $this->getAmountInteger(); + $reference = $this->getTransactionReference(); + + $data = ['transaction' => $reference]; + + if ($amount) { + $data['amount'] = $amount; + } + + return $data; + } + + /** + * @inheritdoc + */ + public function sendData($data) + { + try { + $response = $this->httpClient->request('POST', $this->getApiEndpoint(), $this->getRequestHeaders(), json_encode($data)); + $responseData = json_decode((string)$response->getBody(), true); + } catch (\Exception $e) { + throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e); + } + + return $this->response = new RefundResponse($this, $responseData); + } +} diff --git a/src/Message/RefundResponse.php b/src/Message/RefundResponse.php new file mode 100644 index 0000000..5e8e1b9 --- /dev/null +++ b/src/Message/RefundResponse.php @@ -0,0 +1,49 @@ +data['message']) && $message = $this->data['message']) { + return $message; + } + + return ''; + } + + public function getCode() + { + if (isset($this->data['data']) && $data = $this->data['data']) { + return $data['access_code']; + } + + return ''; + } + + public function getTransactionReference() + { + if (isset($this->data['data']) && $data = $this->data['data']) { + if(isset($data['transaction']) && $transaction = $data['transaction']) + { + return $transaction['id']; + } + } + + return ''; + } +} diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index 365f356..04eb6d9 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -20,7 +20,7 @@ public function setUp() $this->gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest()); $this->options = [ - 'amount' => '100', + 'amount' => '2500', 'reference' => static::PURCHASE_REFERENCE ]; } @@ -30,8 +30,7 @@ public function testPurchase() $this->setMockHttpResponse('PurchaseSuccess.txt'); $options = array_merge($this->options, [ - 'email' => 'email@email.com', - 'reference' => static::PURCHASE_REFERENCE + 'email' => 'email@email.com' ]); $response = $this->gateway->purchase($options)->send();