Skip to content

Commit f8854ce

Browse files
authored
Merge pull request #11 from iamport/fix/add_parameter_support_cancel_payment
Fix/add parameter support cancel payment
2 parents b12a53a + f79dd19 commit f8854ce

File tree

5 files changed

+117
-2
lines changed

5 files changed

+117
-2
lines changed

example/payment/cancel.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
require_once '../../vendor/autoload.php';
44

5+
use Iamport\RestClient\Enum\Naver\CancelPaymentRequester;
56
use Iamport\RestClient\Iamport;
67
use Iamport\RestClient\Request\CancelPayment;
8+
use Iamport\RestClient\Request\CancelPaymentExtra;
79

810
$iamport = new Iamport('imp_apikey', 'ekKoeW8RyKuT0zgaZsUtXXTLQ4AhPFW3ZGseDA6bkA5lamv9OqDMnxyeB9wqOsuO9W3Mx9YSJ4dTqJ3f');
911

1012
// 클라이언트로부터 전달받은 주문번호, 환불사유, 환불금액
1113
$merchant_uid = filter_input(INPUT_POST, 'merchant_uid', FILTER_SANITIZE_STRING);
1214
$reason = filter_input(INPUT_POST, 'reason', FILTER_SANITIZE_STRING);
1315
$cancelRequestAmount = filter_input(INPUT_POST, 'cancel_request_amount', FILTER_SANITIZE_STRING);
16+
$extra_requester = filter_input(INPUT_POST, 'extra_requester', FILTER_SANITIZE_STRING);
1417

1518
// TODO : 아래 가맹점 DB 접근 코드는 예시를 돕고자 작성된 샘플코드로 실제 가맹점의 환경에 맞게 직접 작성하셔야 됩니다.
1619
// 가맹점 DB에서 환불할 결제 정보 조회
@@ -39,6 +42,16 @@
3942
$request->refund_holder = '환불될 가상계좌 예금주';
4043
$request->refund_bank = '환불될 가상계좌 은행코드';
4144
$request->refund_account = '환불될 가상계좌 번호';
45+
46+
$extra = new CancelPaymentExtra();
47+
// 취소 요청자, API를 호출하는 출처 (optional)
48+
if ($extra_requester === 'admin') {
49+
$extra->requester = CancelPaymentRequester::ADMIN;
50+
} else if ($extra_requester === 'customer'){
51+
$extra->requester = CancelPaymentRequester::CUSTOMER;
52+
}
53+
54+
$request->extra = $extra;
4255
$result = $iamport->callApi($request);
4356

4457
if ($result->isSuccess()) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Iamport\RestClient\Enum\Naver;
4+
5+
use Iamport\RestClient\Enum\Enum;
6+
7+
/**
8+
* Class CancelPaymentRequester.
9+
*/
10+
class CancelPaymentRequester extends Enum
11+
{
12+
public const CUSTOMER = 'customer';
13+
public const ADMIN = 'admin';
14+
15+
16+
/**
17+
* Enum의 설명을 가져옵니다.
18+
*
19+
* @param string $value
20+
*/
21+
public static function getDescription($value): ?string
22+
{
23+
switch ($value) {
24+
case self::CUSTOMER:
25+
return '구매자';
26+
case self::ADMIN:
27+
return '가맹점 관리자';
28+
default:
29+
return null;
30+
}
31+
}
32+
}

src/Request/CancelPayment.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* @property string $refund_holder
1818
* @property string $refund_bank
1919
* @property string $refund_account
20+
* @property string $refund_tel
21+
* @property CancelPaymentExtra $extra
2022
*/
2123
class CancelPayment extends RequestBase
2224
{
@@ -67,6 +69,16 @@ class CancelPayment extends RequestBase
6769
*/
6870
protected $refund_account;
6971

72+
/**
73+
* @var string 환불계좌 예금주 연락처(가상계좌취소,스마트로 PG사 인경우 필수 )
74+
*/
75+
protected $refund_tel;
76+
77+
/**
78+
* @var CancelPaymentExtra
79+
*/
80+
protected $extra;
81+
7082
/**
7183
* CancelPayment constructor.
7284
*/
@@ -146,6 +158,16 @@ public function setRefundAccount(string $refund_account): void
146158
$this->refund_account = $refund_account;
147159
}
148160

161+
public function setRefundTel(string $refund_tel): void
162+
{
163+
$this->refund_tel = $refund_tel;
164+
}
165+
166+
public function setExtra(CancelPaymentExtra $extra): void
167+
{
168+
$this->extra = $extra;
169+
}
170+
149171
/**
150172
* 주문취소.
151173
* [POST] /payments/cancel.
@@ -157,13 +179,19 @@ public function path(): string
157179

158180
public function attributes(): array
159181
{
182+
$result = $this->toArray();
183+
184+
if ($this->extra instanceof CancelPaymentExtra) {
185+
$result['extra'] = $this->extra->toArray();
186+
}
187+
160188
return [
161-
'body' => json_encode($this->toArray()),
189+
'body' => json_encode($result),
162190
];
163191
}
164192

165193
public function verb(): string
166194
{
167195
return 'POST';
168196
}
169-
}
197+
}

src/Request/CancelPaymentExtra.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Iamport\RestClient\Request;
4+
5+
use Iamport\RestClient\Request\RequestTrait;
6+
7+
/**
8+
* Class CancelPaymentExtra
9+
*
10+
* @property string $requester
11+
*/
12+
class CancelPaymentExtra
13+
{
14+
use RequestTrait;
15+
16+
/**
17+
* @var string API를 호출하는 출처 [ customer, admin ]
18+
*/
19+
protected $requester;
20+
21+
/**
22+
* CustomerExtra constructor.
23+
*/
24+
public function __construct()
25+
{
26+
}
27+
28+
public function setRequester(string $requester): void
29+
{
30+
$this->requester = $requester;
31+
}
32+
}

tests/PaymentTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3+
use Iamport\RestClient\Enum\Naver\CancelPaymentRequester;
34
use Iamport\RestClient\Request\CancelPayment;
5+
use Iamport\RestClient\Request\CancelPaymentExtra;
46
use Iamport\RestClient\Request\Payment;
57
use Iamport\RestClient\Test\IamportTestTrait;
68
use PHPUnit\Framework\TestCase;
@@ -81,6 +83,10 @@ public function payment_cancel_by_imp_uid()
8183
$cancelPayment->refund_holder = '환불될 가상계좌 예금주';
8284
$cancelPayment->refund_bank = '환불될 가상계좌 은행코드';
8385
$cancelPayment->refund_account = '환불될 가상계좌 번호';
86+
$cancelPayment->refund_tel = "01012341234";
87+
$extra = new CancelPaymentExtra();
88+
$extra->requester = CancelPaymentRequester::ADMIN;
89+
$cancelPayment->setExtra($extra);
8490

8591
$this->assertSame($cancelPayment->imp_uid, self::$impUid);
8692
$this->assertEquals('/payments/cancel/', $cancelPayment->path());
@@ -103,6 +109,10 @@ public function payment_cancel_by_merchant_uid()
103109
$cancelPayment->refund_holder = '환불될 가상계좌 예금주';
104110
$cancelPayment->refund_bank = '환불될 가상계좌 은행코드';
105111
$cancelPayment->refund_account = '환불될 가상계좌 번호';
112+
$cancelPayment->refund_tel = "01012341234";
113+
$extra = new CancelPaymentExtra();
114+
$extra->requester = CancelPaymentRequester::ADMIN;
115+
$cancelPayment->setExtra($extra);
106116

107117
$this->assertSame($cancelPayment->merchant_uid, self::$merchantUid);
108118
$this->assertEquals('/payments/cancel/', $cancelPayment->path());

0 commit comments

Comments
 (0)