-
Notifications
You must be signed in to change notification settings - Fork 1
/
pingback.go
198 lines (168 loc) · 4.26 KB
/
pingback.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
package paymentwall
import (
"bytes"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"hash"
"net"
"net/url"
"sort"
)
// The whitelisted start and end range of which Paymentwall callbacks are permissible to come from.
const (
ipWhitelistStart = "216.127.71.0" // Start
ipWhitelistEnd = "216.127.71.255" // End
)
// https://docs.paymentwall.com/reference/pingback-home
func NewPingback(
values url.Values,
ip string, apiType ApiType, secretKey string) *Pingback {
p := Pingback{
m: make(map[string]string, len(values)),
keys: make([]string, 0, len(values)),
signVersion: DefaultSignVersion,
IsTest: false,
ip: ip,
apiType: apiType,
secretKey: secretKey,
errors: make([]error, 0, 2),
}
for k := range values {
v := values.Get(k)
if k == "sign_version" {
p.signVersion = v
} else if k == "is_test" && v == "1" {
p.IsTest = true
}
p.set(k, v)
}
return &p
}
type Pingback struct {
keys []string
m map[string]string
signVersion string
IsTest bool
ip string
apiType ApiType
secretKey string
errors []error
}
func (p *Pingback) set(key, value string) {
p.m[key] = value
p.keys = append(p.keys, key)
}
func (p *Pingback) appendToError(errMsg string) {
p.errors = append(p.errors, errors.New(errMsg))
}
func (p *Pingback) GetError() error {
return p.errors[0]
}
func (p *Pingback) GetErrors() []error {
return p.errors
}
func (p *Pingback) Validate(skipIPCheck bool) bool {
var validated bool
if p.IsParametersValid() {
if p.IsIPValid() || skipIPCheck {
if p.IsSignatureValid() {
validated = true
} else {
p.appendToError("Wrong signature")
}
} else {
p.appendToError("IP address is not whitelisted")
}
}
return validated
}
func (p *Pingback) IsParametersValid() bool {
var requiredParams []string
if p.apiType == API_VC {
requiredParams = []string{"uid", "type", "ref", "sig", "sign_version", "currency"}
} else if p.apiType == API_GOODS {
requiredParams = []string{"uid", "type", "ref", "sig", "sign_version", "goodsid"}
}
for _, k := range requiredParams {
if _, ok := p.m[k]; !ok {
p.appendToError(fmt.Sprintf("Parameter %s is missing.", k))
return false
}
}
return true
}
// IsIPValid checks to ensure the IP from which the pingback was received is within the allowed range of whitelisted
// IPs provided by Paymentwall.
// @see: https://docs.paymentwall.com/reference/pingback-new-ip-subnet
func (p *Pingback) IsIPValid() bool {
whitelistStart := net.ParseIP(ipWhitelistStart)
whitelistEnd := net.ParseIP(ipWhitelistEnd)
reqIP := net.ParseIP(p.ip)
// Ensure IP is IPv4 only.
if reqIP.To4() == nil {
return false
}
// Check if IP is within range of whitelisted IPs.
if bytes.Compare(reqIP, whitelistStart) >= 0 && bytes.Compare(reqIP, whitelistEnd) <= 0 {
return true
}
return false
}
func (p *Pingback) IsSignatureValid() bool {
var h hash.Hash
if p.signVersion == SignVersion3 {
h = sha256.New()
} else {
h = md5.New()
}
sort.Strings(p.keys)
baseString := ""
for _, k := range p.keys {
if k == "sig" {
continue
}
baseString += fmt.Sprintf(`%s=%s`, k, p.m[k])
}
baseString += p.secretKey
h.Write([]byte(baseString))
return hex.EncodeToString(h.Sum(nil)) == p.m["sig"]
}
func (p *Pingback) Get(key string) string {
return p.m[key]
}
func (p *Pingback) GetType() PingbackType {
return PingbackType(p.Get("type"))
}
// Unique identifier of the user. The value of parameter “uid” from Widget call.
func (p *Pingback) GetUID() string {
return p.Get("uid")
}
func (p *Pingback) GetVCAmount() string {
return p.Get("currency")
}
func (p *Pingback) GetProductID() string {
return p.Get("goodsid")
}
func (p *Pingback) GetProductPeriod() (length string, period string) {
return p.Get("slength"), p.Get("speriod")
}
func (p *Pingback) GetReferenceID() string {
return p.Get("ref")
}
func (p *Pingback) IsDeliverable() bool {
type_ := p.GetType()
return type_ == PingbackTypeRegular ||
type_ == PingbackTypeGoodwill ||
type_ == PingbackTypeRiskReviewedAccepted
}
func (p *Pingback) IsCancelable() bool {
type_ := p.GetType()
return type_ == PingbackTypeNegative ||
type_ == PingbackTypeRiskReviewedDeclined
}
func (p *Pingback) IsUnderReview() bool {
return p.GetType() == PingbackTypeRiskUnderReview
}