Skip to content

Commit

Permalink
Merge pull request #3 from pixelandtonic/master
Browse files Browse the repository at this point in the history
Updates including refunds
  • Loading branch information
tolu-paystack authored Jun 17, 2020
2 parents 87a6bb3 + f5507dd commit 31740ac
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 31 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": "^7.2",
"php": "^7.0",
"omnipay/common": "^3.0"
},
"require-dev": {
Expand Down
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);
}
}
31 changes: 17 additions & 14 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace Omnipay\Paystack\Message;

use Omnipay\Common\Exception\BadMethodCallException;
use Omnipay\Common\Exception\InvalidRequestException;

abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
{
protected $baseApiEndpoint = 'https://api.paystack.co/';
protected $baseApiEndpoint = 'https://api.paystack.co';

/**
* @return string The URL endpoint for the request
Expand Down Expand Up @@ -40,17 +39,21 @@ public function setSecretKey($value)

protected function sendRequest($method, $endpoint, array $data = null)
{

$headers = [
'Authorization' => 'Bearer ' . $this->getSecretKey(),
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache'
];

$response = $this->httpClient->request($method, $this->baseApiEndpoint. $endpoint, $headers, json_encode($data));
$responseData = json_decode((string)$response->getBody(), true);

return $responseData;

$headers = [
'Authorization' => 'Bearer ' . $this->getSecretKey(),
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache'
];

$url = $this->baseApiEndpoint . $endpoint;
$response = $this->httpClient->request(
$method,
$url,
$headers,
json_encode($data)
);
$responseData = json_decode((string)$response->getBody(), true);

return $responseData;
}
}
9 changes: 3 additions & 6 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CompletePurchaseRequest extends AbstractRequest
*/
public function getApiEndpoint()
{
return $this->baseApiEndpoint . 'transaction/verify/' . rawurlencode($this->getTransactionReference());
return '/transaction/verify/' . rawurlencode($this->getTransactionReference());
}

/**
Expand All @@ -28,14 +28,11 @@ public function getData()
public function sendData($data)
{
try {

$response = $this->sendRequest('GET', $this->getApiEndpoint(), json_encode($data));

$response = $this->sendRequest('GET', $this->getApiEndpoint(), $data);
} catch (\Exception $e) {

throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
}

return $this->response = new CompletePurchaseResponse($this, $responseData);
return $this->response = new CompletePurchaseResponse($this, $response);
}
}
4 changes: 3 additions & 1 deletion src/Message/CompletePurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Omnipay\Paystack\Message;

class CompletePurchaseResponse extends \Omnipay\Common\Message\AbstractResponse
use Omnipay\Common\Message\AbstractResponse;

class CompletePurchaseResponse extends AbstractResponse
{

public function isSuccessful()
Expand Down
7 changes: 4 additions & 3 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PurchaseRequest extends AbstractRequest
*/
public function getApiEndpoint()
{
return $this->baseApiEndpoint . '/transaction/initialize/';
return '/transaction/initialize';
}

/**
Expand All @@ -23,13 +23,14 @@ public function getData()

$amount = $this->getAmountInteger();
$email = $this->getParameter('email');
$reference = $this->getTransactionReference();

return [
'amount' => $amount,
'currency' => $this->getCurrency(),
'email' => $email,
'reference' => $this->getTransactionReference(),
'callback_url' => $this->getReturnUrl(),
'reference' => $reference
'metadata' => $this->getParameter('metadata')
];
}

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
46 changes: 46 additions & 0 deletions src/Message/RefundRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Omnipay\Paystack\Message;

use Omnipay\Common\Exception\InvalidRequestException;

class RefundRequest extends AbstractRequest
{
/**
* @inheritDoc
*/
public function getApiEndpoint()
{
return '/refund';
}

/**
* @inheritdoc
*/
public function getData()
{
$data = [];
$data['transaction'] = $this->getTransactionReference();
$data['currency'] = $this->getCurrency();

if ($this->getAmountInteger()) {
$data['amount'] = $this->getAmountInteger();
}

return $data;
}

/**
* @inheritdoc
*/
public function sendData($data)
{
try {
$response = $this->sendRequest('POST', $this->getApiEndpoint(), $data);
} catch (\Exception $e) {
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
}

return $this->response = new RefundResponse($this, $response);
}
}
55 changes: 55 additions & 0 deletions src/Message/RefundResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Omnipay\Paystack\Message;

use Omnipay\Common\Message\AbstractResponse;

class RefundResponse extends AbstractResponse
{

public function isSuccessful()
{

if (isset($this->data['data']) && $transaction = $this->data['data']['transaction']) {
if (isset($transaction['status']) && $transaction['status'] == 'reversed') {
return true;
}
}

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['reference'];
}
}

return '';
}
}
23 changes: 19 additions & 4 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,49 @@ class GatewayTest extends GatewayTestCase
/** @var Gateway */
protected $gateway;

/** @var array */
protected $options;

/**
*
*/
public function setUp()
{
parent::setUp();

$this->gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest());

$this->options = [
'amount' => '100',
'amount' => '2500',
'reference' => static::PURCHASE_REFERENCE
];
}

/**
*
*/
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();

$this->assertTrue($response->isRedirect(), 'Purchase response is a redirect');
$this->assertEquals(static::PURCHASE_REFERENCE, $response->getTransactionReference(), 'Reference is as we gave it.');
$this->assertEquals(
static::PURCHASE_REFERENCE,
$response->getTransactionReference(),
'Reference is as we gave it.'
);
$this->assertEquals('Authorization URL created', $response->getMessage());
}

/**
*
*/
public function testRefund()
{
$this->setMockHttpResponse('RefundSuccess.txt');
Expand Down

0 comments on commit 31740ac

Please sign in to comment.