-
Notifications
You must be signed in to change notification settings - Fork 1
/
merchant.go
194 lines (174 loc) · 5.58 KB
/
merchant.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
package tripay
import (
"github.com/valyala/fasthttp"
)
type ChannelResponse struct{
Success bool `json:"success"`
Message string `json:"message"`
Data []struct{
Group string `json:"group"`
PaymentMethod PaymentChannelCode `json:"code"`
Name string `json:"name"`
Type string `json:"type"`
MerchantFee Fee `json:"fee_merchant"`
CustomerFee Fee `json:"fee_customer"`
TotalFee Fee `json:"total_fee"`
Active bool `json:"active"`
} `json:"data,omitempty"`
}
//due to inconsistent fee data type we can't specify the type of data. the data type may will variant(string, int, float).
type Fee struct{
Flat interface{} `json:"flat"`
Percent interface{} `json:"percent"`
}
/*
GetChannel used for payment channel fetching information from tripay.
u can retrieve this natively or use ChannelResponse struct
If the parameter is empty, this will return all payment channels information.
*/
func (t *Tripay) GetChannel(code PaymentChannelCode)([]byte, error) {
uri := []byte(t.Host + "/merchant/payment-channel?code=" + string(code))
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(res)
req.SetRequestURIBytes(uri)
req.Header.AddBytesV("Authorization", t.ApiKey)
if err := fasthttp.Do(req, res); err != nil {
return nil, err
}
return res.Body(), nil
}
type CostResponse struct{
Success bool `json:"success"`
Message string `json:"message"`
Data []struct{
PaymentMethod PaymentChannelCode `json:"code"`
Name string `json:"name"`
Fee struct{
Flat interface{} `json:"flat"`
Percent interface{} `json:"percent"`
Min interface{} `json:"min"`
Max interface{} `json:"max"`
} `json:"Fee"`
TotalFee struct{
Merchant int `json:"merchant"`
Customer int `json:"customer"`
} `json:"total_fee"`
Active bool `json:"active"`
} `json:"data,omitempty"`
}
/*
GetCost used for calculating the payment fees.
u can retrieve this natively or use CostResponse struct.
If the parameter is empty, this will return all payment channels fees information.
*/
func (t *Tripay) GetCost(amount int,code PaymentChannelCode)([]byte, error) {
var cost struct{
Amount int `query:"amount"`
Code PaymentChannelCode `query:"code,omitempty"`
}
cost.Amount = amount
cost.Code = code
q, _ :=structToQuery(&cost)
uri := []byte(t.Host + "/merchant/fee-calculator?"+q)
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(res)
req.SetRequestURIBytes(uri)
req.Header.AddBytesV("Authorization", t.ApiKey)
if err := fasthttp.Do(req, res); err != nil {
return nil, err
}
return res.Body(), nil
}
type TransactionListResponse struct{
Success bool `json:"success"`
Message string `json:"message"`
Data []struct{
Reference string `json:"reference"`
MerchantRef string `json:"merchant_ref"`
PaymentType string `json:"payment_selection_type"`
//same like payment channel code
PaymentMethod PaymentChannelCode `json:"payment_method"`
PaymentName string `json:"payment_name"`
CustomerName string `json:"customer_name"`
CustomerEmail string `json:"customer_email"`
CustomerPhone string `json:"customer_phone"`
CallbackUrl string `json:"callback_url"`
ReturnUrl string `json:"return_url"`
Amount int `json:"amount"`
MerchantFee interface{} `json:"fee_merchant"`
CustomerFee interface{} `json:"fee_customer"`
TotalFee interface{} `json:"total_fee"`
AmountReceived int `json:"amount_received"`
PayCode int `json:"pay_code"`
PayUrl string `json:"pay_url"`
CheckoutUrl string `json:"checkout_url"`
OrderItems []struct{
Sku string `json:"sku"`
Name string `json:"name"`
Price int `json:"price"`
Quantity int `json:"quantity"`
Subtotal int `json:"Subtotal"`
}`json:"order_items"`
Status string `json:"status"`
Note string `json:"note"`
CreatedAt int `json:"created_at"`
ExpiredAt int `json:"expired_at"`
PaidAt int `json:"paid_at"`
} `json:"data,omitempty"`
Pagination struct{
Sort string `json:"sort"`
Offset struct{
From int `json:"from"`
To int `json:"to"`
} `json:"offset"`
CurrentPage int `json:"current_page"`
//null json data will converted to 0
PreviousPage int `json:"previous_page"`
NextPage int `json:"next_page"`
LastPage int `json:"last_page"`
PerPage int `json:"per_page"`
TotalRecords int `json:"total_records"`
}
}
/*
GetTransaction list used for retrieve payment list
u can retrieve this natively or use TransactionListResponse struct
for param check https://tripay.co.id/developer?tab=merchant-transactions
*/
func (t *Tripay)GetTransactionList(page,perPage int,channelCode PaymentChannelCode, sort, reference,merchantRef, status string)([]byte, error){
var list struct{
Page int `query:"page,omitempty"`
PerPage int `query:"perPage,omitempty"`
Code PaymentChannelCode `query:"method,omitempty"`
Sort string `query:"sort,omitempty"`
Reference string `query:"reference,omitempty"`
MerchantRef string `query:"merchant_ref,omitempty"`
Status string `query:"status,omitempty"`
}
list.Page = page
list.PerPage = perPage
list.Code = channelCode
list.Sort = sort
list.Reference = reference
list.MerchantRef = merchantRef
list.Status = status
q, err :=structToQuery(&list)
if err != nil{
return nil, err
}
uri := []byte(t.Host + "/merchant/transactions?"+q)
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(res)
req.SetRequestURIBytes(uri)
req.Header.AddBytesV("Authorization", t.ApiKey)
if err := fasthttp.Do(req, res); err != nil {
return nil, err
}
return res.Body(), nil
}