Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface IOrderService

void CancelOrder(Order order);

bool IsAwaitingPayment(Order order);

Task<decimal> GetTax(Guid checkoutId, string countryId, long stateOrProvinceId, string zipCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
.Include(c => c.CheckoutItems).ThenInclude(x => x.Product)
.Include(c => c.Customer)
.Include(c => c.CreatedBy)
.Where(x => x.Id == checkoutId).FirstOrDefault();

Check warning on line 154 in src/Modules/SimplCommerce.Module.Orders/Services/OrderService.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Drop 'Where' and move the condition into the 'FirstOrDefault'.

See more on https://sonarcloud.io/project/issues?id=simplcommerce_SimplCommerce&issues=AaHKTjogySpEBDukaSei&open=AaHKTjogySpEBDukaSei&pullRequest=1155

if (checkout == null)
{
Expand Down Expand Up @@ -367,6 +367,11 @@
}
}

public bool IsAwaitingPayment(Order order)
{
return order.OrderStatus == OrderStatus.PendingPayment || order.OrderStatus == OrderStatus.PaymentFailed;
}

public async Task<decimal> GetTax(Guid checkoutId, string countryId, long stateOrProvinceId, string zipCode)
{
decimal taxAmount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ public async Task<IActionResult> Result(PaymentSubmitResult result)
}
else
{
TempData["Error"] = result.LocalMessage;
TempData["Error"] = order.OrderStatus == OrderStatus.Canceled && result.ErrorCode == 0
? "Your payment was received after the order had been canceled. Please contact us for a refund."
: result.LocalMessage;
return Redirect($"~/checkout/error?orderId={orderId}");
}
}
Expand All @@ -137,17 +139,52 @@ public async Task<IActionResult> Notify([FromBody]PaymentSubmitResult result)
var order = await _orderRepository.Query().FirstOrDefaultAsync(x => x.Id == orderId);

// Update status in case the redirect fail
if(order != null && order.OrderStatus == OrderStatus.PendingPayment)
if(order != null && (_orderService.IsAwaitingPayment(order) || order.OrderStatus == OrderStatus.Canceled))
{
await UpdatePaymentStatus(order, result);
}

return Accepted();
}

private async Task RecordPaymentForCanceledOrder(Order order, PaymentSubmitResult paymentSubmitResult)
{
// result and notify may both report the same transaction
var alreadyRecorded = await _paymentRepository.Query()
.AnyAsync(x => x.OrderId == order.Id && x.GatewayTransactionId == paymentSubmitResult.TransId);
if (alreadyRecorded)
{
return;
}

_paymentRepository.Add(new Payment()
{
OrderId = order.Id,
PaymentFee = order.PaymentFeeAmount,
Amount = order.OrderTotal,
PaymentMethod = "MomoPayment",
CreatedOn = DateTimeOffset.UtcNow,
Status = PaymentStatus.Succeeded,
GatewayTransactionId = paymentSubmitResult.TransId,
FailureMessage = "Payment received after the order was canceled. Refund required."
});
await _paymentRepository.SaveChangesAsync();
}

public async Task<bool> UpdatePaymentStatus(Order order, PaymentSubmitResult paymentSubmitResult)
{
bool status = false;
if (!_orderService.IsAwaitingPayment(order))
{
if (order.OrderStatus == OrderStatus.Canceled && paymentSubmitResult.ErrorCode == 0)
{
await RecordPaymentForCanceledOrder(order, paymentSubmitResult);
}

// Do not move a canceled (or otherwise already processed) order back to a payment status.
return order.OrderStatus != OrderStatus.Canceled && paymentSubmitResult.ErrorCode == 0;
}

var payment = new Payment()
{
OrderId = order.Id,
Expand Down
26 changes: 26 additions & 0 deletions test/SimplCommerce.Module.Orders.Tests/OrderServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using SimplCommerce.Module.Orders.Models;
using SimplCommerce.Module.Orders.Services;
using Xunit;

namespace SimplCommerce.Module.Orders.Tests
{
public class OrderServiceTests
{
// IsAwaitingPayment does not use any of the dependencies
private readonly OrderService _orderService = new OrderService(null, null, null, null, null, null, null, null, null, null, null);

[Theory]
[InlineData(OrderStatus.PendingPayment, true)]
[InlineData(OrderStatus.PaymentFailed, true)]
[InlineData(OrderStatus.Canceled, false)]
[InlineData(OrderStatus.PaymentReceived, false)]
[InlineData(OrderStatus.New, false)]
[InlineData(OrderStatus.Shipped, false)]
public void IsAwaitingPayment_ReturnsTrueOnlyForUnpaidOrders(OrderStatus status, bool expected)
{
var order = new Order { OrderStatus = status };

Assert.Equal(expected, _orderService.IsAwaitingPayment(order));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Modules\SimplCommerce.Module.Orders\SimplCommerce.Module.Orders.csproj" />
</ItemGroup>

</Project>