Exemplo n.º 1
0
        private void btnMakePayment_Click(object sender, EventArgs e)
        {
            if (store != null && cart != null && IsValidForCheckout())
            {
                IPaymentGateway gateway = commerceConfig.GetDirectPaymentGateway();

                if (gateway == null)
                {
                    lblMessage.Text        = WebStoreResources.PaymentGatewayNotConfiguredForDirectCardProcessing;
                    btnMakePayment.Enabled = false;

                    return;
                }

                gateway.MerchantInvoiceNumber = cart.CartGuid.ToString("N");

                var cartItems = new List <string>();

                foreach (var cartOffer in cart.CartOffers)
                {
                    cartItems.Add($"({cartOffer.Quantity}x) {cartOffer.Name} {cartOffer.OfferPrice.ToString("c", currencyCulture)} (GUID: {cartOffer.OfferGuid:N})");
                }

                gateway.MerchantTransactionDescription = string.Join("\n", cartItems);

                gateway.CardType         = ddCardType.SelectedValue;
                gateway.CardNumber       = txtCardNumber.Text;
                gateway.CardExpiration   = ddExpireMonth.SelectedValue + ddExpireYear.SelectedValue;
                gateway.ChargeTotal      = cart.OrderTotal;
                gateway.CardSecurityCode = txtCardSecurityCode.Text;

                gateway.CardOwnerFirstName = txtCardOwnerFirstName.Text;
                gateway.CardOwnerLastName  = txtCardOwnerLastName.Text;

                gateway.CardOwnerCompanyName = cart.OrderInfo.BillingCompany;
                gateway.CardBillingAddress   = cart.OrderInfo.BillingAddress1 + " " + cart.OrderInfo.BillingAddress2;

                gateway.CardBillingCity       = cart.OrderInfo.BillingCity;
                gateway.CardBillingState      = cart.OrderInfo.BillingState;
                gateway.CardBillingPostalCode = cart.OrderInfo.BillingPostalCode;
                gateway.CardBillingCountry    = cart.OrderInfo.BillingCountry;

                gateway.CardBillingCountryCode = cart.OrderInfo.BillingCountry;

                gateway.CardBillingPhone  = cart.OrderInfo.CustomerTelephoneDay;
                gateway.CustomerIPAddress = SiteUtils.GetIP4Address();
                gateway.CurrencyCulture   = currencyCulture;

                // this is where the actual request is made, it can timeout here
                bool executed;

                try
                {
                    executed = gateway.ExecuteTransaction();
                }
                catch (WebException ex)
                {
                    if (commerceConfig.PaymentGatewayUseTestMode)
                    {
                        lblMessage.Text = ex.Message;
                    }
                    else
                    {
                        lblMessage.Text = WebStoreResources.PaymentGatewayCouldNotConnectMessage;
                    }

                    return;
                }

                var serializedCart = string.Empty;

                gateway.LogTransaction(
                    siteSettings.SiteGuid,
                    store.ModuleGuid,
                    store.Guid,
                    cart.CartGuid,
                    cart.UserGuid,
                    string.Empty,
                    "WebStoreCheckout",
                    serializedCart
                    );

                if (executed)
                {
                    switch (gateway.Response)
                    {
                    case PaymentGatewayResponse.Approved:
                        cart.LastUserActivity          = DateTime.UtcNow;
                        cart.OrderInfo.CompletedFromIP = SiteUtils.GetIP4Address();
                        cart.OrderInfo.Completed       = DateTime.UtcNow;
                        StoreHelper.EnsureUserForOrder(cart);
                        cart.Save();

                        Order order = Order.CreateOrder(store, cart, gateway, siteSettings.GetCurrency().Code, "CreditCard");
                        StoreHelper.ClearCartCookie(cart.StoreGuid);

                        // send confirmation email
                        try
                        {
                            // this also happens in StoreHelper.ConfirmOrder
                            StoreHelper.ConfirmOrder(store, order);
                            PayPalLog.DeleteByCart(order.OrderGuid);
                            GoogleCheckoutLog.DeleteByCart(order.OrderGuid);
                        }
                        catch (Exception ex)
                        {
                            log.Error("error sending confirmation email", ex);
                        }

                        // redirect to order details
                        string redirectUrl = SiteRoot +
                                             "/WebStore/OrderDetail.aspx?pageid="
                                             + pageId.ToInvariantString()
                                             + "&mid=" + moduleId.ToInvariantString()
                                             + "&orderid=" + order.OrderGuid.ToString();

                        //TODO: if we charged a card here we can safely delete any paypal log or googlecheckout logs
                        // need methods to delete those by carguid

                        if (WebStoreConfiguration.LogCardTransactionStatus)
                        {
                            log.Info("accepted transaction " + gateway.ChargeTotal.ToString("c") + " " + gateway.ResponseCode + " " + gateway.ResponseReason);
                        }

                        WebUtils.SetupRedirect(this, redirectUrl);

                        return;

                    case PaymentGatewayResponse.Declined:

                        lblMessage.Text = WebStoreResources.TransactionDeclinedMessage;

                        if (WebStoreConfiguration.LogCardTransactionStatus || WebStoreConfiguration.LogCardFailedTransactionStatus)
                        {
                            log.Info("declined transaction " + gateway.ChargeTotal.ToString("c") + " " + gateway.ResponseCode + " " + gateway.ResponseReason);
                        }

                        break;

                    case PaymentGatewayResponse.Error:

                        if (gateway.UseTestMode)
                        {
                            if (gateway.LastExecutionException != null)
                            {
                                lblMessage.Text = gateway.LastExecutionException.ToString();
                            }
                            else
                            {
                                // TODO: should not show user real messages? Mask CCNumber and login credentials in the gateways RawResponse property ... those shouldn't be logged either
                                lblMessage.Text = gateway.RawResponse;
                            }
                        }
                        else
                        {
                            lblMessage.Text = WebStoreResources.TransactionErrorMessage;

                            if (WebStoreConfiguration.LogCardTransactionStatus || WebStoreConfiguration.LogCardFailedTransactionStatus)
                            {
                                if (gateway.LastExecutionException != null)
                                {
                                    log.Info("transaction error " + gateway.LastExecutionException.ToString());
                                }
                            }
                        }

                        break;

                    case PaymentGatewayResponse.NoRequestInitiated:

                        lblMessage.Text = WebStoreResources.TransactionNotInitiatedMessage;

                        break;
                    }
                }
                else
                {
                    lblMessage.Text = WebStoreResources.TransactionNotInitiatedMessage + (!string.IsNullOrEmpty(gateway.ResponseReason) ? "<br>" + gateway.ResponseReason : string.Empty);

                    if (gateway.LastExecutionException != null)
                    {
                        if (commerceConfig.PaymentGatewayUseTestMode)
                        {
                            lblMessage.Text = gateway.LastExecutionException.ToString();
                        }
                    }
                }
            }

            btnMakePayment.Text = WebStoreResources.PaymentButton;
        }
Exemplo n.º 2
0
        private void btnMakePayment_Click(object sender, System.EventArgs e)
        {
            //Page.Validate();

            if (
                (store != null) &&
                (cart != null) &&
                (IsValidForCheckout())
                )
            {
                IPaymentGateway gateway = commerceConfig.GetDirectPaymentGateway();
                if (gateway == null)
                {
                    lblMessage.Text        = WebStoreResources.PaymentGatewayNotConfiguredForDirectCardProcessing;
                    btnMakePayment.Enabled = false;
                    return;
                }
                //if (gateway is PlugNPayPaymentGateway)
                //{
                gateway.MerchantInvoiceNumber = cart.CartGuid.ToString("N");
                string CartItems = "";
                int    itemnum   = 0;
                foreach (CartOffer coffer in cart.CartOffers)
                {
                    itemnum++;
                    CartItems += string.Format("&item{1}={0}&cost{1}={2}&description{1}={3}&quantity{1}={4}", coffer.OfferGuid, itemnum, coffer.OfferPrice, coffer.Name, coffer.Quantity);
                }
                gateway.MerchantTransactionDescription = CartItems;     //not sure if this is the intended purpose or not
                //}

                gateway.CardType         = ddCardType.SelectedValue;
                gateway.CardNumber       = txtCardNumber.Text;
                gateway.CardExpiration   = ddExpireMonth.SelectedValue + ddExpireYear.SelectedValue;
                gateway.ChargeTotal      = cart.OrderTotal;
                gateway.CardSecurityCode = txtCardSecurityCode.Text;

                gateway.CardOwnerFirstName = txtCardOwnerFirstName.Text;
                gateway.CardOwnerLastName  = txtCardOwnerLastName.Text;

                gateway.CardOwnerCompanyName = cart.OrderInfo.BillingCompany;
                gateway.CardBillingAddress   = cart.OrderInfo.BillingAddress1
                                               + " " + cart.OrderInfo.BillingAddress2;

                gateway.CardBillingCity       = cart.OrderInfo.BillingCity;
                gateway.CardBillingState      = cart.OrderInfo.BillingState;
                gateway.CardBillingPostalCode = cart.OrderInfo.BillingPostalCode;
                gateway.CardBillingCountry    = cart.OrderInfo.BillingCountry;


                gateway.CardBillingCountryCode = cart.OrderInfo.BillingCountry;

                gateway.CardBillingPhone  = cart.OrderInfo.CustomerTelephoneDay;
                gateway.CustomerIPAddress = SiteUtils.GetIP4Address();
                gateway.CurrencyCulture   = currencyCulture;

                // this is where the actual request is made, it can timeout here
                bool executed = false;
                try
                {
                    executed = gateway.ExecuteTransaction();
                }
                catch (WebException ex)
                {
                    if (commerceConfig.PaymentGatewayUseTestMode)
                    {
                        lblMessage.Text = ex.Message;
                    }
                    else
                    {
                        lblMessage.Text = WebStoreResources.PaymentGatewayCouldNotConnectMessage;
                    }
                    return;
                }

                string serializedCart = string.Empty;

                gateway.LogTransaction(
                    siteSettings.SiteGuid,
                    store.ModuleGuid,
                    store.Guid,
                    cart.CartGuid,
                    cart.UserGuid,
                    string.Empty,
                    "WebStoreCheckout",
                    serializedCart);

                //if (gateway is PayPalDirectPaymentGateway)
                //{
                //    // this is capturing of the serialized cart is not needed for other providers and I'm not sure its even needed for PayPal Direct
                //    // it was needed for other paypal solutions. I just don't want to remove it until I'm sure
                //    cart.SerializeCartOffers();
                //    serializedCart = SerializationHelper.SerializeToString(cart);

                //    gateway.LogTransaction(
                //        siteSettings.SiteGuid,
                //        store.ModuleGuid,
                //        store.Guid,
                //        cart.CartGuid,
                //        cart.UserGuid,
                //        "WebStorePayPalDirect",
                //        "DirectPayment",
                //        serializedCart);

                //}
                //else
                //{
                //    gateway.LogTransaction(
                //        siteSettings.SiteGuid,
                //        store.ModuleGuid,
                //        store.Guid,
                //        cart.CartGuid,
                //        cart.UserGuid,
                //        string.Empty,
                //        "WebStoreCheckout",
                //        serializedCart);
                //}


                if (executed)
                {
                    switch (gateway.Response)
                    {
                    case PaymentGatewayResponse.Approved:
                        cart.LastUserActivity          = DateTime.UtcNow;
                        cart.OrderInfo.CompletedFromIP = SiteUtils.GetIP4Address();
                        cart.OrderInfo.Completed       = DateTime.UtcNow;
                        StoreHelper.EnsureUserForOrder(cart);
                        cart.Save();

                        //Order order = Order.CreateOrder(store, cart, gateway, store.DefaultCurrency, "CreditCard");
                        Order order = Order.CreateOrder(store, cart, gateway, siteSettings.GetCurrency().Code, "CreditCard");
                        StoreHelper.ClearCartCookie(cart.StoreGuid);

                        // send confirmation email
                        try
                        {
                            // this also happens in StoreHelper.ConfirmOrder
                            //Module m = new Module(store.ModuleId);
                            //Order.EnsureSalesReportData(m.ModuleGuid, pageId, moduleId);

                            StoreHelper.ConfirmOrder(store, order);
                            PayPalLog.DeleteByCart(order.OrderGuid);
                            GoogleCheckoutLog.DeleteByCart(order.OrderGuid);
                        }
                        catch (Exception ex)
                        {
                            log.Error("error sending confirmation email", ex);
                        }

                        // redirect to order details
                        string redirectUrl = SiteRoot +
                                             "/WebStore/OrderDetail.aspx?pageid="
                                             + pageId.ToInvariantString()
                                             + "&mid=" + moduleId.ToInvariantString()
                                             + "&orderid=" + order.OrderGuid.ToString();

                        //TODO: if we charged a card here we can safely delete any paypal log or googlecheckout logs
                        // need methods to delete those by carguid


                        if (WebStoreConfiguration.LogCardTransactionStatus)
                        {
                            log.Info("accepted transaction " + gateway.ChargeTotal.ToString("c") + " " + gateway.ResponseCode + " " + gateway.ResponseReason);
                        }

                        WebUtils.SetupRedirect(this, redirectUrl);
                        return;

                    case PaymentGatewayResponse.Declined:

                        lblMessage.Text = WebStoreResources.TransactionDeclinedMessage;

                        if ((WebStoreConfiguration.LogCardTransactionStatus) || (WebStoreConfiguration.LogCardFailedTransactionStatus))
                        {
                            log.Info("declined transaction " + gateway.ChargeTotal.ToString("c") + " " + gateway.ResponseCode + " " + gateway.ResponseReason);
                        }

                        break;


                    case PaymentGatewayResponse.Error:

                        if (gateway.UseTestMode)
                        {
                            if (gateway.LastExecutionException != null)
                            {
                                lblMessage.Text = gateway.LastExecutionException.ToString();
                            }
                            else
                            {
                                // TODO: should not show user real messages? Mask CCNumber and login credentials in the gateways RawResponse property ... those shouldn't be logged either
                                lblMessage.Text = gateway.RawResponse;
                            }
                        }
                        else
                        {
                            lblMessage.Text = WebStoreResources.TransactionErrorMessage;
                            if ((WebStoreConfiguration.LogCardTransactionStatus) || (WebStoreConfiguration.LogCardFailedTransactionStatus))
                            {
                                if (gateway.LastExecutionException != null)
                                {
                                    log.Info("transaction error " + gateway.LastExecutionException.ToString());
                                }
                            }
                        }


                        break;

                    case PaymentGatewayResponse.NoRequestInitiated:

                        lblMessage.Text = WebStoreResources.TransactionNotInitiatedMessage;
                        break;
                    }
                }
                else
                {
                    lblMessage.Text = WebStoreResources.TransactionNotInitiatedMessage;

                    if (gateway.LastExecutionException != null)
                    {
                        if (commerceConfig.PaymentGatewayUseTestMode)
                        {
                            lblMessage.Text = gateway.LastExecutionException.ToString();
                        }
                    }
                }
            }

            btnMakePayment.Text = WebStoreResources.PaymentButton;
        }