From 82110e129163b3e3b7f8171cb4705a11b431d650 Mon Sep 17 00:00:00 2001 From: paula_rodriguezslash <paula.rodriguez@slashmobility.com> Date: Tue, 23 Apr 2024 14:42:27 +0100 Subject: [PATCH] creates payment intent endpoint --- .../Controllers/BookingController.cs | 26 ++++++++++++++++++- .../Models/PaymentIntentCreateRequest.cs | 13 ++++++++++ .../Services/IStripeService.cs | 1 + BookingMicroservice/Services/StripeService.cs | 7 +++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 BookingMicroservice/Models/PaymentIntentCreateRequest.cs diff --git a/BookingMicroservice/Controllers/BookingController.cs b/BookingMicroservice/Controllers/BookingController.cs index 77150c6..c9cfa21 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 0000000..de5bc08 --- /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 0c3a097..bee9db5 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 68963dd..06640cd 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); + } } } -- GitLab