-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlicgen.aspx.vb
331 lines (238 loc) · 9.33 KB
/
licgen.aspx.vb
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
#Region "References"
Imports System.IO
Imports System.Security.Cryptography
Imports System.Xml
#End Region
''' <summary>
''' 2Checkout License Generator
''' Copyright (c) 2019 Smart PC Utilities, Ltd.
''' </summary>
Public Class LicGen
Inherits Page
#Region "Declarations"
Private _orderInfo As OrderInfo
Const MY_SECRET_KEY As String = "2Checkout Secret Key"
#End Region
#Region "Page Events"
Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
'Check HTTP POST
If Request.Form.AllKeys.Count = 0 Then
SendResponse(400, "Null HTTP POST")
Return
End If
'Get order information
_orderInfo = New OrderInfo(Request.Form)
'Validate order signature
If _orderInfo.HmacSignature = _orderInfo.ToCheckoutSignature Then
If Not _orderInfo.IsTestOrder Then 'Non-test order
'Keys to output
Dim keys As New List(Of String)
OutputLicKey(keys)
Else 'Test order
Dim keys As New List(Of String)
For I = 0 To _orderInfo.ProductQuantity - 1
keys.Add("Test-Test-Test-Test-Test")
Next
OutputLicKey(keys)
End If
Else
SendResponse(400, "Invalid Signature")
Return
End If
End Sub
#End Region
#Region "Helper Methods"
Private Sub OutputLicKey(keys As List(Of String))
Dim xSettings = New XmlWriterSettings()
xSettings.Encoding = New UTF8Encoding(False)
xSettings.ConformanceLevel = ConformanceLevel.Document
xSettings.Indent = True
Dim xMemoryStream = New MemoryStream()
Using xWriter As XmlWriter = XmlWriter.Create(xMemoryStream, xSettings)
With xWriter
xWriter.WriteStartDocument()
xWriter.WriteStartElement("data")
For Each key As String In keys
.WriteElementString("code", key)
Next
xWriter.WriteEndElement()
xWriter.WriteEndDocument()
End With
End Using
SendResponse(200, Encoding.UTF8.GetString(xMemoryStream.ToArray()), "text/xml")
End Sub
Private Shared Sub SendResponse(Optional responseCode As Integer = 200, Optional responseContent As String = "", Optional responseContentType As String = "text/plain")
With HttpContext.Current
.Response.StatusCode = responseCode
.Response.Clear()
If Not String.IsNullOrWhiteSpace(responseContent) Then
.Response.ContentEncoding = Encoding.UTF8
.Response.ContentType = responseContentType
.Response.Output.Write(responseContent)
End If
.Response.Flush()
.Response.SuppressContent = True
.ApplicationInstance.CompleteRequest()
End With
End Sub
Friend Shared Function MyHmacMd5(msg As String, key As String) As String
Dim encoding = New ASCIIEncoding()
Dim msgByte As Byte() = encoding.GetBytes(msg)
Dim keyByte As Byte() = encoding.GetBytes(key)
Dim hmacMd5 = New HMACMD5(keyByte)
Dim msgHash As Byte() = hmacMd5.ComputeHash(msgByte)
Dim strBldHmac As New StringBuilder
For i = 0 To msgHash.Length - 1
strBldHmac.Append(msgHash(i).ToString("x2"))
Next
Return (strBldHmac.ToString)
End Function
#End Region
#Region "Structures"
Private Structure OrderInfo
#Region "Members"
''' <summary>
''' Whether the order is a test order or not
''' </summary>
Public ReadOnly IsTestOrder As Boolean
''' <summary>
''' 2Checkout order reference number
''' </summary>
Public ReadOnly RefNumber As Integer
''' <summary>
''' Custom order reference number
''' </summary>
Public ReadOnly RefNumberExt As Integer
''' <summary>
''' 2Checkout Product Id
''' </summary>
Public ReadOnly ProductId As Integer
''' <summary>
''' Product Code
''' </summary>
Public ReadOnly ProductCode As String
''' <summary>
''' Product SKU
''' </summary>
Public ProductSku As String
''' <summary>
''' Ordered product quantity
''' </summary>
Public ReadOnly ProductQuantity As Integer
''' <summary>
''' Shopper first name
''' </summary>
Public ReadOnly FirstName As String
''' <summary>
''' Shopper last name
''' </summary>
Public ReadOnly LastName As String
''' <summary>
''' Shopper company name
''' </summary>
Public ReadOnly Company As String
''' <summary>
''' Shopper fax number
''' </summary>
Public ReadOnly Fax As String
''' <summary>
''' Shopper language (ISO 639-1 two-letter code)
''' </summary>
Public LangCode As String
''' <summary>
''' Shopper email address
''' </summary>
Public ReadOnly Email As String
''' <summary>
''' Shopper phone number
''' </summary>
Public ReadOnly Phone As String
''' <summary>
''' Shopper country
''' </summary>
Public ReadOnly Country As String
''' <summary>
''' ISO code for the shopper country
''' </summary>
Public ReadOnly CountryCode As String
''' <summary>
''' Shopper zip code
''' </summary>
Public ReadOnly ZipCode As String
''' <summary>
''' Shopper city
''' </summary>
Public ReadOnly City As String
''' <summary>
''' Shopper address
''' </summary>
Public ReadOnly Address As String
''' <summary>
''' License type (REGULAR, TRIAL, RENEWAL, UPGRADE)
''' </summary>
Public LicenseType As String
''' <summary>
''' Partner code (If empty = e-commerce order)
''' </summary>
Public PartnerCode As String
''' <summary>
''' Md5 HMAC hash signature provided by 2Checkout
''' </summary>
Public ReadOnly ToCheckoutSignature As String
''' <summary>
''' Md5 HMAC hash signature calculated using all the fields sent through HTTP/HTTPS POST
''' </summary>
Public ReadOnly HmacSignature As String
#End Region
#Region "Methods"
Public Sub New(htmlPost As NameValueCollection)
If htmlPost("TESTORDER").Equals("YES", StringComparison.InvariantCultureIgnoreCase) Then
IsTestOrder = True
Else
IsTestOrder = False
End If
If Not Integer.TryParse(htmlPost("REFNO"), RefNumber) Then
RefNumber = 0
End If
If Not Integer.TryParse(htmlPost("REFNOEXT"), RefNumberExt) Then
RefNumberExt = 0
End If
If Not Integer.TryParse(htmlPost("PID"), ProductId) Then
ProductId = 0
End If
ProductCode = htmlPost("PCODE")
ProductSku = htmlPost("PSKU")
If Not Integer.TryParse(htmlPost("QUANTITY"), ProductQuantity) Then
ProductQuantity = 1
End If
FirstName = htmlPost("FIRSTNAME")
LastName = htmlPost("LASTNAME")
Company = htmlPost("COMPANY")
Fax = htmlPost("FAX")
LangCode = htmlPost("LANG")
Email = htmlPost("EMAIL")
Phone = htmlPost("PHONE")
Country = htmlPost("COUNTRY")
CountryCode = htmlPost("COUNTRY_CODE")
ZipCode = htmlPost("ZIPCODE")
City = htmlPost("CITY")
Address = htmlPost("ADDRESS")
LicenseType = htmlPost("LICENSE_TYPE")
PartnerCode = htmlPost("PARTNER_CODE")
ToCheckoutSignature = htmlPost("HASH")
Dim paramsValue As String = (From key In htmlPost.AllKeys Where key <> "HASH").Aggregate("",
Function _
(current,
key) _
current &
(htmlPost(
key).
Length &
htmlPost(
key)))
HmacSignature = MyHmacMd5(paramsValue, MY_SECRET_KEY)
End Sub
#End Region
End Structure
#End Region
End Class