-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
138 lines (115 loc) · 4.16 KB
/
utils.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
package client
import (
"errors"
"net/url"
"time"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
)
//NewChargeRequest create a new ChargeRequest performing validations on parameters as specified in Satispay API documentation https://s3-eu-west-1.amazonaws.com/docs.online.satispay.com/index.html#create-a-charge
func NewChargeRequest(userID, description, currency, callbackURL string, metadata map[string]string, requiredSuccessEmail bool, amount int64, expireIn int) (*ChargeRequest, error) {
_, err := uuid.FromString(userID)
if err != nil {
log.Errorf("UserID is not a valid UUID: %v", err)
return nil, err
}
if currency != "EUR" { //Temporary check, only EURO is supported for currency
log.Error("Only EUR is supported for currency by Satispay Platform")
return nil, errors.New("Only EUR is supported for currency by Satispay Platform")
}
if expireIn < 0 {
log.Error("Invalid value for expiration time")
return nil, errors.New("Invalid value for expiration time")
}
if amount < 0 {
log.Error("Invalid value for amount")
return nil, errors.New("Invalid value for amount")
}
if _, err := url.ParseRequestURI(callbackURL); err != nil {
log.Errorf("Got error validating callback url %v", err)
return nil, err
}
if expireIn < 60 {
log.Error("Error: expiration time must be at least 60 seconds, using default one (15 minutes, 900 seconds)")
expireIn = 900
}
return &ChargeRequest{Amount: amount, CallBackURL: callbackURL, Currency: currency, Description: description, ExpireIn: expireIn, Metdata: metadata, RequiredSuccessEmail: requiredSuccessEmail, UserID: userID}, nil
}
//NewRefundRequest create a new refund request structure performing validation described in Satispay API https://s3-eu-west-1.amazonaws.com/docs.online.satispay.com/index.html#create-a-refund
func NewRefundRequest(chargeID, description, currency string, amount int64, reason RefundReason, metadata map[string]string) (*RefundRequest, error) {
if _, err := uuid.FromString(chargeID); err != nil {
log.Errorf("Invalid charge id %v", err)
return nil, err
}
if len(description) > 255 {
log.Errorf("Description to long!")
return nil, errors.New("Description to long")
}
if currency != "EUR" {
log.Errorf("Invalid currency only EUR is supported (until now)")
return nil, errors.New("Invalid currency only EUR is supported (until now)")
}
if amount < 0 {
log.Errorf("Negative amount")
return nil, errors.New("Negative amount")
}
return &RefundRequest{
Amount: amount,
ChargeID: chargeID,
Currency: currency,
Description: description,
Metadata: metadata,
Reason: reason,
}, nil
}
//NewCheckoutRequest create a new request for checkout
func NewCheckoutRequest(phoneNumber, redirectURL, description, callbackURL, currency string, amount int64) (*CheckoutRequest, error) {
if phoneNumber == "" {
log.Errorf("Phone number required")
return nil, errors.New("Phone number required")
}
if amount < 0 {
log.Errorf("Amount or expiration time negative")
return nil, errors.New("Amount or expiration time negative")
}
if currency != "EUR" {
log.Errorf("Only EUR currency is supported for the moment")
return nil, errors.New("Only EUR currency is supported for the moment")
}
return &CheckoutRequest{
AmountUnit: amount,
CallbackURL: callbackURL,
Currency: currency,
Description: description,
PhoneNumber: phoneNumber,
RedirectURL: redirectURL,
}, nil
}
func composeURL(limit int, initialURL, startingAfter, endingBefore string) string {
url := initialURL + "?"
if limit > 0 && limit < 100 {
url += "limit=" + string(limit)
} else {
url += "limit=20"
}
if startingAfter != "" {
_, err := uuid.FromString(startingAfter)
if err != nil {
log.Errorf("Invalid initial UUID, parsing error: %v\nIgnoring parameter", err)
} else {
url += "&starting_after=" + startingAfter
}
}
if endingBefore != "" {
_, err := uuid.FromString(endingBefore)
if err != nil {
log.Errorf("Invalid ending UUID, parsing error: %v\nIgnoring parameter", err)
} else {
url += "&ending_before=" + endingBefore
}
}
return url
}
func makeTimestamp(date time.Time) int64 {
return date.UnixNano() / int64(time.Millisecond)
}