diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs index e51bf90..10fa620 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs @@ -176,6 +176,68 @@ public class InvoiceController : Controller { AddressModel ad; user.Wait(); + inv.Products = new(); + + foreach (var prod in prods) { + inv.Products.Add(new() { + Product = prod, + Quantity = sinv.ProdQuant.First(x => x.Key == prod.Id).Value + }); + } + + foreach (var prod in sinv.ProdQuant) { // Update de quantités dans l'inventaire. + ProductModel inventProd = prods.Where(x => x.Id == prod.Key).First(); + if (inventProd.Quantity < prod.Value) + badprods.Add(prod.Key, inventProd.Quantity); + else if (inventProd.Quantity == prod.Value) { + inventProd.Quantity = 0; + inventProd.Status = inventProd.Status == ProductModel.States.Clearance ? + ProductModel.States.Discontinued : + ProductModel.States.BackOrder; + } else inventProd.Quantity -= prod.Value; + inventProd.LastSale = DateTime.Now; + inventProd.Sales += prod.Value; + } + + if (badprods.Count > 0) // Retour des produits non-achetable avec l'inventaire restant. + return BadRequest(badprods.ToArray()); + + // Charges! + StripeConfiguration.ApiKey = _stripeOptions.Value.SecretKey; + ChargesModel chr; + + chr = new() { + Token = sinv.Token, + AmountInCents = sinv.AmountInCents, + Name = sinv.Name, + Phone = sinv.Phone, + Email = sinv.Email, + Description = sinv.Description, + CurrencyCode = sinv.CurrencyCode + }; + + inv.Payment = chr; + + var options = new ChargeCreateOptions { + Amount = sinv.AmountInCents, + Description = sinv.Description, + Source = sinv.Token, + Currency = sinv.CurrencyCode, + }; + var service = new ChargeService(); + Charge charge = service.Create(options); + + if (charge.FailureMessage is not null && charge.FailureMessage != "") + return Json(charge.ToJson()); + + try { + _context.Invoices.Update(inv); + _context.SaveChanges(); + } catch (Exception ex) { + _logger.LogError(20, ex.Message); + return BadRequest(ex.Message); + } + if (user.Result is not null) { inv.LinkedAccount = user.Result; ad = _context.Addresses.FirstOrDefault(x => x.CivicNumber == sinv.CivicNumber && @@ -212,37 +274,10 @@ public class InvoiceController : Controller { _logger.LogError(8, e.Message); return BadRequest(); } - if (prods.Count == 0) return BadRequest("Vous devez inclure au moins un produit à votre commande."); - inv.Products = new(); - - foreach (var prod in prods) { - inv.Products.Add(new() { - Product = prod, - Quantity = sinv.ProdQuant.First(x => x.Key == prod.Id).Value - }); - } - - foreach (var prod in sinv.ProdQuant) { // Update de quantités dans l'inventaire. - ProductModel inventProd = prods.Where(x => x.Id == prod.Key).First(); - if (inventProd.Quantity < prod.Value) - badprods.Add(prod.Key, inventProd.Quantity); - else if (inventProd.Quantity == prod.Value) { - inventProd.Quantity = 0; - inventProd.Status = inventProd.Status == ProductModel.States.Clearance ? - ProductModel.States.Discontinued : - ProductModel.States.BackOrder; - } else inventProd.Quantity -= prod.Value; - inventProd.LastSale = DateTime.Now; - inventProd.Sales += prod.Value; - } - - if (badprods.Count > 0) // Retour des produits non-achetable avec l'inventaire restant. - return BadRequest(badprods.ToArray()); - try { // Faire les updates dans la BD. if (ad.Id == 0) _context.Addresses.Add(ad); @@ -255,7 +290,7 @@ public class InvoiceController : Controller { } _cache.askForRefresh(); - return Ok(inv); + return Json(charge.ToJson()); } [HttpPost("Cancel/{id}")] diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/SendInvoiceModel.cs b/GrossesMitaines/GrossesMitainesAPI/Models/SendInvoiceModel.cs index 3e0a9b4..aa3cf68 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Models/SendInvoiceModel.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Models/SendInvoiceModel.cs @@ -26,4 +26,13 @@ namespace GrossesMitainesAPI.Models; public string PostalCode { get; set; } public Dictionary ProdQuant { get; set; } + + // partie pour la charge. + 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; } } \ No newline at end of file