Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ibrahimlawal
Created February 2, 2017 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibrahimlawal/278e66ce341a4f3fb7f04d7f87c9755f to your computer and use it in GitHub Desktop.
Save ibrahimlawal/278e66ce341a4f3fb7f04d7f87c9755f to your computer and use it in GitHub Desktop.
Paystack .NET Event Handling
using System;
public class MyHashHMACTester
{
public static void Main()
{
// Usage sample
Console.WriteLine(MyHashHMAC.sha512WithKey("{\"event\":\"charge.success\",\"data\":{\"id\":708206,\"domain\":\"test\",\"status\":\"success\",\"reference\":\"AB8760\",\"amount\":9000,\"message\":null,\"gateway_response\":\"Successful\",\"paid_at\":\"2017-02-02T08:18:39.000Z\",\"created_at\":\"2017-02-02T08:18:13.000Z\",\"channel\":\"card\",\"currency\":\"NGN\",\"ip_address\":\"154.118.4.232\",\"metadata\":{\"cancel_action\":\"https://gbeta.gigm.com/PayStack-Response.aspx\",\"custom_fields\":[],\"referrer\":\"http://gbeta.gigm.com/Passenger-Details.aspx\"},\"log\":{\"time_spent\":22,\"attempts\":1,\"authentication\":null,\"errors\":0,\"success\":false,\"mobile\":false,\"input\":[],\"channel\":null,\"history\":[{\"type\":\"input\",\"message\":\"Filled these fields: card number, card expiry, card cvv\",\"time\":22},{\"type\":\"action\",\"message\":\"Attempted to pay\",\"time\":22}]},\"fees\":null,\"fees_split\":null,\"customer\":{\"id\":167423,\"first_name\":null,\"last_name\":null,\"email\":\"bakare.wilmot@thegiggroupng.com\",\"customer_code\":\"CUS_v636z2w7kvztrfe\",\"phone\":null,\"metadata\":null,\"risk_action\":\"default\"},\"authorization\":{\"authorization_code\":\"AUTH_ahil74ij\",\"bin\":\"412345\",\"last4\":\"1381\",\"exp_month\":\"01\",\"exp_year\":\"2020\",\"card_type\":\"visa visa\",\"bank\":\"TEST BANK\",\"country_code\":\"NG\",\"brand\":\"visa\",\"reusable\":true},\"plan\":{},\"subaccount\":{},\"paidAt\":\"2017-02-02T08:18:39.000Z\"}}", "sk_test_811ea39163d21a9625dd9b5cb9a791522d89b824"));
}
}
public class MyHashHMAC
{
public static string sha512WithKey(string message, string key)
{
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
byte [] keyByte = encoding.GetBytes(key);
System.Security.Cryptography.HMACSHA512 hmacsha512 = new System.Security.Cryptography.HMACSHA512(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
byte[] hashmessage = hmacsha512.ComputeHash(messageBytes);
return ByteToString(hashmessage);
}
public static string ByteToString(byte [] buff)
{
string sbinary="";
for (int i=0;i<buff.Length;i++)
{
sbinary+=buff[i].ToString("X2"); // hex format
}
return(sbinary);
}
}
using System;
protected void Page_Load(object sender, EventArgs e)
{
MemoryStream memstream = new MemoryStream();
Request.InputStream.CopyTo(memstream);
memstream.Position = 0;
using (StreamReader reader = new StreamReader(memstream))
{
string text = reader.ReadToEnd();
// text comes escaped, let us return it to normal
paystackValuesStringified = Regex.Replace(text, "(\"(?:[^\"\\\\]|\\\\.)*\")|\\s+", "$1");
}
}
@RockMaxi
Copy link

Hi Ibrahim, I'm trying to implement paystack for a aspx website project on localhost but it seems not to be accepting inputs from client side. Do you have any idea of what I'm not doing right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment