예제 #1
0
        public IActionResult StoreCreditsList(StoreCreditSearchModel searchModel)
        {
            if (searchModel.UserId == 0)
            {
                return(R.Fail.WithError(ErrorCodes.ParentEntityMustBeNonZero).Result);
            }

            var userId = searchModel.UserId;

            if (userId <= 0 || _userService.Count(x => x.Id == userId) == 0)
            {
                return(NotFound());
            }
            var userCredits  = _storeCreditService.Get(out int totalResults, x => x.UserId == userId, x => x.Id, RowOrder.Descending, searchModel.Current, searchModel.RowCount);
            var creditsTotal = _storeCreditService.GetBalance(userId);
            var models       = userCredits.Select(_userModelFactory.Create).ToList();

            return(R.Success.With("userId", userId).With("storeCredits", models).With("availableBalance", creditsTotal)
                   .WithGridResponse(totalResults, searchModel.Current, searchModel.RowCount).Result);
        }
예제 #2
0
        public IActionResult StoreCreditsInfo(int page = 1)
        {
            if (page < 1)
            {
                page = 1;
            }
            var count        = 15;
            var storeCredits = _storeCreditService.Get(out var total, x => x.UserId == CurrentUser.Id, x => x.Id, RowOrder.Descending,
                                                       page, count);
            var balance           = _storeCreditService.GetBalance(CurrentUser.Id);
            var storeCreditModels = storeCredits.Select(x => new StoreCreditModel()
            {
                Description = x.Description,
                CreatedOn   = x.CreatedOn,
                AvailableOn = x.AvailableOn,
                Credit      = x.Credit,
                ExpiresOn   = x.ExpiresOn
            }).ToList();

            return(R.Success.WithGridResponse(total, page, count).With("storeCredits", storeCreditModels)
                   .With("availableBalance", balance).Result);
        }
예제 #3
0
        public IActionResult PaymentInfoSave(PaymentMethodModel requestModel)
        {
            Order order = null;
            Cart  cart  = null;

            if (!requestModel.OrderGuid.IsNullEmptyOrWhiteSpace())
            {
                order = _orderService.GetByGuid(requestModel.OrderGuid);
                if (order == null || order.PaymentStatus != PaymentStatus.Pending)
                {
                    return(R.Fail.With("error", T("The order has been already paid")).Result);
                }
            }
            else
            {
                if (!CanCheckout(out cart))
                {
                    return(R.Fail.Result);
                }
            }

            if (order == null && cart == null)
            {
                return(R.Fail.Result);
            }
            var paymentMethodRequired = true;

            //can we use store credits
            if (_affiliateSettings.AllowStoreCreditsForPurchases && requestModel.UseStoreCredits)
            {
                //get the balance
                var balance = _storeCreditService.GetBalance(CurrentUser.Id);
                paymentMethodRequired = _affiliateSettings.MinimumStoreCreditsToAllowPurchases > balance || balance < (order?.OrderTotal ?? cart.FinalAmount);
            }

            IPaymentHandlerPlugin paymentHandler = null;
            var formAsDictionary =
                requestModel.FormCollection.Keys.ToDictionary(x => x, x => requestModel.FormCollection[x].ToString());

            if (paymentMethodRequired)
            {
                //check if payment method is valid
                paymentHandler = PluginHelper.GetPaymentHandler(requestModel.SystemName);
                if (paymentHandler == null)
                {
                    return(R.Fail.With("error", T("Payment method unavailable")).Result);
                }

                if (!paymentHandler.ValidatePaymentInfo(formAsDictionary, out string error))
                {
                    return(R.Fail.With("error", error).Result);
                }
            }


            CustomResponse response;

            if (order != null)
            {
                order.PaymentMethodName = requestModel.SystemName;
                order.PaymentMethodFee  = paymentMethodRequired ? paymentHandler.GetPaymentHandlerFee(order) : 0;
                order.OrderTotal        = order.Subtotal + order.Tax + (order.PaymentMethodFee ?? 0) + (order.ShippingMethodFee ?? 0);
                //check if discount is available
                if (order.DiscountId.HasValue)
                {
                    if (_priceAccountant.CanApplyDiscount(order.DiscountId.Value, order.UserId, out _))
                    {
                        order.OrderTotal -= order.Discount;
                    }
                }
                _orderService.Update(order);
                var creditAmount = 0m;
                if (requestModel.UseStoreCredits)
                {
                    //do we have available credit
                    GetAvailableStoreCredits(out _, out creditAmount, ApplicationEngine.BaseCurrency, null);
                }
                //process the payment immediately
                ProcessPayment(order, creditAmount, formAsDictionary.ToDictionary(x => x.Key, x => (object)x.Value), out response);
                return(response.Result);
            }
            else
            {
                //if we are here, the payment method can be saved
                cart.PaymentMethodName = paymentMethodRequired ? requestModel.SystemName : "";
                //always store encrypted information about payment data
                cart.PaymentMethodData        = _cryptographyService.Encrypt(_dataSerializer.Serialize(formAsDictionary));
                cart.PaymentMethodDisplayName = paymentMethodRequired ? paymentHandler.PluginInfo.Name : "Store Credits";
                cart.UseStoreCredits          = requestModel.UseStoreCredits;
                _cartService.Update(cart);

                RaiseEvent(NamedEvent.OrderPaymentInfoSaved, cart);
            }

            response = R.Success;
            if (!requestModel.OrderGuid.IsNullEmptyOrWhiteSpace())
            {
                response.With("orderGuid", requestModel.OrderGuid);
            }
            return(response.Result);
        }