11using dotnet_qrshop . Abstractions ;
2+ using dotnet_qrshop . Features . Payments . Webhook ;
23using Stripe ;
34
45namespace dotnet_qrshop . Services ;
56
6- public class StripePaymentService (
7- IConfiguration _configuration ) : IPaymentService
7+ public class StripePaymentService : IPaymentService
88{
9+ private readonly string _apiSecretKey ;
10+ private readonly string _webhookSecret ;
11+
12+ public StripePaymentService ( IConfiguration configuration )
13+ {
14+ _apiSecretKey = configuration [ "Stripe:SecretKey" ] ?? throw new InvalidOperationException ( "Stripe Api secret not configured." ) ;
15+ _webhookSecret = configuration [ "Stripe:WebhookSecret" ] ?? throw new InvalidOperationException ( "Stripe Webhook secret not configured." ) ;
16+ }
17+
918 public async Task < string > CreatePaymentIntentAsync ( int orderId , decimal amount , CancellationToken cancellationToken )
1019 {
11- StripeConfiguration . ApiKey = _configuration [ "StripeApiKey" ] ;
20+ StripeConfiguration . ApiKey = _apiSecretKey ;
1221
1322 var options = new PaymentIntentCreateOptions
1423 {
@@ -22,4 +31,36 @@ public async Task<string> CreatePaymentIntentAsync(int orderId, decimal amount,
2231
2332 return paymentIntent . ClientSecret ;
2433 }
34+
35+ public async Task HandleWebhookAsync ( HttpRequest request )
36+ {
37+ var json = await new StreamReader ( request . Body ) . ReadToEndAsync ( ) ;
38+ Event stripeEvent ;
39+
40+ try
41+ {
42+ stripeEvent = EventUtility . ConstructEvent (
43+ json ,
44+ request . Headers [ "Stripe-Signature" ] ,
45+ _webhookSecret
46+ ) ;
47+ }
48+ catch ( StripeException e )
49+ {
50+ // logger.LogError(e, "⚠️ Webhook signature verification failed."); TODO DYLAN: Add Logger
51+ throw new BadHttpRequestException ( "Invalid signature" ) ;
52+ }
53+
54+ var parsedEvent = StripeEventTypeParser . Parse ( stripeEvent . Type ) ;
55+
56+ switch ( parsedEvent )
57+ {
58+ case StripeEventType . CheckoutSessionCompleted :
59+ // handle
60+ break ;
61+ case StripeEventType . PaymentIntentPaymentFailed :
62+ // handle
63+ break ;
64+ }
65+ }
2566}
0 commit comments