using System; using System.Net; using System.IO; using System.Data; namespace Gateways { public class AuthorizeNET : GatewayBase { private const string URL = "https://secure.authorize.net/gateway/transact.dll"; public AuthorizeNET() { } public override GatewayResult InternalAuthorize(int OrderID, string FirstName, string LastName, string Address1, string Address2, string City, string State, string Zip, string Country, string Phone, string CustomerID, string CustomerIP, decimal Amount, string CardNumber, string ExpDate, string CSC) { GatewayResult gwResult = new GatewayResult(); gwResult.Operation = GatewayOperation.Authorize; StreamWriter myWriter = null; string strPost = AssemblePostString(Configuration.Get("Gateways.AuthorizeNET.LoginID"), Configuration.Get("Gateways.AuthorizeNET.TransactionKey"), "CC", "AUTH_ONLY", FirstName, LastName, Address1, City, State, Zip, Phone, CustomerID, CustomerIP, Amount.ToString("f"), CardNumber, ExpDate, CSC, "", "", // no transaction id (TestMode ? "TRUE" : "FALSE")); HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(URL); objRequest.Method = "POST"; objRequest.ContentLength = strPost.Length; objRequest.ContentType = "application/x-www-form-urlencoded"; // write to the connection try { myWriter = new StreamWriter(objRequest.GetRequestStream()); myWriter.Write(strPost); } catch (Exception e) { throw e; } finally { myWriter.Close(); } // grab the response and store it to the db string result; HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) { result = sr.ReadToEnd(); // Close and clean up the StreamReader sr.Close(); } // parse the result // position 4 holds the approval text // position 5 holds the approval code // position 7 holds the transaction id string[] allResults = result.Split('|'); if (allResults[2].Equals("1")) gwResult.Successful = true; else gwResult.Successful = false; gwResult.PaymentReferenceCode = allResults[6]; gwResult.ResponseCode = allResults[2]; gwResult.ResponseText = allResults[3]; return gwResult; } public override GatewayResult InternalCapture(string PaymentReferenceCode, decimal Amount) { GatewayResult gwResult = new GatewayResult(); gwResult.Operation = GatewayOperation.Capture; // craft the post params StreamWriter myWriter = null; string strPost = AssemblePostString(Configuration.Get("Gateways.AuthorizeNET.LoginID"), Configuration.Get("Gateways.AuthorizeNET.TransactionKey"), "", "PRIOR_AUTH_CAPTURE", "", "", "", "", "", "", "", "", "", "", "", "", "", "", PaymentReferenceCode, (TestMode ? "TRUE" : "FALSE")); HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(URL); objRequest.Method = "POST"; objRequest.ContentLength = strPost.Length; objRequest.ContentType = "application/x-www-form-urlencoded"; // write to the connection try { myWriter = new StreamWriter(objRequest.GetRequestStream()); myWriter.Write(strPost); } catch (Exception e) { throw e; } finally { myWriter.Close(); } // grab the response and store it to the db string result; HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) { result = sr.ReadToEnd(); // Close and clean up the StreamReader sr.Close(); } // parse the result // position 4 holds the approval text // position 5 holds the approval code // position 7 holds the transaction id string[] allResults = result.Split('|'); gwResult.PaymentReferenceCode = allResults[6]; gwResult.Successful = allResults[2].Equals("1"); gwResult.ResponseCode = allResults[2]; gwResult.ResponseText = allResults[3]; return gwResult; } public override GatewayResult InternalVoid(string PaymentReferenceCode) { // craft the post params StreamWriter myWriter = null; string strPost = AssemblePostString(Configuration.Get("Gateways.AuthorizeNET.LoginID"), Configuration.Get("Gateways.AuthorizeNET.TransactionKey"), "", "VOID", "", "", "", "", "", "", "", "", "", "", "", "", "", "", PaymentReferenceCode, (TestMode ? "TRUE" : "FALSE")); HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(URL); objRequest.Method = "POST"; objRequest.ContentLength = strPost.Length; objRequest.ContentType = "application/x-www-form-urlencoded"; // write to the connection try { myWriter = new StreamWriter(objRequest.GetRequestStream()); myWriter.Write(strPost); } catch (Exception e) { throw e; } finally { myWriter.Close(); } // grab the response and store it to the db string result; HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) { result = sr.ReadToEnd(); // Close and clean up the StreamReader sr.Close(); } GatewayResult gwResult = new GatewayResult(); gwResult.Operation = GatewayOperation.Void; // parse the result // position 4 holds the approval text // position 5 holds the approval code // position 7 holds the transaction id string[] allResults = result.Split('|'); gwResult.PaymentReferenceCode = allResults[6]; gwResult.Successful = allResults[2].Equals("1"); gwResult.ResponseCode = allResults[2]; gwResult.ResponseText = allResults[3]; return gwResult; } public override GatewayResult InternalCredit(string PaymentReferenceCode, string LastFour, decimal Amount) { // craft the post params StreamWriter myWriter = null; string strPost = AssemblePostString(Configuration.Get("Gateways.AuthorizeNET.LoginID"), Configuration.Get("Gateways.AuthorizeNET.TransactionKey"), "", "CREDIT", "", "", "", "", "", "", "", "", "", String.Format("{0:f}", Amount), LastFour, "", "", "", PaymentReferenceCode, (TestMode ? "TRUE" : "FALSE")); HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(URL); objRequest.Method = "POST"; objRequest.ContentLength = strPost.Length; objRequest.ContentType = "application/x-www-form-urlencoded"; // write to the connection try { myWriter = new StreamWriter(objRequest.GetRequestStream()); myWriter.Write(strPost); } catch (Exception e) { throw e; } finally { myWriter.Close(); } // grab the response and store it to the db string result; HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) { result = sr.ReadToEnd(); // Close and clean up the StreamReader sr.Close(); } GatewayResult gwResult = new GatewayResult(); gwResult.Operation = GatewayOperation.Credit; // parse the result // position 4 holds the approval text // position 5 holds the approval code // position 7 holds the transaction id string[] allResults = result.Split('|'); gwResult.PaymentReferenceCode = allResults[6]; gwResult.Successful = allResults[2].Equals("1"); gwResult.ResponseCode = allResults[2]; gwResult.ResponseText = allResults[3]; return gwResult; } #region helper functions public string AssemblePostString(string loginid, string transactionkey, string method, string type, string firstname, string lastname, string address, string city, string state, string zip, string phone, string customer_id, string customer_ip, string amount, string cardnum, string expdate, string cardcode, string description, string transid, string testmode) { return "x_login=" + loginid + "&x_tran_key=" + transactionkey + "&x_method=" + method + "&x_type=" + type + "&x_amount=" + amount + "&x_card_num=" + cardnum + "&x_exp_date=" + expdate + "&x_card_code=" + cardcode + "&x_first_name=" + firstname + "&x_first_name=" + firstname + "&x_last_name=" + lastname + "&x_address=" + address + "&x_city=" + city + "&x_state=" + state + "&x_zip=" + zip + "&x_phone=" + phone + "&x_cust_id=" + customer_id + "&x_customer_ip=" + customer_ip + "&x_description=" + description + "&x_test_request=" + testmode + "&x_trans_id=" + transid + "&x_delim_data=TRUE&x_delim_char=|&x_relay_response=FALSE&x_version=3.1"; } #endregion } }