/// <summary> /// Performs basic validation of the transaction result (you should also implement your own e.g. check amounts against order) /// </summary> /// <param name="result">Transaction result</param> public void ValidateResult(ServerTransactionResult result, String MerchantId, String MerchantPassword, String PreSharedKey) { NameValueCollection nameValueCollection = new NameValueCollection(); HashMethod hashMethod = HashMethod.SHA1; nameValueCollection.Add("PreSharedKey", PreSharedKey); nameValueCollection.Add("MerchantID", MerchantId); nameValueCollection.Add("Password", MerchantPassword); nameValueCollection.Add("StatusCode", Convert.ToInt32(result.StatusCode)); nameValueCollection.Add("Message", result.Message); if (result.StatusCode == TransactionStatus.DuplicateTransaction) { nameValueCollection.Add("PreviousStatusCode", Convert.ToInt32(result.PreviousStatusCode)); } else { nameValueCollection.Add("PreviousStatusCode", ""); } nameValueCollection.Add("PreviousMessage", result.PreviousMessage); nameValueCollection.Add("CrossReference", result.CrossReference); nameValueCollection.Add("AddressNumericCheckResult", result.AddressNumericCheckResult); nameValueCollection.Add("PostCodeCheckResult", result.PostCodeCheckResult); nameValueCollection.Add("CV2CheckResult", result.CV2CheckResult); nameValueCollection.Add("ThreeDSecureAuthenticationCheckResult", result.ThreeDSecureAuthenticationCheckResult); nameValueCollection.Add("CardType", result.CardType); nameValueCollection.Add("CardClass", result.CardClass); nameValueCollection.Add("CardIssuer", result.CardIssuer); nameValueCollection.Add("CardIssuerCountryCode", result.CardIssuerCountryCode); nameValueCollection.Add("Amount", result.Amount); nameValueCollection.Add("CurrencyCode", Convert.ToString(result.CurrencyCode)); nameValueCollection.Add("OrderID", result.OrderID); nameValueCollection.Add("TransactionType", result.TransactionType); nameValueCollection.Add("TransactionDateTime", Convert.ToString(result.TransactionDateTime)); nameValueCollection.Add("OrderDescription", result.OrderDescription); nameValueCollection.Add("CustomerName", result.CustomerName); nameValueCollection.Add("Address1", result.Address1); nameValueCollection.Add("Address2", result.Address2); nameValueCollection.Add("Address3", result.Address3); nameValueCollection.Add("Address4", result.Address4); nameValueCollection.Add("City", result.City); nameValueCollection.Add("State", result.State); nameValueCollection.Add("PostCode", result.PostCode); nameValueCollection.Add("CountryCode", Convert.ToString(result.CountryCode)); nameValueCollection.Add("EmailAddress", result.EmailAddress); nameValueCollection.Add("PhoneNumber", result.PhoneNumber); bool flag = false; string queryString = nameValueCollection.ToQueryString("&", false, flag); string str = HashUtil.ComputeHashDigest(queryString, PreSharedKey, hashMethod); if (result.HashDigest != str) { throw new Exception("Hash Check Failed"); } }
/// <summary> /// Submits a payment request to the hosted payment page /// </summary> /// <param name="request">The request to submit</param> /// <param name="merchantPassword">The merchant password that corresponds to the gateway account the transaction will be run through.</param> /// <param name="preSharedKey">The merchant gateway account pre shared key</param> /// <param name="postUrl">The url of the hosted payment page</param> public void SubmitTransaction(HostedTransactionRequest request, string merchantPassword, string preSharedKey, string postUrl) { if (CommonUtils.AreNullOrEmpty(merchantPassword, preSharedKey, postUrl)) { throw new ArgumentNullException(); } if (request == null) { throw new ArgumentNullException("request"); } var hashInputs = new NameValueCollection(); var hashMethod = HashMethod.SHA1; if (hashMethod == HashMethod.SHA1 || hashMethod == HashMethod.MD5) { // only add if using standard hash method (MD5 or SHA1) hashInputs.Add("PreSharedKey", preSharedKey); } hashInputs.Add("MerchantID", _merchantId); hashInputs.Add("Password", merchantPassword); var requestInputs = request.ToNameValueCollection(); foreach (var k in requestInputs.AllKeys) { hashInputs.Add(k, requestInputs.GetValues(k)[0]); } var hashString = hashInputs.ToQueryString(encode: false); var hash = HashUtil.ComputeHashDigest(hashString, preSharedKey, hashMethod); // ready to post - just return the NameValue Collection var remotePost = new RemotePost(_context, postUrl, FormMethod.POST); remotePost.AddInput("HashDigest", hash); remotePost.AddInput("MerchantID", _merchantId); // add the rest of the form variables foreach (var k in requestInputs.AllKeys) { remotePost.AddInput(k, requestInputs.GetValues(k)[0]); } remotePost.Post("CardsavePaymentForm"); }
public void SubmitTransaction(TransactionRequest request, string merchantPassword, string preSharedKey, string postUrl, HashMethod hashMethod = HashMethod.Sha1) { if (request == null) { throw new ArgumentNullException("Request Error!"); } string[] strArrays = { merchantPassword, preSharedKey, postUrl }; var remotePost = new RemotePost(_context, postUrl, FormMethod.Post); var nvCollection = new NameValueCollection(); if (hashMethod == HashMethod.Sha1 || hashMethod == HashMethod.Md5) { nvCollection.Add("PreSharedKey", preSharedKey); } nvCollection.Add("MerchantID", MerchantId); nvCollection.Add("Password", merchantPassword); var requestNVCol = request.ToNameValueCollection(); for (int i = 0; i < requestNVCol.AllKeys.Length; i++) { var key = requestNVCol.AllKeys[i]; nvCollection.Add(key, requestNVCol.GetValues(key)[0]); remotePost.AddInput(key, requestNVCol.GetValues(key)[0]); } var qStr = nvCollection.ToQueryString(false, false); var digest = HashUtil.ComputeHashDigest(qStr, preSharedKey, hashMethod); remotePost.AddInput("HashDigest", digest); remotePost.AddInput("MerchantID", MerchantId); remotePost.AddInput("ThreeDSecureCompatMode", "false"); remotePost.AddInput("ServerResultCompatMode", "false"); remotePost.Post("CardsavePaymentForm"); }