-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathiamport.go
601 lines (502 loc) · 14.6 KB
/
iamport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package iamport
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
// Client 아임포트 서버와 통신하는 클라이언트
type Client struct {
APIKey string
APISecret string
AccessToken accessToken
HTTP *http.Client
}
type accessToken struct {
Token string
Expired time.Time
}
// NewClient 아임포트로 부터 부여받은 API Key와 Api Secret을 사용하여 클라이언트를 새로 만든다.
func NewClient(apiKey string, apiSecret string, cli *http.Client) *Client {
if cli == nil {
cli = &http.Client{}
}
return &Client{
APIKey: apiKey,
APISecret: apiSecret,
HTTP: cli,
}
}
// GetToken APIKey와 APISecret을 사용하여 AccessToken을 받아 온다.
func (cli *Client) GetToken() error {
if cli.APIKey == "" {
return errors.New("iamport: APIKey is missing")
}
if cli.APISecret == "" {
return errors.New("iamport: APISecret is missing")
}
form := url.Values{}
form.Set("imp_key", cli.APIKey)
form.Set("imp_secret", cli.APISecret)
res, err := cli.HTTP.PostForm("https://api.iamport.kr/users/getToken", form)
if err != nil {
return err
}
if res.StatusCode == http.StatusUnauthorized {
return errors.New("iamport: unauthorized")
}
if res.StatusCode != http.StatusOK {
return errors.New("iamport: unknown error")
}
data := struct {
Code int `json:"code"`
Message string `json:"message"`
Response struct {
AccessToken string `json:"access_token"`
ExpiredAt int64 `json:"expired_at"`
Now int64 `json:"now"`
} `json:"response"`
}{}
err = json.NewDecoder(res.Body).Decode(&data)
if err != nil {
return err
}
if data.Code != 0 {
return fmt.Errorf("iamport: %s", data.Message)
}
cli.AccessToken.Token = data.Response.AccessToken
cli.AccessToken.Expired = time.Unix(data.Response.ExpiredAt, 0)
return nil
}
// Payment 결제 정보
type Payment struct {
ImpUID string `json:"imp_uid"`
MerchantUID string `json:"merchant_uid"`
PayMethod string `json:"pay_method"`
PGProvider string `json:"pg_provider"`
PGTID string `json:"pg_tid"`
ApplyNum string `json:"apply_num"`
CardName string `json:"card_name"`
CardQuota int `json:"card_quota"`
VBankName string `json:"vbank_name"`
VBankNum string `json:"vbank_num"`
VBankHolder string `json:"vbank_holder"`
Name string `json:"name"`
Amount int64 `json:"amount"`
CancelAmount int64 `json:"cancel_amount"`
BuyerName string `json:"buyer_name"`
BuyerEmail string `json:"buyer_email"`
BuyerTel string `json:"buyer_tel"`
BuyerAddr string `json:"buyer_addr"`
BuyerPostCode string `json:"buyer_postcode"`
CustomData string `json:"custom_data"`
UserAgent string `json:"user_agent"`
Status string `json:"status"`
PaidAt int64 `json:"paid_at"`
FailedAt int64 `json:"failed_at"`
CanceledAt int64 `json:"canceled_at"`
FailReason string `json:"fail_reason"`
CancelReason string `json:"cancel_reason"`
ReceiptURL string `json:"receipt_url"`
}
func (cli *Client) authorization() (string, error) {
now := time.Now()
switch {
case cli.AccessToken.Token == "",
cli.AccessToken.Expired.IsZero(),
!cli.AccessToken.Expired.Before(now):
err := cli.GetToken()
if err != nil {
return "", err
}
}
return cli.AccessToken.Token, nil
}
// GetPaymentImpUID imp_uid로 결제 정보 가져오기
//
// GET /payments/{imp_uid}
func (cli *Client) GetPaymentImpUID(iuid string) (Payment, error) {
data := struct {
Code int `json:"code"`
Message string `json:"message"`
Response Payment `json:"response"`
}{}
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.iamport.kr/payments/%s", iuid), nil)
if err != nil {
return data.Response, err
}
auth, err := cli.authorization()
if err != nil {
return data.Response, err
}
req.Header.Set("Authorization", auth)
res, err := cli.HTTP.Do(req)
if err != nil {
return data.Response, err
}
if res.StatusCode == http.StatusUnauthorized {
return data.Response, errors.New("iamport: unauthorized")
}
if res.StatusCode == http.StatusNotFound {
return data.Response, errors.New("iamport: invalid imp_uid")
}
if res.StatusCode != http.StatusOK {
return data.Response, errors.New("iamport: unknown error")
}
err = json.NewDecoder(res.Body).Decode(&data)
if err != nil {
return data.Response, err
}
if data.Code != 0 {
return data.Response, fmt.Errorf("iamport: %s", data.Message)
}
return data.Response, nil
}
// GetPaymentMerchantUID merchant_uid로 결제 정보 가져오기
//
// GET /payments/find/{merchant_uid}
func (cli *Client) GetPaymentMerchantUID(muid string) (Payment, error) {
data := struct {
Code int `json:"code"`
Message string `json:"message"`
Response Payment `json:"response"`
}{}
req, err := http.NewRequest("GET",
fmt.Sprintf("https://api.iamport.kr/payments/find/%s", muid), nil)
if err != nil {
return data.Response, err
}
auth, err := cli.authorization()
if err != nil {
return data.Response, err
}
req.Header.Set("Authorization", auth)
res, err := cli.HTTP.Do(req)
if err != nil {
return data.Response, err
}
if res.StatusCode == http.StatusUnauthorized {
return data.Response, errors.New("iamport: unauthorized")
}
if res.StatusCode == http.StatusNotFound {
return data.Response, errors.New("iamport: invalid merchant_uid")
}
if res.StatusCode != http.StatusOK {
return data.Response, errors.New("iamport: unknown error")
}
err = json.NewDecoder(res.Body).Decode(&data)
if err != nil {
return data.Response, err
}
if data.Code != 0 {
return data.Response, fmt.Errorf("iamport: %s", data.Message)
}
return data.Response, nil
}
// Status 결제 상태
type Status string
const (
// StatusAll 전체
StatusAll = "all"
// StatusReady 미결제
StatusReady = "ready"
// StatusPaid 결제 완료
StatusPaid = "paid"
// StatusCanceled 결제 취소
StatusCanceled = "canceled"
// StatusFailed 결제 실패
StatusFailed = "failed"
)
// PagedPayments 다수의 결제 정보
type PagedPayments struct {
Total int `json:"total"`
Previous int `json:"previous"`
Next int `json:"next"`
Payments []Payment `json:"list"`
}
// GetPaymentsStatus 결제 상태에 따른 결제 정보들 가져오기
//
// GET /payments/status/{payment_status}
func (cli *Client) GetPaymentsStatus(status Status, page int) (PagedPayments, error) {
data := struct {
Code int `json:"code"`
Message string `json:"message"`
Response PagedPayments `json:"response"`
}{}
url := fmt.Sprintf("https://api.iamport.kr/payments/status/%s", status)
if page > 0 {
url += fmt.Sprintf("?page=%d", page)
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return data.Response, err
}
auth, err := cli.authorization()
if err != nil {
return data.Response, err
}
req.Header.Set("Authorization", auth)
res, err := cli.HTTP.Do(req)
if err != nil {
return data.Response, err
}
if res.StatusCode == http.StatusUnauthorized {
return data.Response, errors.New("iamport: unauthorized")
}
if res.StatusCode == http.StatusNotFound {
return data.Response, errors.New("iamport: invalid status or page")
}
if res.StatusCode != http.StatusOK {
return data.Response, errors.New("iamport: unknown error")
}
err = json.NewDecoder(res.Body).Decode(&data)
if err != nil {
return data.Response, err
}
if data.Code != 0 {
return data.Response, fmt.Errorf("iamport: %s", data.Message)
}
return data.Response, nil
}
// Bank 은행코드
type Bank string
const (
// Bank기업은행 기업은행
Bank기업은행 = Bank("03")
// Bank국민은행 국민은행
Bank국민은행 = Bank("04")
// Bank외환은행 외환은행
Bank외환은행 = Bank("05")
// Bank수협중앙회 수협중앙회
Bank수협중앙회 = Bank("07")
// Bank농협중앙회 농협중앙회
Bank농협중앙회 = Bank("11")
// Bank우리은행 우리은행
Bank우리은행 = Bank("20")
// BankSC제일은행 SC제일은행
BankSC제일은행 = Bank("23")
// Bank대구은행 대구은행
Bank대구은행 = Bank("31")
// Bank부산은행 부산은행
Bank부산은행 = Bank("32")
// Bank광주은행 광주은행
Bank광주은행 = Bank("34")
// Bank전북은행 전북은행
Bank전북은행 = Bank("37")
// Bank경남은행 경남은행
Bank경남은행 = Bank("39")
// Bank한국씨티은행 한국씨티은행
Bank한국씨티은행 = Bank("53")
// Bank우체국 우체국
Bank우체국 = Bank("71")
// Bank하나은행 하나은행
Bank하나은행 = Bank("81")
// Bank통합신한은행 통합신한은행
Bank통합신한은행 = Bank("88")
// Bank동양종합금융증권 동양종합금융증권
Bank동양종합금융증권 = Bank("D1")
// Bank현대증권 현대증권
Bank현대증권 = Bank("D2")
// Bank미래에셋증권 미래에셋증권
Bank미래에셋증권 = Bank("D3")
// Bank한국투자증권 한국투자증권
Bank한국투자증권 = Bank("D4")
// Bank우리투자증권 우리투자증권
Bank우리투자증권 = Bank("D5")
// Bank하이투자증권 하이투자증권
Bank하이투자증권 = Bank("D6")
// BankHMC투자증권 HMC투자증권
BankHMC투자증권 = Bank("D7")
// BankSK증권 SK증권
BankSK증권 = Bank("D8")
// Bank대신증권 대신증권
Bank대신증권 = Bank("D9")
// Bank하나대투증권 하나대투증권
Bank하나대투증권 = Bank("DA")
// Bank굿모닝신한증권 굿모닝신한증권
Bank굿모닝신한증권 = Bank("DB")
// Bank동부증권 동부증권
Bank동부증권 = Bank("DC")
// Bank유진투자증권 유진투자증권
Bank유진투자증권 = Bank("DE")
// Bank신영증권 신영증권
Bank신영증권 = Bank("DF")
)
// CancelOptions 결제 취소 옵션
type CancelOptions struct {
Amount string
Reason string
RefundHolder string
RefundBank Bank
RefundAccount string
}
func (ops *CancelOptions) form() url.Values {
vals := url.Values{}
if ops.Amount != "" {
vals.Set("amount", ops.Amount)
}
if ops.Reason != "" {
vals.Set("reason", ops.Reason)
}
if ops.RefundHolder != "" {
vals.Set("refund_holder", ops.RefundHolder)
}
if ops.RefundBank != "" {
vals.Set("refund_bank", string(ops.RefundBank))
}
if ops.RefundAccount != "" {
vals.Set("refund_account", ops.RefundAccount)
}
return vals
}
// CancelPaymentImpUID imp_uid로 결제 취소하기
//
// GET /payments/cancel
func (cli *Client) CancelPaymentImpUID(iuid string, options *CancelOptions) (Payment, error) {
return cli.cancelPayment("imp_uid", iuid, options)
}
// CancelPaymentMerchantUID merchant_uid로 결제 취소하기
//
// GET /payments/cancel
func (cli *Client) CancelPaymentMerchantUID(muid string, options *CancelOptions) (Payment, error) {
return cli.cancelPayment("merchant_uid", muid, options)
}
func (cli *Client) cancelPayment(key string, uid string, options *CancelOptions) (Payment, error) {
data := struct {
Code int `json:"code"`
Message string `json:"message"`
Response Payment `json:"response"`
}{}
var form url.Values
if options != nil {
form = options.form()
} else {
form = url.Values{}
}
form.Set(key, uid)
req, err := http.NewRequest("POST",
"https://api.iamport.kr/payments/cancel",
bytes.NewBufferString(form.Encode()))
if err != nil {
return data.Response, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
auth, err := cli.authorization()
if err != nil {
return data.Response, err
}
req.Header.Set("Authorization", auth)
res, err := cli.HTTP.Do(req)
if err != nil {
return data.Response, err
}
if res.StatusCode == http.StatusUnauthorized {
return data.Response, errors.New("iamport: unauthorized")
}
if res.StatusCode != http.StatusOK {
return data.Response, errors.New("iamport: unknown error")
}
err = json.NewDecoder(res.Body).Decode(&data)
if err != nil {
return data.Response, err
}
if data.Code != 0 {
return data.Response, fmt.Errorf("iamport: %s", data.Message)
}
return data.Response, nil
}
// Prepared 사전 등록된 결제 정보
type Prepared struct {
MerchantUID string `json:"merchant_uid"`
Amount int64 `json:"amount"`
}
// PreparePayment 결제 정보 사전 등록하기
//
// POST /payments/prepare
func (cli *Client) PreparePayment(muid string, amount int64) (Prepared, error) {
data := struct {
Code int `json:"code"`
Message string `json:"message"`
Response Prepared `json:"response"`
}{}
form := url.Values{}
form.Set("merchant_uid", muid)
form.Set("amount", strconv.FormatInt(amount, 10))
req, err := http.NewRequest("POST",
"https://api.iamport.kr/payments/prepare",
bytes.NewBufferString(form.Encode()))
if err != nil {
return data.Response, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
auth, err := cli.authorization()
if err != nil {
return data.Response, err
}
req.Header.Set("Authorization", auth)
res, err := cli.HTTP.Do(req)
if err != nil {
return data.Response, err
}
if res.StatusCode == http.StatusUnauthorized {
return data.Response, errors.New("iamport: unauthorized")
}
if res.StatusCode != http.StatusOK {
return data.Response, errors.New("iamport: unknown error")
}
err = json.NewDecoder(res.Body).Decode(&data)
if err != nil {
return data.Response, err
}
if data.Code != 0 {
return data.Response, fmt.Errorf("iamport: %s", data.Message)
}
return data.Response, nil
}
// GetPreparedPayment 사전 등록된 결제 정보 보기
//
// GET /payments/prepare/{merchant_uid}
func (cli *Client) GetPreparedPayment(muid string) (Prepared, error) {
data := struct {
Code int `json:"code"`
Message string `json:"message"`
Response Prepared `json:"response"`
}{}
req, err := http.NewRequest("GET",
fmt.Sprintf("https://api.iamport.kr/payments/prepare/%s", muid), nil)
if err != nil {
return data.Response, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
auth, err := cli.authorization()
if err != nil {
return data.Response, err
}
req.Header.Set("Authorization", auth)
res, err := cli.HTTP.Do(req)
if err != nil {
return data.Response, err
}
if res.StatusCode == http.StatusUnauthorized {
return data.Response, errors.New("iamport: unauthorized")
}
if res.StatusCode == http.StatusNotFound {
return data.Response, errors.New("iamport: invalid merchant_uid")
}
if res.StatusCode != http.StatusOK {
fmt.Println(res.StatusCode)
return data.Response, errors.New("iamport: unknown error")
}
err = json.NewDecoder(res.Body).Decode(&data)
if err != nil {
return data.Response, err
}
if data.Code != 0 {
return data.Response, fmt.Errorf("iamport: %s", data.Message)
}
return data.Response, nil
}