Last active
December 16, 2015 01:59
-
-
Save spaomalley/5358738 to your computer and use it in GitHub Desktop.
.NET(C#) Basic payment submission.
This file contains hidden or 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
namespace Card_Account_Transactions | |
{ | |
public class ConfigFactory | |
{ | |
public string consumerKey; | |
public string consumerSecret; | |
public string merchantId; | |
public string baseURL; | |
public static string[] endPointRequestToken = { "https://sandbox.api.prioritypaymentsystems.com/checkout/v1.1/oauth/1a/requesttoken", "POST" }; | |
public static string[] endPointAccessToken = { "https://sandbox.api.prioritypaymentsystems.com/checkout/v1.1/oauth/1a/accesstoken", "POST" }; | |
public static string[] payment = { "https://sandbox.api.prioritypaymentsystems.com/checkout/v1.1/payment", "POST" }; | |
public ConfigFactory() | |
{ | |
this.consumerKey = getConsumerKey(); | |
this.consumerSecret = getConsumerSecret(); | |
this.merchantId = getMerchantId(); | |
} | |
private string getConsumerKey() | |
{ | |
return ""; | |
} | |
private string getConsumerSecret() | |
{ | |
return ""; | |
} | |
private string getMerchantId() | |
{ | |
string temp = ""; | |
return temp; | |
} | |
} | |
} |
This file contains hidden or 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.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace Card_Account_Transactions | |
{ | |
public class newOAuth | |
{ | |
ConfigFactory ConfigFactory = new ConfigFactory(); | |
OAuth_Utilities Utilitay = new OAuth_Utilities(); | |
public string[] endpointData; | |
public IList<Card_Account_Transactions.OAuth_Utilities.QueryParameter> queryParameters; | |
public NameValueCollection tokens; | |
public string startDate; | |
public string endDate; | |
public string datePostedFilter; | |
public Dictionary<string, string> headers; | |
public List<OAuth_Utilities.QueryParameter> parameters; | |
public newOAuth(string[] endpointData, NameValueCollection tokens = null, string startDate = null, string endDate = null, string datePostedFilter = null) | |
{ | |
this.endpointData = endpointData; | |
this.tokens = tokens; | |
this.datePostedFilter = datePostedFilter; | |
this.startDate = startDate; | |
this.endDate = endDate; | |
} | |
public static class Nonce_Stamp | |
{ | |
public static string getTimeStamp(){ | |
int stamp = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds; | |
string sendTime = stamp.ToString(); | |
return sendTime; | |
} | |
public static string GenerateNonce() | |
{ | |
Random random = new Random(); | |
return random.Next(123400, 9999999).ToString(); | |
} | |
} | |
public List<OAuth_Utilities.QueryParameter> setupParams() | |
{ | |
List<OAuth_Utilities.QueryParameter> parameters = new List<OAuth_Utilities.QueryParameter>(); | |
if (this.startDate != null) | |
{ | |
if (this.datePostedFilter != null) | |
{ | |
parameters.Add(new OAuth_Utilities.QueryParameter("datePostedFilter", "true")); | |
} | |
parameters.Add(new OAuth_Utilities.QueryParameter("endDate", this.endDate)); | |
parameters.Add(new OAuth_Utilities.QueryParameter("startDate", this.startDate)); | |
} | |
parameters.Add(new OAuth_Utilities.QueryParameter("oauth_version", "1.0")); | |
parameters.Add(new OAuth_Utilities.QueryParameter("oauth_nonce", newOAuth.Nonce_Stamp.GenerateNonce())); | |
parameters.Add(new OAuth_Utilities.QueryParameter("oauth_timestamp", newOAuth.Nonce_Stamp.getTimeStamp())); | |
if(this.tokens != null) | |
{ | |
parameters.Add(new OAuth_Utilities.QueryParameter("oauth_token", this.tokens["oauth_token"])); | |
} | |
parameters.Add(new OAuth_Utilities.QueryParameter("oauth_signature_method", "HMAC-SHA1")); | |
parameters.Add(new OAuth_Utilities.QueryParameter("oauth_consumer_key", ConfigFactory.consumerKey)); | |
return parameters; | |
} | |
public static class RequestNormalizer | |
{ | |
public static string NormalizeRequestParameters(IList<Card_Account_Transactions.OAuth_Utilities.QueryParameter> parameters) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
OAuth_Utilities.QueryParameter p = null; | |
for (int i = 0; i < parameters.Count; i++) | |
{ | |
p = parameters[i]; | |
sb.AppendFormat("{0}={1}", p.Name, p.Value); | |
if (i < parameters.Count - 1) | |
{ | |
sb.Append("&"); | |
} | |
} | |
return sb.ToString(); | |
} | |
} | |
public string GenerateSignatureBase( string[] endPointData) | |
{ | |
parameters = setupParams(); | |
parameters.Sort(new OAuth_Utilities.QueryParameterComparer()); | |
StringBuilder signatureBase = new StringBuilder(); | |
signatureBase.AppendFormat("{0}&", endPointData[1].ToUpper()); | |
signatureBase.AppendFormat("{0}&", Utilitay.UrlEncode(endPointData[0])); | |
signatureBase.AppendFormat("{0}", Utilitay.UrlEncode(newOAuth.RequestNormalizer.NormalizeRequestParameters(parameters))); | |
return signatureBase.ToString(); | |
} | |
public string GenerateSignatureUsingHash(string signatureBase, HashAlgorithm hash) | |
{ | |
OAuth_Utilities O = new OAuth_Utilities(); | |
return O.ComputeHash(hash, signatureBase); | |
} | |
public string GenerateSignature(string[] endpointData) | |
{ | |
string baseString = GenerateSignatureBase(endpointData); | |
HMACSHA1 hmacsha1 = new HMACSHA1(); | |
SHA1 sha1 = new SHA1CryptoServiceProvider(); | |
OAuth_Utilities O = new OAuth_Utilities(); | |
hmacsha1.Key = O.keyBuilder(this.tokens); | |
string signature = GenerateSignatureUsingHash(baseString, hmacsha1); | |
return signature; | |
} | |
public void recaptureParams() | |
{ | |
string signature = GenerateSignature(this.endpointData); | |
this.parameters.Add(new OAuth_Utilities.QueryParameter("oauth_signature", Utilitay.UrlEncode(signature))); | |
} | |
public string createHeaders() | |
{ | |
recaptureParams(); | |
this.parameters.Sort(new OAuth_Utilities.QueryParameterComparer()); | |
StringBuilder headerString = new StringBuilder(); | |
headerString.AppendFormat("{0}", (Utilitay.createHeader(parameters))); | |
string h = "OAuth " + headerString; | |
return h; | |
} | |
} | |
} |
This file contains hidden or 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.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.IO; | |
using System.Net; | |
using System.Runtime.Serialization.Json; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace Card_Account_Transactions | |
{ | |
public class OAuth_Utilities | |
{ | |
public string UrlEncode(string value) | |
{ | |
string unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"; | |
StringBuilder result = new StringBuilder(); | |
foreach (char symbol in value) | |
{ | |
if (unreservedChars.IndexOf(symbol) != -1) | |
{ | |
result.Append(symbol); | |
} | |
else | |
{ | |
result.Append('%' + String.Format("{0:X2}", (int)symbol)); | |
} | |
} | |
return result.ToString(); | |
} | |
public string GenerateSignatureUsingHash(string signatureBase, HashAlgorithm hash) | |
{ | |
return ComputeHash(hash, signatureBase); | |
} | |
public byte[] keyBuilder(NameValueCollection tokens) | |
{ | |
ConfigFactory ConfigFactory = new ConfigFactory(); | |
SHA1 sha1 = new SHA1CryptoServiceProvider(); | |
string key = string.Format("{0}&", UrlEncode(ConfigFactory.consumerSecret)); | |
if(tokens != null) | |
{ | |
key += UrlEncode(tokens["oauth_token_secret"]); | |
} | |
byte[] keyBytes = Encoding.ASCII.GetBytes(key); | |
return keyBytes; | |
} | |
public string createHeader(IList<QueryParameter> parameters) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
OAuth_Utilities.QueryParameter p = null; | |
for (int i = 0; i < parameters.Count; i++) | |
{ | |
p = parameters[i]; | |
sb.AppendFormat("{0}={1}", p.Name, p.Value); | |
if (i < parameters.Count - 1) | |
{ | |
sb.Append(","); | |
} | |
} | |
return sb.ToString(); | |
} | |
public string ComputeHash(HashAlgorithm hashAlgorithm, string data) | |
{ | |
if (hashAlgorithm == null) | |
{ | |
throw new ArgumentNullException("hashAlgorithm"); | |
} | |
if (string.IsNullOrEmpty(data)) | |
{ | |
throw new ArgumentNullException("data"); | |
} | |
byte[] dataBuffer = System.Text.Encoding.ASCII.GetBytes(data); | |
byte[] hashBytes = hashAlgorithm.ComputeHash(dataBuffer); | |
return Convert.ToBase64String(hashBytes); | |
} | |
public class QueryParameter | |
{ | |
private string name = null; | |
private string value = null; | |
public QueryParameter(string name, string value) | |
{ | |
this.name = name; | |
this.value = value; | |
} | |
public string Name | |
{ | |
get { return name; } | |
} | |
public string Value | |
{ | |
get { return value; } | |
} | |
} | |
public class QueryParameterComparer : IComparer<QueryParameter> | |
{ | |
#region IComparer<QueryParameter> Members | |
public int Compare(QueryParameter x, QueryParameter y) | |
{ | |
if (x.Name == y.Name) | |
{ | |
return string.Compare(x.Value, y.Value); | |
} | |
else | |
{ | |
return string.Compare(x.Name, y.Name); | |
} | |
} | |
#endregion | |
} | |
public string sendRequest(string[] endpointData, string headers, PaymentFactory JSON = null, List<OAuth_Utilities.QueryParameter> qp = null) | |
{ | |
HttpWebRequest r; | |
if (qp != null) | |
{ | |
qp.Sort(new OAuth_Utilities.QueryParameterComparer()); | |
endpointData[0] = endpointData[0] + "?" + newOAuth.RequestNormalizer.NormalizeRequestParameters(qp); | |
Console.WriteLine(endpointData[0]); | |
r = (HttpWebRequest)WebRequest.Create(endpointData[0]); | |
} | |
else | |
{ | |
r = (HttpWebRequest)WebRequest.Create(endpointData[0]); | |
} | |
string responseFromServer = ""; | |
r = (HttpWebRequest)WebRequest.Create(endpointData[0]); | |
r.Method = endpointData[1]; | |
r.Headers.Add("Authorization", headers); | |
if (JSON != null) | |
{ | |
r.ContentType = "application/json"; | |
MemoryStream stream1 = new MemoryStream(); | |
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(PaymentFactory)); | |
ser.WriteObject(stream1, JSON); | |
byte[] json = stream1.ToArray(); | |
stream1.Close(); | |
string actualJson = Encoding.UTF8.GetString(json, 0, json.Length).ToString(); | |
using (var streamWriter = new StreamWriter(r.GetRequestStream())) | |
{ | |
streamWriter.Write(actualJson); | |
} | |
var httpResponse = (HttpWebResponse)r.GetResponse(); | |
NameValueCollection H = httpResponse.Headers; | |
Stream dataStream = r.GetRequestStream(); | |
dataStream = httpResponse.GetResponseStream(); | |
StreamReader reader = new StreamReader(dataStream); | |
responseFromServer = H["Location"]; | |
} | |
else | |
{ | |
Stream dataStream = r.GetRequestStream(); | |
WebResponse response = r.GetResponse(); | |
dataStream = response.GetResponseStream(); | |
StreamReader reader = new StreamReader(dataStream); | |
responseFromServer = reader.ReadToEnd(); | |
} | |
return responseFromServer; | |
} | |
} | |
} |
This file contains hidden or 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.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Runtime.Serialization; | |
namespace Card_Account_Transactions | |
{ | |
[DataContract] | |
public class PaymentFactory | |
{ | |
[DataMember] | |
public string merchantId; | |
[DataMember] | |
public string tenderType; | |
[DataMember] | |
public string amount; | |
[DataMember] | |
public cardAccount cardAccount; | |
} | |
[DataContract] | |
public class cardAccount | |
{ | |
[DataMember] | |
public string number; | |
[DataMember] | |
public string expiryMonth; | |
[DataMember] | |
public string expiryYear; | |
[DataMember] | |
public string cvv; | |
[DataMember] | |
public string avsZip; | |
[DataMember] | |
public string avsStreet; | |
} | |
} |
This file contains hidden or 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.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Web; | |
using System; | |
namespace Card_Account_Transactions | |
{ | |
public class RetrieveTokens | |
{ | |
public NameValueCollection getAccessToken() | |
{ | |
var C = new ConfigFactory(); | |
var OAuth = new newOAuth(ConfigFactory.endPointRequestToken, null); | |
OAuth_Utilities U = new OAuth_Utilities(); | |
string Headers = OAuth.createHeaders(); | |
string request_Token_Response = U.sendRequest(ConfigFactory.endPointRequestToken, Headers); | |
NameValueCollection rt = HttpUtility.ParseQueryString(request_Token_Response); | |
OAuth = new newOAuth(ConfigFactory.endPointAccessToken, rt); | |
Headers = OAuth.createHeaders(); | |
string access_Token_Response = U.sendRequest(ConfigFactory.endPointAccessToken, Headers); | |
NameValueCollection at = HttpUtility.ParseQueryString(access_Token_Response); | |
var token = at["oauth_token"]; | |
var tokenSecret = at["oauth_token_secret"]; | |
return at; | |
} | |
} | |
} |
This file contains hidden or 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.Collections.Generic; | |
using System.Collections.Specialized; | |
namespace Card_Account_Transactions | |
{ | |
public class Test | |
{ | |
public static void Main(string[] args) | |
{ | |
var C = new ConfigFactory(); | |
var OAuth = new newOAuth(ConfigFactory.endPointRequestToken); | |
OAuth_Utilities U = new OAuth_Utilities(); | |
var t = new RetrieveTokens(); | |
NameValueCollection toke = t.getAccessToken(); | |
PaymentFactory p = new PaymentFactory(); | |
p.merchantId = C.merchantId; | |
p.tenderType = "Card"; | |
p.amount = "0.01"; | |
p.cardAccount = new cardAccount(); | |
p.cardAccount.number = "4444555566667777"; | |
p.cardAccount.expiryMonth = "07"; | |
p.cardAccount.expiryYear = "2016"; | |
p.cardAccount.cvv = "123"; | |
p.cardAccount.avsZip = "12345"; | |
p.cardAccount.avsStreet = "1234"; | |
OAuth = new newOAuth(ConfigFactory.payment, toke); | |
string Headers = OAuth.createHeaders(); | |
string createdPayment = U.sendRequest(ConfigFactory.payment, Headers, p); | |
Console.WriteLine(createdPayment); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment