Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Jun 16, 2020
1 parent 40b0588 commit 9918a73
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 22 deletions.
10 changes: 9 additions & 1 deletion src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
9 changes: 9 additions & 0 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
}
}
8 changes: 1 addition & 7 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 9 additions & 10 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/Message/PurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class PurchaseResponse extends AbstractResponse implements RedirectResponseInterface
{

public function isSuccessful()
{
return false;
Expand Down
48 changes: 48 additions & 0 deletions src/Message/RefundRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Omnipay\Paystack\Message;

use Omnipay\Common\Exception\InvalidRequestException;

class RefundRequest extends AbstractRequest
{
/**
* @inheritDoc
*/
public function getApiEndpoint()
{
return $this->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);
}
}
49 changes: 49 additions & 0 deletions src/Message/RefundResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Omnipay\Paystack\Message;

use Omnipay\Common\Message\AbstractResponse;

class RefundResponse extends AbstractResponse
{

public function isSuccessful()
{
return false;
}

public function isRedirect()
{
return false;
}

public function getMessage()
{
if (isset($this->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 '';
}
}
5 changes: 2 additions & 3 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}
Expand All @@ -30,8 +30,7 @@ public function testPurchase()
$this->setMockHttpResponse('PurchaseSuccess.txt');

$options = array_merge($this->options, [
'email' => '[email protected]',
'reference' => static::PURCHASE_REFERENCE
'email' => '[email protected]'
]);

$response = $this->gateway->purchase($options)->send();
Expand Down

0 comments on commit 9918a73

Please sign in to comment.