Skip to content

Commit d8a5430

Browse files
authored
Merge pull request #21 from Leop0ld/master
비인증 결제예약/예약 취소 기능 및 테스트코드 추가
2 parents 7ab400c + 63d573a commit d8a5430

File tree

6 files changed

+181
-4
lines changed

6 files changed

+181
-4
lines changed

README.rst

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,62 @@ Python 사용자를 위한 아임포트 REST API 연동 모듈입니다.
150150
pass
151151
152152
153+
정기 결제를 예약합니다.
154+
155+
.. code-block:: python
156+
157+
# 테스트용 값
158+
payload = {
159+
'customer_uid': '{고객 아이디}',
160+
'schedules': [
161+
{
162+
'merchant_uid': 'test_merchant_01',
163+
# UNIX timestamp
164+
'schedule_at': 1478150985,
165+
'amount': 1004
166+
},
167+
{
168+
'merhcant_uid': 'test_merchant_02',
169+
# UNIX timestamp
170+
'schedule_at': 1478150985,
171+
'amount': 5000,
172+
'name': '{주문명}',
173+
'buyer_name': '{주문자명}',
174+
'buyer_email': '{주문자 이메일}',
175+
'buyer_tel': '{주문자 전화번호}',
176+
'buyer_addr': '{주문자 주소}',
177+
'buyer_postcode': '{주문자 우편번호}',
178+
},
179+
]
180+
}
181+
try:
182+
reponse = iamport.pay_schedule(**payload)
183+
except KeyError:
184+
# 필수 값이 없을때 에러 처리
185+
pass
186+
except Iamport.ResponseError as e:
187+
# 응답 에러 처리
188+
pass
189+
190+
정기 결제 예약을 취소합니다.
191+
192+
.. code-block:: python
193+
194+
# 테스트용 값 (merchant_uid 가 누락되면 customer_uid 에 대한 결제예약정보 일괄취소)
195+
payload = {
196+
'customer_uid': '{고객 아이디}',
197+
'merchant_uid': 'test_merchant_01',
198+
}
199+
try:
200+
response = iamport.pay_unschedule(**payload)
201+
except KeyError:
202+
# 필수 값이 없을때 에러 처리
203+
pass
204+
except Iamport.ResponseError as e:
205+
# 응답 에러 처리
206+
pass
207+
208+
153209
결제 사전 검증
154210
----------------
155211

iamport/client.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
import json
3+
24
import requests
35

46
__all__ = ['IAMPORT_API_URL', 'Iamport']
@@ -96,6 +98,27 @@ def pay_foreign(self, **kwargs):
9698

9799
return self._post(url, kwargs)
98100

101+
def pay_schedule(self, **kwargs):
102+
headers = self.get_headers()
103+
headers['Content-Type'] = 'application/json'
104+
url = '{}subscribe/payments/schedule'.format(self.imp_url)
105+
if 'customer_uid' not in kwargs:
106+
raise KeyError('customer_uid is required')
107+
for key in ['merchant_uid', 'schedule_at', 'amount']:
108+
for schedules in kwargs['schedules']:
109+
if key not in schedules:
110+
raise KeyError('Essential parameter is missing!: %s' % key)
111+
112+
response = self.requests_session.post(url, headers=headers, data=json.dumps(kwargs))
113+
return self.get_response(response)
114+
115+
def pay_unschedule(self, **kwargs):
116+
url = '{}subscribe/payments/unschedule'.format(self.imp_url)
117+
if 'customer_uid' not in kwargs:
118+
raise KeyError('customer_uid is required')
119+
120+
return self._post(url, kwargs)
121+
99122
def cancel_by_merchant_uid(self, merchant_uid, reason, **kwargs):
100123
payload = {'merchant_uid': merchant_uid, 'reason': reason}
101124
if kwargs:

tests/test_pay_again.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
def test_pay_again(iamport):
55
# Without 'customer_uid'
66
payload_notEnough = {
7-
'merchant_uid': '00000000',
7+
'merchant_uid': '1234qwer',
88
'amount': 5000,
99
}
1010

@@ -15,7 +15,7 @@ def test_pay_again(iamport):
1515

1616
payload_full = {
1717
'customer_uid': '00000000',
18-
'merchant_uid': '00000000',
18+
'merchant_uid': '1234qwer',
1919
'amount': 5000,
2020
}
2121

tests/test_pay_onetime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
def test_pay_onetime(iamport):
55
# Without 'card_number'
66
payload_notEnough = {
7-
'merchant_uid': '00000000',
7+
'merchant_uid': 'qwer1234',
88
'amount': 5000,
99
'expiry': '2019-03',
1010
'birth': '500203',
@@ -17,7 +17,7 @@ def test_pay_onetime(iamport):
1717
assert "Essential parameter is missing!: card_number" in str(e)
1818

1919
payload_full = {
20-
'merchant_uid': '00000000',
20+
'merchant_uid': 'qwer1234',
2121
'amount': 5000,
2222
'card_number': '4092-0230-1234-1234',
2323
'expiry': '2019-03',

tests/test_pay_schedule.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# -*- coding: utf-8 -*-
2+
import time
3+
4+
5+
def test_pay_schedule(iamport):
6+
schedule_at = int(time.time() + 1000)
7+
8+
payload_without_customer_uid = {
9+
# without 'customer_uid'
10+
'schedules': [
11+
{
12+
'merchant_uid': 'pay_schedule_%s' % str(time.time()),
13+
'schedule_at': schedule_at,
14+
'amount': 2001,
15+
'name': '주문명1',
16+
'buyer_name': '주문자명',
17+
'buyer_email': '주문자 Email주소',
18+
'buyer_tel': '주문자 전화번호',
19+
'buyer_addr': '주문자 주소',
20+
'buyer_postcode': '주문자 우편번호'
21+
},
22+
],
23+
}
24+
25+
try:
26+
iamport.pay_schedule(**payload_without_customer_uid)
27+
except KeyError as e:
28+
assert 'customer_uid is required' in str(e)
29+
30+
payload_without_merchant_uid = {
31+
'customer_uid': '00000000',
32+
'schedules': [
33+
{
34+
# without 'merchant_uid'
35+
'schedule_at': schedule_at,
36+
'amount': 10000,
37+
'name': '주문명2',
38+
'buyer_name': '주문자명',
39+
'buyer_email': '주문자 Email주소',
40+
'buyer_tel': '주문자 전화번호',
41+
'buyer_addr': '주문자 주소',
42+
'buyer_postcode': '주문자 우편번호'
43+
},
44+
],
45+
}
46+
47+
try:
48+
iamport.pay_schedule(**payload_without_merchant_uid)
49+
except KeyError as e:
50+
assert 'Essential parameter is missing!: merchant_uid' in str(e)
51+
52+
payload_full = {
53+
'customer_uid': '00000000',
54+
'schedules': [
55+
{
56+
'merchant_uid': 'pay_schedule_%s' % str(time.time()),
57+
'schedule_at': schedule_at,
58+
'amount': 5000,
59+
'name': '주문명',
60+
'buyer_name': '주문자명',
61+
'buyer_email': '주문자 Email주소',
62+
'buyer_tel': '주문자 전화번호',
63+
'buyer_addr': '주문자 주소',
64+
'buyer_postcode': '주문자 우편번호'
65+
},
66+
],
67+
}
68+
69+
try:
70+
iamport.pay_schedule(**payload_full)
71+
except iamport.ResponseError as e:
72+
assert e.code == 1
73+
assert u'등록되지 않은 구매자입니다.' in e.message

tests/test_pay_unschedule.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
import time
3+
4+
5+
def test_pay_unschedule(iamport):
6+
payload_without_customer_uid = {
7+
# without 'customer_uid'
8+
'merchant_uid': 'pay_unschedule_%s' % str(time.time()),
9+
}
10+
11+
try:
12+
iamport.pay_unschedule(**payload_without_customer_uid)
13+
except KeyError as e:
14+
assert 'customer_uid is required' in str(e)
15+
16+
payload_full = {
17+
'customer_uid': '00000000',
18+
'merchant_uid': 'pay_unschedule_%s' % str(time.time()),
19+
}
20+
21+
try:
22+
iamport.pay_unschedule(**payload_full)
23+
except iamport.ResponseError as e:
24+
assert e.code == 1
25+
assert u'취소할 예약결제 기록이 존재하지 않습니다.' in e.message

0 commit comments

Comments
 (0)