This repository has been archived by the owner on Jun 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
270 lines (245 loc) · 6.31 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
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
package komoju
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
)
// ApiResponseError maps to a response error body
type ApiResponseError struct {
// Data
Data ApiResponseErrorMessage `json:"error"`
}
// ApiResponseErrorMessage
type ApiResponseErrorMessage struct {
Message string `json:"message"`
Code string `json:"code"`
Param string `json:"param"`
}
// NewApiResponseError creates a new response error
func NewApiResponseError(code, param, message string) *ApiResponseError {
return &ApiResponseError{
Data: ApiResponseErrorMessage{
Message: message,
Code: code,
Param: param,
},
}
}
// Error returns the error in a simplfied manner
func (err ApiResponseError) Error() string {
return fmt.Sprintf("Code: %s, Param: %s, Message: %s", err.Data.Code, err.Data.Param, err.Data.Message)
}
// ApiResponseList provides properties across all
// api response body lists
type ApiResponseList struct {
// Resource
Resource string `json:"list"`
// Total
Total int64 `json:"total"`
// Page
Page int64 `json:"page"`
// PerPage
PerPage int64 `json:"per_page"`
// lastPage
LastPage int64 `json:"last_page"`
}
// Events
// ApiResponseEvents maps to a response to list events
type ApiResponseEvents struct {
ApiResponseList
Data []ApiResponseEvent `json:"data"`
}
// ApiResponseEvent maps to a response for a single event
type ApiResponseEvent struct {
// Id
Id string `json:"id"`
// Type
Type string `json:"type"`
// Resource
Resource string `json:"resource"`
// Data
Data ApiResponsePaymentDetail `json:"data"`
// CreatedAt
CreatedAt string `json:"created_at"`
}
// Payment
// ApiResponsePayments maps to a response for a list of payments
type ApiResponsePayments struct {
ApiResponseList
// Data
Data []ApiResponsePayment `json:"data"`
}
// ApiResponsePayment maps to a response for a single payment
type ApiResponsePayment struct {
// Id
Id string `json:"id"`
// Resource
Resource string `json:"resource"`
// Status
Status string `json:"status"`
// Amount
Amount float64 `json:"amount"`
// Tax
Tax float64 `json:"tax"`
// Customer
Customer ApiResponseCustomer `json:"customer"`
// PaymentDeadline
PaymentDeadline string `json:"payment_deadline"`
// PaymentDetails
PaymentDetails ApiResponsePaymentDetail `json:"payment_details"`
// PaymentMethodFee
PaymentMethodFee float64 `json:"payment_method_fee"`
// Total
Total float64 `json:"total"`
// Currency
Currency string `json:"currency"`
// Description
Description string `json:"description"`
// CapturedAt
CapturedAt string `json:"captured_at"`
// ExternalOrderNumber
ExternalOrderNumber string `json:"external_order_num"`
// Metadata
MetaData map[string]string `json:"metadata"`
// CreatedAt
CreatedAt string `json:"created_at"`
// AmountRefunded
AmountRefunded float64 `json:"amount_refunded"`
// Locale
Locale string `json:"locale"`
// Refunds
Refunds []ApiResponseRefund `json:"refunds"`
// RefundRequests
RefundRequests []string `json:"refund_requests"`
}
// ApiResponsePaymentDetail maps to a response for a Payment
type ApiResponsePaymentDetail struct {
// Type
Type PaymentMethod `json:"type"`
// Email
Email string `json:"email"`
// GivenName
GivenName string `json:"given_name"`
// FamilyName
FamilyName string `json:"family_name"`
// GivenNamek\Kana
GivenNameKana string `json:"given_name_kana"`
// FamilyNameKana
FamilyNameKana string `json:"family_name_kana"`
// Phone
Phone string `json:"phone"`
// Credit card
// Brand
Brand string `json:"brand"`
// last4Digits
Last4Digits string `json:"last_four_digits"`
// Month
Month int64 `json:"month"`
// Year
Year int64 `json:"year"`
// Konbini
// Store
Store string `json:"store"`
// ConfirmationCode
ConfirmationCode string `json:"confirmation_code"`
// Receipt
Receipt string `json:"receipt"`
// InstructionsUrl
InstructionsUrl string `json:"instructions_url"`
// Bank transfer
// orderId
OrderId string `json:"order_id"`
// BankName
BankName string `json:"bank_name"`
// AccountBranchName
AccountBranchName string `json:"account_branch_name"`
// AccountNumber
AccountNumber string `json:"account_number"`
// AccountType
AccountType string `json:"account_type"`
// AccountName
AccountName string `json:"account_name"`
// PayEasy
// BankId
BankId string `json:"bank_id"`
// CustomerId
CustomerId string `json:"customer_id"`
// ConfirmationId
ConfirmationId string `json:"confirmation_id"`
// WebMoney/Bitcash/Nanaco/Net Cash
// ShortAmount
ShortAmount float64 `json:"short_amount"`
// PrepaidCards
PrepaidCards []ApiResponsePaymentPrepaidCard `json:"prepaid_cards"`
}
// ApiResponsePaymentPrepaidCard maps to prepaid card
// properties on payment details
type ApiResponsePaymentPrepaidCard struct {
// Last4Digits
Last4Digits string `json:"last_four_digits"`
// Points
Points int64 `json:"points"`
}
// ApiResponseRefund maps to refund property on payment details
type ApiResponseRefund struct {
// id
Id string `json:"id"`
// Resource
Resource string `json:"resource"`
// Amount
Amount float64 `json:"amount"`
// Currency
Currency string `json:"currency"`
// Payment
Payment string `json:"payment"`
// Description
Description string `json:"description"`
// CreatedAt
CreatedAt string `json:"created_at"`
// Chargeback
Chargeback string `json:"chargeback"`
}
// Customers
// ApiResponseCustomers maps to response body of customer list
type ApiResponseCustomers struct {
ApiResponseList
// Data
Data []ApiResponseCustomer `json:"data"`
}
// ApiResponseCustomer maps to response body of single customer
type ApiResponseCustomer struct {
// id
Id string `json:"id"`
// Resource
Resource string `json:"resource"`
// Email
Email string `json:"email"`
// Source
Source string `json:"source"`
// Metadata
Metadata map[string]string `json:"metadata"`
// CreatedAt
CreatedAt string `json:"created_at"`
}
// ParseApiResponse
func ParseApiResponse(payload io.ReadCloser, resp interface{}) (err error) {
if err := unmarshallApiResponse(payload, resp); err != nil {
return err
}
return nil
}
func unmarshallApiResponse(payload io.ReadCloser, target interface{}) error {
body, err := ioutil.ReadAll(payload)
if err != nil {
return err
}
if err = json.Unmarshal(body, target); err != nil {
apiResponseErr := ApiResponseError{}
if err = json.Unmarshal(body, &apiResponseErr); err != nil {
return err
}
return apiResponseErr
}
return err
}