-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
105 lines (97 loc) · 2.63 KB
/
api.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
package TossPaymentsApi
import (
"encoding/base64"
"errors"
"github.com/imroc/req/v3"
)
// NewClient
// Create a new TossPaymentsApi client
func NewClient(apiKey string) *Client {
client := &Client{}
client.httpClient = req.C().
SetBaseURL("https://api.tosspayments.com").
SetCommonHeader("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(apiKey+":"))).
SetCommonHeader("Content-Type", "application/json")
return client
}
type Client struct {
httpClient *req.Client
}
func (c *Client) ApprovePayment(paymentKey string, option *ApprovePaymentOption) (*Payment, error) {
payment := &Payment{}
errorMessage := &Error{}
resp, err := c.httpClient.R().
SetBody(*option).
SetResult(payment).
SetError(errorMessage).
Post("/v1/payments/" + paymentKey)
if err != nil {
return nil, err
}
if resp.IsSuccess() != true {
return nil, errors.New(errorMessage.Code + ":" + errorMessage.Message)
}
return payment, nil
}
func (c *Client) InquiryPaymentByPaymentKey(paymentKey string) (*Payment, error) {
payment := &Payment{}
errorMessage := &Error{}
resp, err := c.httpClient.R().
SetResult(payment).
SetError(errorMessage).
Get("/v1/payments/" + paymentKey)
if err != nil {
return nil, err
}
if resp.IsSuccess() != true {
return nil, errors.New(errorMessage.Code + ":" + errorMessage.Message)
}
return payment, nil
}
func (c *Client) InquiryPaymentByOrderId(orderId string) (*Payment, error) {
payment := &Payment{}
errorMessage := &Error{}
resp, err := c.httpClient.R().
SetResult(payment).
SetError(errorMessage).
Get("/v1/payments/orders/" + orderId)
if err != nil {
return nil, err
}
if resp.IsSuccess() != true {
return nil, errors.New(errorMessage.Code + ":" + errorMessage.Message)
}
return payment, nil
}
func (c *Client) CancelPayment(paymentKey string, option *CancelPaymentOption) (*Payment, error) {
payment := &Payment{}
errorMessage := &Error{}
resp, err := c.httpClient.R().
SetBody(*option).
SetResult(payment).
SetError(errorMessage).
Post("/v1/payments/" + paymentKey + "/cancel")
if err != nil {
return nil, err
}
if resp.IsSuccess() != true {
return nil, errors.New(errorMessage.Code + ":" + errorMessage.Message)
}
return payment, nil
}
func (c *Client) KeyinPaymentRequest(option *KeyinPaymentOption) (*Payment, error) {
payment := &Payment{}
errorMessage := &Error{}
resp, err := c.httpClient.R().
SetBody(*option).
SetResult(payment).
SetError(errorMessage).
Post("/v1/payments/key-in")
if err != nil {
return nil, err
}
if resp.IsSuccess() != true {
return nil, errors.New(errorMessage.Code + ":" + errorMessage.Message)
}
return payment, nil
}