2022-05-30 13:25:27 -07:00
|
|
|
using API.Errors;
|
2022-05-27 15:40:30 -07:00
|
|
|
using Core.Entities;
|
|
|
|
using Core.Interfaces;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-05-31 11:38:23 -07:00
|
|
|
using Stripe;
|
|
|
|
using Order = Core.Entities.OrderAggregate.Order;
|
2022-05-27 15:40:30 -07:00
|
|
|
|
|
|
|
namespace API.Controllers
|
|
|
|
{
|
|
|
|
public class PaymentsController : BaseApiController
|
|
|
|
{
|
|
|
|
private readonly IPaymentService _paymentService;
|
2022-05-31 11:38:23 -07:00
|
|
|
private const string WhSecret = "whsec_231beef81e46f9d27d4543dda6f4fbbd72adcc08605ff5549cc9cb36f38fcf78";
|
|
|
|
private readonly ILogger<IPaymentService> _logger;
|
|
|
|
public PaymentsController(IPaymentService paymentService, ILogger<IPaymentService> logger)
|
2022-05-27 15:40:30 -07:00
|
|
|
{
|
2022-05-31 11:38:23 -07:00
|
|
|
_logger = logger;
|
2022-05-27 15:40:30 -07:00
|
|
|
_paymentService = paymentService;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
[HttpPost("{basketId}")]
|
|
|
|
public async Task<ActionResult<CustomerBasket>> CreateOrUpdatePaymentIntent(string basketId)
|
|
|
|
{
|
2022-05-30 13:25:27 -07:00
|
|
|
var basket = await _paymentService.CreateOrUpdatePaymentIntent(basketId);
|
|
|
|
if(basket == null) return BadRequest(new ApiResponse(400, "Problem with your basket"));
|
|
|
|
return basket;
|
2022-05-27 15:40:30 -07:00
|
|
|
}
|
2022-05-31 11:38:23 -07:00
|
|
|
|
|
|
|
[HttpPost("webhook")]
|
|
|
|
public async Task<ActionResult> StripeWebhook()
|
|
|
|
{
|
|
|
|
var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
|
|
|
|
var stripeEvent = EventUtility.ConstructEvent(json, Request.Headers["Stripe-Signature"], WhSecret);
|
|
|
|
|
|
|
|
PaymentIntent intent;
|
|
|
|
Order order;
|
|
|
|
|
|
|
|
switch (stripeEvent.Type)
|
|
|
|
{
|
|
|
|
case "payment_intent.succeeded":
|
|
|
|
intent = (PaymentIntent) stripeEvent.Data.Object;
|
|
|
|
_logger.LogInformation("Payment Succeeded ", intent.Id);
|
|
|
|
order = await _paymentService.UpdateOrderPaymentSucceeeded(intent.Id);
|
|
|
|
_logger.LogInformation("Order updated to payment received ", order.Id);
|
|
|
|
break;
|
|
|
|
case "payment_intent.payment_failed":
|
|
|
|
intent = (PaymentIntent) stripeEvent.Data.Object;
|
|
|
|
_logger.LogInformation("Payment Failed ", intent.Id);
|
|
|
|
order = await _paymentService.UpdateOrderPaymentFailed(intent.Id);
|
|
|
|
_logger.LogInformation("Payment Failed ", order.Id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new EmptyResult();
|
|
|
|
}
|
2022-05-27 15:40:30 -07:00
|
|
|
}
|
2022-05-31 11:38:23 -07:00
|
|
|
}
|