Skip to content
Snippets Groups Projects
Commit 137c3f70 authored by paula_rodriguezslash's avatar paula_rodriguezslash
Browse files

stripe working woop woop

parent e2976612
No related branches found
No related tags found
No related merge requests found
Showing
with 71 additions and 9 deletions
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
<PackageReference Include="Stripe.net" Version="44.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup> </ItemGroup>
... ...
......
...@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Authorization; ...@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Security.Claims; using System.Security.Claims;
namespace BookingMicroservice.Controllers namespace BookingMicroservice.Controllers
{ {
[ApiController] [ApiController]
...@@ -13,11 +14,13 @@ namespace BookingMicroservice.Controllers ...@@ -13,11 +14,13 @@ namespace BookingMicroservice.Controllers
{ {
private readonly IReservationComplianceService reservationComplianceService; private readonly IReservationComplianceService reservationComplianceService;
private readonly IBookingService bookingService; private readonly IBookingService bookingService;
private readonly IStripeService stripeService;
public BookingController(IReservationComplianceService reservationComplianceService, IBookingService bookingService) public BookingController(IReservationComplianceService reservationComplianceService, IBookingService bookingService, IStripeService stripeService)
{ {
this.reservationComplianceService = reservationComplianceService; this.reservationComplianceService = reservationComplianceService;
this.bookingService = bookingService; this.bookingService = bookingService;
this.stripeService = stripeService;
} }
[Authorize] [Authorize]
...@@ -46,14 +49,20 @@ namespace BookingMicroservice.Controllers ...@@ -46,14 +49,20 @@ namespace BookingMicroservice.Controllers
[HttpPost()] [HttpPost()]
public async Task<IActionResult> MakeBooking([FromBody] BookingCreation bookingCreationModel) public async Task<IActionResult> MakeBooking([FromBody] BookingCreation bookingCreationModel)
{ {
// TODO: add stripe handling
string? userIdValue = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; string? userIdValue = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (!int.TryParse(userIdValue, out int userId)) if (!int.TryParse(userIdValue, out int userId))
return BadRequest("Unable to get User Id from Token"); return BadRequest("Unable to get User Id from Token");
try try
{ {
Booking? booking = await reservationComplianceService.TryCreateBookingAsync(bookingCreationModel.FlightId, userId, bookingCreationModel.BookingClass, bookingCreationModel.SeatId); bool isPaymentSuccessful = await stripeService.VerifyPaymentIntent(bookingCreationModel.PaymentIntentId);
if (!isPaymentSuccessful)
{
return BadRequest("Payment verification failed.");
}
Booking? booking = await reservationComplianceService.TryCreateBookingAsync(bookingCreationModel.FlightId, userId, bookingCreationModel.BookingClass, bookingCreationModel.SeatId, bookingCreationModel.PaymentIntentId);
if (booking == null) if (booking == null)
return BadRequest("Error in creating booking"); return BadRequest("Error in creating booking");
... ...
......
...@@ -5,5 +5,6 @@ ...@@ -5,5 +5,6 @@
public required int FlightId { get; set; } public required int FlightId { get; set; }
public required BookingClass BookingClass { get; set; } public required BookingClass BookingClass { get; set; }
public int? SeatId { get; set; } public int? SeatId { get; set; }
public required string PaymentIntentId { get; set; }
} }
} }
...@@ -13,6 +13,7 @@ builder.Services.AddHttpContextAccessor(); ...@@ -13,6 +13,7 @@ builder.Services.AddHttpContextAccessor();
builder.Services.AddTransient<RequestCookieHandler>(); builder.Services.AddTransient<RequestCookieHandler>();
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddLogging();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<ApplicationDbContext>(options => builder.Services.AddDbContext<ApplicationDbContext>(options =>
...@@ -59,6 +60,7 @@ builder.Services.AddHttpClient<IFlightServiceClient, FlightServiceClient>(client ...@@ -59,6 +60,7 @@ builder.Services.AddHttpClient<IFlightServiceClient, FlightServiceClient>(client
builder.Services.AddScoped<IBookingService, BookingService>(); builder.Services.AddScoped<IBookingService, BookingService>();
builder.Services.AddScoped<IReservationComplianceService, ReservationComplianceService>(); builder.Services.AddScoped<IReservationComplianceService, ReservationComplianceService>();
builder.Services.AddScoped<IStripeService, StripeService>();
var app = builder.Build(); var app = builder.Build();
... ...
......
...@@ -30,7 +30,7 @@ namespace BookingMicroservice.Services ...@@ -30,7 +30,7 @@ namespace BookingMicroservice.Services
return bookings; return bookings;
} }
public Booking CreateBooking(int flightId, int userId, BookingClass bookingClass) public Booking CreateBooking(int flightId, int userId, BookingClass bookingClass, string paymentIntentId)
{ {
Booking booking = new Booking(flightId, userId, bookingClass, null); Booking booking = new Booking(flightId, userId, bookingClass, null);
... ...
......
...@@ -4,7 +4,7 @@ namespace BookingMicroservice.Services ...@@ -4,7 +4,7 @@ namespace BookingMicroservice.Services
{ {
public interface IBookingService public interface IBookingService
{ {
Booking CreateBooking(int flightId, int userId, BookingClass bookingClass); Booking CreateBooking(int flightId, int userId, BookingClass bookingClass, string paymentIntentId);
List<Booking> GetBookings(int? flightId = null, int? userId = null, BookingClass? bookingClass = null); List<Booking> GetBookings(int? flightId = null, int? userId = null, BookingClass? bookingClass = null);
Booking? GetBooking(int bookingId); Booking? GetBooking(int bookingId);
void DeleteBooking(int bookingId); void DeleteBooking(int bookingId);
... ...
......
...@@ -4,7 +4,7 @@ namespace BookingMicroservice.Services ...@@ -4,7 +4,7 @@ namespace BookingMicroservice.Services
{ {
public interface IReservationComplianceService public interface IReservationComplianceService
{ {
Task<Booking?> TryCreateBookingAsync(int flightId, int userId, BookingClass bookingClass, int? seatId); Task<Booking?> TryCreateBookingAsync(int flightId, int userId, BookingClass bookingClass, int? seatId, string paymentIntentId);
Task TryBookSeatAsync(int bookingId, int seatId); Task TryBookSeatAsync(int bookingId, int seatId);
} }
... ...
......
using BookingMicroservice.Models;
using Stripe;
namespace BookingMicroservice.Services
{
public interface IStripeService
{
Task<bool> VerifyPaymentIntent(string paymentIntentId);
}
}
\ No newline at end of file
...@@ -18,7 +18,7 @@ namespace BookingMicroservice.Services ...@@ -18,7 +18,7 @@ namespace BookingMicroservice.Services
this.flightServiceClient = flightServiceClient; this.flightServiceClient = flightServiceClient;
} }
public async Task<Booking?> TryCreateBookingAsync(int flightId, int userId, BookingClass bookingClass, int? seatId) public async Task<Booking?> TryCreateBookingAsync(int flightId, int userId, BookingClass bookingClass, int? seatId, string paymentIntentId)
{ {
HttpResponseMessage capacityResponse = await flightServiceClient.GetFlightCapacityAsync(flightId, (int)bookingClass); HttpResponseMessage capacityResponse = await flightServiceClient.GetFlightCapacityAsync(flightId, (int)bookingClass);
if (!capacityResponse.IsSuccessStatusCode) if (!capacityResponse.IsSuccessStatusCode)
...@@ -34,7 +34,7 @@ namespace BookingMicroservice.Services ...@@ -34,7 +34,7 @@ namespace BookingMicroservice.Services
if (currentBookings >= capacity) if (currentBookings >= capacity)
throw new BookingException(string.Format(NO_AVAILABLE_SEAT_MSG, bookingClass.ToString().ToLower())); throw new BookingException(string.Format(NO_AVAILABLE_SEAT_MSG, bookingClass.ToString().ToLower()));
Booking booking = bookingService.CreateBooking(flightId, userId, bookingClass); Booking booking = bookingService.CreateBooking(flightId, userId, bookingClass, paymentIntentId);
if(seatId.HasValue && booking?.Id != null) if(seatId.HasValue && booking?.Id != null)
{ {
try try
... ...
......
using BookingMicroservice.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Stripe;
using System;
using System.Threading.Tasks;
namespace BookingMicroservice.Services
{
public class StripeService : IStripeService
{
private readonly ILogger<StripeService> _logger;
public StripeService(ILogger<StripeService> logger, IConfiguration configuration)
{
_logger = logger;
StripeConfiguration.ApiKey = configuration["Stripe:SecretKey"];
}
public async Task<bool> VerifyPaymentIntent(string paymentIntentId)
{
var service = new PaymentIntentService();
try
{
var paymentIntent = await service.GetAsync(paymentIntentId);
_logger.LogInformation($"Payment Intent Status: {paymentIntent.Status}");
return paymentIntent.Status == "succeeded";
}
catch (StripeException ex)
{
_logger.LogError($"Error verifying payment intent: {ex.Message}");
throw new Exception("Failed to verify payment intent: " + ex.Message);
}
}
}
}
...@@ -16,5 +16,8 @@ ...@@ -16,5 +16,8 @@
}, },
"FlightMicroservice": { "FlightMicroservice": {
"BaseUrl": "http://localhost:5175" "BaseUrl": "http://localhost:5175"
},
"Stripe": {
"SecretKey": "sk_test_51P5UhOExQclpActcAn0yMrhaHESmCjtM69JOhjmo4B5AK67WwZn317QZmbunbuanwytn3xxhgVh6wfxEONorblgl00rQbt1YZk"
} }
} }
...@@ -5,6 +5,6 @@ ...@@ -5,6 +5,6 @@
public required int FlightId { get; set; } public required int FlightId { get; set; }
public required int BookingClass { get; set; } public required int BookingClass { get; set; }
public int? SeatId { get; set; } public int? SeatId { get; set; }
public required string PaymentIntentId { get; set; }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment