2 posts / 0 new
Last post
dennymunson5750
VB Code In Code Behind Returning 401 Not Authorized

I have this code that I converted from C# in a code behind of a page.

Imports System.Net
Imports System.Text
Imports System
Imports System.Security.Cryptography
Imports System.Globalization
Partial Class catalog
Inherits System.Web.UI.Page

Private Sub TestPayeezy()
Dim builder = New StringBuilder("{ " + """merchant_ref"": ""Acme Sock"", " + """transaction_type"": ""authorize"", " + """method"": ""credit_card"", " + """amount"": ""1299"", " + """currency_code"": ""USD"", " + """credit_card"": { " + " ""type"": ""visa"", " + " ""cardholder_name"": ""John Smith"", " + " ""card_number"": ""4788250000028291"", " + " ""exp_date"": ""1020"", " + " ""cvv"": ""123"" " + "} " + "}")
Dim payload = builder.ToString()
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(payload)
Dim apiKey = "XXXXXXXXXXXXXXXXXXXXXX"
Dim timeStamp = CLng((DateTime.UtcNow - New DateTime(1970, 1, 1)).TotalMilliseconds).ToString(CultureInfo.InvariantCulture)
Dim nonce = (10000000000000000000UL * New Random(DateTime.Now.Millisecond).NextDouble()).ToString("0000000000000000000")
Dim merchantToken = "xxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Dim secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

Dim post = DirectCast(HttpWebRequest.Create("https://api.demo.globalgatewaye4.firstdata.com/transaction/v12"), HttpWebRequest)
post.Method = "POST"
post.KeepAlive = True
post.Accept = "*/*"
post.Headers.Add("Accept-Encoding", "gzip")
post.Headers.Add("Accept-Language", "en-US")
post.Headers.Add("apikey", apiKey)
post.Headers.Add("nonce", nonce)
post.Headers.Add("timestamp", timeStamp)
Dim authorize = CreateHMAC(apiKey, secretKey, merchantToken, payload, nonce, timeStamp)
post.Headers.Add("Authorization", authorize)
post.ContentType = "application/json"
post.Headers.Add("token", merchantToken)
post.UserAgent = Request.UserAgent.ToString
post.GetRequestStream().Write(byteArray, 0, byteArray.Length)
Dim response = post.GetResponse()
Dim reader = New StreamReader(response.GetResponseStream())
Dim responseFromServer = reader.ReadToEnd()
'Me.write(responseFromServer.ToString)
End Sub

Public Shared Function CreateHMAC(apiKey As String, apiSecret As String, token As String, payload As String, nonce As String, timeStamp As String) As String
Dim hmacData = Convert.ToString(Convert.ToString(Convert.ToString(apiKey & nonce) & timeStamp) & token) & payload
Dim hmac = New HMACSHA256(Encoding.UTF8.GetBytes(apiSecret))
Dim encBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(hmacData))
Dim encStr = ByteArrayToHexString(encBytes)
Return Convert.ToBase64String(Encoding.UTF8.GetBytes((encStr)))
End Function

Public Shared Function ByteArrayToHexString(ba As Byte()) As String
Dim hex As New StringBuilder(ba.Length * 2)
For Each b As Byte In ba
hex.AppendFormat("{0:x2}", b)
Next
Return hex.ToString()
End Function

Protected Sub LinkButton1_Click(sender As Object, e As System.EventArgs) Handles LinkButton1.Click
TestPayeezy()
End Sub
End Class

Where do I get the Merchant Token, API Key and Secret Key so I can test this code?


rohitrajagopal3402
Re: VB Code In Code Behind Returning 401 Not Authorized

Hi Denny,

Please go through our getting started guide. It explains how to get the api key, api secret and merchant token.

https://github.com/payeezy/get_started_with_payeezy/blob/master/get_started_with_payeezy042015.pdf

In the code that you pasted, the URL is incorrect. The correct URL is mentioned in the guide itself.

Let me know if you have more questions.

Regards,

Rohit