-
Notifications
You must be signed in to change notification settings - Fork 1
/
widget.go
228 lines (199 loc) · 4.99 KB
/
widget.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
package paymentwall
import (
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"hash"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
const (
baseUrl = "https://api.paymentwall.com/api"
VC_CONTROLLER = "ps"
GOODS_CONTROLLER = "subscription"
CART_CONTROLLER = "cart"
)
var (
ErrorOnlyOneProductAllowed = errors.New("only one product is allowed when ApiType is API_GOODS")
)
func NewWidget(
appKey, secretKey string,
apiType ApiType,
uid, widgetCode, email string,
skipSignature bool) *Widget {
w := &Widget{
appKey: appKey,
secretKey: secretKey,
apiType: apiType,
uid: uid,
code: widgetCode,
email: email,
ps: "all",
skipSignature: skipSignature,
}
return w
}
type Widget struct {
appKey string
secretKey string
apiType ApiType
skipSignature bool
uid string
code string // widget code
email string
ps string
products []Product
extraParams map[string]string
}
func (w *Widget) AppendProduct(products ...Product) error {
if len(w.products) == 0 {
w.products = make([]Product, 0, len(products))
}
if w.apiType == API_GOODS {
if len(products) > 1 {
return ErrorOnlyOneProductAllowed
}
if len(w.products) == 1 {
return ErrorOnlyOneProductAllowed
}
}
w.products = append(w.products, products...)
return nil
}
func (w *Widget) SetPS(ps string) {
w.ps = ps
}
func (w *Widget) SetCallbackUrl(successUrl, failureUrl string) {
w.SetExtraParam("success_url", successUrl)
w.SetExtraParam("failure_url", failureUrl)
}
func (w *Widget) SetExtraParams(m map[string]string) {
for k, v := range m {
w.SetExtraParam(k, v)
}
}
func (w *Widget) SetExtraParam(k, v string) {
if w.extraParams == nil {
w.extraParams = make(map[string]string)
}
w.extraParams[k] = v
}
func (w *Widget) GetHtmlCode(attributes map[string]string) string {
defaultAttributes := map[string]string{
"frameborder": "0",
"width": "750",
"height": "800",
}
for k, v := range attributes {
defaultAttributes[k] = v
}
attrsSlice := make([]string, 0, len(defaultAttributes))
for attr, value := range defaultAttributes {
attrsSlice = append(attrsSlice,
fmt.Sprintf(`%s="%s"`, attr, value))
}
return fmt.Sprintf(`<iframe src="%s" %s></iframe>`,
w.GetUrl(), strings.Join(attrsSlice, " "))
}
func (w *Widget) GetUrl() string {
return baseUrl + "/" + w.buildController() + "?" + w.getParams().Encode()
}
func (w *Widget) getDefaultWidgetSignature() string {
if w.apiType != API_CART {
return DefaultSignVersion
} else {
return SignVersion2
}
}
func (w *Widget) buildController() string {
if w.apiType == API_VC {
return VC_CONTROLLER
} else if w.apiType == API_GOODS {
return GOODS_CONTROLLER
} else {
return CART_CONTROLLER
}
}
func (w *Widget) mergeSignVersion() string {
signVersion := w.getDefaultWidgetSignature()
if s, ok := w.extraParams["sign_version"]; ok {
signVersion = s
}
return signVersion
}
func (w *Widget) mergeExtraParams(params url.Values) {
for k, v := range w.extraParams {
params.Set(k, v)
}
}
func (w *Widget) getParams() url.Values {
params := url.Values{}
params.Set("key", w.appKey)
params.Set("uid", w.uid)
params.Set("widget", w.code)
params.Set("email", w.email)
params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("ps", w.ps)
if len(w.products) > 0 {
if w.apiType == API_GOODS {
product := w.products[0]
params.Set("amount", product.DisplayAmount())
params.Set("currencyCode", product.Currency)
params.Set("ag_name", product.Name)
params.Set("ag_external_id", product.Identity)
params.Set("ag_type", string(product.Type))
if product.Type == ProductTypeSubscription {
params.Set("ag_period_length", product.DisplayPeriodLength())
params.Set("ag_period_type", string(product.PeriodType))
if product.Recurring {
params.Set("ag_recurring", "1")
// TODO: add trial product
}
}
} else if w.apiType == API_CART {
for i, product := range w.products {
params.Set(fmt.Sprintf("external_ids[%d]", i), product.Identity)
if product.Amount > 0 {
params.Set(fmt.Sprintf("prices[%d]", i), product.DisplayAmount())
}
if product.Currency != "" {
params.Set(fmt.Sprintf("currencies[%d]", i), product.Currency)
}
}
}
}
w.mergeExtraParams(params)
if !w.skipSignature {
signVersion := w.mergeSignVersion()
params.Set("sign_version", signVersion)
params.Set("sign", w.calculateSignature(params, signVersion))
} else {
params.Del("sign_version")
}
return params
}
func (w *Widget) calculateSignature(params url.Values, signVersion string) string {
keys := make([]string, 0, len(params))
for k := range params {
keys = append(keys, k)
}
var h hash.Hash
if signVersion == SignVersion3 {
h = sha256.New()
} else {
h = md5.New()
}
sort.Strings(keys)
baseString := ""
for _, k := range keys {
baseString += fmt.Sprintf("%s=%s", k, params.Get(k))
}
baseString += w.secretKey
h.Write([]byte(baseString))
return hex.EncodeToString(h.Sum(nil))
}