Exemplo n.º 1
0
        public async Task <IActionResult> Charge(string ref_payco)
        {
            var epaycoProvider = await _paymentProviderRepository.Query().FirstOrDefaultAsync(x => x.Id == PaymentProviderHelper.EPayCoProviderId);

            var epaycoSetting = JsonConvert.DeserializeObject <EPayCoConfigForm>(epaycoProvider.AdditionalSettings);
            //var epaycoChargeService = new ChargeService(epaycoSetting);
            var epaycoValidationService = new ConfirmationService(epaycoSetting);
            var currentUser             = await _workContext.GetCurrentUser();

            var cart = await _cartService.GetActiveCart(currentUser.Id);

            if (cart == null)
            {
                return(NotFound());
            }

            var orderCreationResult = await _orderService.CreateOrder(cart.Id, "EPayCo", 0, OrderStatus.PendingPayment);

            if (!orderCreationResult.Success)
            {
                TempData["Error"] = orderCreationResult.Error;
                return(Redirect("~/checkout/payment"));
            }

            var order = orderCreationResult.Value;
            var zeroDecimalOrderAmount = order.OrderTotal;
            var zeroDecimalTax         = order.TaxAmount;
            var zeroDecimalSubTotal    = order.SubTotalWithDiscount;

            if (!CurrencyHelper.IsZeroDecimalCurrencies())
            {
                zeroDecimalOrderAmount = zeroDecimalOrderAmount * 100;
                zeroDecimalTax         = zeroDecimalTax * 100;
                zeroDecimalSubTotal    = zeroDecimalSubTotal * 100;
            }

            var regionInfo = new RegionInfo(CultureInfo.CurrentCulture.LCID);
            var payment    = new Payment()
            {
                OrderId       = order.Id,
                Amount        = order.OrderTotal,
                PaymentMethod = "EPayCo",
                CreatedOn     = DateTimeOffset.UtcNow
            };

            try
            {
                //var charge = await epaycoChargeService.Create(new ChargeDTO
                //{
                //    Customer_Id = currentUser.Id,
                //    Name = currentUser.FullName,
                //    Value = (decimal)zeroDecimalOrderAmount,
                //    Description = cart.OrderNote,
                //    Currency = regionInfo.ISOCurrencySymbol,
                //    Token_Card = ref_payco
                //});
                var validation = await epaycoValidationService.Get(ref_payco);

                if (validation.Success)
                {
                    payment.GatewayTransactionId = validation.Data.TransactionId;
                    payment.Status    = PaymentStatus.Succeeded;
                    order.OrderStatus = OrderStatus.PaymentReceived;
                    _paymentRepository.Add(payment);
                    await _paymentRepository.SaveChangesAsync();

                    return(Redirect($"~/checkout/success?orderId={order.Id}"));
                }
                else
                {
                    payment.Status         = PaymentStatus.Failed;
                    payment.FailureMessage = validation.Message;
                    order.OrderStatus      = OrderStatus.PaymentFailed;

                    _paymentRepository.Add(payment);
                    await _paymentRepository.SaveChangesAsync();

                    TempData["Error"] = validation.Message;
                    return(Redirect($"~/checkout/error?orderId={order.Id}"));
                }
            }
            catch (EPayCoException ex)
            {
                payment.Status         = PaymentStatus.Failed;
                payment.FailureMessage = ex.Message;
                order.OrderStatus      = OrderStatus.PaymentFailed;

                _paymentRepository.Add(payment);
                await _paymentRepository.SaveChangesAsync();

                TempData["Error"] = ex.Message;
                return(Redirect($"~/checkout/error?orderId={order.Id}"));
            }
        }