public async Task <IActionResult> Buy(int?id) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (id == null) { return(NotFound()); } string userType = ""; if (User.IsInRole("Customer")) { userType = "Customer"; } if (User.IsInRole("Supplier")) { userType = "Supplier"; } var paymentPackage = await db.PaymentPackage .FirstOrDefaultAsync(m => m.Id == id); if (paymentPackage == null) { return(NotFound()); } ViewData["PaymentPackageId"] = (int)id; ViewData["PaymentPackage"] = paymentPackage; try { #region Stripe // Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys StripeConfiguration.ApiKey = StripeOptions?.Value?.SecretKey; var options = new PaymentIntentCreateOptions { Amount = (int)(paymentPackage.Price * 100), Currency = paymentPackage.PriceUnit, // Verify your integration in this guide by including this parameter Metadata = new Dictionary <string, string>() { { "integration_check", "accept_a_payment" }, { "paymentPackageId", paymentPackage.Id.ToString() }, { "userId", userId }, { "userType", userType }, } }; var service = new PaymentIntentService(); var paymentIntent = service.Create(options); ViewBag.ClientSecret = paymentIntent.ClientSecret; #endregion } catch (Exception e) { Serilog.Log.Error(e, Helpers.Constants.PAYMENT_ERROR, $"User : {User.Identity.Name}"); Message = Toast.ErrorToastFront(GetExceptionMessage(e, "Payment Error. Contact site administrator.")); //return Content(e.ToString()); } if (User.IsInRole("Supplier")) { FuelSupplier fuelSupplier = db.FuelSupplier.Where(s => s.UserId == userId).FirstOrDefault(); ViewData["Supplier"] = fuelSupplier; return(View("BuyS")); } else { DBContext.Models.Customer customer = db.Customer.Where(c => c.UserId == userId).FirstOrDefault(); ViewData["Customer"] = customer; return(View("Buy")); } }
public async Task <IActionResult> Buy(CustomerPackagesLog customerPackagesLog, int PaymentPackageId) { if (customerPackagesLog.Id != 0) { customerPackagesLog.Id = 0; } var paymentPackage = await db.PaymentPackage .FirstOrDefaultAsync(m => m.Id == PaymentPackageId); if (paymentPackage == null) { return(NotFound()); } ViewData["PaymentPackageId"] = (int)PaymentPackageId; ViewData["PaymentPackage"] = paymentPackage; try { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); int OfferId = 0; if (TempData["OfferId"] != null) { OfferId = Convert.ToInt32(TempData["OfferId"]); } DBContext.Models.Customer customer = db.Customer.Where(c => c.UserId == userId).FirstOrDefault(); if (ModelState.IsValid) { var user = await userManager.FindByIdAsync(userId); DateTime startDate = DateTime.UtcNow; DateTime expiryDate = startDate.AddDays((int)paymentPackage.Period); var claims = await userManager.GetClaimsAsync(user); foreach (var claim in claims) { if (claim.Type == "PackageExpiryDate" || claim.Type == "PaymentPackageId" || claim.Type == "PaymentPackageLevel") { await userManager.RemoveClaimAsync(user, claim); } } await userManager.AddClaimAsync(user, new Claim("PackageExpiryDate", expiryDate.ToString())); await userManager.AddClaimAsync(user, new Claim("PaymentPackageId", PaymentPackageId.ToString())); await userManager.AddClaimAsync(user, new Claim("PaymentPackageLevel", paymentPackage.ItemLevel.ToString())); customerPackagesLog.StartDate = DateTime.UtcNow; customerPackagesLog.EndDate = expiryDate; customerPackagesLog.PaymentPackageId = PaymentPackageId; customerPackagesLog.CustomerId = customer.Id; db.CustomerPackagesLog.Add(customerPackagesLog); await db.SaveChangesAsync(); Message = Toast.SucsessToast(); var url = Url.Action("Index", "Home", new { id = OfferId }); return(Redirect(url)); } } catch (Exception e) { Message = Toast.ErrorToastFront(GetExceptionMessage(e)); } ViewData["PaymentPackageId"] = (int)PaymentPackageId; ViewData["PaymentPackage"] = paymentPackage; return(View(customerPackagesLog)); }