Début du Stripe dans l'API (clés pas là)

This commit is contained in:
MarcEricMartel
2022-12-04 08:28:12 -08:00
parent 6cc93bade0
commit 21cb3f0aa3
8 changed files with 92 additions and 1 deletions

View File

@@ -12,6 +12,8 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.AspNet.Identity;
using System.Data;
using System.Linq;
using Microsoft.Extensions.Options;
using Stripe;
#endregion
@@ -24,6 +26,7 @@ public class InvoiceController : Controller {
private readonly DatabaseCacheService _cache;
private readonly SignInManager<InventoryUser> _signInMan;
private readonly Microsoft.AspNetCore.Identity.UserManager<InventoryUser> _userMan;
private readonly IOptions<StripeOptions> _stripeOptions;
#endregion
@@ -32,17 +35,63 @@ public class InvoiceController : Controller {
InventoryContext context,
DatabaseCacheService cache,
SignInManager<InventoryUser> signInMan,
Microsoft.AspNetCore.Identity.UserManager<InventoryUser> userMan) {
Microsoft.AspNetCore.Identity.UserManager<InventoryUser> userMan,
IOptions<StripeOptions> stripeOptions) {
_logger = logger;
_context = context;
_cache = cache;
_userMan = userMan;
_signInMan = signInMan;
_stripeOptions = stripeOptions;
}
#endregion
#region API Methods
[HttpPost("Payment")]
public IActionResult Charges([FromBody] ChargeReturnModel model) {
StripeConfiguration.ApiKey = _stripeOptions.Value.SecretKey;
InvoiceModel inv;
ChargesModel chr;
try {
inv = _context.Invoices.First(x => x.Id == model.Invoice);
} catch {
return BadRequest("Numéro de commande invalide.");
}
chr = new() {
Token = model.Token,
AmountInCents = model.AmountInCents,
Name = model.Name,
Phone = model.Phone,
Email = model.Email,
Description = model.Description,
CurrencyCode = model.CurrencyCode
};
inv.Payment = chr;
var options = new ChargeCreateOptions {
Amount = model.AmountInCents,
Description = model.Description,
Source = model.Token,
Currency = model.CurrencyCode,
};
var service = new ChargeService();
Charge charge = service.Create(options);
try {
_context.Invoices.Update(inv);
_context.SaveChanges();
} catch (Exception ex) {
_logger.LogError(20, ex.Message);
return BadRequest(ex.Message);
}
return Json(charge.ToJson());
}
[HttpGet]
public ActionResult<List<InvoiceModel>> Get(bool? all = false) {
IList<string> roles;
@@ -64,12 +113,14 @@ public class InvoiceController : Controller {
return Ok(_context.Invoices
.Include(x => x.ShippingAddress)
.Include(x => x.LinkedAccount)
.Include(x => x.Payment)
.Include(x => x.Products)
.ThenInclude(y => y.Product)
.ToList());
else return Ok(_context.Invoices
.Include(x => x.ShippingAddress)
.Include(x => x.LinkedAccount)
.Include(x => x.Payment)
.Include(x => x.Products)
.ThenInclude(y => y.Product)
.Where(x => x.LinkedAccount != null && x.LinkedAccount.Id == id).ToList());