Début du Stripe dans l'API (clés pas là)
This commit is contained in:
		| @@ -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()); | ||||
|   | ||||
| @@ -22,6 +22,7 @@ | ||||
|       <PrivateAssets>all</PrivateAssets> | ||||
|       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||||
|     </PackageReference> | ||||
|     <PackageReference Include="Stripe.net" Version="41.1.0" /> | ||||
|     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> | ||||
|     <PackageReference Include="System.Drawing.Common" Version="6.0.0" /> | ||||
|   </ItemGroup> | ||||
|   | ||||
| @@ -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; } | ||||
| } | ||||
							
								
								
									
										10
									
								
								GrossesMitaines/GrossesMitainesAPI/Models/ChargesModel.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								GrossesMitaines/GrossesMitainesAPI/Models/ChargesModel.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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; } | ||||
| } | ||||
| @@ -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) {  | ||||
|   | ||||
| @@ -0,0 +1,6 @@ | ||||
| namespace GrossesMitainesAPI.Models; | ||||
| public class StripeOptions { | ||||
|     public string SecretKey { get; set; } | ||||
|     public string PublicKey { get; set; } | ||||
| } | ||||
|  | ||||
| @@ -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<StripeOptions>(options => | ||||
|                builder.Configuration.GetSection("StripeTest").Bind(options) | ||||
|            ); | ||||
|  | ||||
| builder.Services.AddIdentityCore<InventoryUser>() | ||||
|                     .AddRoles<IdentityRole>() | ||||
|                     .AddEntityFrameworkStores<InventoryContext>() | ||||
|   | ||||
| @@ -8,5 +8,9 @@ | ||||
|   "AllowedHosts": "*", | ||||
|   "ConnectionStrings": { | ||||
|     "DefaultConnection": "Server=(localdb)\\mssqllocaldb; Database=GrossesMitainesDB; Trusted_Connection=True; MultipleActiveResultSets=true" | ||||
|   }, | ||||
|   "StripeTest": { | ||||
|     "PublicKey": "pk_test_", | ||||
|     "SecretKey": "sk_test_" | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user