-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselly.go
436 lines (400 loc) · 12 KB
/
selly.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright 2018 The go-selly authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Selly API wrapper for selly.gg
package selly
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/aerth/tgun"
)
// DefaultUserAgent is used if UserAgent is empty
const DefaultUserAgent = "go-selly/0.0.4"
// Selly settings
type Selly struct {
httpClient *tgun.Client
Email string
Token string
UserAgent string // "Yourusername - website-using-api.com"
}
// New creates a new Selly instance
func New(email, token, useragent string) *Selly {
if useragent == "" {
useragent = DefaultUserAgent
}
httpclient := &tgun.Client{
UserAgent: useragent,
AuthUser: email,
AuthPassword: token,
// Headers: map[string]string{
// "Content-Type": "application/javascript",
// },
}
return &Selly{
httpClient: httpclient,
}
}
// NewProxy creates a new Selly instance using proxy for requests
// Proxy format: socks5://127.0.0.1:1080
func NewProxy(email, token, useragent, proxy string) *Selly {
s := New(email, token, useragent)
s.httpClient.Proxy = proxy
return s
}
type Gateway string
const (
Litecoin Gateway = "Litecoin"
Bitcoin Gateway = "Bitcoin"
Paypal Gateway = "Paypal"
Ethereum Gateway = "Ethereum"
Ripple Gateway = "Ripple"
Dash Gateway = "Dash"
BitcoinCash Gateway = "Bitcoin Cash"
)
type ErrorResponse struct {
Message []string `json:"message"`
Errors struct {
Title []string `json:"title"`
} `json:"errors"`
}
func (e ErrorResponse) Error() string {
return e.String()
}
func (e ErrorResponse) String() string {
if len(e.Errors.Title) == 0 {
return strings.Join(e.Message, ";")
}
return fmt.Sprintf("%s: %s", e.Message, e.Errors.Title)
}
// Product ...
type Product struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Stock int `json:"stock"`
Price string `json:"price"`
Currency string `json:"currency"`
ProductType int `json:"product_type"`
Bitcoin bool `json:"bitcoin"`
Paypal bool `json:"paypal"`
Stripe bool `json:"stripe"`
Litecoin bool `json:"litecoin"`
Dash bool `json:"dash"`
Ethereum bool `json:"ethereum"`
PerfectMoney bool `json:"perfect_money"`
BitcoinCash bool `json:"bitcoin_cash"`
Ripple bool `json:"ripple"`
Private bool `json:"private"`
Unlisted bool `json:"unlisted"`
SellerNote string `json:"seller_note"`
MaximumQuantity interface{} `json:"maximum_quantity"`
MinimumQuantity int `json:"minimum_quantity"`
Custom struct{} `json:"custom"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// ProductGroup ...
type ProductGroup struct {
ID string `json:"id"`
Title string `json:"title"`
ProductIds []string `json:"product_ids"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// Order ...
type Order struct {
ID string `json:"id"`
ProductID string `json:"product_id"`
Email string `json:"email"`
IPAddress string `json:"ip_address"`
CountryCode string `json:"country_code"`
UserAgent string `json:"user_agent"`
Value string `json:"value"`
Currency string `json:"currency"`
Gateway Gateway `json:"gateway,string"`
RiskLevel int `json:"risk_level"`
Status int `json:"status"`
Delivered string `json:"delivered"`
CryptoValue interface{} `json:"crypto_value"`
CryptoAddress interface{} `json:"crypto_address"`
Referral interface{} `json:"referral"`
UsdValue string `json:"usd_value"`
ExchangeRate string `json:"exchange_rate"`
Custom map[string]string `json:"custom"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// Coupon ...
type Coupon struct {
ID int `json:"id"`
Code string `json:"code"`
Discount int `json:"discount"`
MaxUses interface{} `json:"max_uses"`
ProductIds []string `json:"product_ids"`
Uses int `json:"uses"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// Query ...
type Query struct {
ID string `json:"id"`
Secret string `json:"secret"`
Title string `json:"title"`
Email string `json:"email"`
Message string `json:"message"`
Status int `json:"status"`
CountryCode string `json:"country_code"`
IPAddress string `json:"ip_address"`
AvatarURL string `json:"avatar_url"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type Request struct {
Title string `json:"title"`
Gateway Gateway `json:"gateway,string"`
Email string `json:"email"`
Value string `json:"value"`
Currency string `json:"currency"`
ReturnURL string `json:"return_url"`
WebhookURL string `json:"webhook_url"`
}
type Webhook struct {
ID string `json:"id"`
ProductID string `json:"product_id"`
Email string `json:"email"`
IPAddress string `json:"ip_address"`
CountryCode string `json:"country_code"`
UserAgent string `json:"user_agent"`
Value string `json:"value"`
Currency string `json:"currency"`
Gateway Gateway `json:"gateway,string"`
RiskLevel int `json:"risk_level"`
Status int `json:"status"`
Delivered string `json:"delivered"`
CryptoValue interface{} `json:"crypto_value"`
CryptoAddress interface{} `json:"crypto_address"`
Referral string `json:"referral"`
WebhookType int `json:"webhook_type"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// Payments can be created or deleted
type Payment struct {
Title string `json:"title"`
Gateway Gateway `json:"gateway, string"`
Email string `json:"email"`
Value string `json:"value"`
Currency string `json:"currency"`
ReturnURL string `json:"return_url"`
WebhookURL string `json:"webhook_url"`
Confirmations int `json:"confirmations"`
}
// PaymentResponse ...
type PaymentResponse struct {
URL string `json:"url"`
ErrorMessage []string `json:"message"` // Error Messages
}
// GetProduct returns product
func (s *Selly) GetProduct(id string) (*Product, error) {
furl := "https://selly.gg/api/v2/products/%s"
b, err := s.httpClient.GetBytes(fmt.Sprintf(furl, id))
if err != nil {
return nil, err
}
p := Product{}
err = json.Unmarshal(b, &p)
if err != nil {
errar := ErrorResponse{}
err = json.Unmarshal(b, &errar)
return nil, errar
}
return &p, err
}
// GetProducts returns all products
func (s *Selly) GetProducts() ([]Product, error) {
furl := "https://selly.gg/api/v2/products"
b, err := s.httpClient.GetBytes(furl)
if err != nil {
return nil, err
}
p := []Product{}
err = json.Unmarshal(b, &p)
if err != nil {
errar := ErrorResponse{}
err = json.Unmarshal(b, &errar)
return nil, errar
}
return p, err
}
// GetOrder returns specific orders
func (s *Selly) GetOrder(id string) (*Order, error) {
furl := "https://selly.gg/api/v2/orders/%s"
b, err := s.httpClient.GetBytes(fmt.Sprintf(furl, id))
if err != nil {
return nil, err
}
o := Order{}
err = json.Unmarshal(b, &o)
if err != nil {
errar := ErrorResponse{}
err = json.Unmarshal(b, &errar)
return nil, errar
}
return &o, err
}
// GetOrders returns all orders
func (s *Selly) GetOrders() ([]Order, error) {
furl := "https://selly.gg/api/v2/orders"
b, err := s.httpClient.GetBytes(furl)
if err != nil {
return nil, err
}
o := []Order{}
err = json.Unmarshal(b, &o)
if err != nil {
errar := ErrorResponse{}
err = json.Unmarshal(b, &errar)
return nil, errar
}
return o, err
}
// GetCoupon returns specific coupon
func (s *Selly) GetCoupon(id string) (*Coupon, error) {
furl := "https://selly.gg/api/v2/coupons/%s"
b, err := s.httpClient.GetBytes(fmt.Sprintf(furl, id))
if err != nil {
return nil, err
}
c := Coupon{}
err = json.Unmarshal(b, &c)
if err != nil {
errar := ErrorResponse{}
err = json.Unmarshal(b, &errar)
return nil, errar
}
return &c, err
}
// GetCoupons returns all coupons
func (s *Selly) GetCoupons() ([]Coupon, error) {
furl := "https://selly.gg/api/v2/coupons"
b, err := s.httpClient.GetBytes(furl)
if err != nil {
return nil, err
}
c := []Coupon{}
err = json.Unmarshal(b, &c)
if err != nil {
errar := ErrorResponse{}
err = json.Unmarshal(b, &errar)
return nil, errar
}
return c, err
}
// NewCoupon creates a new coupon and returns a coupon or error
func (s *Selly) NewCoupon(code string, discount int, productIDs []string) (*Coupon, error) {
url := "https://selly.gg/api/v2/coupons"
coupon := Coupon{
Discount: discount,
Code: code,
ProductIds: productIDs,
}
data, err := json.Marshal(coupon)
if err != nil {
return nil, err
}
resp, err := s.postreq(url, data)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &coupon)
if err != nil {
errar := ErrorResponse{}
err = json.Unmarshal(body, &errar)
return nil, errar
}
return &coupon, err
}
func (s *Selly) postreq(url string, data interface{}) (*http.Response, error) {
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
body := bytes.NewReader(b)
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
return s.httpClient.Do(req)
}
// StatusString returns the order's status
func (o Order) StatusString() (msg string) {
switch o.Status {
case 0:
msg = "No payment has been received"
case 51:
msg = "PayPal dispute/reversal"
case 52:
msg = "Order blocked due to risk level exceeding the maximum for the product"
case 53:
msg = "Partial payment. When crypto currency orders do not receive the full amount required due to fees, etc."
case 54:
msg = "Crypto currency transaction confirming"
//api docs have duplicate 55
//case 55:
//msg = "Payment pending on PayPal. Most commonly due to e-checks."
case 55:
msg = "Refunded"
case 100:
msg = "Payment complete"
default:
msg = "Unknown Status Code"
}
return msg
}
// DeletePayment deletes a payment url, need ID (first section of url)
func (s *Selly) DeletePayment(id string) error {
furl := fmt.Sprintf("https://selly.gg/api/v2/pay/%s", id)
// "81971eae19ff0924026d7b2a7502b20372c15df5"
req, err := http.NewRequest(http.MethodDelete, furl, nil)
if err != nil {
return err
}
resp, err := s.httpClient.Do(req)
deleteResponse := struct {
Status string `json:"status"`
// unknown other fields
}{}
err = json.NewDecoder(resp.Body).Decode(deleteResponse)
if deleteResponse.Status != "true" {
return fmt.Errorf("bad response: %s", deleteResponse.Status)
}
return nil
}
// Pay returns a checkout link.
// If paymentresponse.URL is empty, check pay.ErrorMessage
func (s *Selly) Pay(payment Payment) PaymentResponse {
url := "https://selly.gg/api/v2/pay"
// debug
//data = []byte(`{"title":"Selly Pay Example", "gateway":"Bitcoin", "email":"[email protected]", "value":"10", "currency":"USD", "return_url":"https://website.com/return", "webhook_url":"https://website.com/webhook?secret=cEZMeEVlTz"}`)
resp, err := s.postreq(url, payment)
if err != nil {
return PaymentResponse{ErrorMessage: []string{err.Error()}}
}
paymentResponse := PaymentResponse{}
err = json.NewDecoder(resp.Body).Decode(&paymentResponse)
if err != nil {
return PaymentResponse{ErrorMessage: []string{err.Error()}}
}
return paymentResponse
}