-
Notifications
You must be signed in to change notification settings - Fork 1
/
transactions.go
226 lines (205 loc) · 8.48 KB
/
transactions.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
package paystack
import (
"context"
"fmt"
)
type Transaction struct {
client *Client
}
// TransactionList is a list object for transactions.
type TransactionList struct {
Meta PaginationMeta `json:"meta"`
Data []TransactionResponse `json:"data"`
}
type Authorization struct {
AuthorizationCode string `json:"authorization_code"`
Bin string `json:"bin"`
Last4 string `json:"last4"`
ExpMonth string `json:"exp_month"`
ExpYear string `json:"exp_year"`
Channel string `json:"channel"`
CardType string `json:"card_type"`
Bank string `json:"bank"`
CountryCode string `json:"country_code"`
Brand string `json:"brand"`
Reusable bool `json:"reusable"`
Signature string `json:"signature"`
AccountName interface{} `json:"account_name"`
}
type Customer struct {
ID int `json:"id"`
FirstName interface{} `json:"first_name"`
LastName interface{} `json:"last_name"`
Email string `json:"email"`
CustomerCode string `json:"customer_code"`
Phone interface{} `json:"phone"`
Metadata interface{} `json:"metadata"`
RiskAction string `json:"risk_action"`
InternationalFormatPhone interface{} `json:"international_format_phone"`
}
type TransactionTimeline struct {
StartTime int `json:"start_time"`
TimeSpent int `json:"time_spent,omitempty"`
Attempts int `json:"attempts,omitempty"`
Authentication interface{} `json:"authentication,omitempty"`
Errors int `json:"errors,omitempty"`
Success bool `json:"success,omitempty"`
Mobile bool `json:"mobile,omitempty"`
Input []interface{} `json:"input,omitempty"`
Channel string `json:"channel,omitempty"`
History []struct {
Type string `json:"type,omitempty"`
Message string `json:"message,omitempty"`
Time int `json:"time,omitempty"`
} `json:"history,omitempty"`
}
// TransactionRequest represents a request to start a transaction.
type TransactionRequest struct {
Amount float32 `json:"amount,omitempty"`
Email string `json:"email,omitempty"`
Currency string `json:"currency,omitempty"`
Reference string `json:"reference,omitempty"`
CallbackURL string `json:"callback_url,omitempty"`
Plan string `json:"plan,omitempty"`
InvoiceLimit int `json:"invoice_limit,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
Channels []string `json:"channels,omitempty"`
SplitCode string `json:"split_code,omitempty"`
SubAccount string `json:"subaccount,omitempty"`
TransactionCharge int `json:"transaction_charge,omitempty"`
AuthorizationCode string `json:"authorization_code,omitempty"`
Bearer string `json:"bearer,omitempty"`
Queue bool `json:"queue,omitempty"`
}
type InitializeTransactionResponse struct {
AuthorizationURL string `json:"authorization_url,omitempty"`
AccessCode string `json:"access_code,omitempty"`
Reference string `json:"reference,omitempty"`
}
type TransactionResponse struct {
ID int `json:"id"`
Domain string `json:"domain"`
Status string `json:"status"`
Reference string `json:"reference"`
Amount int `json:"amount"`
Message interface{} `json:"message"`
GatewayResponse string `json:"gateway_response"`
Channel string `json:"channel"`
Currency string `json:"currency"`
IPAddress string `json:"ip_address"`
Metadata interface{} `json:"metadata"`
Log TransactionTimeline `json:"log"`
Fees int `json:"fees"`
FeesSplit interface{} `json:"fees_split"`
Authorization Authorization `json:"authorization"`
Customer Customer `json:"customer"`
Plan interface{} `json:"plan"`
Split interface{} `json:"split"`
OrderID interface{} `json:"order_id"`
PaidAt string `json:"paidAt"`
CreatedAt string `json:"createdAt"`
RequestedAmount int `json:"requested_amount"`
PosTransactionData interface{} `json:"pos_transaction_data"`
Source interface{} `json:"source"`
FeesBreakdown interface{} `json:"fees_breakdown"`
TransactionDate string `json:"transaction_date"`
PlanObject interface{} `json:"plan_object"`
Subaccount interface{} `json:"subaccount"`
}
type ExportTransaction struct {
Path string `json:"path,omitempty"`
}
func newTransaction(client *Client) *Transaction {
return &Transaction{
client: client,
}
}
// Initialize a transaction
// For more details see https://paystack.com/docs/api/transaction/#initialize
func (t *Transaction) Initialize(ctx context.Context, txn *TransactionRequest) (*InitializeTransactionResponse, error) {
url := "transaction/initialize"
resp := &InitializeTransactionResponse{}
err := postResource(ctx, t.client, url, txn, resp)
return resp, err
}
// Verify a transaction
//
// For more details see https://paystack.com/docs/api/transaction/#verify
func (t *Transaction) Verify(ctx context.Context, reference string) (*TransactionResponse, error) {
url := fmt.Sprintf("/transaction/verify/%s", reference)
resp := &TransactionResponse{}
err := getResource(ctx, t.client, url, resp)
return resp, err
}
// List of transactions
//
// For more details see https://paystack.com/docs/api/transaction/#list
func (t *Transaction) List(ctx context.Context, params ...QueryType) (*TransactionList, error) {
var url string
if len(params) > 0 {
url = addQueryToUrl("transaction", params...)
} else {
url = "/transaction"
}
resp := &TransactionList{}
err := getResource(ctx, t.client, url, resp)
return resp, err
}
// Fetch a transaction
// For more details see https://paystack.com/docs/api/transaction/#fetch
func (t *Transaction) FetchById(ctx context.Context, id int) (*TransactionResponse, error) {
url := fmt.Sprintf("/transaction/%v", id)
resp := &TransactionResponse{}
err := getResource(ctx, t.client, url, resp)
return resp, err
}
// Charge Authorization
// For more details see https://paystack.com/docs/api/transaction/#charge-authorization
func (t *Transaction) Charge(ctx context.Context, txn *TransactionRequest) (*TransactionResponse, error) {
url := "transaction/charge_authorization"
resp := &TransactionResponse{}
err := postResource(ctx, t.client, url, txn, resp)
return resp, err
}
// Total Transactions
// For more details see https://paystack.com/docs/api/transaction/#totals
func (t *Transaction) Total(ctx context.Context, params ...QueryType) (*GenericResponse, error) {
var url string
if len(params) > 0 {
url = addQueryToUrl("transaction/totals", params...)
} else {
url = "/transaction/totals"
}
resp := &GenericResponse{}
err := getResource(ctx, t.client, url, resp)
return resp, err
}
// View the timeline of a transaction
// For more details see https://paystack.com/docs/api/transaction/#view-timeline
func (t *Transaction) Timeline(ctx context.Context, id_or_ref string) (*TransactionResponse, error) {
url := fmt.Sprintf("/transaction/timeline/%v", id_or_ref)
resp := &TransactionResponse{}
err := getResource(ctx, t.client, url, resp)
return resp, err
}
// Export your transaction
// For more details see https://paystack.com/docs/api/transaction/#export
func (t *Transaction) Export(ctx context.Context, params ...QueryType) (*ExportTransaction, error) {
var url string
if len(params) > 0 {
url = addQueryToUrl("transaction/export", params...)
} else {
url = "/transaction/export"
}
resp := &ExportTransaction{}
err := getResource(ctx, t.client, url, resp)
return resp, err
}
// Partial debit
// For more details see https://paystack.com/docs/api/transaction/#partial-debit
func (t *Transaction) PartialDebit(ctx context.Context, txn *TransactionRequest) (*TransactionResponse, error) {
url := "transaction/partial_debit"
resp := &TransactionResponse{}
err := postResource(ctx, t.client, url, txn, resp)
return resp, err
}