This commit is contained in:
MarcEricMartel 2022-12-08 18:24:36 -08:00
parent 7f252bc030
commit 481e4f6d81
2 changed files with 72 additions and 28 deletions

View File

@ -176,6 +176,68 @@ public class InvoiceController : Controller {
AddressModel ad; AddressModel ad;
user.Wait(); 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) { if (user.Result is not null) {
inv.LinkedAccount = user.Result; inv.LinkedAccount = user.Result;
ad = _context.Addresses.FirstOrDefault(x => x.CivicNumber == sinv.CivicNumber && ad = _context.Addresses.FirstOrDefault(x => x.CivicNumber == sinv.CivicNumber &&
@ -212,37 +274,10 @@ public class InvoiceController : Controller {
_logger.LogError(8, e.Message); _logger.LogError(8, e.Message);
return BadRequest(); return BadRequest();
} }
if (prods.Count == 0) if (prods.Count == 0)
return BadRequest("Vous devez inclure au moins un produit à votre commande."); 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. try { // Faire les updates dans la BD.
if (ad.Id == 0) if (ad.Id == 0)
_context.Addresses.Add(ad); _context.Addresses.Add(ad);
@ -255,7 +290,7 @@ public class InvoiceController : Controller {
} }
_cache.askForRefresh(); _cache.askForRefresh();
return Ok(inv); return Json(charge.ToJson());
} }
[HttpPost("Cancel/{id}")] [HttpPost("Cancel/{id}")]

View File

@ -26,4 +26,13 @@ namespace GrossesMitainesAPI.Models;
public string PostalCode { get; set; } public string PostalCode { get; set; }
public Dictionary<int, uint> ProdQuant { get; set; } public Dictionary<int, uint> 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; }
} }