-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet_transaction.go
123 lines (99 loc) · 4.21 KB
/
wallet_transaction.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
package lago
import (
"context"
"net/url"
"strconv"
"time"
"github.com/google/uuid"
)
type WalletTransactionStatus string
const (
WalletTransactionStatusPending WalletTransactionStatus = "pending"
WalletTransactionStatusSettled WalletTransactionStatus = "settled"
)
type TransactionStatus string
const (
Purchased TransactionStatus = "purchased"
Granted TransactionStatus = "granted"
Voided TransactionStatus = "voided"
Invoiced TransactionStatus = "invoiced"
)
type TransactionType string
const (
Outbound TransactionType = "outbound"
Inbound TransactionType = "inbound"
)
type WalletTransactionListInput struct {
PerPage int `json:"per_page,omitempty,string"`
Page int `json:"page,omitempty,string"`
WalletID string `json:"wallet_id,omitempty"`
Status WalletTransactionStatus `json:"status,omitempty"`
TransactionStatus TransactionStatus `json:"transaction_status,omitempty"`
TransactionType TransactionType `json:"transaction_type,omitempty"`
}
func (i *WalletTransactionListInput) query() url.Values {
q := make(url.Values)
if i.PerPage > 0 {
q.Add("per_page", strconv.Itoa(i.PerPage))
}
if i.Page > 0 {
q.Add("page", strconv.Itoa(i.Page))
}
if i.WalletID != "" {
q.Add("wallet_id", i.WalletID)
}
if i.Status != "" {
q.Add("status", string(i.Status))
}
if i.TransactionStatus != "" {
q.Add("transaction_status", string(i.TransactionStatus))
}
if i.TransactionType != "" {
q.Add("transaction_type", string(i.TransactionType))
}
return q
}
type walletTransactionParams struct {
WalletTransactionInput *WalletTransactionInput `json:"wallet_transaction"`
}
type WalletTransactionInput struct {
WalletID string `json:"wallet_id,omitempty"`
PaidCredits string `json:"paid_credits,omitempty"`
GrantedCredits string `json:"granted_credits,omitempty"`
VoidedCredits string `json:"voided_credits,omitempty"`
InvoiceRequiresSuccessfulPayment bool `json:"invoice_requires_successful_payment,omitempty"`
Metadata []*WalletTransactionMetadata `json:"metadata,omitempty"`
}
type WalletTransactionMetadata struct {
Key string `json:"key"`
Value string `json:"value"`
}
type WalletTransactionList struct {
WalletTransactions []*WalletTransaction `json:"wallet_transactions,omitempty"`
Meta Metadata `json:"meta,omitempty"`
}
type WalletTransaction struct {
LagoID uuid.UUID `json:"lago_id,omitempty"`
LagoWalletID uuid.UUID `json:"lago_wallet_id,omitempty"`
Status WalletTransactionStatus `json:"status,omitempty"`
TransactionType TransactionType `json:"transaction_type,omitempty"`
Amount string `json:"amount,omitempty"`
CreditAmount string `json:"credit_amount,omitempty"`
InvoiceRequiresSuccessfulPayment bool `json:"invoice_requires_successful_payment,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
SettledAt time.Time `json:"settled_at,omitempty"`
Metadata []*WalletTransactionMetadata `json:"metadata,omitempty"`
}
func (c *Client) CreateWalletTransaction(ctx context.Context, walletTransactionInput *WalletTransactionInput) (*WalletTransactionList, error) {
u := c.url("wallet_transactions", nil)
return post[walletTransactionParams, WalletTransactionList](
ctx,
c,
u,
&walletTransactionParams{WalletTransactionInput: walletTransactionInput},
)
}
func (c *Client) ListWalletTransactions(ctx context.Context, walletTransactionListInput *WalletTransactionListInput) (*WalletTransactionList, error) {
u := c.url("wallets/"+walletTransactionListInput.WalletID+"/wallet_transactions", walletTransactionListInput.query())
return get[WalletTransactionList](ctx, c, u)
}