-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrazorpay.go
332 lines (309 loc) · 10.1 KB
/
razorpay.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package razorpay
import (
"bytes"
"errors"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
)
const (
//APIURL RazorPay Endpoint
APIURL = "https://api.razorpay.com/v1/"
)
//RazorPay ...
type RazorPay struct {
apikey string //rzp_test_eq0OycgzlfwDIs
apisecret string //vt9YFHVw8EgMb2phcKz9SZjd
merchantID string
}
//Payments ...
type Payments struct {
Count int `json:"count"`
Entity string `json:"entity"`
Items []Payment
}
//Payment ...
type Payment struct {
ID string `json:"id"`
Entity string `json:"entity"`
Amount int `json:"amount"`
Currency string `json:"currency"`
Status string `json:"status"`
OrderID string `json:"order_id"`
InvoiceID interface{} `json:"invoice_id"`
International bool `json:"international"`
Method string `json:"method"`
AmountRefunded int `json:"amount_refunded"`
RefundStatus interface{} `json:"refund_status"`
Captured bool `json:"captured"`
Description string `json:"description"`
CardID interface{} `json:"card_id"`
Bank interface{} `json:"bank"`
Wallet string `json:"wallet"`
Vpa interface{} `json:"vpa"`
Email string `json:"email"`
Contact string `json:"contact"`
Notes struct {
MerchantOrderID string `json:"merchant_order_id"`
} `json:"notes"`
Fee int `json:"fee"`
Tax int `json:"tax"`
ErrorCode interface{} `json:"error_code"`
ErrorDescription interface{} `json:"error_description"`
CreatedAt int `json:"created_at"`
}
//Refund ...
type Refund struct {
ID string `json:"id"`
Entity string `json:"entity"`
Amount int `json:"amount"`
Currency string `json:"currency"`
PaymentID string `json:"payment_id"`
Notes struct {
} `json:"notes"`
CreatedAt int `json:"created_at"`
}
//Refunds ...
type Refunds struct {
Count int `json:"count"`
Entity string `json:"entity"`
Items []struct {
ID string `json:"id"`
Entity string `json:"entity"`
Amount int `json:"amount"`
Currency string `json:"currency"`
PaymentID string `json:"payment_id"`
Notes struct {
} `json:"notes"`
CreatedAt int `json:"created_at"`
} `json:"items"`
}
type Order struct {
ID string `json:"id"`
Entity string `json:"entity"`
Amount int `json:"amount"`
Currency string `json:"currency"`
Receipt string `json:"receipt"`
Status string `json:"status"`
Attempts int `json:"attempts"`
Notes []interface{} `json:"notes"`
CreatedAt int `json:"created_at"`
}
type Orders struct {
Entity string `json:"entity"`
Count int `json:"count"`
Items []Order `json:"items"`
}
//New creates new instance of client
func New(APIKey string, apiSecret string) *RazorPay {
return &RazorPay{apikey: APIKey, apisecret: apiSecret}
}
//PaymentLink payment link request json data structure
type PaymentLink struct {
Customer Cust `json:"customer"`
Type string `json:"type"`
ViewLess int `json:"view_less"`
Amount int `json:"amount"`
Currency string `json:"currency"`
Description string `json:"description"`
ExpireBy int `json:"expire_by,omitempty"`
CustomerID string `json:"customer_id,omitempty"`
SmsNotify string `json:"sms_notify,omitempty"`
EmailNotify string `json:"email_notify,omitempty"`
Date int `json:"date,omitempty"`
Terms string `json:"terms,omitempty"`
Notes []interface{} `json:"notes,omitempty"`
}
type Cust struct {
Name string `json:"name"`
Email string `json:"email"`
Contact string `json:"contact"`
}
//PaymentLinkResponse link response json data structure
type PaymentLinkResponse struct {
ID string `json:"id"`
Entity string `json:"entity"`
Receipt interface{} `json:"receipt"`
CustomerID string `json:"customer_id"`
CustomerDetails struct {
Name string `json:"name"`
Email string `json:"email"`
Contact string `json:"contact"`
BillingAddress interface{} `json:"billing_address"`
} `json:"customer_details"`
OrderID string `json:"order_id"`
LineItems []interface{} `json:"line_items"`
PaymentID interface{} `json:"payment_id"`
Status string `json:"status"`
ExpireBy interface{} `json:"expire_by"`
IssuedAt int `json:"issued_at"`
PaidAt interface{} `json:"paid_at"`
CancelledAt interface{} `json:"cancelled_at"`
ExpiredAt interface{} `json:"expired_at"`
SmsStatus string `json:"sms_status"`
EmailStatus string `json:"email_status"`
Date int `json:"date"`
Terms interface{} `json:"terms"`
PartialPayment bool `json:"partial_payment"`
GrossAmount int `json:"gross_amount"`
TaxAmount int `json:"tax_amount"`
Amount int `json:"amount"`
AmountPaid int `json:"amount_paid"`
AmountDue int `json:"amount_due"`
Currency string `json:"currency"`
Description string `json:"description"`
Notes []interface{} `json:"notes"`
Comment interface{} `json:"comment"`
ShortURL string `json:"short_url"`
ViewLess bool `json:"view_less"`
BillingStart interface{} `json:"billing_start"`
BillingEnd interface{} `json:"billing_end"`
Type string `json:"type"`
GroupTaxesDiscounts bool `json:"group_taxes_discounts"`
UserID interface{} `json:"user_id"`
CreatedAt int `json:"created_at"`
}
type Response struct{
Success bool `json:"success"`
}
type NewOrder struct {
Amount int `json:"amount"`
Currency string `json:"currency"`
Receipt string `json:"receipt"`
PaymentCapture bool `json:"payment_capture"`
Notes map[string]string `json:"notes"`
}
type Data struct {
Amount int `json:"amount"`
}
type Customer struct {
ID string `json:"id"`
Entity string `json:"entity"`
Name string `json:"name"`
Email string `json:"email"`
Contact string `json:"contact"`
Notes map[string]string `json:"notes,omitempty"`
CreatedAt int `json:"created_at"`
}
type Customers struct {
Entity string `json:"entity"`
Count int `json:"count"`
Items []Customer `json:"items"`
}
type CustomerInput struct {
Name string `json:"name"`
Email string `json:"email"`
Contact string `json:"contact"`
Notes map[string]string `json:"notes,omitempty"`
}
type Settlement struct{
ID string `json:"id"`
Entity string `json:"entity"`
Amount int `json:"amount"`
Status string `json:"status"`
Fees int `json:"fees"`
Tax int `json:"tax"`
Utr string `json:"utr"`
CreatedAt int `json:"created_at"`
}
type Settlements struct{
Entity string `json:"entity"`
Count int `json:"count"`
Items []Settlement `json:"items"`
}
func (r *RazorPay) call(operation string, reqbody []byte, pathparams string, queryparams map[string]string) ([]byte, error) {
var rurl string
var rmethod string
switch operation {
case "CreateOrder":
rmethod = "POST"
rurl = APIURL + "orders"
case "GetOrders":
rmethod = "GET"
rurl = APIURL + "orders"
case "GetOrderByID":
rmethod = "GET"
rurl = APIURL + "orders" + pathparams
case "CreatePaymentLink":
rmethod = "POST"
rurl = APIURL + "invoices"
case "GetPaymentLink":
rmethod = "GET"
rurl = APIURL + "invoices/" + pathparams
case "SendPaymentLink":
rmethod = "POST"
rurl = APIURL + "invoices/" + pathparams
case "CancelPaymentLink":
rmethod = "POST"
rurl = APIURL + "invoices/" + pathparams
case "CapturePayment":
rmethod = "POST"
rurl = APIURL + "payments/" + pathparams
case "GetPaymentByID":
rmethod = "GET"
rurl = APIURL + "payment/" + pathparams
case "GetPayments":
rmethod = "GET"
rurl = APIURL + "payments/"
case "CreateRefund":
rmethod = "POST"
rurl = APIURL + "payments/" + pathparams
case "GetRefunds":
rmethod = "GET"
rurl = APIURL + "refunds/"
case "GetRefundsByPaymentID":
rmethod = "GET"
rurl = APIURL + "payments/" + pathparams
case "GetRefundByID":
rmethod = "GET"
rurl = APIURL + "refunds/" + pathparams
case "CreateCustomer":
rmethod = "POST"
rurl = APIURL + "customers"
case "EditCustomerByID":
rmethod = "PUT"
rurl = APIURL + "customers/" + pathparams
case "GetCustomerByID":
rmethod = "GET"
rurl = APIURL + "customers/" + pathparams
case "GetCustomers":
rmethod = "GET"
rurl = APIURL + "customers"
case "GetSettlements":
rmethod = "GET"
rurl = APIURL + "settlements"
case "GetSettlementByID":
rmethod = "GET"
rurl = APIURL + "settlements/" + pathparams
default:
err := errors.New("Invalid Method/Operation")
return nil, err
}
log.Debugln("[Razopay] Request URL:", rurl)
log.Debugln("[Razopay] Path Params:", pathparams)
log.Debugln("[Razopay] Query Params:", queryparams)
log.Debugln("[Razopay] Request Body:", string(reqbody))
req, err := http.NewRequest(rmethod, rurl, bytes.NewBuffer(reqbody))
req.SetBasicAuth(r.apikey, r.apisecret)
req.Header.Add("Content-Type", "application/json")
if err != nil {
log.Errorln(err)
return nil, err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Errorln(err)
return nil, err
}
defer resp.Body.Close()
log.Debugln("[Razopay] Response Status:", resp.Status)
log.Debugln("[Razopay] Response Headers:", resp.Header)
respbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorln(err)
return nil, err
}
log.Debug("[Razopay] Response Body:", string(respbody))
return respbody, nil
}