Skip to content
Snippets Groups Projects
Commit 82110e12 authored by paula_rodriguezslash's avatar paula_rodriguezslash
Browse files

creates payment intent endpoint

parent a2a53b87
No related branches found
No related tags found
No related merge requests found
......@@ -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 });
}
}
}
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
......@@ -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
......@@ -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);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment