Пример #1
0
        public ActionResult SubmitButton()
        {
            try
            {
                //user validation
                if ((Services.WorkContext.CurrentCustomer.IsGuest() && !_orderSettings.AnonymousCheckoutAllowed))
                {
                    return(RedirectToRoute("Login"));
                }

                var store    = Services.StoreContext.CurrentStore;
                var customer = Services.WorkContext.CurrentCustomer;
                var settings = Services.Settings.LoadSetting <PayPalExpressPaymentSettings>(store.Id);
                var cart     = Services.WorkContext.CurrentCustomer.GetCartItems(ShoppingCartType.ShoppingCart, store.Id);

                if (cart.Count == 0)
                {
                    return(RedirectToRoute("ShoppingCart"));
                }

                if (String.IsNullOrEmpty(settings.ApiAccountName))
                {
                    throw new ApplicationException("PayPal API Account Name is not set");
                }
                if (String.IsNullOrEmpty(settings.ApiAccountPassword))
                {
                    throw new ApplicationException("PayPal API Password is not set");
                }
                if (String.IsNullOrEmpty(settings.Signature))
                {
                    throw new ApplicationException("PayPal API Signature is not set");
                }

                var provider  = PaymentService.LoadPaymentMethodBySystemName(PayPalExpressProvider.SystemName, true);
                var processor = provider != null ? provider.Value as PayPalExpressProvider : null;
                if (processor == null)
                {
                    throw new SmartException("PayPal Express Checkout module cannot be loaded");
                }

                var processPaymentRequest = new PayPalProcessPaymentRequest();

                processPaymentRequest.StoreId = store.Id;

                //Get sub-total and discounts that apply to sub-total
                decimal  orderSubTotalDiscountAmountBase = decimal.Zero;
                Discount orderSubTotalAppliedDiscount    = null;
                decimal  subTotalWithoutDiscountBase     = decimal.Zero;
                decimal  subTotalWithDiscountBase        = decimal.Zero;

                _orderTotalCalculationService.GetShoppingCartSubTotal(cart,
                                                                      out orderSubTotalDiscountAmountBase, out orderSubTotalAppliedDiscount, out subTotalWithoutDiscountBase, out subTotalWithDiscountBase);

                //order total
                decimal resultTemp = decimal.Zero;
                resultTemp += subTotalWithDiscountBase;

                //Get discounts that apply to Total
                Discount appliedDiscount = null;
                var      discountAmount  = _orderTotalCalculationService.GetOrderTotalDiscount(customer, resultTemp, out appliedDiscount);

                //if the current total is less than the discount amount, we only make the discount equal to the current total
                if (resultTemp < discountAmount)
                {
                    discountAmount = resultTemp;
                }

                //reduce subtotal
                resultTemp -= discountAmount;

                if (resultTemp < decimal.Zero)
                {
                    resultTemp = decimal.Zero;
                }

                decimal tempDiscount = discountAmount + orderSubTotalDiscountAmountBase;

                resultTemp = _currencyService.ConvertFromPrimaryStoreCurrency(resultTemp, Services.WorkContext.WorkingCurrency);
                if (tempDiscount > decimal.Zero)
                {
                    tempDiscount = _currencyService.ConvertFromPrimaryStoreCurrency(tempDiscount, Services.WorkContext.WorkingCurrency);
                }

                processPaymentRequest.PaymentMethodSystemName = PayPalExpressProvider.SystemName;
                processPaymentRequest.OrderTotal         = resultTemp;
                processPaymentRequest.Discount           = tempDiscount;
                processPaymentRequest.IsRecurringPayment = false;

                //var selectedPaymentMethodSystemName = _workContext.CurrentCustomer.GetAttribute<string>(SystemCustomerAttributeNames.SelectedPaymentMethod, _storeContext.CurrentStore.Id);

                processPaymentRequest.CustomerId = Services.WorkContext.CurrentCustomer.Id;
                this.Session["OrderPaymentInfo"] = processPaymentRequest;

                var resp = processor.SetExpressCheckout(processPaymentRequest, cart);

                if (resp.Ack == AckCodeType.Success)
                {
                    processPaymentRequest.PaypalToken         = resp.Token;
                    processPaymentRequest.OrderGuid           = new Guid();
                    processPaymentRequest.IsShippingMethodSet = ControllerContext.RouteData.IsRouteEqual("ShoppingCart", "Cart");
                    this.Session["OrderPaymentInfo"]          = processPaymentRequest;

                    _genericAttributeService.SaveAttribute <string>(customer, SystemCustomerAttributeNames.SelectedPaymentMethod, PayPalExpressProvider.SystemName, store.Id);

                    var result = new RedirectResult(String.Format(settings.GetPayPalUrl() + "?cmd=_express-checkout&useraction=commit&token={0}", resp.Token));

                    return(result);
                }
                else
                {
                    var error = new StringBuilder("We apologize, but an error has occured.<br />");
                    foreach (var errormsg in resp.Errors)
                    {
                        error.AppendLine(String.Format("{0} | {1} | {2}", errormsg.ErrorCode, errormsg.ShortMessage, errormsg.LongMessage));
                    }

                    Logger.InsertLog(LogLevel.Error, resp.Errors[0].ShortMessage, resp.Errors[0].LongMessage, customer);

                    NotifyError(error.ToString(), false);

                    return(RedirectToAction("Cart", "ShoppingCart", new { area = "" }));
                }
            }
            catch (Exception ex)
            {
                Logger.InsertLog(LogLevel.Error, ex.Message, ex.StackTrace, Services.WorkContext.CurrentCustomer);

                NotifyError(ex.Message, false);

                return(RedirectToAction("Cart", "ShoppingCart", new { area = "" }));
            }
        }