Created
March 8, 2021 20:59
-
-
Save thlinux1107/a1e70ed4e522d03dbdc264cd84c1c2ab to your computer and use it in GitHub Desktop.
PayaGateway - Direct API - Get Charges - C#
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.Security.Cryptography; | |
using System.IO; | |
using System.Net; | |
using System.Text; | |
namespace PayaGateway_GET_Transactions | |
{ | |
class MainClass { | |
public static void Main (string[] args) | |
{ | |
//TH - Test Data. This is the test account infomation we provide | |
//in the API Sandbox. Please contact us at [email protected] if | |
//you need a unique test account and receive the Merchant ID and | |
//Merchant Key. In order to get your own Client ID and Client Secret | |
//you must register at https://developer.sagepayments.com and setup | |
//an App under My Apps. Please let us know if you have any questions. | |
string merchantId = "173859436515"; // use your own | |
string merchantKey = "P1J2V8P2Q3D8"; // use your own | |
//TH - The Client ID and Client Key should be hard coded and not displayed | |
//in the production product. These are your API credentials used for | |
//security and tracking purposes. | |
string clientId = "W8yvKQ5XbvAn7dUDJeAnaWCEwA4yXEgd"; // use your own | |
string clientSecret = "iLzODV5AUsCGWGkr"; // user your own | |
//TH - Set the variables | |
//string startDateX = startDate.ToShortDateString(); | |
string startDateX = "01-01-2020"; | |
//string endDateX = endDate.ToShortDateString(); | |
string endDateX = "01-01-2022"; | |
string url_sale = "https://api-cert.sagepayments.com/bankcard/v1/transactions?startDate=" + | |
startDateX + "&endDate=" + endDateX + "&PageSize=250&PageNumber=1"; | |
string verb = "GET"; | |
string contentType = "application/json"; | |
string nl = System.Environment.NewLine; | |
//TH - Building timestamp and nonce. Needs to be epoch in seconds. | |
//TH - I am using the timestamp as my Nonce, but it is best to create | |
//separate, unique value. | |
TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0); | |
string timestamp = t.TotalSeconds.ToString(); | |
string nonce = timestamp; | |
//TH - Build the Authorization | |
string authToken = verb + url_sale + merchantId + nonce + timestamp; | |
byte[] hash_authToken = new HMACSHA512(Encoding.ASCII.GetBytes(clientSecret)).ComputeHash(Encoding.ASCII.GetBytes(authToken)); | |
string hash64_authToken = Convert.ToBase64String(hash_authToken); | |
////////////////////////////////////////////////////////////////// | |
//TH - 20181003 - Added for TLS 1.0 shutdown requirements. | |
ServicePointManager.Expect100Continue = true; | |
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; | |
////////////////////////////////////////////////////////////////// | |
try | |
{ | |
//TH - Initiate Web Request | |
var web_request = (HttpWebRequest)WebRequest.Create(url_sale); | |
//TH - Set the headers and details | |
var web_request_headers = web_request.Headers; | |
//Console.WriteLine("Configuring web_request to include headers for Paya Direct API"); | |
Console.WriteLine("Configuring web_request to include headers for Paya Direct API"); | |
web_request_headers["clientId"] = clientId; | |
web_request_headers["merchantId"] = merchantId; | |
web_request_headers["merchantKey"] = merchantKey; | |
web_request_headers["nonce"] = nonce; | |
web_request_headers["timestamp"] = timestamp; | |
web_request_headers["authorization"] = hash64_authToken; | |
web_request.Method = verb; | |
//TH - Send the data | |
web_request.ContentType = contentType; | |
//TH - Get the Response and Catch any errors. | |
var web_response = (HttpWebResponse)web_request.GetResponse(); | |
Console.WriteLine(web_response.StatusDescription); | |
var datastream = web_response.GetResponseStream(); | |
var reader = new StreamReader(datastream); | |
string responseFromServer = reader.ReadToEnd(); | |
Console.WriteLine(responseFromServer); | |
reader.Close(); | |
datastream.Close(); | |
web_response.Close(); | |
//TH - Display response. | |
//TH - 20170304 - Added more detail to the displayed response. | |
Console.WriteLine("Server Status Code: " + web_response.StatusCode + nl + | |
"Server Status: " + web_response.StatusDescription + nl + | |
"API Response: " + responseFromServer); | |
} | |
//TH - 20170304 - Added WebExecption to allow code to gather added detail from the exception. | |
catch (WebException exception) | |
{ | |
//TH - 20170304 - Added more detail regarding exception to gather the API response data. | |
var sResponse = new StreamReader(exception.Response.GetResponseStream()).ReadToEnd(); | |
Console.WriteLine("Server Response: " + exception.Message + nl + nl + | |
"API Response: " + sResponse); | |
} | |
catch (Exception exception) | |
{ | |
Console.WriteLine("Server Response: " + exception.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment