64 lines
2.4 KiB
C#
Executable File
64 lines
2.4 KiB
C#
Executable File
using Microsoft.AspNetCore.Mvc;
|
|
using MistoxWebsite.Server.Controllers.Payment;
|
|
using MistoxWebsite.Server.Services.DatabaseService;
|
|
using MistoxWebsite.Server.Entities;
|
|
|
|
namespace MistoxWebsite.Server.Controllers {
|
|
[ApiController]
|
|
public class PaymentController : ControllerBase {
|
|
|
|
DatabaseService _databaseService;
|
|
IPayment _paymentService;
|
|
|
|
public PaymentController(DatabaseService databaseService) {
|
|
_databaseService = databaseService;
|
|
|
|
if (IPayment._PaymentType == PaymentType.StripeIntent) {
|
|
_paymentService = new StripeIntent(_databaseService);
|
|
} else {
|
|
// Fallback
|
|
_paymentService = new StripeIntent(_databaseService);
|
|
}
|
|
// Add new payment plugins here
|
|
|
|
}
|
|
|
|
[Route("api/getCheckoutToken")]
|
|
[HttpPost]
|
|
public async Task<string> GetPaymentKey( [FromQuery] string userID ) {
|
|
string OrderNumber = Guid.NewGuid().ToString().Substring(0,10);
|
|
Account? acc = await _databaseService.GetAccount(userID);
|
|
if (acc != null) {
|
|
List<Cart> cart = await _databaseService.GetCart(acc);
|
|
(bool, string) PaymentResponse = await _paymentService.TryGetCheckoutToken(OrderNumber, acc, cart);
|
|
if (PaymentResponse.Item1) {
|
|
// Returns client secret
|
|
return PaymentResponse.Item2;
|
|
} else {
|
|
Console.WriteLine("An error has occured in the payment plugin\n\n");
|
|
Console.WriteLine(PaymentResponse.Item2);
|
|
Console.WriteLine("\n");
|
|
return "An error has occured in the payment plugin";
|
|
}
|
|
} else {
|
|
return "Unable to find account";
|
|
}
|
|
}
|
|
|
|
[Route( "/api/payment/response" )]
|
|
[HttpPost]
|
|
public async Task<IActionResult> paymentWebhook() {
|
|
try {
|
|
string body = await new StreamReader(Request.Body).ReadToEndAsync();
|
|
await _paymentService.ValidatePurchase(body, Request.Headers["Stripe-Signature"].ToString());
|
|
return Ok();
|
|
} catch (Exception ex) {
|
|
return NotFound(ex.ToString());
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|