Skip to content

Commit 9f22614

Browse files
committed
feat: void request
1 parent 17aefd3 commit 9f22614

File tree

9 files changed

+141
-5
lines changed

9 files changed

+141
-5
lines changed

src/Gateway.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
use Omnipay\ECPay\Message\FetchTransactionRequest;
1111
use Omnipay\ECPay\Message\PurchaseRequest;
1212
use Omnipay\ECPay\Message\RefundRequest;
13+
use Omnipay\ECPay\Message\VoidRequest;
1314
use Omnipay\ECPay\Traits\HasDefaults;
1415

1516
/**
1617
* Skeleton Gateway.
1718
* @method RequestInterface authorize(array $options = [])
1819
* @method RequestInterface completeAuthorize(array $options = [])
1920
* @method RequestInterface capture(array $options = [])
20-
* @method RequestInterface void(array $options = [])
2121
* @method RequestInterface createCard(array $options = [])
2222
* @method RequestInterface updateCard(array $options = [])
2323
* @method RequestInterface deleteCard(array $options = [])
@@ -86,4 +86,9 @@ public function refund(array $options = [])
8686
{
8787
return $this->createRequest(RefundRequest::class, $options);
8888
}
89+
90+
public function void(array $options = [])
91+
{
92+
return $this->createRequest(VoidRequest::class, $options);
93+
}
8994
}

src/Message/RefundRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function getTradeNo()
2828

2929
public function getData()
3030
{
31-
$this->validate('transactionId', 'transactionReference');
31+
$this->validate('transactionId', 'transactionReference', 'amount');
3232

3333
return [
3434
'MerchantTradeNo' => $this->getTransactionId(),
@@ -40,7 +40,7 @@ public function getData()
4040

4141
public function sendData($data)
4242
{
43-
return $this->response = new RefundResponse($this, $this->doAction($data));
43+
return $this->response = new VoidOrRefundResponse($this, $this->doAction($data));
4444
}
4545

4646
/**

src/Message/RefundResponse.php renamed to src/Message/VoidOrRefundResponse.php

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

33
namespace Omnipay\ECPay\Message;
44

5-
class RefundResponse extends AbstractResponse
5+
class VoidOrRefundResponse extends AbstractResponse
66
{
77
/**
88
* Is the response successful?

src/Message/VoidRequest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Omnipay\ECPay\Message;
4+
5+
use ECPay_ActionType;
6+
7+
class VoidRequest extends RefundRequest
8+
{
9+
public function setAction($value)
10+
{
11+
return $this->setParameter('action', $value);
12+
}
13+
14+
public function getAction()
15+
{
16+
return $this->getParameter('action') ?: ECPay_ActionType::N;
17+
}
18+
19+
public function getData()
20+
{
21+
$this->validate('transactionId', 'transactionReference', 'amount');
22+
23+
return [
24+
'MerchantTradeNo' => $this->getTransactionId(),
25+
'TradeNo' => $this->getTransactionReference(),
26+
'Action' => $this->getAction(),
27+
'TotalAmount' => $this->getAmount(),
28+
];
29+
}
30+
}

tests/GatewayTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,20 @@ public function testRefund()
114114
]))->send();
115115

116116
self::assertTrue($response->isSuccessful());
117+
self::assertEquals('1909021549160081', $response->getTransactionReference());
118+
self::assertEquals('2821567410556', $response->getTransactionId());
119+
}
120+
121+
public function testVoid()
122+
{
123+
$response = $this->gateway->void(array_merge($this->options, [
124+
'transactionReference' => '1909021549160081',
125+
'transactionId' => '2821567410556',
126+
'amount' => 1000,
127+
]))->send();
128+
117129
self::assertTrue($response->isSuccessful());
118-
self::assertTrue($response->isSuccessful());
130+
self::assertEquals('1909021549160081', $response->getTransactionReference());
131+
self::assertEquals('2821567410556', $response->getTransactionId());
119132
}
120133
}

tests/Message/RefundRequestTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Omnipay\ECPay\Tests\Message;
4+
5+
use Omnipay\ECPay\Tests\Stubs\StubRefundRequest;
6+
use Omnipay\Tests\TestCase;
7+
8+
class RefundRequestTest extends TestCase
9+
{
10+
public function testGetData()
11+
{
12+
$options = [
13+
'MerchantID' => '2000132',
14+
'MerchantTradeNo' => '2821567410556',
15+
'TradeNo' => '1909021549160081',
16+
'TotalAmount' => 1000,
17+
];
18+
$request = new StubRefundRequest($this->getHttpClient(), $this->getHttpRequest());
19+
$request->initialize($options);
20+
21+
self::assertEquals([
22+
'MerchantTradeNo' => '2821567410556',
23+
'TradeNo' => '1909021549160081',
24+
'Action' => 'R',
25+
'TotalAmount' => '1000.00',
26+
], $request->getData());
27+
}
28+
}

tests/Message/VoidRequestTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Omnipay\ECPay\Tests\Message;
4+
5+
use Omnipay\ECPay\Tests\Stubs\StubVoidRequest;
6+
use Omnipay\Tests\TestCase;
7+
8+
class VoidRequestTest extends TestCase
9+
{
10+
public function testGetData()
11+
{
12+
$options = [
13+
'MerchantID' => '2000132',
14+
'MerchantTradeNo' => '2821567410556',
15+
'TradeNo' => '1909021549160081',
16+
'TotalAmount' => 1000,
17+
];
18+
$request = new StubVoidRequest($this->getHttpClient(), $this->getHttpRequest());
19+
$request->initialize($options);
20+
21+
self::assertEquals([
22+
'MerchantTradeNo' => '2821567410556',
23+
'TradeNo' => '1909021549160081',
24+
'Action' => 'N',
25+
'TotalAmount' => '1000.00',
26+
], $request->getData());
27+
}
28+
}

tests/Stubs/StubGateway.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@ public function refund(array $options = [])
2424
{
2525
return $this->createRequest(StubRefundRequest::class, $options);
2626
}
27+
28+
/**
29+
* @param array $options
30+
* @return RequestInterface
31+
*/
32+
public function void(array $options = [])
33+
{
34+
return $this->createRequest(StubVoidRequest::class, $options);
35+
}
2736
}

tests/Stubs/StubVoidRequest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Omnipay\ECPay\Tests\Stubs;
4+
5+
use Omnipay\ECPay\Message\VoidRequest;
6+
7+
class StubVoidRequest extends VoidRequest
8+
{
9+
/**
10+
* @param array $data
11+
* @return array
12+
*/
13+
protected function doAction($data)
14+
{
15+
return [
16+
'MerchantID' => $this->getMerchantID(),
17+
'MerchantTradeNo' => $data['MerchantTradeNo'],
18+
'TradeNo' => $data['TradeNo'],
19+
'RtnCode' => '1',
20+
'RtnMsg' => '',
21+
];
22+
}
23+
}

0 commit comments

Comments
 (0)