diff --git a/BookingMicroservice/Controllers/BookingController.cs b/BookingMicroservice/Controllers/BookingController.cs index 77150c643b32ab9807a43b5f67069ae2e135fc05..71d73923f50a9fd4a9d09948bc7489e31d2a9f8f 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 async Task<IActionResult> 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 { clientSecret = paymentIntent.ClientSecret }); + } + } } 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); + } } } diff --git a/GatewayAPI/Clients/BookingService/BookingServiceClient.cs b/GatewayAPI/Clients/BookingService/BookingServiceClient.cs index be1534f6625a2bd965338ee04da172178c0d95c1..d6b89c3445e2d285487c769c02ba61203b16d7f1 100644 --- a/GatewayAPI/Clients/BookingService/BookingServiceClient.cs +++ b/GatewayAPI/Clients/BookingService/BookingServiceClient.cs @@ -57,6 +57,10 @@ namespace GatewayAPI.Clients.BookingService return httpClient.GetAsync($"{API_PATH}/history"); } + public Task<HttpResponseMessage> CreatePaymentIntent(PaymentIntentCreateRequest request) + { + return httpClient.PostAsJsonAsync($"{API_PATH}/create-payment-intent", request); + } } } diff --git a/GatewayAPI/Clients/BookingService/IBookingServiceClient.cs b/GatewayAPI/Clients/BookingService/IBookingServiceClient.cs index be2db11631d06c73ce80fe82ada5f986c71129a9..669bf30e87fcc17b1cbf16422b1b38256c5d30e9 100644 --- a/GatewayAPI/Clients/BookingService/IBookingServiceClient.cs +++ b/GatewayAPI/Clients/BookingService/IBookingServiceClient.cs @@ -10,5 +10,6 @@ namespace GatewayAPI.Clients.BookingService Task<HttpResponseMessage> UpdateBookingAsync(int bookindId, BookingUpdate bookingModel); Task<HttpResponseMessage> GetUpcomingFlightBookingsAsync(); Task<HttpResponseMessage> GetPreviousFlightBookingsAsync(); + Task<HttpResponseMessage> CreatePaymentIntent(PaymentIntentCreateRequest request); } } diff --git a/GatewayAPI/Controllers/BookingController.cs b/GatewayAPI/Controllers/BookingController.cs index b6ce381f4239cc4f86ae459d3bdc98b757d63e12..857ce7a08774623c9b7fd5293ac50e1aa985fc4b 100644 --- a/GatewayAPI/Controllers/BookingController.cs +++ b/GatewayAPI/Controllers/BookingController.cs @@ -57,5 +57,12 @@ namespace GatewayAPI.Controllers HttpResponseMessage response = await bookingServiceClient.GetPreviousFlightBookingsAsync(); return new HttpResponseMessageResult(response); } + + [HttpPost("create-payment-intent")] + public async Task<IActionResult> CreatePaymentIntent([FromBody] PaymentIntentCreateRequest request) + { + HttpResponseMessage response = await bookingServiceClient.CreatePaymentIntent(request); + return new HttpResponseMessageResult(response); + } } } diff --git a/GatewayAPI/Models/PaymentIntentCreateRequest.cs b/GatewayAPI/Models/PaymentIntentCreateRequest.cs new file mode 100644 index 0000000000000000000000000000000000000000..6ebf13a12857fddbd5647b47f4607bb266aa866c --- /dev/null +++ b/GatewayAPI/Models/PaymentIntentCreateRequest.cs @@ -0,0 +1,13 @@ +namespace GatewayAPI.Models +{ + public class PaymentIntentCreateRequest + { + public List<Item> Items { get; set; } + } + + public class Item + { + public int Price { get; set; } + } + +} \ No newline at end of file