Something went wrong on our end
-
Abdelsamad, Mouaz R (UG - SISC) authored0a931a09
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
StripeController.cs 2.23 KiB
using FlightBooking.Service.Data.Configs;
using FlightBooking.Service.Services;
using FlightBooking.Service.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Stripe;
using System.Text;
namespace FlightBooking.Service.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StripeController : ControllerBase
{
private readonly StripeConfig _stripeConfig;
private readonly IStripeService _stripeService;
private readonly ILogger<StripeController> _logger;
public StripeController(IOptionsMonitor<StripeConfig> options, IStripeService stripeService, ILogger<StripeController> logger)
{
_stripeConfig = options.CurrentValue;
_stripeService = stripeService;
_logger = logger;
}
//webhook for stripe to verify payment
[HttpPost]
public async Task<IActionResult> NotificationWebhookAsync()
{
//var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
string requestBody;
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
requestBody = await reader.ReadToEndAsync();
}
if (string.IsNullOrEmpty(requestBody))
{
//_logger.LogInformation("Event object is empty", eventObject);
return BadRequest();
}
string stripeSigningKey = _stripeConfig.SigningSecret;
Event stripeEvent;
try
{
stripeEvent = EventUtility.ConstructEvent(requestBody, Request.Headers["Stripe-Signature"], stripeSigningKey, throwOnApiVersionMismatch: false);
}
catch (StripeException exception)
{
_logger.LogError(exception.ToString());
return BadRequest();
}
//Since this is the only event we are handling.
if (stripeEvent.Type == Events.CheckoutSessionCompleted)
{
var response = await _stripeService.ProcessPayment(stripeEvent);
return response.FormatResponse();
}
return Ok();
}
}
}