Skip to content

Commit 31740ac

Browse files
Merge pull request #3 from pixelandtonic/master
Updates including refunds
2 parents 87a6bb3 + f5507dd commit 31740ac

10 files changed

+157
-31
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
}
1616
],
1717
"require": {
18-
"php": "^7.2",
18+
"php": "^7.0",
1919
"omnipay/common": "^3.0"
2020
},
2121
"require-dev": {

src/Gateway.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Omnipay\Common\AbstractGateway;
66
use Omnipay\Paystack\Message\CompletePurchaseRequest;
77
use Omnipay\Paystack\Message\PurchaseRequest;
8-
use Omnipay\Paystack\Message\PurchaseResponse;
8+
use Omnipay\Paystack\Message\RefundRequest;
99

1010
/**
1111
* PayStack Gateway
@@ -81,4 +81,12 @@ public function completePurchase(array $parameters = [])
8181
{
8282
return $this->createRequest(CompletePurchaseRequest::class, $parameters);
8383
}
84+
85+
/**
86+
* @inheritDoc
87+
*/
88+
public function refund(array $parameters = [])
89+
{
90+
return $this->createRequest(RefundRequest::class, $parameters);
91+
}
8492
}

src/Message/AbstractRequest.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
namespace Omnipay\Paystack\Message;
44

5-
use Omnipay\Common\Exception\BadMethodCallException;
65
use Omnipay\Common\Exception\InvalidRequestException;
76

87
abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
98
{
10-
protected $baseApiEndpoint = 'https://api.paystack.co/';
9+
protected $baseApiEndpoint = 'https://api.paystack.co';
1110

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

4140
protected function sendRequest($method, $endpoint, array $data = null)
4241
{
43-
44-
$headers = [
45-
'Authorization' => 'Bearer ' . $this->getSecretKey(),
46-
'Content-Type' => 'application/json',
47-
'Cache-Control' => 'no-cache'
48-
];
49-
50-
$response = $this->httpClient->request($method, $this->baseApiEndpoint. $endpoint, $headers, json_encode($data));
51-
$responseData = json_decode((string)$response->getBody(), true);
52-
53-
return $responseData;
54-
42+
$headers = [
43+
'Authorization' => 'Bearer ' . $this->getSecretKey(),
44+
'Content-Type' => 'application/json',
45+
'Cache-Control' => 'no-cache'
46+
];
47+
48+
$url = $this->baseApiEndpoint . $endpoint;
49+
$response = $this->httpClient->request(
50+
$method,
51+
$url,
52+
$headers,
53+
json_encode($data)
54+
);
55+
$responseData = json_decode((string)$response->getBody(), true);
56+
57+
return $responseData;
5558
}
5659
}

src/Message/CompletePurchaseRequest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CompletePurchaseRequest extends AbstractRequest
1111
*/
1212
public function getApiEndpoint()
1313
{
14-
return $this->baseApiEndpoint . 'transaction/verify/' . rawurlencode($this->getTransactionReference());
14+
return '/transaction/verify/' . rawurlencode($this->getTransactionReference());
1515
}
1616

1717
/**
@@ -28,14 +28,11 @@ public function getData()
2828
public function sendData($data)
2929
{
3030
try {
31-
32-
$response = $this->sendRequest('GET', $this->getApiEndpoint(), json_encode($data));
33-
31+
$response = $this->sendRequest('GET', $this->getApiEndpoint(), $data);
3432
} catch (\Exception $e) {
35-
3633
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
3734
}
3835

39-
return $this->response = new CompletePurchaseResponse($this, $responseData);
36+
return $this->response = new CompletePurchaseResponse($this, $response);
4037
}
4138
}

src/Message/CompletePurchaseResponse.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Omnipay\Paystack\Message;
44

5-
class CompletePurchaseResponse extends \Omnipay\Common\Message\AbstractResponse
5+
use Omnipay\Common\Message\AbstractResponse;
6+
7+
class CompletePurchaseResponse extends AbstractResponse
68
{
79

810
public function isSuccessful()

src/Message/PurchaseRequest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class PurchaseRequest extends AbstractRequest
1111
*/
1212
public function getApiEndpoint()
1313
{
14-
return $this->baseApiEndpoint . '/transaction/initialize/';
14+
return '/transaction/initialize';
1515
}
1616

1717
/**
@@ -23,13 +23,14 @@ public function getData()
2323

2424
$amount = $this->getAmountInteger();
2525
$email = $this->getParameter('email');
26-
$reference = $this->getTransactionReference();
2726

2827
return [
2928
'amount' => $amount,
29+
'currency' => $this->getCurrency(),
3030
'email' => $email,
31+
'reference' => $this->getTransactionReference(),
3132
'callback_url' => $this->getReturnUrl(),
32-
'reference' => $reference
33+
'metadata' => $this->getParameter('metadata')
3334
];
3435
}
3536

src/Message/PurchaseResponse.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
class PurchaseResponse extends AbstractResponse implements RedirectResponseInterface
99
{
10-
1110
public function isSuccessful()
1211
{
1312
return false;

src/Message/RefundRequest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Omnipay\Paystack\Message;
4+
5+
use Omnipay\Common\Exception\InvalidRequestException;
6+
7+
class RefundRequest extends AbstractRequest
8+
{
9+
/**
10+
* @inheritDoc
11+
*/
12+
public function getApiEndpoint()
13+
{
14+
return '/refund';
15+
}
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
public function getData()
21+
{
22+
$data = [];
23+
$data['transaction'] = $this->getTransactionReference();
24+
$data['currency'] = $this->getCurrency();
25+
26+
if ($this->getAmountInteger()) {
27+
$data['amount'] = $this->getAmountInteger();
28+
}
29+
30+
return $data;
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function sendData($data)
37+
{
38+
try {
39+
$response = $this->sendRequest('POST', $this->getApiEndpoint(), $data);
40+
} catch (\Exception $e) {
41+
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
42+
}
43+
44+
return $this->response = new RefundResponse($this, $response);
45+
}
46+
}

src/Message/RefundResponse.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Omnipay\Paystack\Message;
4+
5+
use Omnipay\Common\Message\AbstractResponse;
6+
7+
class RefundResponse extends AbstractResponse
8+
{
9+
10+
public function isSuccessful()
11+
{
12+
13+
if (isset($this->data['data']) && $transaction = $this->data['data']['transaction']) {
14+
if (isset($transaction['status']) && $transaction['status'] == 'reversed') {
15+
return true;
16+
}
17+
}
18+
19+
return false;
20+
}
21+
22+
public function isRedirect()
23+
{
24+
return false;
25+
}
26+
27+
public function getMessage()
28+
{
29+
if (isset($this->data['message']) && $message = $this->data['message']) {
30+
return $message;
31+
}
32+
33+
return '';
34+
}
35+
36+
public function getCode()
37+
{
38+
if (isset($this->data['data']) && $data = $this->data['data']) {
39+
return $data['access_code'];
40+
}
41+
42+
return '';
43+
}
44+
45+
public function getTransactionReference()
46+
{
47+
if (isset($this->data['data']) && $data = $this->data['data']) {
48+
if (isset($data['transaction']) && $transaction = $data['transaction']) {
49+
return $transaction['reference'];
50+
}
51+
}
52+
53+
return '';
54+
}
55+
}

tests/GatewayTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,49 @@ class GatewayTest extends GatewayTestCase
1313
/** @var Gateway */
1414
protected $gateway;
1515

16+
/** @var array */
17+
protected $options;
18+
19+
/**
20+
*
21+
*/
1622
public function setUp()
1723
{
1824
parent::setUp();
1925

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

2228
$this->options = [
23-
'amount' => '100',
29+
'amount' => '2500',
2430
'reference' => static::PURCHASE_REFERENCE
2531
];
2632
}
2733

34+
/**
35+
*
36+
*/
2837
public function testPurchase()
2938
{
3039
$this->setMockHttpResponse('PurchaseSuccess.txt');
3140

3241
$options = array_merge($this->options, [
33-
'email' => 'email@email.com',
34-
'reference' => static::PURCHASE_REFERENCE
42+
'email' => 'email@email.com'
3543
]);
3644

3745
$response = $this->gateway->purchase($options)->send();
3846

3947
$this->assertTrue($response->isRedirect(), 'Purchase response is a redirect');
40-
$this->assertEquals(static::PURCHASE_REFERENCE, $response->getTransactionReference(), 'Reference is as we gave it.');
48+
$this->assertEquals(
49+
static::PURCHASE_REFERENCE,
50+
$response->getTransactionReference(),
51+
'Reference is as we gave it.'
52+
);
4153
$this->assertEquals('Authorization URL created', $response->getMessage());
4254
}
4355

56+
/**
57+
*
58+
*/
4459
public function testRefund()
4560
{
4661
$this->setMockHttpResponse('RefundSuccess.txt');

0 commit comments

Comments
 (0)