diff --git a/BookingMicroservice/Controllers/BookingController.cs b/BookingMicroservice/Controllers/BookingController.cs index 77150c643b32ab9807a43b5f67069ae2e135fc05..c9cfa21b771a693df821fc79d07e8b0868e305b0 100644 --- a/BookingMicroservice/Controllers/BookingController.cs +++ b/BookingMicroservice/Controllers/BookingController.cs @@ -3,7 +3,11 @@ using BookingMicroservice.Models; using BookingMicroservice.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; using System.Security.Claims; +using Stripe.Checkout; +using Stripe; + namespace BookingMicroservice.Controllers @@ -16,11 +20,14 @@ namespace BookingMicroservice.Controllers private readonly IBookingService bookingService; private readonly IStripeService stripeService; - public BookingController(IReservationComplianceService reservationComplianceService, IBookingService bookingService, IStripeService stripeService) + private readonly IConfiguration configuration; + + public BookingController(IReservationComplianceService reservationComplianceService, IBookingService bookingService, IStripeService stripeService, IConfiguration configuration) { this.reservationComplianceService = reservationComplianceService; this.bookingService = bookingService; this.stripeService = stripeService; + this.configuration = configuration; } [Authorize] @@ -135,5 +142,22 @@ namespace BookingMicroservice.Controllers } } + [HttpPost("create-payment-intent")] + public ActionResult CreatePaymentIntent([FromBody] PaymentIntentCreateRequest request) + { + var paymentIntentService = new PaymentIntentService(); + var paymentIntent = paymentIntentService.Create(new PaymentIntentCreateOptions + { + Amount = stripeService.CalculateOrderAmount(request.Items), + Currency = "eur", + AutomaticPaymentMethods = new PaymentIntentAutomaticPaymentMethodsOptions + { + Enabled = true, + }, + }); + + return new JsonResult(new { PaymentIntentId = paymentIntent.Id }); + } + } } diff --git a/BookingMicroservice/Models/PaymentIntentCreateRequest.cs b/BookingMicroservice/Models/PaymentIntentCreateRequest.cs new file mode 100644 index 0000000000000000000000000000000000000000..de5bc080cc10369974a609d04974c35a56e5d8b4 --- /dev/null +++ b/BookingMicroservice/Models/PaymentIntentCreateRequest.cs @@ -0,0 +1,13 @@ +namespace BookingMicroservice.Models +{ + public class PaymentIntentCreateRequest + { + public List<Item> Items { get; set; } + } + + public class Item + { + public int Price { get; set; } + } + +} \ No newline at end of file diff --git a/BookingMicroservice/Services/IStripeService.cs b/BookingMicroservice/Services/IStripeService.cs index 0c3a0975033b5d9ec786fc54caa8c051cc1a44ba..bee9db54575dcc07d6d9ce818f7c950f2bd1f735 100644 --- a/BookingMicroservice/Services/IStripeService.cs +++ b/BookingMicroservice/Services/IStripeService.cs @@ -6,5 +6,6 @@ namespace BookingMicroservice.Services public interface IStripeService { Task<bool> VerifyPaymentIntent(string paymentIntentId); + int CalculateOrderAmount(List<Item> items); } } \ No newline at end of file diff --git a/BookingMicroservice/Services/StripeService.cs b/BookingMicroservice/Services/StripeService.cs index 68963ddf55794bf57b3de7cb95532fdabc7f7def..06640cd5ac39c314aa305299360378d005f9b8fd 100644 --- a/BookingMicroservice/Services/StripeService.cs +++ b/BookingMicroservice/Services/StripeService.cs @@ -4,12 +4,14 @@ using Microsoft.Extensions.Configuration; using Stripe; using System; using System.Threading.Tasks; +using System.Linq; namespace BookingMicroservice.Services { public class StripeService : IStripeService { private readonly ILogger<StripeService> _logger; + private readonly IConfiguration _configuration; public StripeService(ILogger<StripeService> logger, IConfiguration configuration) { @@ -32,5 +34,10 @@ namespace BookingMicroservice.Services throw new Exception("Failed to verify payment intent: " + ex.Message); } } + + public int CalculateOrderAmount(List<Item> items) + { + return items.Sum(item => item.Price); + } } }