Пример #1
0
        public async Task <IViewComponentResult> InvokeAsync()
        {
            var cashfreeProvider = await _paymentProviderRepository.Query().FirstOrDefaultAsync(x => x.Id == PaymentProviderHelper.CashfreeProviderId);

            var cashfreeSetting = JsonConvert.DeserializeObject <CashfreeConfigForm>(cashfreeProvider.AdditionalSettings);
            var currentUser     = await _workContext.GetCurrentUser();

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

            var amount = String.Format("{0:.##}", cart.OrderTotal);

            var orderId      = DateTime.Today.ToString("ddMMyyyy") + "_" + cart.Id;
            var message      = "appId=" + cashfreeSetting.AppId + "&orderId=" + orderId + "&orderAmount=" + amount + "&returnUrl=" + cashfreeSetting.ReturnURL + "&paymentModes=" + cashfreeSetting.PaymentModes;
            var paymentToken = PaymentProviderHelper.GetToken(message, cashfreeSetting.SecretKey);
            var model        = new CashfreeCheckoutForm
            {
                AppId         = cashfreeSetting.AppId,
                PaymentToken  = paymentToken,
                OrderId       = orderId,
                OrderAmount   = amount,
                CustomerName  = currentUser.FullName,
                CustomerEmail = currentUser.Email,
                CustomerPhone = string.IsNullOrWhiteSpace(currentUser.PhoneNumber) ? "1234567890" : currentUser.PhoneNumber, // Phone number is mandatory for Cashfree payment
                Mode          = cashfreeSetting.IsSandbox ? "TEST" : "PROD",
                ReturnURL     = cashfreeSetting.ReturnURL,
                NotifyURL     = cashfreeSetting.NotifyURL
            };

            return(View(this.GetViewPath(), model));
        }
Пример #2
0
        public async Task <IActionResult> Charge([FromForm] CashfreeResponse cashfreeResponse)
        {
            var cashfreeProvider = await _paymentProviderRepository.Query().FirstOrDefaultAsync(x => x.Id == PaymentProviderHelper.CashfreeProviderId);

            var cashfreeSetting = JsonConvert.DeserializeObject <CashfreeConfigForm>(cashfreeProvider.AdditionalSettings);
            // Check the response signature
            string data = "";

            data = data + cashfreeResponse.OrderId;
            data = data + cashfreeResponse.OrderAmount;
            data = data + cashfreeResponse.ReferenceId;
            data = data + cashfreeResponse.TxStatus;
            data = data + cashfreeResponse.PaymentMode;
            data = data + cashfreeResponse.TxMsg;
            data = data + cashfreeResponse.TxTime;
            var responseToken = PaymentProviderHelper.GetToken(data, cashfreeSetting.SecretKey);

            if (responseToken.Equals(cashfreeResponse.Signature))
            {
                var curentUser = await _workContext.GetCurrentUser();

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

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

                var orderCreateResult = await _orderService.CreateOrder(cart.Id, cashfreeResponse.PaymentMode, 0, OrderStatus.PendingPayment);

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

                var order   = orderCreateResult.Value;
                var payment = new Payment()
                {
                    OrderId       = order.Id,
                    Amount        = order.OrderTotal,
                    PaymentMethod = PaymentProviderHelper.CashfreeProviderId + " - " + cashfreeResponse.PaymentMode,
                    CreatedOn     = DateTimeOffset.UtcNow
                };

                if (cashfreeResponse.TxStatus == "SUCCESS")
                {
                    payment.GatewayTransactionId = cashfreeResponse.ReferenceId;
                    payment.Status    = PaymentStatus.Succeeded;
                    order.OrderStatus = OrderStatus.PaymentReceived;
                    _paymentRepository.Add(payment);
                    await _paymentRepository.SaveChangesAsync();

                    return(Ok(new { Status = "success", OrderId = order.Id }));
                }
                else
                {
                    payment.GatewayTransactionId = cashfreeResponse.ReferenceId;
                    payment.Status         = PaymentStatus.Failed;
                    payment.FailureMessage = cashfreeResponse.TxMsg;
                    order.OrderStatus      = OrderStatus.PaymentFailed;
                    _paymentRepository.Add(payment);
                    await _paymentRepository.SaveChangesAsync();

                    var error = "Error: " + cashfreeResponse.TxStatus + " - " + cashfreeResponse.TxMsg;
                    return(BadRequest(error));
                }
            }
            else
            {
                return(BadRequest("PaymentTampered"));
            }
        }