forked from getlago/lago-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
218 lines (182 loc) · 7.4 KB
/
wallet.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
package lago
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/google/uuid"
)
type Status string
const (
Active Status = "active"
Terminated Status = "terminated"
)
type RecurringTransactionRuleInput struct {
LagoID uuid.UUID `json:"lago_id,omitempty"`
Interval string `json:"interval,omitempty"`
Method string `json:"method,omitempty"`
StartedAt *time.Time `json:"started_at,omitempty"`
TargetOngoingBalance string `json:"target_ongoing_balance,omitempty"`
ThresholdCredits string `json:"threshold_credits,omitempty"`
Trigger string `json:"trigger,omitempty"`
PaidCredits string `json:"paid_credits,omitempty"`
GrantedCredits string `json:"granted_credits,omitempty"`
}
type RecurringTransactionRuleResponse struct {
LagoID uuid.UUID `json:"lago_id,omitempty"`
Interval string `json:"interval,omitempty"`
Method string `json:"method,omitempty"`
StartedAt *time.Time `json:"started_at,omitempty"`
TargetOngoingBalance string `json:"target_ongoing_balance,omitempty"`
ThresholdCredits string `json:"threshold_credits,omitempty"`
Trigger string `json:"trigger,omitempty"`
PaidCredits string `json:"paid_credits,omitempty"`
GrantedCredits string `json:"granted_credits,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
type WalletRequest struct {
client *Client
}
type WalletParams struct {
WalletInput *WalletInput `json:"wallet"`
}
type WalletInput struct {
RateAmount string `json:"rate_amount,omitempty"`
Currency Currency `json:"currency,omitempty"`
Name string `json:"name,omitempty"`
PaidCredits string `json:"paid_credits,omitempty"`
GrantedCredits string `json:"granted_credits,omitempty"`
ExpirationAt *time.Time `json:"expiration_at,omitempty"`
ExternalCustomerID string `json:"external_customer_id,omitempty"`
RecurringTransactionRules []RecurringTransactionRuleInput `json:"recurring_transaction_rules,omitempty"`
}
type WalletListInput struct {
PerPage int `json:"per_page,omitempty,string"`
Page int `json:"page,omitempty,string"`
ExternalCustomerID string `json:"external_customer_id,omitempty,string"`
}
type WalletResult struct {
Wallet *Wallet `json:"wallet,omitempty"`
Wallets []Wallet `json:"wallets,omitempty"`
Meta Metadata `json:"meta,omitempty"`
}
type Wallet struct {
LagoID uuid.UUID `json:"lago_id,omitempty"`
LagoCustomerID uuid.UUID `json:"lago_customer_id,omitempty"`
ExternalCustomerID string `json:"external_customer_id,omitempty"`
Status Status `json:"status,omitempty"`
Currency Currency `json:"currency,omitempty"`
Name string `json:"name,omitempty"`
RateAmount string `json:"rate_amount,omitempty"`
CreditsBalance string `json:"credits_balance,omitempty"`
Balance string `json:"balance,omitempty"` // NOTE(legacy)
BalanceCents int `json:"balance_cents,omitempty"`
ConsumedCredits string `json:"consumed_credits,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
ExpirationDate time.Time `json:"expiration_date,omitempty"`
LastBalanceSyncAt time.Time `json:"last_balance_sync_at,omitempty"`
LastConsumedCreditAt time.Time `json:"last_consumed_credit_at,omitempty"`
TerminatedAt time.Time `json:"terminated_at,omitempty"`
RecurringTransactionRules []RecurringTransactionRuleResponse `json:"recurring_transaction_rules,omitempty"`
OngoingBalanceCents int `json:"ongoing_balance_cents,omitempty"`
OngoingUsageBalanceCents int `json:"ongoing_usage_balance_cents,omitempty"`
CreditsOngoingBalance string `json:"credits_ongoing_balance,omitempty"`
CreditsOngoingUsageBalance string `json:"credits_ongoing_usage_balance,omitempty"`
}
func (c *Client) Wallet() *WalletRequest {
return &WalletRequest{
client: c,
}
}
func (bmr *WalletRequest) Get(ctx context.Context, walletID string) (*Wallet, *Error) {
subPath := fmt.Sprintf("%s/%s", "wallets", walletID)
clientRequest := &ClientRequest{
Path: subPath,
Result: &WalletResult{},
}
result, err := bmr.client.Get(ctx, clientRequest)
if err != nil {
return nil, err
}
walletResult, ok := result.(*WalletResult)
if !ok {
return nil, &ErrorTypeAssert
}
return walletResult.Wallet, nil
}
func (bmr *WalletRequest) GetList(ctx context.Context, walletListInput *WalletListInput) (*WalletResult, *Error) {
jsonQueryParams, err := json.Marshal(walletListInput)
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: "wallets",
QueryParams: queryParams,
Result: &WalletResult{},
}
result, clientErr := bmr.client.Get(ctx, clientRequest)
if clientErr != nil {
return nil, clientErr
}
walletResult, ok := result.(*WalletResult)
if !ok {
return nil, &ErrorTypeAssert
}
return walletResult, nil
}
func (bmr *WalletRequest) Create(ctx context.Context, walletInput *WalletInput) (*Wallet, *Error) {
walletParams := &WalletParams{
WalletInput: walletInput,
}
clientRequest := &ClientRequest{
Path: "wallets",
Result: &WalletResult{},
Body: walletParams,
}
result, err := bmr.client.Post(ctx, clientRequest)
if err != nil {
return nil, err
}
walletResult, ok := result.(*WalletResult)
if !ok {
return nil, &ErrorTypeAssert
}
return walletResult.Wallet, nil
}
func (bmr *WalletRequest) Update(ctx context.Context, walletInput *WalletInput, walletID string) (*Wallet, *Error) {
subPath := fmt.Sprintf("%s/%s", "wallets", walletID)
clientRequest := &ClientRequest{
Path: subPath,
Result: &WalletResult{},
Body: walletInput,
}
result, err := bmr.client.Put(ctx, clientRequest)
if err != nil {
return nil, err
}
walletResult, ok := result.(*WalletResult)
if !ok {
return nil, &ErrorTypeAssert
}
return walletResult.Wallet, nil
}
func (bmr *WalletRequest) Delete(ctx context.Context, walletID string) (*Wallet, *Error) {
subPath := fmt.Sprintf("%s/%s", "wallets", walletID)
clientRequest := &ClientRequest{
Path: subPath,
Result: &WalletResult{},
}
result, err := bmr.client.Delete(ctx, clientRequest)
if err != nil {
return nil, err
}
walletResult, ok := result.(*WalletResult)
if !ok {
return nil, &ErrorTypeAssert
}
return walletResult.Wallet, nil
}