protected void btnProcess_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (!Page.IsValid)
            {
                return;
            }

            StoreInfo storeInfo = CheckoutControl.StoreData;
            IAddressInfo shippingAddress = CheckoutControl.ShippingAddress;
            IAddressInfo billingAddress = CheckoutControl.BillingAddress;
            OrderInfo orderInfo = CheckoutControl.GetFinalizedOrderInfo();
            //lblOrderNumber.Text = orderInfo.OrderID.ToString();

            PortalSecurity security = new PortalSecurity();

            TransactionDetails transaction = new TransactionDetails();
            //transaction.CardType = (TransactionDetails.CardTypes)Enum.Parse(typeof(TransactionDetails.CardTypes), rbCard.SelectedValue);
            //transaction.CardType = null;
            transaction.CardNumber = security.InputFilter(txtNumber.Text, PortalSecurity.FilterFlag.NoMarkup | PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoSQL);;
            //transaction.NameOnCard = security.InputFilter(txtName.Text, PortalSecurity.FilterFlag.NoMarkup | PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoSQL);;
            transaction.NameOnCard = string.Empty;
            transaction.VerificationCode = int.Parse(txtVer.Text);
            transaction.ExpirationMonth = int.Parse(ddlMonth.SelectedValue);
            transaction.ExpirationYear = int.Parse(ddlYear.SelectedValue);

            if (transaction.IsValid())
            {
                AuthNetGatewayProvider provider = new AuthNetGatewayProvider(storeInfo.GatewaySettings);

                TransactionResult orderResult = provider.ProcessTransaction(shippingAddress, billingAddress, orderInfo, transaction.ToString());
                if (!orderResult.Succeeded)
                {
                    litError.Text = Localization.GetString(orderResult.Message.ToString(), this.LocalResourceFile);
                }
                else
                {
                    invokePaymentSucceeded();

                    //Clear basket
                    CurrentCart.ClearItems(PortalId);

                    //Clear cookies
                    SetOrderIdCookie(-1);
                }
            }
            else
            {
                litError.Text = Localization.GetString("ErrorCardNotValid", this.LocalResourceFile);
            }
        }
Esempio n. 2
0
        public TransactionResult ProcessTransaction(IAddressInfo shipping, IAddressInfo billing, 
			OrderInfo orderInfo, object transDetails)
        {
            TransactionResult result = new TransactionResult();

            // Check data before performing transaction
            AuthNetSettings settings = new AuthNetSettings(_gatewaySettings);
            if ((settings == null) || (!settings.IsValid()))
            {
                result.Succeeded = false;
                result.Message = "ErrorPaymentOption";
                return result;
            }

            if (billing == null)
            {
                result.Succeeded = false;
                result.Message = "ErrorBillingAddress";
                return result;
            }

            TransactionDetails trans = new TransactionDetails(transDetails as string);
            if ((trans == null) || (!trans.IsValid()))
            {
                result.Succeeded = false;
                result.Message = "ErrorCardInformation";
                return result;
            }

            // Gather transaction information
            string url = settings.GatewayURL;
            string firstName = string.Empty;
            string lastName = trans.NameOnCard;
            if (lastName.IndexOf(" ") >= 0)
            {
                firstName = lastName.Substring(0, lastName.IndexOf(" ")).Trim();
                lastName = lastName.Substring(lastName.LastIndexOf(" ")).Trim();
            }

            string address = billing.Address1 + " " + billing.Address2;
            address = address.Trim();

            NameValueCollection NVCol = new NameValueCollection();

            //NVCol.Add("x_version",				settings.Version);
            NVCol.Add("x_delim_data",			"True");
            NVCol.Add("x_relay_response",       "False");
            NVCol.Add("x_login",				settings.Username);
            NVCol.Add("x_tran_key",				settings.Password);
            NVCol.Add("x_test_request",			settings.IsTest.ToString());
            NVCol.Add("x_delim_char",			"~");
            NVCol.Add("x_encap_char",			"'");

            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);
            NVCol.Add("x_invoice_num",			orderInfo.OrderID.ToString());

            NVCol.Add("x_amount",				orderInfo.OrderTotal.ToString());
            NVCol.Add("x_method",				"CC");
            NVCol.Add("x_card_num",				trans.CardNumber);
            NVCol.Add("x_card_code",			trans.VerificationCode.ToString());
            NVCol.Add("x_exp_date",				trans.ExpirationMonth.ToString() + "/" + trans.ExpirationYear.ToString());
            NVCol.Add("x_recurring_billing",	"NO");
            NVCol.Add("x_type",					settings.Capture.ToString());

            // Perform transaction
            try
            {
                Encoding enc = Encoding.GetEncoding(1252);
                StreamReader loResponseStream = new StreamReader(PostEx(url, NVCol).GetResponseStream(), enc);

                string lcHtml = loResponseStream.ReadToEnd();
                loResponseStream.Close();

                string[] resultArray = Microsoft.VisualBasic.Strings.Split(lcHtml.TrimStart("'".ToCharArray()), "'~'", -1, Microsoft.VisualBasic.CompareMethod.Binary);

                //TODO: What transaction details to return???
                result.Succeeded = (resultArray[0] == "1");
                result.Message = resultArray[3];
            }
            catch (Exception ex)
            {
                //Return error
                string[] resultArray = Microsoft.VisualBasic.Strings.Split("2|0|0|No Connection Available", "|", -1, Microsoft.VisualBasic.CompareMethod.Binary);

                //TODO: What transaction details to return???
                result.Succeeded = false;
                result.Message = ex.Message;
                //result.Message = resultArray[3];
            }

            return result;
        }