using System; using System.Data; namespace Gateways { // every gateway module must do its OWN database work! public abstract class GatewayBase { protected DatabaseClass Database; public bool TestMode; #region statics - modify this when adding a new gateway public static GatewayBase GetGateway(string s) { switch (s) { case "Authorize.NET": return new AuthorizeNET(); case "PayFlowPro": return new PayFlowPro(); case "Order Email": return new OrderEmail(); default: return new NullGateway(); } } #endregion #region Methods and members provided for each gateway to use public GatewayBase() { TestMode = Configuration.Get("TestMode").ToLower().Equals("on"); Database = DatabaseClass.Current; } public bool AuthorizeFunds(int OrderID, string CardNumber, string ExpDate, string CSC) { DataRow dr = Database.Orders_GetOrderAndBillingAddress(OrderID); string firstname, lastname, address, city, state, zip, country, phone, customerid, customerip; decimal amount = 0; firstname = (string)dr["FirstName"]; lastname = (string)dr["LastName"]; address = (string)dr["Address"]; city = (string)dr["City"]; state = (string)dr["State"]; zip = (string)dr["ZipCode"]; country = (string)dr["Country"]; phone = (string)dr["Phone"]; customerid = dr["CustomerID"].ToString(); customerip = (string)dr["IPAddress"]; amount = decimal.Round((decimal)dr["Amount"], 2); GatewayResult result = InternalAuthorize(OrderID, firstname, lastname, address, "", city, state, zip, country, phone, customerid, customerip, amount, CardNumber, ExpDate, CSC); Database.Orders_UpdateForPayment(OrderID, result.PaymentReferenceCode, result.Successful, false, result.ResponseCode, result.ResponseText); if(result.Successful) { if((bool)dr["IsShipped"]) { Database.Orders_SetOrderStatusID(OrderID, 3); } else { Database.Orders_SetOrderStatusID(OrderID, 1); } } return result.Successful; } public bool CaptureFunds(int OrderID) { string RefCode; int CustomerID; decimal Amount; DataRow dr = Database.Orders_GetByOrderID(OrderID); Amount = (decimal)dr["Amount"]; CustomerID = (int)dr["CustomerID"]; RefCode = (string)dr["PaymentRefCode"]; GatewayResult result = InternalCapture(RefCode, Amount); Database.Orders_UpdateForPayment(OrderID, result.PaymentReferenceCode, true, result.Successful, result.ResponseCode, result.ResponseText); if (result.Successful) { if ((int)dr["OrderStatusID"] == 3) Database.Orders_SetOrderStatusID(OrderID, 13); // 13 = completed else Database.Orders_SetOrderStatusID(OrderID, 14); // 14 = captured } return result.Successful; } public bool VoidFunds(int OrderID) { string RefCode; DataRow dr = Database.Orders_GetByOrderID(OrderID); RefCode = (string)dr["PaymentRefCode"]; GatewayResult result = InternalVoid(RefCode); Database.Orders_UpdateForPayment(OrderID, result.PaymentReferenceCode, false, null, result.ResponseCode, result.ResponseText); return result.Successful; } public bool CreditFunds(int OrderID, decimal Amount) { string RefCode, LastFour; DataRow dr = Database.Orders_GetByOrderID(OrderID); RefCode = (string)dr["PaymentRefCode"]; LastFour = (string)dr["LastFour"]; GatewayResult result = InternalCredit(RefCode, LastFour, Amount); if (result.Successful) { Database.Orders_UpdateForPayment(OrderID, result.PaymentReferenceCode, false, false, result.ResponseCode, result.ResponseText); } else { Database.Orders_UpdateForPayment(OrderID, result.PaymentReferenceCode, null, null, result.ResponseCode, result.ResponseText); } return result.Successful; } #endregion #region abstracts - to be implemented by each gateway public abstract 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); public abstract GatewayResult InternalCapture(string PaymentReferenceCode, decimal Amount); public abstract GatewayResult InternalVoid(string PaymentReferenceCode); public abstract GatewayResult InternalCredit(string PaymentReferenceCode, string LastFour, decimal Amount); #endregion } }