forked from getlago/lago-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit_note.go
355 lines (286 loc) · 11.3 KB
/
credit_note.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
package lago
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/google/uuid"
)
type CreditNoteCreditStatus string
type CreditNoteRefundStatus string
type CreditNoteReason string
const (
CreditNoteCreditStatusAvailable CreditNoteCreditStatus = "available"
CreditNoteCreditStatusConsumed CreditNoteCreditStatus = "consumed"
)
const (
CreditNoteRefundStatusPending CreditNoteRefundStatus = "pending"
CreditNoteRefundStatusRefunded CreditNoteRefundStatus = "refunded"
)
const (
CreditNoteReasonDuplicatedCharge CreditNoteReason = "duplicated_charge"
CreditNoteReasonProductUnsatisfactory CreditNoteReason = "product_unsatisfactory"
CreditNoteReasonOrderChange CreditNoteReason = "order_change"
CreditNoteReasonOrderCancellation CreditNoteReason = "order_cancellation"
CreditNoteReasonFraudulentCharge CreditNoteReason = "fraudulent_charge"
CreditNoteReasonOther CreditNoteReason = "other"
)
type CreditNoteRequest struct {
client *Client
}
type CreditNoteParams struct {
CreditNote *CreditNoteInput `json:"credit_note"`
}
type CreditNoteResult struct {
CreditNote *CreditNote `json:"credit_note,omitempty"`
CreditNotes []CreditNote `json:"credit_notes,omitempty"`
Meta Metadata `json:"meta,omitempty"`
}
type CreditNoteEstimatedResult struct {
CreditNoteEstimated *CreditNoteEstimated `json:"credit_note_estimated"`
}
type CreditListInput struct {
PerPage int `json:"per_page,omitempty,string"`
Page int `json:"page,omitempty,string"`
ExternalCustomerID string `json:"external_customer_id,omitempty"`
}
type CreditNoteItem struct {
LagoID uuid.UUID `json:"lago_id,omitempty"`
AmountCents int `json:"amount_cents,omitempty"`
AmountCurrency Currency `json:"amount_currency,omitempty"`
Fee Fee `json:"fee,omitempty"`
}
type CreditNoteAppliedTax struct {
LagoId uuid.UUID `json:"lago_id,omitempty"`
LagoCreditNoteId uuid.UUID `json:"lago_credit_note_id,omitempty"`
LagoTaxId uuid.UUID `json:"lago_tax_id,omitempty"`
TaxName string `json:"tax_name,omitempty"`
TaxCode string `json:"tax_code,omitempty"`
TaxRate float32 `json:"tax_rate,omitempty"`
TaxDescription string `json:"tax_description,omitempty"`
AmountCents int `json:"amount_cents,omitempty"`
AmountCurrency Currency `json:"amount_currency,omitempty"`
BaseAmountCents int `json:"base_amount_cents,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
type CreditNote struct {
LagoID uuid.UUID `json:"lago_id,omitempty"`
SequentialID int `json:"sequential_id,omitempty"`
Number string `json:"number,omitempty"`
LagoInvoiceID uuid.UUID `json:"lago_invoice_id,omitempty"`
InvoiceNumber string `json:"invoice_number,omitempty"`
Reason CreditNoteReason `json:"reason,omitempty"`
CreditStatus CreditNoteCreditStatus `json:"credit_status,omitempty"`
RefundStatus CreditNoteRefundStatus `json:"refund_status,omitempty"`
Currency Currency `json:"currency,omitempty"`
TotalAmountCents int `json:"total_amount_cents,omitempty"`
CreditAmountCents int `json:"credit_amount_cents,omitempty"`
BalanceAmountCents int `json:"balance_amount_cents,omitempty"`
RefundAmountCents int `json:"refund_amount_cents,omitempty"`
TaxesAmountCents int `json:"taxes_amount_cents,omitempty"`
TaxesRate float32 `json:"taxes_rate,omitempty"`
SubTotalExcludingTaxesAmountCents int `json:"sub_total_excluding_taxes_amount_cents,omitempty"`
CouponsAdjustmentAmountCents int `json:"coupons_adjustment_amount_cents,omitempty"`
FileURL string `json:"file_url,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Items []CreditNoteItem `json:"items,omitempty"`
// Deprecated: Will be removed in the future
TotalAmountCurrency Currency `json:"total_amount_currency,omitempty"`
CreditAmountCurrency Currency `json:"credit_amount_currency,omitempty"`
BalanceAmountCurrency Currency `json:"balance_amount_currency,omitempty"`
RefundAmountCurrency Currency `json:"refund_amount_currency,omitempty"`
VatAmountCents int `json:"vat_amount_cents,omitempty"`
VatAmountCurrency Currency `json:"vat_amount_currency,omitempty"`
SubTotalVatExcludedAmountCents int `json:"sub_total_vat_excluded_amount_cents,omitempty"`
SubTotalVatExcludedAmountCurrency Currency `json:"sub_total_vat_excluded_amount_currency,omitempty"`
}
type CreditNoteEstimated struct {
LagoInvoiceID uuid.UUID `json:"lago_invoice_id,omitempty"`
InvoiceNumber string `json:"invoice_number,omitempty"`
Currency Currency `json:"currency,omitempty"`
MaxCreditableAmountCents int `json:"max_creditable_amount_cents,omitempty"`
MaxRefundableAmountCents int `json:"max_refundable_amount_cents,omitempty"`
TaxesAmountCents int `json:"taxes_amount_cents,omitempty"`
TaxesRate float32 `json:"taxes_rate,omitempty"`
SubTotalExcludingTaxesAmountCents int `json:"sub_total_excluding_taxes_amount_cents,omitempty"`
CouponsAdjustmentAmountCents int `json:"coupons_adjustment_amount_cents,omitempty"`
Items []CreditNoteEstimatedItem `json:"items,omitempty"`
AppliedTaxes []CreditNoteEstimatedAppliedTax `json:"applied_taxes,omitempty"`
}
type CreditNoteEstimatedItem struct {
AmountCents int `json:"amount_cents,omitempty"`
LagoFeeID uuid.UUID `json:"lago_fee_id,omitempty"`
}
type CreditNoteEstimatedAppliedTax struct {
LagoTaxId uuid.UUID `json:"lago_tax_id,omitempty"`
TaxName string `json:"tax_name,omitempty"`
TaxCode string `json:"tax_code,omitempty"`
TaxRate float32 `json:"tax_rate,omitempty"`
TaxDescription string `json:"tax_description,omitempty"`
AmountCents int `json:"amount_cents,omitempty"`
AmountCurrency Currency `json:"amount_currency,omitempty"`
}
type CreditNoteItemInput struct {
LagoFeeID uuid.UUID `json:"fee_id,omitempty"`
AmountCents int `json:"amount_cents,omitempty"`
}
type CreditNoteInput struct {
LagoInvoiceID uuid.UUID `json:"invoice_id,omitempty"`
Reason CreditNoteReason `json:"reason,omitempty"`
Items []CreditNoteItemInput `json:"items,omitempty"`
CreditAmountCents int `json:"refund_amount_cents,omitempty"`
RefundAmountCents int `json:"credit_amount_cents,omitempty"`
}
type CreditNoteUpdateInput struct {
LagoID string `json:"id,omitempty"`
RefundStatus CreditNoteRefundStatus `json:"refund_status,omitempty"`
}
type CreditNoteUpdateParams struct {
CreditNote *CreditNoteUpdateInput `json:"credit_note"`
}
type CreditNoteEstimateInput struct {
LagoInvoiceID uuid.UUID `json:"invoice_id,omitempty"`
Items []CreditNoteItemInput `json:"items,omitempty"`
}
type CreditNoteEstimateParams struct {
CreditNote *CreditNoteEstimateInput `json:"credit_note"`
}
func (c *Client) CreditNote() *CreditNoteRequest {
return &CreditNoteRequest{
client: c,
}
}
func (cr *CreditNoteRequest) Get(ctx context.Context, creditNoteID uuid.UUID) (*CreditNote, *Error) {
subPath := fmt.Sprintf("%s/%s", "credit_notes", creditNoteID)
clientRequest := &ClientRequest{
Path: subPath,
Result: &CreditNoteResult{},
}
result, err := cr.client.Get(ctx, clientRequest)
if err != nil {
return nil, err
}
creditNoteResult, ok := result.(*CreditNoteResult)
if !ok {
return nil, &ErrorTypeAssert
}
return creditNoteResult.CreditNote, nil
}
func (cr *CreditNoteRequest) Download(ctx context.Context, creditNoteID string) (*CreditNote, *Error) {
subPath := fmt.Sprintf("%s/%s/%s", "credit_notes", creditNoteID, "download")
clientRequest := &ClientRequest{
Path: subPath,
Result: &CreditNoteResult{},
}
result, err := cr.client.PostWithoutBody(ctx, clientRequest)
if err != nil {
return nil, err
}
if result != nil {
creditNoteResult, ok := result.(*CreditNoteResult)
if !ok {
return nil, &ErrorTypeAssert
}
return creditNoteResult.CreditNote, nil
}
return nil, nil
}
func (cr *CreditNoteRequest) GetList(ctx context.Context, creditNoteListInput *CreditListInput) (*CreditNoteResult, *Error) {
jsonQueryParams, err := json.Marshal(creditNoteListInput)
if err != nil {
return nil, &Error{Err: err}
}
queryParams := make(map[string]string)
if err = json.Unmarshal(jsonQueryParams, &queryParams); err != nil {
return nil, &Error{Err: err}
}
clientRequest := &ClientRequest{
Path: "credit_notes",
QueryParams: queryParams,
Result: &CreditNoteResult{},
}
result, clientErr := cr.client.Get(ctx, clientRequest)
if clientErr != nil {
return nil, clientErr
}
creditNoteResult, ok := result.(*CreditNoteResult)
if !ok {
return nil, &ErrorTypeAssert
}
return creditNoteResult, nil
}
func (cr *CreditNoteRequest) Create(ctx context.Context, creditNoteInput *CreditNoteInput) (*CreditNote, *Error) {
creditNoteParams := &CreditNoteParams{
CreditNote: creditNoteInput,
}
clientRequest := &ClientRequest{
Path: "credit_notes",
Result: &CreditNoteResult{},
Body: creditNoteParams,
}
result, err := cr.client.Post(ctx, clientRequest)
if err != nil {
return nil, err
}
creditNoteResult, ok := result.(*CreditNoteResult)
if !ok {
return nil, &ErrorTypeAssert
}
return creditNoteResult.CreditNote, nil
}
func (cr *CreditNoteRequest) Update(ctx context.Context, creditNoteUpdateInput *CreditNoteUpdateInput) (*CreditNote, *Error) {
subPath := fmt.Sprintf("%s/%s", "credit_notes", creditNoteUpdateInput.LagoID)
creditNoteParams := &CreditNoteUpdateParams{
CreditNote: creditNoteUpdateInput,
}
ClientRequest := &ClientRequest{
Path: subPath,
Result: &PlanResult{},
Body: creditNoteParams,
}
result, err := cr.client.Put(ctx, ClientRequest)
if err != nil {
return nil, err
}
creditNoteResult, ok := result.(*CreditNoteResult)
if !ok {
return nil, &ErrorTypeAssert
}
return creditNoteResult.CreditNote, nil
}
func (cr *CreditNoteRequest) Void(ctx context.Context, creditNoteID string) (*CreditNote, *Error) {
subPath := fmt.Sprintf("%s/%s/%s", "credit_notes", creditNoteID, "void")
clientRequest := &ClientRequest{
Path: subPath,
Result: &CreditNoteResult{},
}
result, err := cr.client.Put(ctx, clientRequest)
if err != nil {
return nil, err
}
creditNoteResult, ok := result.(*CreditNoteResult)
if !ok {
return nil, &ErrorTypeAssert
}
return creditNoteResult.CreditNote, nil
}
func (cr *CreditNoteRequest) Estimate(ctx context.Context, creditNoteEstimateInput *CreditNoteEstimateInput) (*CreditNoteEstimated, *Error) {
creditNoteEstimateParams := &CreditNoteEstimateParams{
CreditNote: creditNoteEstimateInput,
}
clientRequest := &ClientRequest{
Path: "credit_notes/estimate",
Result: &CreditNoteEstimatedResult{},
Body: creditNoteEstimateParams,
}
result, err := cr.client.Post(ctx, clientRequest)
if err != nil {
return nil, err
}
creditNoteEstimatedResult, ok := result.(*CreditNoteEstimatedResult)
if !ok {
return nil, &ErrorTypeAssert
}
return creditNoteEstimatedResult.CreditNoteEstimated, nil
}