-
Notifications
You must be signed in to change notification settings - Fork 17
/
wallet_transaction.go
142 lines (116 loc) · 4.83 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package lago
import (
"context"
"encoding/json"
"fmt"
"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 WalletTransactionRequest struct {
client *Client
}
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"`
}
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 WalletTransactionResult 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) WalletTransaction() *WalletTransactionRequest {
return &WalletTransactionRequest{
client: c,
}
}
func (wtr *WalletTransactionRequest) Create(ctx context.Context, walletTransactionInput *WalletTransactionInput) (*WalletTransactionResult, *Error) {
walletTransactionParams := &WalletTransactionParams{
WalletTransactionInput: walletTransactionInput,
}
clientRequest := &ClientRequest{
Path: "wallet_transactions",
Result: &WalletTransactionResult{},
Body: walletTransactionParams,
}
result, err := wtr.client.Post(ctx, clientRequest)
if err != nil {
return nil, err
}
walletTransactionResult, ok := result.(*WalletTransactionResult)
if !ok {
return nil, &ErrorTypeAssert
}
return walletTransactionResult, nil
}
func (wtr *WalletTransactionRequest) GetList(ctx context.Context, walletTransactionListInput *WalletTransactionListInput) (*WalletTransactionResult, *Error) {
jsonQueryParams, err := json.Marshal(walletTransactionListInput)
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}
}
subPath := fmt.Sprintf("%s/%s/%s", "wallets", walletTransactionListInput.WalletID, "wallet_transactions")
clientRequest := &ClientRequest{
Path: subPath,
QueryParams: queryParams,
Result: &WalletTransactionResult{},
}
result, clientErr := wtr.client.Get(ctx, clientRequest)
if clientErr != nil {
return nil, clientErr
}
walletTransactionResult, ok := result.(*WalletTransactionResult)
if !ok {
return nil, &ErrorTypeAssert
}
return walletTransactionResult, nil
}