Repository navigation
Do not mark a canceled order as paid on Momo callbacks #1155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
blurman-ai
wants to merge
1
commit into
simplcommerce:master
Choose a base branch
from
blurman-ai:fix/momo-payment-after-auto-cancel
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -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}"); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
@@ -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(); | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| 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, | ||||||||
|
|
||||||||
26 changes: 26 additions & 0 deletions
26
test/SimplCommerce.Module.Orders.Tests/OrderServiceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| 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)); | ||||
| } | ||||
| } | ||||
| } | ||||
21 changes: 21 additions & 0 deletions
21
test/SimplCommerce.Module.Orders.Tests/SimplCommerce.Module.Orders.Tests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.