Imports PaymentKeys.API_Tookit
’Define a command object for populating command parametersDim PK_Command
As New PK_PaymentKeyAdmin_Command
’Build the command to send to the PaymentKeys Application FrameworkPK_Command.command =
"paymentkey.activate"PK_Command.version =
"1.0"
PK_Command.api_call_id = Guid.NewGuid.ToString
PK_Command.paymentkey =
"v1111_00000_00000_00000.pk"’Serialize the command object to a JSON stringDim serializer
As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim api_call As String = serializer.Serialize(PK_Command)
’Create HMACSHA1 signature hash for the JSON command string using the GatewayCodeDim sha1
As New System.Security.Cryptography.HMACSHA1()
’Specify the GatewayCode secret keysha1.Key = System.Text.Encoding.UTF8.GetBytes(
"PK_Demo")
’Convert the JSON command string to a byte arrayDim byteArray
As Byte() = System.Text.Encoding.UTF8.GetBytes(api_call)
’Generate the hash signatureDim api_sig
As String = Convert.ToBase64String(sha1.ComputeHash(byteArray))
'Initialize .Net web request variablesDim PostURL
As String =
"https://www.paymentkeys.com/api.rest/appserver"Dim http_request As
HttpWebRequest = CType(WebRequest.Create(PostURL), HttpWebRequest)
'Define the data to postDim postData
As New StringBuilder
postData.Append(
"api_KeyID=PaymentKeys_Demo.pk")
postData.Append(
"&api_sig=" & Server.UrlEncode(api_sig))
postData.Append(
"&api_call=" & Server.UrlEncode(api_call))
postData.Append(
"&api_output=json")
’Encode the post data into a byte arrayDim encodedData
As New System.Text.UTF8Encoding
Dim postData_byteArray
As Byte() = encodedData.GetBytes(postData.ToString)
’Set http request headershttp_request.Method =
"POST"http_request.ContentType =
"application/x-www-form-urlencoded"http_request.ContentLength = postData_byteArray.Length
’Stream the post data to the PaymentKeys application gatewayDim request_stream
As Stream = http_request.GetRequestStream()
request_stream.Write(postData_byteArray, 0, postData_byteArray.Length)
request_stream.Close()
’Process the responseDim http_response
As HttpWebResponse =
NothingDim response_stream
As StreamReader =
NothingDim JSON_Response
As String =
String.Empty
Tryhttp_response = CType(http_request.GetResponse(), HttpWebResponse)
response_stream = New StreamReader(http_response.GetResponseStream(), Encoding.UTF8)
JSON_Response = response_stream.ReadToEnd()
Catch Ex As WebException
’Handle http communication errors here
Finally
If Not http_response Is Nothing Then
http_response.Close()
http_response = Nothing
End If
End Try
’Deserialize JSON response string into response objectIf Not JSON_Response =
String.Empty
ThenDim PK_Response As PK_PaymentKey_Activate_Response
PK_Response = serializer.Deserialize(Of PK_PaymentKey_Activate_Response)(JSON_Response)
’Parse the Response per your application and policy rules
Select Case PK_Response.status
Case ...
End If