Merge master to feature/oauth
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
using Hutopy.Application.Common.Models;
|
||||
using Hutopy.Application.Stripe.Commands;
|
||||
|
||||
namespace Hutopy.Application.Common.Interfaces;
|
||||
|
||||
|
||||
public interface IStripeService
|
||||
{
|
||||
public Task<string> CreateCheckoutSession(int amount, string currency);
|
||||
public Task<string> CreateCheckoutSession(int amount, string creatorId, string currency);
|
||||
public Result ValidateTransaction(ConfirmStripeTransactionCommand request);
|
||||
}
|
||||
|
||||
@@ -1,24 +1,73 @@
|
||||
using Hutopy.Application.Common.Interfaces;
|
||||
|
||||
namespace Hutopy.Application.Stripe.Commands;
|
||||
public record ConfirmStripeTransactionCommand : IRequest<string>
|
||||
public class ConfirmStripeTransactionCommand : IRequest<string>
|
||||
{
|
||||
public required Guid UserTransactionId { get; init; }
|
||||
public required bool IsConfirmed { get; init; }
|
||||
public string Id { get; set; }
|
||||
public string Object { get; set; }
|
||||
public int Created { get; set; }
|
||||
public Data Data { get; set; }
|
||||
public Request Request { get; set; }
|
||||
}
|
||||
|
||||
public class Data
|
||||
{
|
||||
public Object Object { get; set; }
|
||||
}
|
||||
|
||||
public class Object
|
||||
{
|
||||
public string Id { get; set; } = String.Empty;
|
||||
public int Amount { get; set; }
|
||||
public BillingDetails Billing_details { get; set; } = new();
|
||||
public string Calculated_statement_descriptor { get; set; } = String.Empty;
|
||||
public string Currency { get; set; } = String.Empty;
|
||||
public bool Paid { get; set; }
|
||||
public string Payment_intent { get; set; } = String.Empty;
|
||||
public string Payment_method { get; set; } = String.Empty;
|
||||
public string Receipt_url { get; set; } = String.Empty;
|
||||
public string Status { get; set; } = String.Empty;
|
||||
public string Failure_message { get; set; } = String.Empty;
|
||||
}
|
||||
|
||||
public class BillingDetails
|
||||
{
|
||||
public string Email { get; set; } = String.Empty;
|
||||
public string Name { get; set; } = String.Empty;
|
||||
public string Phone { get; set; } = String.Empty;
|
||||
}
|
||||
|
||||
public class Request
|
||||
{
|
||||
public string Id { get; set; } = String.Empty;
|
||||
}
|
||||
|
||||
public class ConfirmStripeTransactionCommandHandler(
|
||||
IApplicationDbContext dbContext
|
||||
IApplicationDbContext dbContext,
|
||||
IStripeService stripeService
|
||||
)
|
||||
: IRequestHandler<ConfirmStripeTransactionCommand, string>
|
||||
{
|
||||
public async Task<string> Handle(ConfirmStripeTransactionCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var transaction = await dbContext.UserTransactions.FirstOrDefaultAsync(x => x.Id == request.UserTransactionId, cancellationToken);
|
||||
if (transaction is null) return "";
|
||||
transaction.IsConfirmed = request.IsConfirmed;
|
||||
dbContext.UserTransactions.Update(transaction);
|
||||
var lastTransaction = await dbContext.UserTransactions.OrderBy(x => x.Created).LastAsync(cancellationToken);
|
||||
var stripeConfirmation = stripeService.ValidateTransaction(request);
|
||||
|
||||
return transaction.Id.ToString();
|
||||
if (stripeConfirmation.Succeeded)
|
||||
{
|
||||
lastTransaction.IsConfirmed = true;
|
||||
}
|
||||
lastTransaction.Paid = request.Data.Object.Paid;
|
||||
lastTransaction.StripeChargeId = request.Data.Object.Id;
|
||||
lastTransaction.StripeEventId = request.Id;
|
||||
lastTransaction.StripeReceiptUrl = request.Data.Object.Receipt_url;
|
||||
lastTransaction.StripePaymentIntent = request.Data.Object.Payment_intent;
|
||||
lastTransaction.StripePaymentMethod = request.Data.Object.Payment_method;
|
||||
lastTransaction.StripeBillingDetailEmail = request.Data.Object.Billing_details.Email;
|
||||
lastTransaction.StripeBillingDetailName = request.Data.Object.Billing_details.Name;
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,13 +18,11 @@ public class CreateSessionCheckoutCommandHandler(
|
||||
{
|
||||
public async Task<string> Handle(CreateSessionCheckoutCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var stripeSecret = await stripeService.CreateCheckoutSession(request.Amount, request.Currency);
|
||||
var stripeSecret = await stripeService.CreateCheckoutSession(request.Amount, request.CreatorId, request.Currency);
|
||||
|
||||
// ReSharper disable once PossibleLossOfFraction
|
||||
decimal priceInDollars = (request.Amount / 100);
|
||||
|
||||
|
||||
//todo: Need to add this transaction after the confirmation from stripe in the frontEnd ( redirect, re-call backend )
|
||||
var userTransaction = new UserTransaction
|
||||
{
|
||||
Currency = request.Currency, Amount = priceInDollars, TipMessage = request.TipMessage, ApplicationUserId = request.CreatorId
|
||||
|
||||
29
src/Application/Stripe/Queries/GetMyLastReceipt.cs
Normal file
29
src/Application/Stripe/Queries/GetMyLastReceipt.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Hutopy.Application.Common.Interfaces;
|
||||
|
||||
namespace Hutopy.Application.Stripe.Queries;
|
||||
|
||||
public record GetMyLastReceiptQuery : IRequest<MyLastReceiptDto>
|
||||
{
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public string CreatorId { get; set; } = string.Empty;
|
||||
};
|
||||
|
||||
public class GetMyLastReceiptQueryHandler(
|
||||
IApplicationDbContext dbContext
|
||||
)
|
||||
: IRequestHandler<GetMyLastReceiptQuery, MyLastReceiptDto>
|
||||
{
|
||||
public async Task<MyLastReceiptDto> Handle(GetMyLastReceiptQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var lastTransaction = await dbContext.UserTransactions.OrderBy(x => x.Created)
|
||||
.LastOrDefaultAsync(x => x.ApplicationUserId == request.CreatorId && x.StripeBillingDetailEmail == request.Email,
|
||||
cancellationToken);
|
||||
|
||||
var receiptUrl = new MyLastReceiptDto
|
||||
{
|
||||
ReceiptUrl = lastTransaction?.StripeReceiptUrl ?? "",
|
||||
};
|
||||
|
||||
return receiptUrl;
|
||||
}
|
||||
}
|
||||
6
src/Application/Stripe/Queries/MyLastReceiptDto.cs
Normal file
6
src/Application/Stripe/Queries/MyLastReceiptDto.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Hutopy.Application.Stripe.Queries;
|
||||
|
||||
public class MyLastReceiptDto
|
||||
{
|
||||
public string ReceiptUrl { get; set; }
|
||||
}
|
||||
@@ -18,12 +18,11 @@ public class GetCurrentUserQueryHandler(
|
||||
var currentUserId = new Guid(identityUser?.Id ?? "");
|
||||
|
||||
var transactions = await context.UserTransactions
|
||||
.Where(x => x.Id == currentUserId)
|
||||
.Where(x => x.ApplicationUserId == currentUserId.ToString())
|
||||
.OrderBy(x => x.LastModified)
|
||||
.ProjectTo<UserTransactionDto>(mapper.ConfigurationProvider)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
|
||||
var user = new UserDto()
|
||||
{
|
||||
Id = currentUserId,
|
||||
@@ -7,6 +7,7 @@ public class UserDto
|
||||
public required string FirstName { get; init; }
|
||||
|
||||
public required string LastName { get; init; }
|
||||
public string UserName { get; init; } = String.Empty;
|
||||
|
||||
public List<UserTransactionDto> UserTransactions { get; init; } = [];
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Hutopy.Domain.Interfaces;
|
||||
|
||||
namespace Hutopy.Application.Users.Queries.GetMinimalUser;
|
||||
|
||||
public record GetMinimalUserQuery : IRequest<MinimalUserDto>
|
||||
{
|
||||
public string UserId { get; set; } = string.Empty;
|
||||
};
|
||||
|
||||
public class GetMinimalUserQueryHandler(
|
||||
IUserService userService
|
||||
)
|
||||
: IRequestHandler<GetMinimalUserQuery, MinimalUserDto>
|
||||
{
|
||||
public async Task<MinimalUserDto> Handle(GetMinimalUserQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var identityUser = await userService.FindUserByIdAsync(request.UserId);
|
||||
|
||||
var user = new MinimalUserDto()
|
||||
{
|
||||
FirstName = identityUser?.FirstName ?? "",
|
||||
LastName = identityUser?.LastName ?? "",
|
||||
UserName = identityUser?.UserName ?? ""
|
||||
};
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Hutopy.Application.Users.Queries.GetMinimalUser;
|
||||
|
||||
public class MinimalUserDto
|
||||
{
|
||||
public required string FirstName { get; init; }
|
||||
public required string LastName { get; init; }
|
||||
public string UserName { get; init; } = String.Empty;
|
||||
}
|
||||
Reference in New Issue
Block a user