Skip to content

Commit eb9201c

Browse files
committed
feat: refund
1 parent c6afda8 commit eb9201c

File tree

9 files changed

+222
-33
lines changed

9 files changed

+222
-33
lines changed

src/Gateway.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
namespace Omnipay\MyPay;
44

55
use Omnipay\Common\AbstractGateway;
6-
use Omnipay\Common\Message\AbstractRequest;
7-
use Omnipay\Common\Message\NotificationInterface;
86
use Omnipay\Common\Message\RequestInterface;
97
use Omnipay\MyPay\Message\AcceptNotificationRequest;
108
use Omnipay\MyPay\Message\CompletePurchaseRequest;
119
use Omnipay\MyPay\Message\FetchTransactionRequest;
1210
use Omnipay\MyPay\Message\PurchaseRequest;
11+
use Omnipay\MyPay\Message\RefundRequest;
1312
use Omnipay\MyPay\Traits\HasLocale;
1413
use Omnipay\MyPay\Traits\HasStore;
1514

@@ -18,7 +17,6 @@
1817
* @method RequestInterface authorize(array $options = [])
1918
* @method RequestInterface completeAuthorize(array $options = [])
2019
* @method RequestInterface capture(array $options = [])
21-
* @method RequestInterface refund(array $options = [])
2220
* @method RequestInterface void(array $options = [])
2321
* @method RequestInterface createCard(array $options = [])
2422
* @method RequestInterface updateCard(array $options = [])
@@ -78,4 +76,13 @@ public function fetchTransaction(array $options = [])
7876
{
7977
return $this->createRequest(FetchTransactionRequest::class, $options);
8078
}
79+
80+
/**
81+
* @param array $options
82+
* @return RequestInterface
83+
*/
84+
public function refund(array $options = [])
85+
{
86+
return $this->createRequest(RefundRequest::class, $options);
87+
}
8188
}

src/Message/RefundRequest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Omnipay\MyPay\Message;
4+
5+
use Omnipay\MyPay\Encryption;
6+
use Omnipay\MyPay\Traits\HasCost;
7+
use Omnipay\MyPay\Traits\HasKey;
8+
use Omnipay\MyPay\Traits\HasStore;
9+
use Omnipay\MyPay\Traits\HasUid;
10+
11+
class RefundRequest extends AbstractRequest
12+
{
13+
use HasStore;
14+
use HasUid;
15+
use HasKey;
16+
use HasCost;
17+
18+
/**
19+
* 4.作廢或作廢重開(預設)
20+
* 6.折讓 如有電子發票此欄位有效.
21+
*
22+
* @param int $value
23+
* @return $this
24+
*/
25+
public function setInvoiceState($value)
26+
{
27+
return $this->setParameter('invoice_state', $value);
28+
}
29+
30+
/**
31+
* @return int
32+
*/
33+
public function getInvoiceState()
34+
{
35+
return $this->getParameter('invoice_state') ?: 4;
36+
}
37+
38+
public function getData()
39+
{
40+
$this->validate('key', 'uid', 'amount');
41+
42+
return [
43+
'key' => $this->getKey(),
44+
'uid' => $this->getUid(),
45+
'cost' => $this->getAmount(),
46+
'invoice_state' => $this->getInvoiceState(),
47+
];
48+
}
49+
50+
protected function createBody(Encryption $encryption, array $data)
51+
{
52+
return [
53+
'store_uid' => $this->getStoreUid(),
54+
'service' => $encryption->encrypt([
55+
'service_name' => 'api',
56+
'cmd' => 'api/refund',
57+
]),
58+
'encry_data' => $encryption->encrypt($data),
59+
];
60+
}
61+
62+
protected function createResponse($data)
63+
{
64+
return $this->response = new RefundResponse($this, $data);
65+
}
66+
}

src/Message/RefundResponse.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Omnipay\MyPay\Message;
4+
5+
use Omnipay\Common\Message\AbstractResponse;
6+
7+
class RefundResponse extends AbstractResponse
8+
{
9+
/**
10+
* Is the response successful?
11+
*
12+
* @return bool
13+
*/
14+
public function isSuccessful()
15+
{
16+
return $this->getCode() === 'B200';
17+
}
18+
19+
/**
20+
* Response Message.
21+
*
22+
* @return null|string A response message from the payment gateway
23+
*/
24+
public function getMessage()
25+
{
26+
return $this->data['msg'];
27+
}
28+
29+
/**
30+
* Response code.
31+
*
32+
* @return null|string A response code from the payment gateway
33+
*/
34+
public function getCode()
35+
{
36+
return $this->data['code'];
37+
}
38+
39+
/**
40+
* Gateway Reference.
41+
*
42+
* @return null|string A reference provided by the gateway to represent this transaction
43+
*/
44+
public function getTransactionReference()
45+
{
46+
return $this->data['uid'];
47+
}
48+
}

src/Traits/HasCost.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\MyPay\Traits;
4+
5+
trait HasCost
6+
{
7+
/**
8+
* @param string|int $value
9+
* @return $this
10+
*/
11+
public function setCost($value)
12+
{
13+
return $this->setAmount($value);
14+
}
15+
16+
/**
17+
* @return string
18+
*/
19+
public function getCost()
20+
{
21+
return $this->getAmount();
22+
}
23+
}

src/Traits/HasOrderInfo.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,7 @@
44

55
trait HasOrderInfo
66
{
7-
/**
8-
* @param string|int $value
9-
* @return $this
10-
*/
11-
public function setCost($value)
12-
{
13-
return $this->setAmount($value);
14-
}
15-
16-
/**
17-
* @return string
18-
*/
19-
public function getCost()
20-
{
21-
return $this->getAmount();
22-
}
7+
use HasCost;
238

249
/**
2510
* @param string $value

src/Traits/HasOrderResult.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ public function getTransType()
3434
*/
3535
public function setRedeem($value)
3636
{
37-
$data = json_decode($value, true);
38-
if (json_last_error() !== JSON_ERROR_NONE) {
39-
$data = $value;
40-
}
41-
42-
return $this->setParameter('redeem', $data);
37+
return $this->setParameter('redeem', $this->asArray($value));
4338
}
4439

4540
/**
@@ -94,12 +89,7 @@ public function getResultContentType()
9489
*/
9590
public function setResultContent($value)
9691
{
97-
$data = json_decode($value, true);
98-
if (json_last_error() !== JSON_ERROR_NONE) {
99-
$data = $value;
100-
}
101-
102-
return $this->setParameter('result_content', $data);
92+
return $this->setParameter('result_content', $this->asArray($value));
10393
}
10494

10595
/**
@@ -109,4 +99,18 @@ public function getResultContent()
10999
{
110100
return $this->getParameter('result_content');
111101
}
102+
103+
private function asArray($value)
104+
{
105+
if (! is_string($value)) {
106+
return $value;
107+
}
108+
109+
$data = json_decode($value, true);
110+
if (json_last_error() === JSON_ERROR_NONE) {
111+
return $data;
112+
}
113+
114+
return $value;
115+
}
112116
}

tests/GatewayTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,21 @@ public function test_fetch_transaction()
178178
self::assertEquals('d5jUed1tkQ9cDaD1', $response->getTransactionId());
179179
self::assertEquals('86579', $response->getTransactionReference());
180180
}
181+
182+
public function test_refund()
183+
{
184+
$this->setMockHttpResponse('RefundSuccess.txt');
185+
186+
$options = [
187+
'uid' => '86579',
188+
'key' => 'dee886ee19ddbb97e2968a1a8777fc7d',
189+
'cost' => '1000',
190+
];
191+
$response = $this->gateway->refund($options)->send();
192+
193+
self::assertTrue($response->isSuccessful());
194+
self::assertEquals('B200', $response->getCode());
195+
self::assertEquals('執行成功', $response->getMessage());
196+
self::assertEquals('86584', $response->getTransactionReference());
197+
}
181198
}

tests/Message/PurchaseRequestTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function setUp()
3131
'user_id' => 'phper',
3232
'pfn' => 'all',
3333
'items' => [
34-
new Item(['id' => '0886449', 'name' => '商品名稱', 'quantity' => 1, 'price' => 10]),
34+
new Item(['name' => '商品名稱', 'quantity' => 1, 'price' => 10]),
3535
],
3636
'vouchers' => [
3737
new Voucher([
@@ -58,7 +58,7 @@ public function test_get_data()
5858
'ip' => '127.0.0.1',
5959
'pfn' => 'all',
6060
'item' => 1,
61-
'i_0_id' => '',
61+
'i_0_id' => 'cffc597b139c38b7f737ddfe6f8c11a8',
6262
'i_0_name' => '商品名稱',
6363
'i_0_cost' => 10,
6464
'i_0_amount' => 1,

tests/Mock/RefundSuccess.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
HTTP/1.1 200 OK
2+
Server: nginx
3+
Date: Sat, 22 May 2021 12:28:50 GMT
4+
Content-Type: application/json; charset=UTF-8
5+
Connection: keep-alive
6+
Cache-Control: no-cache, private
7+
Status: 200 OK
8+
9+
{
10+
"row_data": {
11+
"uid": 86584,
12+
"refund_uid": 86592,
13+
"key": "4175cdb107caf0543a5ca4868fe66630",
14+
"prc": "230",
15+
"finishtime": "20210524022622",
16+
"order_id": "BIFUR3vXzDnclT4j",
17+
"user_id": "DoSuccess3D",
18+
"cost": 10,
19+
"currency": "TWD",
20+
"actual_cost": 10,
21+
"actual_currency": "TWD",
22+
"retmsg": "退款完成",
23+
"pfn": "CREDITCARD",
24+
"payment_name": "",
25+
"nois": "",
26+
"group_id": "",
27+
"refund_type": 1,
28+
"expected_refund_date": "",
29+
"echo_0": "",
30+
"echo_1": "",
31+
"echo_2": "",
32+
"echo_3": "",
33+
"echo_4": ""
34+
},
35+
"key": "4175cdb107caf0543a5ca4868fe66630",
36+
"uid": "86584",
37+
"code": "B200",
38+
"msg": "執行成功"
39+
}

0 commit comments

Comments
 (0)