Ejemplo n.º 1
0
        public async Task <IActionResult> MomoCheckout()
        {
            var currentUser = await _workContext.GetCurrentUser();

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

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

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

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

            var rootUrl = Request.GetFullHostingUrlRoot();

            var paymentRequest = new PaymentSubmitRequest(
                secretKey: _setting.Value.SecretKey,
                partnerCode: _setting.Value.PartnerCode,
                accessKey: _setting.Value.AccessKey,
                amount: orderCreateResult.Value.OrderTotal,
                orderId: orderCreateResult.Value.Id,
                orderInfo: "test",
                returnUrl: $"{rootUrl}/momo/result",
                notifyUrl: $"{rootUrl}/momo/notify",
                extraData: ""
                );

            var httpClient = _httpClientFactory.CreateClient();
            var momoUrl    = _setting.Value.IsSandbox ? "https://test-payment.momo.vn/gw_payment/transactionProcessor" : "https://payment.momo.vn/gw_payment/transactionProcessor";
            var response   = await httpClient.PostAsync(momoUrl, paymentRequest, CreateCamelCaseFormater());

            response.EnsureSuccessStatusCode();
            var result = await response.Content.ReadAsAsync <PaymentSubmitResponse>();

            if (result.Validate(_setting.Value.SecretKey))
            {
                return(Redirect(result.PayUrl));
            }

            TempData["Error"] = result.LocalMessage;
            return(Redirect("~/checkout/error"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> SubmitPayment(string paymentOption, string bankCode)
        {
            var currentUser = await _workContext.GetCurrentUser();

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

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

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

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

            var order = orderCreateResult.Value;

            var rootUrl = Request.GetFullHostingUrlRoot();
            var paymentSubmitRequest = new PaymentSubmitRequest
            {
                MerchantId       = _setting.Value.MerchantId,
                MerchantPassword = _setting.Value.MerchantPassword,
                ReceiverEmail    = _setting.Value.ReceiverEmail,
                OrderCode        = order.Id.ToString(),
                TotalAmount      = (int)order.OrderTotal,
                PaymentMethod    = paymentOption,
                BankCode         = bankCode,
                ReturnUrl        = $"{rootUrl}/ngan-luong/result",
                CancelUrl        = $"{rootUrl}/ngan-luong/cancel?orderCode={order.Id}",
                BuyerFullname    = currentUser.FullName,
                BuyerEmail       = currentUser.Email,
                BuyerMobile      = currentUser.PhoneNumber
            };

            var httpClient    = _httpClientFactory.CreateClient();
            var nganLuongUrl  = _setting.Value.IsSandbox ? "https://sandbox.nganluong.vn:8088/nl35/checkout.api.nganluong.post.php" : "https://www.nganluong.vn/checkout.api.nganluong.post.php";
            var requestMesage = new HttpRequestMessage(HttpMethod.Post, nganLuongUrl)
            {
                Content = paymentSubmitRequest.MakePostContent()
            };
            var response = await httpClient.SendAsync(requestMesage);

            response.EnsureSuccessStatusCode();
            var contentString = await response.Content.ReadAsStringAsync();

            contentString = contentString.Replace("&", "&amp;");
            var xdoc = XDocument.Parse(contentString);
            var paymentSubmitResponse = xdoc.Descendants("result").Select(x => new PaymentSubmitResponse
            {
                ErrorCode   = x.Element("error_code").Value,
                Token       = x.Element("token").Value,
                Description = x.Element("description").Value,
                TimeLimit   = x.Element("time_limit").Value,
                CheckoutUrl = x.Element("checkout_url").Value
            }).First();

            if (paymentSubmitResponse.ErrorCode == "00")
            {
                return(Redirect(paymentSubmitResponse.CheckoutUrl));
            }
            else
            {
                string errorMessage = $"Error code: {paymentSubmitResponse.ErrorCode}, Description: {paymentSubmitResponse.Description}";
                await UpdatePaymentStatusError(order, errorMessage, paymentSubmitResponse.Token);

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