Skip to content

Commit 8ab545b

Browse files
committed
Finished payment core feature
1 parent d35dec7 commit 8ab545b

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using dotnet_qrshop.Abstractions.Messaging;
2+
3+
namespace dotnet_qrshop.Features.Payments.Webhook;
4+
5+
public sealed record PaymentWebhookCommand(HttpRequest Request) : ICommand;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using dotnet_qrshop.Abstractions;
2+
using dotnet_qrshop.Abstractions.Messaging;
3+
using dotnet_qrshop.Common.Results;
4+
5+
namespace dotnet_qrshop.Features.Payments.Webhook;
6+
7+
public class PaymentWebhookCommandHandler(IPaymentService _paymentService) : ICommandHandler<PaymentWebhookCommand>
8+
{
9+
public async Task<Result> Handle(PaymentWebhookCommand command, CancellationToken cancellationToken)
10+
{
11+
await _paymentService.HandleWebhookAsync(command.Request);
12+
return Result.Success();
13+
}
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Carter;
2+
using dotnet_qrshop.Abstractions.Messaging;
3+
using dotnet_qrshop.Common.Extensions;
4+
using dotnet_qrshop.Common.Results;
5+
6+
namespace dotnet_qrshop.Features.Payments.Webhook;
7+
8+
public class PaymentWebhookEndpoint : ICarterModule
9+
{
10+
public void AddRoutes(IEndpointRouteBuilder app)
11+
{
12+
app.MapPost("webhook/payment", async (
13+
HttpRequest request,
14+
ICommandHandler<PaymentWebhookCommand> handler,
15+
CancellationToken cancellationToken) =>
16+
{
17+
var result = await handler.Handle(new PaymentWebhookCommand(request), cancellationToken);
18+
return result.Match(() => Results.Ok(), CustomResults.Problem);
19+
})
20+
.WithName("PaymentWebhook");
21+
}
22+
}

src/Features/Payments/Webhook/StripeEventTypeParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static StripeEventType Parse(string eventType)
66
{
77
return eventType switch
88
{
9-
"checkout.session.completed" => StripeEventType.CheckoutSessionCompleted,
9+
"payment_intent.succeeded" => StripeEventType.CheckoutSessionCompleted,
1010
"payment_intent.payment_failed" => StripeEventType.PaymentIntentPaymentFailed,
1111
_ => StripeEventType.Unknown
1212
};

src/Services/StripePaymentService.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using dotnet_qrshop.Abstractions;
22
using dotnet_qrshop.Features.Payments.Webhook;
3+
using dotnet_qrshop.Infrastructure.Database.DbContext;
34
using Stripe;
45

56
namespace dotnet_qrshop.Services;
@@ -8,11 +9,13 @@ public class StripePaymentService : IPaymentService
89
{
910
private readonly string _apiSecretKey;
1011
private readonly string _webhookSecret;
12+
private readonly ApplicationDbContext _dbContext;
1113

12-
public StripePaymentService(IConfiguration configuration)
14+
public StripePaymentService(IConfiguration configuration, ApplicationDbContext dbContext)
1315
{
1416
_apiSecretKey = configuration["Stripe:SecretKey"] ?? throw new InvalidOperationException("Stripe Api secret not configured.");
1517
_webhookSecret = configuration["Stripe:WebhookSecret"] ?? throw new InvalidOperationException("Stripe Webhook secret not configured.");
18+
_dbContext = dbContext;
1619
}
1720

1821
public async Task<string> CreatePaymentIntentAsync(int orderId, decimal amount, CancellationToken cancellationToken)
@@ -42,7 +45,8 @@ public async Task HandleWebhookAsync(HttpRequest request)
4245
stripeEvent = EventUtility.ConstructEvent(
4346
json,
4447
request.Headers["Stripe-Signature"],
45-
_webhookSecret
48+
_webhookSecret,
49+
throwOnApiVersionMismatch: false
4650
);
4751
}
4852
catch (StripeException e)

0 commit comments

Comments
 (0)