function b64encode (input) {
// Converts each character in the input to its Unicode number,
// then writes out the Unicode numbers in binary, one after
// another, into a string. This string is then split up at
// every 6th character, these substrings are then converted back
// into binary integers and are used to subscript the "swaps"
// array. Since this would create HUGE strings of 1s and 0s,
// the distinct steps above are actually interleaved in the code
// below (ie. the long binary string, called "input_binary",
// gets processed while it is still being created, so that it
// never gets too big (in fact, it stays under 13 characters
// long no matter what).
// The indices of this array provide the map from numbers to
// base64.
var swaps = ["A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"a","b","c","d","e","f","g","h","i","j","k","l","m",
"n","o","p","q","r","s","t","u","v","w","x","y","z",
"0","1","2","3","4","5","6","7","8","9","+","/"],
tb, ib = "",
output = "",
i, L;
for (i=0, L = input.length; i < L; i++) {
// Turn the next character of input into astring of 8-bit binary
tb = input.charCodeAt(i).toString(2);
while (tb.length < 8) {
tb = "0"+tb;
}
// Stick this string on the end of the previous 8-bit binary
// strings to get one big concatenated binary representation
ib = ib + tb;
// Remove all 6-bit sequences from the start of the
// concatenated binary string, convert them to a base 64
// character and append to output. Doing this here prevents
// ib from getting massive
while (ib.length >= 6) {
output = output + swaps[parseInt(ib.substring(0,6),2)];
ib = ib.substring(6);
}
}
// Handle any necessary padding
if (ib.length == 4) {
tb = ib + "00";
output += swaps[parseInt(tb,2)] + "=";
}
if (ib.length == 2) {
tb = ib + "0000";
output += swaps[parseInt(tb,2)] + "==";
}
return output;
}
Yes, you will need enable TransArmor for your sandbox app as well. To do this, go to My APIs --> select your app --> Entitlements --> Add Entitlements --> Check Payeezy and TransArmor --> Save.
Bikram - Unfortunately, we do not have a C# library. Below is only a demonstration of a C# integration for a credit/debit card pre-authorize transaction.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Hi Jitender,
Unfortunately, we do not have an example of using Payeezy.js with asp.net and C# yet.
Once you have the token, the next step is to call the Token Based Payments method. You can view all the details here- https://developer.payeezy.com/payeezy_new_docs/apis/post/transactions-2
Do let us know how we can help further.
Regards,
Payeezy Team
Hello,
Thanks for the reply
Can you please assist me in how to create the HMAC key for the transaction.
I am using this method.
var nonce = Math.random() * 1000000000000000000;
var timeInMillis = new Date().getTime();
var tokendata={
"value":"token-value"
};
var JsonData = {
token_type: "payeezy",
token_data: tokendata,
};
var customer = {
merchant_ref: "Astonishing-Sale",
transaction_type: "purchase",
method: "token",
amount: "1299",
currency_code: "USD",
"token": JsonData
};
var requestJSON = JSON.stringify(customer);
var HMAC1 = generateHMACFunction(customer, nonce, timeInMillis);
function getAuthorizationHeader(apiKey, apiSecret, payload, token, nonce, timestamp) {
debugger;
var data = apiKey + nonce + timestamp + token + payload;
var digest = CryptoJS.HmacSHA256(data, apiSecret);
var header = new Buffer(digest.toString()).toString('base64');
return header;
}
function generateHMACFunction(payload, nonce, timestamp) {
debugger;
var apiSecretForHMACFromModal = '64baebebc238625e5b8422f225de064afa92f6b1ba1224ef798370600e743d25';
var testToken = 'fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6';
var testApiKey = 'oOyLZAIo5Q9ImjCTJC2r1qSD1Gjkra2B';
//var testTimeStamp = new Date().getTime();
//var testNonce = getSecureRandom(19);
var testPayload = payload;
var testMessage = testApiKey + nonce + timestamp + testToken + testPayload;
var hash = CryptoJS.HmacSHA256(testMessage, apiSecretForHMACFromModal);
var hmch = b64encode(hash.toString());
//$("input[name='Authorization']").val(b64encode(hash.toString()));
return b64encode(hash.toString())
}
function getSecureRandom(length) {
return Math.floor(Math.pow(10, length - 1) + Math.random() * 9 * Math.pow(10, length - 1));
}
function b64encode (input) {
// Converts each character in the input to its Unicode number,
// then writes out the Unicode numbers in binary, one after
// another, into a string. This string is then split up at
// every 6th character, these substrings are then converted back
// into binary integers and are used to subscript the "swaps"
// array. Since this would create HUGE strings of 1s and 0s,
// the distinct steps above are actually interleaved in the code
// below (ie. the long binary string, called "input_binary",
// gets processed while it is still being created, so that it
// never gets too big (in fact, it stays under 13 characters
// long no matter what).
// The indices of this array provide the map from numbers to
// base64.
var swaps = ["A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"a","b","c","d","e","f","g","h","i","j","k","l","m",
"n","o","p","q","r","s","t","u","v","w","x","y","z",
"0","1","2","3","4","5","6","7","8","9","+","/"],
tb, ib = "",
output = "",
i, L;
for (i=0, L = input.length; i < L; i++) {
// Turn the next character of input into astring of 8-bit binary
tb = input.charCodeAt(i).toString(2);
while (tb.length < 8) {
tb = "0"+tb;
}
// Stick this string on the end of the previous 8-bit binary
// strings to get one big concatenated binary representation
ib = ib + tb;
// Remove all 6-bit sequences from the start of the
// concatenated binary string, convert them to a base 64
// character and append to output. Doing this here prevents
// ib from getting massive
while (ib.length >= 6) {
output = output + swaps[parseInt(ib.substring(0,6),2)];
ib = ib.substring(6);
}
}
// Handle any necessary padding
if (ib.length == 4) {
tb = ib + "00";
output += swaps[parseInt(tb,2)] + "=";
}
if (ib.length == 2) {
tb = ib + "0000";
output += swaps[parseInt(tb,2)] + "==";
}
return output;
}
Please revert.
Thanks
Hello,
I am getting 400 Bad request error.
my function for caling service is as follow.
$.ajax({
type: "POST",
crossDomain: true, // enable this
beforeSend: function (xhr) {
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
xhr.setRequestHeader("apikey", "oOyLZAIo5Q9ImjCTJC2r1qSD1Gjkra2B");
xhr.setRequestHeader("token", "fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6");
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("Authorization", HMAC1);
xhr.setRequestHeader("nonce", nonce);
xhr.setRequestHeader("timestamp", timeInMillis);
},
dataType: 'jsonp',
data: requestJSON,
url: "https://api-cert.payeezy.com/v1/transactions",
processData: false,
success: function (data) {
debugger;
alert(data.d);
},
error: function (data) {
alert("error");
//responseHandler
alert(data.d);
}
});
Please help.
Thanks
Hello,
It says "You will need TransArmor enabled on your account to do token based transactions."
do i need it for testing also??
Thanks
Hi Jitender,
Yes, you will need enable TransArmor for your sandbox app as well. To do this, go to My APIs --> select your app --> Entitlements --> Add Entitlements --> Check Payeezy and TransArmor --> Save.
Do let us know if you have more questions.
Regards,
Payeezy Team
Hello,
can you answer my question regarding generation of HMAC key.
regards,
Jitender
Hi Jitender,
Here is a sample code in C# to generate HMAC.
Random random = new Random();
string nonce = (random.Next(0, 1000000)).ToString();
DateTime date = DateTime.UtcNow;
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan span = (date - epoch);
string time = span.TotalSeconds.ToString();
string token = Request.Form["token"];//Merchant token
string apiKey = Request.Form["apikey"];//apikey
string apiSecret = Request.Form["apisecret"];//API secret
string hashData = apiKey+nonce+time+token+jsonString;
string base64Hash = Convert.ToBase64String(CalculateHMAC(hashData, apiSecret));
Regards,
Payeezy Team
Please insert CalculateHMAC method as well.
Hi Guys!
Happy news. Finally I could manage to integrate the API with c#. Following is the code.
private void TestPayeezy()
{
var 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\" " +
"} " +
"}");
var payload = builder.ToString();
byte[] byteArray = Encoding.UTF8.GetBytes(payload);
var apiKey = "XXXXXXXXXXXXXXXXXXXXXX";
var timeStamp = ((long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds).ToString(CultureInfo.InvariantCulture);
var nonce = (10000000000000000000 * new Random(DateTime.Now.Millisecond).NextDouble()).ToString("0000000000000000000");
var merchantToken = "xxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var secretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var post = (HttpWebRequest)HttpWebRequest.Create("https://api-cert.payeezy.com/v1/transactions");
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);
var authorize = CreateHMAC(apiKey, secretKey, merchantToken, payload, nonce, timeStamp);
post.Headers.Add("Authorization", authorize);
post.ContentType = "application/json";
post.Headers.Add("token", merchantToken);
post.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7";
post.GetRequestStream().Write(byteArray, 0, byteArray.Length);
var response = post.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
var responseFromServer = reader.ReadToEnd();
}
public static string CreateHMAC(string apiKey, string apiSecret, string token,
string payload, string nonce, string timeStamp)
{
var hmacData = apiKey + nonce + timeStamp + token+ payload;
var hmac = new HMACSHA256(Encoding.UTF8.GetBytes (apiSecret));
var encBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(hmacData));
var encStr = ByteArrayToHexString(encBytes);
return Convert.ToBase64String(Encoding.UTF8.GetBytes((encStr)));
}
public static string ByteArrayToHexString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
I am getting message , The remote server returned an error: (503) Server Unavailable
Hello, your code looks excellent. How is the merchantToken generated? Where do I get that?
Hi Jeter,
This FAQ should answer the question - https://developer.payeezy.com/faqs/my-sandbox-integration-complete-how-do-i-go-live
Regards,
Payeezy team
There was a temporary issue on our server for 1 hour today. It should be ok now.
Regards,
Payeezy team
Hello everybody, how to check my customer's purchase succeed in sandbox environment ???? I using asp.net.
You can query your sandbox transactions using the event search api - http://developer.payeezy.com/payeezy-api/apis/get/events
HI Team,
Could you please give me the working C# code for Credit/Debit card payment and Telecheck payment.
Bikram - Unfortunately, we do not have a C# library. Below is only a demonstration of a C# integration for a credit/debit card pre-authorize transaction.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string jsonString = "{ \"merchant_ref\": \"Payeezy Test\", \"transaction_type\": \"authorize\", \"method\": \"credit_card\", \"amount\": \"1299\", \"currency_code\": \"USD\", \"credit_card\": { \"type\": \"visa\", \"cardholder_name\": \"Test Name\", \"card_number\": \"4111111111111111\", \"exp_date\": \"1020\", \"cvv\": \"123\" } }";
string apiKey = "";
string apiSecret = "";
string token = "fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6";
Random random = new Random();
string nonce = (random.Next(0, 1000000)).ToString();
DateTime date = DateTime.UtcNow;
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan span = (date - epoch);
string time = span.TotalMilliseconds.ToString();
string hashData = apiKey + nonce + time + token + jsonString;
string base64Hash = Convert.ToBase64String(CalculateHMAC(hashData, apiSecret));
string url = "https://api-cert.payeezy.com/v1/transactions";
//begin HttpWebRequest
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.Accept = "*/*";
webRequest.Headers.Add("timestamp", time);
webRequest.Headers.Add("nonce", nonce);
webRequest.Headers.Add("token", token);
webRequest.Headers.Add("apikey", apiKey);
webRequest.Headers.Add("Authorization", base64Hash);
webRequest.ContentLength = jsonString.Length;
webRequest.ContentType = "application/json";
StreamWriter writer = null;
writer = new StreamWriter(webRequest.GetRequestStream());
writer.Write(jsonString);
writer.Close();
string responseString;
try
{
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader responseStream = new StreamReader(webResponse.GetResponseStream()))
{
responseString = responseStream.ReadToEnd();
Console.WriteLine(webRequest.Headers.ToString() + jsonString);
Console.WriteLine(webResponse.Headers.ToString() + responseString);
}
}
}
catch (WebException ex)
{
if (ex.Response != null)
{
using (HttpWebResponse errorResponse = (HttpWebResponse)ex.Response)
{
using (StreamReader reader = new StreamReader(errorResponse.GetResponseStream()))
{
string remoteEx = reader.ReadToEnd();
Console.WriteLine(remoteEx);
}
}
}
}
Console.ReadLine();
}
static byte[] CalculateHMAC(string data, string secret)
{
HMAC hmacSha256 = new HMACSHA256(Encoding.UTF8.GetBytes(secret));
byte[] hmac2Hex = hmacSha256.ComputeHash(Encoding.UTF8.GetBytes(data));
string hex = BitConverter.ToString(hmac2Hex);
hex = hex.Replace("-", "").ToLower();
byte[] hexArray = Encoding.UTF8.GetBytes(hex);
string base64Hash = Convert.ToBase64String(hexArray);
return hexArray;
}
}
}
Can provide a .net sample that demonstrates Integration first to end and also able to manage ECheck Payments (ACH Payments