in src/Modules/SimplCommerce.Module.PaymentNganLuong/Areas/PaymentNganLuong/Controllers/NganLuongController.cs [62:129]
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("&", "&");
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}");
}
}