diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs index 6606d74..b954c59 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs @@ -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 _signInMan; private readonly Microsoft.AspNetCore.Identity.UserManager _userMan; + private readonly IOptions _stripeOptions; #endregion @@ -32,17 +35,63 @@ public class InvoiceController : Controller { InventoryContext context, DatabaseCacheService cache, SignInManager signInMan, - Microsoft.AspNetCore.Identity.UserManager userMan) { + Microsoft.AspNetCore.Identity.UserManager userMan, + IOptions 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> Get(bool? all = false) { IList 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()); diff --git a/GrossesMitaines/GrossesMitainesAPI/GrossesMitainesAPI.csproj b/GrossesMitaines/GrossesMitainesAPI/GrossesMitainesAPI.csproj index b913230..3e940eb 100644 --- a/GrossesMitaines/GrossesMitainesAPI/GrossesMitainesAPI.csproj +++ b/GrossesMitaines/GrossesMitainesAPI/GrossesMitainesAPI.csproj @@ -22,6 +22,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/ChargeReturnModel.cs b/GrossesMitaines/GrossesMitainesAPI/Models/ChargeReturnModel.cs new file mode 100644 index 0000000..b594d91 --- /dev/null +++ b/GrossesMitaines/GrossesMitainesAPI/Models/ChargeReturnModel.cs @@ -0,0 +1,11 @@ +namespace GrossesMitainesAPI.Models; +public class ChargeReturnModel { + public string Token { get; set; } + public string Description { get; set; } + public long AmountInCents { get; set; } + public string CurrencyCode { get; set; } + public string Name { get; set; } + public string Email { get; set; } + public string Phone { get; set; } + public int Invoice { get; set; } +} diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/ChargesModel.cs b/GrossesMitaines/GrossesMitainesAPI/Models/ChargesModel.cs new file mode 100644 index 0000000..76a1d8c --- /dev/null +++ b/GrossesMitaines/GrossesMitainesAPI/Models/ChargesModel.cs @@ -0,0 +1,10 @@ +namespace GrossesMitainesAPI.Models; +public class ChargesModel { + public string Token { get; set; } + public string Description { get; set; } + public long AmountInCents { get; set; } + public string CurrencyCode { get; set; } + public string Name { get; set; } + public string Email { get; set; } + public string Phone { get; set; } +} diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/InvoiceModel.cs b/GrossesMitaines/GrossesMitainesAPI/Models/InvoiceModel.cs index 6457e46..49a5e0d 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Models/InvoiceModel.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Models/InvoiceModel.cs @@ -38,6 +38,7 @@ public class InvoiceModel { [Required] public AddressModel ShippingAddress { get; set; } public InStates Status { get; set; } = InStates.Confirmed; + public ChargesModel Payment { get; set; } // Pour enregistrer le paiement. public InvoiceModel() { } public InvoiceModel(SendInvoiceModel sinv) { diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/StripeOptions.cs b/GrossesMitaines/GrossesMitainesAPI/Models/StripeOptions.cs new file mode 100644 index 0000000..110c31a --- /dev/null +++ b/GrossesMitaines/GrossesMitainesAPI/Models/StripeOptions.cs @@ -0,0 +1,6 @@ +namespace GrossesMitainesAPI.Models; +public class StripeOptions { + public string SecretKey { get; set; } + public string PublicKey { get; set; } +} + diff --git a/GrossesMitaines/GrossesMitainesAPI/Program.cs b/GrossesMitaines/GrossesMitainesAPI/Program.cs index ad50110..a0a022c 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Program.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Program.cs @@ -1,11 +1,14 @@ using GrossesMitainesAPI.Data; +using GrossesMitainesAPI.Models; using GrossesMitainesAPI.Services; using Microsoft.AspNet.Identity; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; +using Stripe; using System.Net; var MyAllowSpecificOrigins = "_myAllowSpecificOrigins"; @@ -24,6 +27,10 @@ builder.Services.AddCors(options => { builder.Services.AddControllers(); +builder.Services.Configure(options => + builder.Configuration.GetSection("StripeTest").Bind(options) + ); + builder.Services.AddIdentityCore() .AddRoles() .AddEntityFrameworkStores() diff --git a/GrossesMitaines/GrossesMitainesAPI/appsettings.json b/GrossesMitaines/GrossesMitainesAPI/appsettings.json index 84d6132..0b2724c 100644 --- a/GrossesMitaines/GrossesMitainesAPI/appsettings.json +++ b/GrossesMitaines/GrossesMitainesAPI/appsettings.json @@ -8,5 +8,9 @@ "AllowedHosts": "*", "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrossesMitainesDB; Trusted_Connection=True; MultipleActiveResultSets=true" + }, + "StripeTest": { + "PublicKey": "pk_test_", + "SecretKey": "sk_test_" } }