From a47b7d201076eda6a14a97b1d017d2aa76416653 Mon Sep 17 00:00:00 2001 From: "Amir.H Khademi" Date: Tue, 13 Feb 2024 17:41:06 +0330 Subject: [PATCH] feat : ver 0.5.8.16 , fix : update payment command --- .version | 2 +- .../Controller/OrderBagController.cs | 6 +- .../Controller/PaymentController.cs | 7 ++- NetinaShop.Core/Abstracts/IPaymentService.cs | 2 +- .../SubmitOrderPaymentCommandHandler.cs | 4 +- .../Commands/PaymentCommands.cs | 3 +- .../Services/ZarinpalService.cs | 15 ++--- .../CreateOrUpdatePaymentCommandHandler.cs | 62 +++++++++++++++++++ .../Accounting/CreatePaymentCommandHandler.cs | 26 -------- .../Accounting/UpdatePaymentCommandHandler.cs | 32 ---------- 10 files changed, 78 insertions(+), 81 deletions(-) create mode 100644 NetinaShop.Repository/Handlers/Accounting/CreateOrUpdatePaymentCommandHandler.cs delete mode 100644 NetinaShop.Repository/Handlers/Accounting/CreatePaymentCommandHandler.cs delete mode 100644 NetinaShop.Repository/Handlers/Accounting/UpdatePaymentCommandHandler.cs diff --git a/.version b/.version index 11ed043..4ad4664 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.5.7.15 \ No newline at end of file +0.5.8.16 \ No newline at end of file diff --git a/NetinaShop.Api/Controller/OrderBagController.cs b/NetinaShop.Api/Controller/OrderBagController.cs index dde92e5..82dd010 100644 --- a/NetinaShop.Api/Controller/OrderBagController.cs +++ b/NetinaShop.Api/Controller/OrderBagController.cs @@ -56,9 +56,7 @@ public class OrderBagController : ICarterModule public async Task SubmitOrderPaymentAsync(Guid orderId, [FromQuery] OrderPaymentMethod paymentMethod, IMediator mediator, CancellationToken cancellationToken) { var result = await mediator.Send(new SubmitOrderPaymentCommand(orderId, paymentMethod), cancellationToken); - if (result.NeedToPayOnline) - return TypedResults.Redirect(result.PaymentUrl,true); - else - return TypedResults.Ok(result); + + return TypedResults.Ok(result); } } \ No newline at end of file diff --git a/NetinaShop.Api/Controller/PaymentController.cs b/NetinaShop.Api/Controller/PaymentController.cs index f8c2d00..4099936 100644 --- a/NetinaShop.Api/Controller/PaymentController.cs +++ b/NetinaShop.Api/Controller/PaymentController.cs @@ -1,4 +1,5 @@ -using TypedResults = Microsoft.AspNetCore.Http.TypedResults; +using static FastExpressionCompiler.ExpressionCompiler; +using TypedResults = Microsoft.AspNetCore.Http.TypedResults; namespace NetinaShop.Api.Controller; @@ -40,11 +41,11 @@ public class PaymentController : ICarterModule if (Status == "OK") { var result = await paymentService.VerifyPaymentAsync(authority: Authority, cancellationToken); - return TypedResults.Redirect(""); + return TypedResults.Redirect($"https://vesmook.com/purchase-callback?refid={result.Item1}&paymentStatus=true&factorNumber={result.Item2}", true); } else { - return TypedResults.Redirect(""); + return TypedResults.Redirect($"https://vesmook.com/purchase-callback?refid=0&paymentStatus=false&factorNumber=0", true); } } catch (Exception e) diff --git a/NetinaShop.Core/Abstracts/IPaymentService.cs b/NetinaShop.Core/Abstracts/IPaymentService.cs index 10d067c..02b1e0a 100644 --- a/NetinaShop.Core/Abstracts/IPaymentService.cs +++ b/NetinaShop.Core/Abstracts/IPaymentService.cs @@ -3,5 +3,5 @@ public interface IPaymentService : IScopedDependency { Task GetPaymentLinkAsync(double amount, string factorNumber, Guid orderId, Guid userId, string phoneNumber, string fullName, CancellationToken cancellationToken = default); - Task VerifyPaymentAsync(string authority, CancellationToken cancellationToken = default); + Task> VerifyPaymentAsync(string authority, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/NetinaShop.Core/EntityServices/OrderBagHandlers/SubmitOrderPaymentCommandHandler.cs b/NetinaShop.Core/EntityServices/OrderBagHandlers/SubmitOrderPaymentCommandHandler.cs index d9178da..84c735c 100644 --- a/NetinaShop.Core/EntityServices/OrderBagHandlers/SubmitOrderPaymentCommandHandler.cs +++ b/NetinaShop.Core/EntityServices/OrderBagHandlers/SubmitOrderPaymentCommandHandler.cs @@ -49,7 +49,7 @@ public class SubmitOrderPaymentCommandHandler : IRequestHandler().Update(order); await _repositoryWrapper.SaveChangesAsync(cancellationToken); - await _mediator.Send(new CreatePaymentCommand(order.FactorCode, order.TotalPrice, + await _mediator.Send(new CreateOrUpdatePaymentCommand(null,order.FactorCode, order.TotalPrice, $"پرداخت نقدی سفارش {order.FactorCode}", string.Empty, string.Empty, string.Empty, PaymentType.PayOnDoor, PaymentStatus.Paid, order.Id , order.UserId), cancellationToken); @@ -61,7 +61,7 @@ public class SubmitOrderPaymentCommandHandler : IRequestHandler().Update(order); await _repositoryWrapper.SaveChangesAsync(cancellationToken); - await _mediator.Send(new CreatePaymentCommand(order.FactorCode, order.TotalPrice, + await _mediator.Send(new CreateOrUpdatePaymentCommand(null,order.FactorCode, order.TotalPrice, $"پرداخت نقدی سفارش {order.FactorCode}", string.Empty, string.Empty, string.Empty, PaymentType.CardTransfer, PaymentStatus.Paid, order.Id , order.UserId), cancellationToken); diff --git a/NetinaShop.Domain/CommandQueries/Commands/PaymentCommands.cs b/NetinaShop.Domain/CommandQueries/Commands/PaymentCommands.cs index 9aba654..f0da37f 100644 --- a/NetinaShop.Domain/CommandQueries/Commands/PaymentCommands.cs +++ b/NetinaShop.Domain/CommandQueries/Commands/PaymentCommands.cs @@ -1,4 +1,3 @@ namespace NetinaShop.Domain.CommandQueries.Commands; -public sealed record CreatePaymentCommand(string FactorNumber, double Amount, string Description, string TransactionCode, string CardPan, string Authority, PaymentType Type, PaymentStatus Status, Guid OrderId, Guid UserId) : IRequest; -public sealed record UpdatePaymentCommand(Guid Id,string FactorNumber, double Amount, string Description, string TransactionCode, string CardPan, string Authority, PaymentType Type, PaymentStatus Status, Guid OrderId, Guid UserId) : IRequest; \ No newline at end of file +public sealed record CreateOrUpdatePaymentCommand(Guid? Id,string FactorNumber, double Amount, string Description, string TransactionCode, string CardPan, string Authority, PaymentType Type, PaymentStatus Status, Guid OrderId, Guid UserId) : IRequest; \ No newline at end of file diff --git a/NetinaShop.Infrastructure/Services/ZarinpalService.cs b/NetinaShop.Infrastructure/Services/ZarinpalService.cs index 25aea25..fc52e11 100644 --- a/NetinaShop.Infrastructure/Services/ZarinpalService.cs +++ b/NetinaShop.Infrastructure/Services/ZarinpalService.cs @@ -1,13 +1,8 @@ -using Amazon.Runtime.Internal; -using MediatR; -using Microsoft.EntityFrameworkCore; +using MediatR; using NetinaShop.Domain.CommandQueries.Commands; using NetinaShop.Domain.CommandQueries.Queries; -using NetinaShop.Domain.Entities.Accounting; -using NetinaShop.Domain.Entities.Orders; using NetinaShop.Domain.Enums; using NetinaShop.Infrastructure.Models.RestApi.Zarinpal; -using NetinaShop.Repository.Repositories.Base.Contracts; namespace NetinaShop.Infrastructure.Services; @@ -37,7 +32,7 @@ public class ZarinpalService : IPaymentService throw new AppException($"Exception in get link from zarinpal | {response.data.message}"); - var createPaymentResult = await _mediator.Send(new CreatePaymentCommand(factorNumber, amount, + var createPaymentResult = await _mediator.Send(new CreateOrUpdatePaymentCommand(null,factorNumber, amount, request.description, string.Empty, string.Empty, response.data.authority, PaymentType.Online,PaymentStatus.InPaymentGateway, orderId, userId), cancellationToken); @@ -45,7 +40,7 @@ public class ZarinpalService : IPaymentService return link; } - public async Task VerifyPaymentAsync(string authority, CancellationToken cancellationToken = default) + public async Task> VerifyPaymentAsync(string authority, CancellationToken cancellationToken = default) { var payment = await _mediator.Send(new GetPaymentQuery(Authority: authority), cancellationToken); var request = new ZarinaplVerifyPaymentRequest @@ -63,12 +58,12 @@ public class ZarinpalService : IPaymentService payment.TransactionCode = response.data.ref_id.ToString(); await _mediator.Send( - new UpdatePaymentCommand(payment.Id, payment.FactorNumber, payment.Amount, payment.Description, + new CreateOrUpdatePaymentCommand(payment.Id, payment.FactorNumber, payment.Amount, payment.Description, payment.TransactionCode, payment.CardPan, payment.Authority, payment.Type, payment.Status, payment.OrderId, payment.UserId), cancellationToken); await _mediator.Send(new SubmitOrderPaymentCommand(payment.OrderId, OrderPaymentMethod.OnlinePayment, true),cancellationToken); - return payment.TransactionCode; + return new Tuple(payment.TransactionCode,payment.FactorNumber); } } \ No newline at end of file diff --git a/NetinaShop.Repository/Handlers/Accounting/CreateOrUpdatePaymentCommandHandler.cs b/NetinaShop.Repository/Handlers/Accounting/CreateOrUpdatePaymentCommandHandler.cs new file mode 100644 index 0000000..34161a3 --- /dev/null +++ b/NetinaShop.Repository/Handlers/Accounting/CreateOrUpdatePaymentCommandHandler.cs @@ -0,0 +1,62 @@ +using Microsoft.EntityFrameworkCore; +using NetinaShop.Domain.Entities.Accounting; + +namespace NetinaShop.Repository.Handlers.Accounting; + +public class CreateOrUpdatePaymentCommandHandler : IRequestHandler +{ + private readonly IRepositoryWrapper _repositoryWrapper; + + public CreateOrUpdatePaymentCommandHandler(IRepositoryWrapper repositoryWrapper) + { + _repositoryWrapper = repositoryWrapper; + } + public async Task Handle(CreateOrUpdatePaymentCommand request, CancellationToken cancellationToken) + { + if (request.Id != null) + { + var ent = await _repositoryWrapper.SetRepository() + .TableNoTracking + .FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken); + if (ent == null) + throw new AppException("Payment not found", ApiResultStatusCode.NotFound); + + var newEnt = Payment.Create(request.FactorNumber, request.Amount, request.Description, request.TransactionCode, + request.CardPan, request.Authority, request.Type, request.Status, request.OrderId, request.UserId); + newEnt.Id = ent.Id; + + _repositoryWrapper.SetRepository() + .Update(newEnt); + + await _repositoryWrapper.SaveChangesAsync(cancellationToken); + } + else + { + var orderPayment = await _repositoryWrapper.SetRepository() + .TableNoTracking + .FirstOrDefaultAsync(p => p.OrderId == request.OrderId && p.Type == request.Type,cancellationToken); + if (orderPayment != null) + { + var newEnt = Payment.Create(request.FactorNumber, request.Amount, request.Description, request.TransactionCode, + request.CardPan, request.Authority, request.Type, request.Status, request.OrderId, request.UserId); + newEnt.Id = orderPayment.Id; + + _repositoryWrapper.SetRepository() + .Update(newEnt); + + await _repositoryWrapper.SaveChangesAsync(cancellationToken); + } + else + { + var payment = Payment.Create(request.FactorNumber, request.Amount, request.Description, request.TransactionCode, + request.CardPan, request.Authority, request.Type, request.Status, request.OrderId, request.UserId); + + _repositoryWrapper.SetRepository() + .Add(payment); + + await _repositoryWrapper.SaveChangesAsync(cancellationToken); + } + } + return true; + } +} \ No newline at end of file diff --git a/NetinaShop.Repository/Handlers/Accounting/CreatePaymentCommandHandler.cs b/NetinaShop.Repository/Handlers/Accounting/CreatePaymentCommandHandler.cs deleted file mode 100644 index 011dbea..0000000 --- a/NetinaShop.Repository/Handlers/Accounting/CreatePaymentCommandHandler.cs +++ /dev/null @@ -1,26 +0,0 @@ -using NetinaShop.Domain.Entities.Accounting; - -namespace NetinaShop.Repository.Handlers.Accounting; - -public class CreatePaymentCommandHandler : IRequestHandler -{ - private readonly IRepositoryWrapper _repositoryWrapper; - - public CreatePaymentCommandHandler(IRepositoryWrapper repositoryWrapper) - { - _repositoryWrapper = repositoryWrapper; - } - public async Task Handle(CreatePaymentCommand request, CancellationToken cancellationToken) - { - - var payment = Payment.Create(request.FactorNumber, request.Amount, request.Description, request.TransactionCode, - request.CardPan, request.Authority, request.Type,request.Status, request.OrderId, request.UserId); - - _repositoryWrapper.SetRepository() - .Add(payment); - - await _repositoryWrapper.SaveChangesAsync(cancellationToken); - - return true; - } -} \ No newline at end of file diff --git a/NetinaShop.Repository/Handlers/Accounting/UpdatePaymentCommandHandler.cs b/NetinaShop.Repository/Handlers/Accounting/UpdatePaymentCommandHandler.cs deleted file mode 100644 index 37df2d7..0000000 --- a/NetinaShop.Repository/Handlers/Accounting/UpdatePaymentCommandHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using NetinaShop.Domain.Entities.Accounting; - -namespace NetinaShop.Repository.Handlers.Accounting; - -public class UpdatePaymentCommandHandler : IRequestHandler -{ - private readonly IRepositoryWrapper _repositoryWrapper; - - public UpdatePaymentCommandHandler(IRepositoryWrapper repositoryWrapper) - { - _repositoryWrapper = repositoryWrapper; - } - public async Task Handle(UpdatePaymentCommand request, CancellationToken cancellationToken) - { - var ent = await _repositoryWrapper.SetRepository() - .TableNoTracking - .FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken); - if (ent == null) - throw new AppException("Payment not found", ApiResultStatusCode.NotFound); - - var newEnt = Payment.Create(request.FactorNumber, request.Amount, request.Description, request.TransactionCode, - request.CardPan, request.Authority, request.Type, request.Status, request.OrderId, request.UserId); - newEnt.Id = ent.Id; - - _repositoryWrapper.SetRepository() - .Update(newEnt); - - await _repositoryWrapper.SaveChangesAsync(cancellationToken); - return true; - } -} \ No newline at end of file