feat : ver 0.5.8.16 , fix : update payment command
parent
83393e7146
commit
a47b7d2010
|
@ -56,9 +56,7 @@ public class OrderBagController : ICarterModule
|
|||
public async Task<IResult> 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);
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -3,5 +3,5 @@
|
|||
public interface IPaymentService : IScopedDependency
|
||||
{
|
||||
Task<string> GetPaymentLinkAsync(double amount, string factorNumber, Guid orderId, Guid userId, string phoneNumber, string fullName, CancellationToken cancellationToken = default);
|
||||
Task<string> VerifyPaymentAsync(string authority, CancellationToken cancellationToken = default);
|
||||
Task<Tuple<string, string>> VerifyPaymentAsync(string authority, CancellationToken cancellationToken = default);
|
||||
}
|
|
@ -49,7 +49,7 @@ public class SubmitOrderPaymentCommandHandler : IRequestHandler<SubmitOrderPayme
|
|||
_repositoryWrapper.SetRepository<Order>().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<SubmitOrderPayme
|
|||
_repositoryWrapper.SetRepository<Order>().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);
|
||||
|
|
|
@ -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<bool>;
|
||||
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<bool>;
|
||||
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<bool>;
|
|
@ -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<string> VerifyPaymentAsync(string authority, CancellationToken cancellationToken = default)
|
||||
public async Task<Tuple<string,string>> 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<string, string>(payment.TransactionCode,payment.FactorNumber);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using NetinaShop.Domain.Entities.Accounting;
|
||||
|
||||
namespace NetinaShop.Repository.Handlers.Accounting;
|
||||
|
||||
public class CreateOrUpdatePaymentCommandHandler : IRequestHandler<CreateOrUpdatePaymentCommand, bool>
|
||||
{
|
||||
private readonly IRepositoryWrapper _repositoryWrapper;
|
||||
|
||||
public CreateOrUpdatePaymentCommandHandler(IRepositoryWrapper repositoryWrapper)
|
||||
{
|
||||
_repositoryWrapper = repositoryWrapper;
|
||||
}
|
||||
public async Task<bool> Handle(CreateOrUpdatePaymentCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (request.Id != null)
|
||||
{
|
||||
var ent = await _repositoryWrapper.SetRepository<Payment>()
|
||||
.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<Payment>()
|
||||
.Update(newEnt);
|
||||
|
||||
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
var orderPayment = await _repositoryWrapper.SetRepository<Payment>()
|
||||
.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<Payment>()
|
||||
.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<Payment>()
|
||||
.Add(payment);
|
||||
|
||||
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
using NetinaShop.Domain.Entities.Accounting;
|
||||
|
||||
namespace NetinaShop.Repository.Handlers.Accounting;
|
||||
|
||||
public class CreatePaymentCommandHandler : IRequestHandler<CreatePaymentCommand,bool>
|
||||
{
|
||||
private readonly IRepositoryWrapper _repositoryWrapper;
|
||||
|
||||
public CreatePaymentCommandHandler(IRepositoryWrapper repositoryWrapper)
|
||||
{
|
||||
_repositoryWrapper = repositoryWrapper;
|
||||
}
|
||||
public async Task<bool> 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<Payment>()
|
||||
.Add(payment);
|
||||
|
||||
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using NetinaShop.Domain.Entities.Accounting;
|
||||
|
||||
namespace NetinaShop.Repository.Handlers.Accounting;
|
||||
|
||||
public class UpdatePaymentCommandHandler : IRequestHandler<UpdatePaymentCommand,bool>
|
||||
{
|
||||
private readonly IRepositoryWrapper _repositoryWrapper;
|
||||
|
||||
public UpdatePaymentCommandHandler(IRepositoryWrapper repositoryWrapper)
|
||||
{
|
||||
_repositoryWrapper = repositoryWrapper;
|
||||
}
|
||||
public async Task<bool> Handle(UpdatePaymentCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var ent = await _repositoryWrapper.SetRepository<Payment>()
|
||||
.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<Payment>()
|
||||
.Update(newEnt);
|
||||
|
||||
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue