Last active
February 15, 2019 09:29
-
-
Save ramanathanrv/ae6db7d6aa1dd9cf8b35255ce5953d1b to your computer and use it in GitHub Desktop.
Example C# code to connect with JusPay APIs
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.Net; | |
using System.IO; | |
using System.Text; | |
public class JusPayService | |
{ | |
public static void Main() | |
{ | |
String apiKey = "1C3CB0373660436BA2895B6CC74629E7"; | |
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(apiKey + ":")); | |
HttpWebResponse response = null; | |
System.Net.HttpWebRequest request = | |
(System.Net.HttpWebRequest) System.Net.WebRequest.Create("https://api.juspay.in/ping"); | |
request.Headers.Add("Authorization", "Basic " + encoded); | |
request.Method = "POST"; | |
request.PreAuthenticate = true; | |
var customerId = "cst001"; | |
var customerEmail = "user@mail.com"; | |
var customerPhone = "9922299223"; | |
long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; | |
var postData = "order_id=" + milliseconds; | |
postData += "&amount=10.00"; | |
postData += "&customer_id=" + customerId; | |
postData += "&customer_email=" + customerEmail; | |
postData += "&customer_phone=" + customerPhone; | |
var data = Encoding.ASCII.GetBytes(postData); | |
request.ContentType = "application/x-www-form-urlencoded"; | |
request.ContentLength = data.Length; | |
Stream dataStream = request.GetRequestStream (); | |
dataStream.Write(data, 0, data.Length); | |
dataStream.Close(); | |
try { | |
response = (HttpWebResponse) request.GetResponse(); | |
StreamReader sr = new StreamReader(response.GetResponseStream()); | |
Console.WriteLine(sr.ReadToEnd()); | |
sr.Close(); | |
response.Close(); | |
} | |
catch (WebException ex) | |
{ | |
using (var stream = ex.Response.GetResponseStream()) | |
using (var reader = new StreamReader(stream)) | |
{ | |
Console.WriteLine(reader.ReadToEnd()); | |
} | |
reader.Close(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Unknown error while trying to create order"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment