Last active
August 29, 2015 14:07
-
-
Save jordanluyke/9e703388683093fa662f to your computer and use it in GitHub Desktop.
SNAPCARD API Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Net; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace apitest1 | |
{ | |
class MainClass | |
{ | |
public static void Main(string[] args) | |
{ | |
SnapcardApi snapcardApi = new SnapcardApi(); | |
string balance = snapcardApi.getMerchantBalance(); | |
Console.WriteLine(balance); | |
string invoiceData = "{ \"fiatAmount\":1, \"fiatType\":\"USD\", \"order\":\"4817123124\", \"customer\":\"testCustomer\" }"; | |
string createdInvoice = snapcardApi.createInvoice(invoiceData); | |
Console.WriteLine(createdInvoice); | |
} | |
} | |
public class SnapcardApi { | |
private const string domain = "https://www.snapcard.io"; | |
private const string apiKey = "9vpiv3noekf2uk8nr1f3pekoi6hf3kll"; | |
private const string secKey = "l512t1lmf1pnse5jnv2f75n5gd59n2to"; | |
public string getMerchantBalance() | |
{ | |
return MakeGetReq("/api/v1/merchant/balance"); | |
} | |
public string createInvoice(string jsonString) | |
{ | |
return MakePostReq("/api/v1/invoice/create", jsonString); | |
} | |
private string MakeGetReq(string apiRoute) | |
{ | |
WebRequest req = AssembleWebRequest(apiRoute); | |
req.Method = "GET"; | |
WebResponse response = req.GetResponse(); | |
string responseString = ParseWebResponse(response); | |
return responseString; | |
} | |
private string MakePostReq(string apiRoute, string jsonData) | |
{ | |
WebRequest req = AssembleWebRequest(apiRoute, jsonData); | |
req.Method = "POST"; | |
WriteJsonToRequest(req, jsonData); | |
WebResponse response = req.GetResponse(); | |
string responseString = ParseWebResponse(response); | |
return responseString; | |
} | |
private WebRequest AssembleWebRequest(string apiRoute, string jsonData = "") | |
{ | |
string url = domain + apiRoute + "?EXPIRATION=" + GetExpirationTime(); | |
WebRequest request = WebRequest.Create(url); | |
request.ContentType = "application/json"; | |
request.Headers["AUTH_API_KEY"] = apiKey; | |
string authSig = CalcAuthSigHash(secKey, url + jsonData); | |
request.Headers["AUTH_SIG"] = authSig; | |
return request; | |
} | |
private void WriteJsonToRequest(WebRequest req, string jsonData) | |
{ | |
var streamWriter = new StreamWriter(req.GetRequestStream ()); | |
streamWriter.Write(jsonData); | |
streamWriter.Close(); | |
} | |
private string GetExpirationTime() | |
{ | |
return DateTime.Now.AddMinutes(5).ToString(); | |
} | |
private byte[] GetBytes(string str) | |
{ | |
return Encoding.UTF8.GetBytes(str); | |
} | |
private string GetString(byte[] bytes) | |
{ | |
return BitConverter.ToString(bytes); | |
} | |
private string CalcAuthSigHash(string key, string value) | |
{ | |
HMACSHA256 hmac = new HMACSHA256(GetBytes(key)); | |
string hash = GetString(hmac.ComputeHash(GetBytes(value))); | |
hash = hash.Replace("-", ""); | |
return hash; | |
} | |
private string ParseWebResponse(WebResponse response) | |
{ | |
StreamReader streamReader = new StreamReader(response.GetResponseStream()); | |
string result = streamReader.ReadToEnd(); | |
streamReader.Close(); | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment