Here is this code-
using System; using System.Collections; using System.Collections.Specialized; using System.Globalization; using System.IO; using System.Net; using System.Text; using System.Web; using DotNetNuke.Modules.Store.Cart; using DotNetNuke.Modules.Store.Components; using DotNetNuke.Modules.Store.Customer; using DotNetNuke.Modules.Store.Providers.Address; using DotNetNuke.Services.Localization;
namespace DotNetNuke.Modules.Store.Cart { /// /// Summary description for AuthNetGatewayProvider. /// public class AuthNetGatewayProvider { #region Constructors public AuthNetGatewayProvider(string gatewaySettings) { _gatewaySettings = gatewaySettings; } #endregion
#region Private Declarations private string _gatewaySettings = string.Empty; #endregion
#region Public Methods public TransactionResult ProcessTransaction(IAddressInfo shipping, IAddressInfo billing, OrderInfo orderInfo, object transDetails) { TransactionResult result = new TransactionResult();
CultureInfo ci_enUS = new CultureInfo("en-US");
// Check data before performing transaction AuthNetSettings settings = new AuthNetSettings(_gatewaySettings); if ((settings == null) || (!settings.IsValid())) { result.Succeeded = false; result.ResultCode = -3;
return result; }
if (billing == null) { result.Succeeded = false; result.ResultCode = -4;
return result; }
TransactionDetails trans = new TransactionDetails(transDetails as string); if ((trans == null) || (!trans.IsValid())) { result.Succeeded = false; result.ResultCode = -5;
return result; }
// Gather transaction information string url = settings.GatewayURL; string firstName = billing.FirstName; string lastName = billing.LastName; string address = billing.Address1 + " " + billing.Address2; address = address.Trim();
NameValueCollection NVCol = new NameValueCollection(); // Merchant infos NVCol.Add("x_login", settings.Username); NVCol.Add("x_tran_key", settings.Password); NVCol.Add("x_version", settings.Version); NVCol.Add("x_test_request", settings.IsTest.ToString()); // Init infos NVCol.Add("x_delim_data", "True"); NVCol.Add("x_delim_char", "~"); NVCol.Add("x_encap_char", "'"); NVCol.Add("x_relay_response", "False"); // Billing infos NVCol.Add("x_first_name", firstName); NVCol.Add("x_last_name", lastName); NVCol.Add("x_company", ""); NVCol.Add("x_address", address); NVCol.Add("x_city", billing.City); NVCol.Add("x_state", billing.RegionCode); NVCol.Add("x_zip", billing.PostalCode); NVCol.Add("x_country", billing.CountryCode); NVCol.Add("x_phone", billing.Phone1); // Shipping infos firstName = shipping.FirstName; lastName = shipping.LastName; address = shipping.Address1 + " " + shipping.Address2; address = address.Trim(); NVCol.Add("x_ship_to_first_name", firstName); NVCol.Add("x_ship_to_last_name", lastName); NVCol.Add("x_ship_to_company", ""); NVCol.Add("x_ship_to_address", address); NVCol.Add("x_ship_to_city", shipping.City); NVCol.Add("x_ship_to_state", shipping.RegionCode); NVCol.Add("x_ship_to_zip", shipping.PostalCode); NVCol.Add("x_ship_to_country", shipping.CountryCode); // Customer infos NVCol.Add("x_cust_id", billing.UserID.ToString()); NVCol.Add("x_customer_ip", HttpContext.Current.Request.UserHostAddress); // Order infos NVCol.Add("x_invoice_num", orderInfo.OrderID.ToString()); NVCol.Add("x_amount", orderInfo.GrandTotal.ToString("0.00", ci_enUS)); NVCol.Add("x_tax", orderInfo.Tax.ToString("0.00", ci_enUS)); NVCol.Add("x_freight", orderInfo.ShippingCost.ToString("0.00", ci_enUS)); // Transaction infos NVCol.Add("x_method", "CC"); NVCol.Add("x_type", settings.Capture.ToString()); NVCol.Add("x_recurring_billing", "NO"); NVCol.Add("x_card_num", trans.CardNumber); NVCol.Add("x_card_code", trans.VerificationCode); NVCol.Add("x_exp_date", trans.ExpirationMonth.ToString("00") + "/" + trans.ExpirationYear.ToString()); // Order details ArrayList line_items = new ArrayList(); string fieldSep = "<|>"; OrderController orderController = new OrderController(); ArrayList orderDetails = orderController.GetOrderDetails(orderInfo.OrderID); foreach (OrderDetailsInfo itemInfo in orderDetails) { line_items.Add(itemInfo.ModelNumber + fieldSep + itemInfo.ModelName + fieldSep + fieldSep + itemInfo.Quantity.ToString() + fieldSep + itemInfo.UnitCost.ToString("0.00", ci_enUS) + fieldSep + "Y"); }
// Perform transaction try { Encoding enc = Encoding.GetEncoding(1252); StreamReader loResponseStream = new StreamReader(PostEx(url, NVCol, line_items).GetResponseStream(), enc); string lcHtml = loResponseStream.ReadToEnd(); loResponseStream.Close(); string[] resultArray = Microsoft.VisualBasic.Strings.Split(lcHtml.TrimStart("'".ToCharArray()), "'~'", -1, Microsoft.VisualBasic.CompareMethod.Binary);
result.Succeeded = (resultArray[0] == "1"); result.ResultCode = int.Parse(resultArray[0]); result.ReasonCode = int.Parse(resultArray[2]); result.Message = resultArray[3]; } catch (Exception ex) { result.Succeeded = false; result.ResultCode = -2; result.Message = ex.Message; }
return result; } #endregion
#region Private Methods private WebResponse PostEx(string url, NameValueCollection values, ArrayList line_items) { WebRequest request = null; StringBuilder builder = null; string[] keys = null; Stream stream = null; byte[] bytes = null; request = WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded";
if (values.Count == 0) { request.ContentLength = 0; } else { builder = new StringBuilder(); keys = values.AllKeys; foreach (string key in keys) { if (builder.Length > 0) { builder.Append("&"); } builder.Append(HttpUtility.UrlEncode(key)); builder.Append("="); builder.Append(HttpUtility.UrlEncode(values[key])); }
string WIPorder = builder.ToString();
foreach (string value in line_items) { WIPorder += ("&x_line_item=" + HttpUtility.UrlEncode(value)); }
bytes = Encoding.UTF8.GetBytes(WIPorder.ToString()); request.ContentLength = bytes.Length; stream = request.GetRequestStream(); stream.Write(bytes, 0, bytes.Length); stream.Close(); } return request.GetResponse(); } #endregion } }
|