示例#1
0
 public static bool Supports(this IPaymentHandlerPlugin plugin, PaymentOperation operation)
 {
     return(plugin.SupportedOperations.Contains(operation));
 }
示例#2
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);
        }