public async Task<ActionResult> Donate(NewDonationModel model) { var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); var currentUser = manager.FindById(User.Identity.GetUserId()); if (!ModelState.IsValid) { return View(model); } if (model.Type == true) { var plan = CreatePlan(model); var customer = GetCustomer(model, plan); var chargeId = await SubscriptionDonation(model, customer); var donate = new Donation() { Donation_Type = "Subscription - Monthly", Donation_Category = model.Category, Id = User.Identity.GetUserId(), Donation_Amount = model.Amount, Donation_Date = System.DateTime.Now, Active_Donation = true, Transaction_ID = chargeId }; db.Donations.Add(donate); db.SaveChanges(); } else { var chargeId = await OneTimeProcessPayment(model); var donate = new Donation() { Donation_Type = "One Time", Donation_Category = model.Category, Id = User.Identity.GetUserId(), Donation_Amount = model.Amount, Donation_Date = System.DateTime.Now, Transaction_ID = chargeId }; db.Donations.Add(donate); db.SaveChanges(); } return View("PaymentSuccessful"); }
private async Task<string> SubscriptionDonation(NewDonationModel model, StripeCustomer customer) { return await Task.Run(() => { var myCharge = new StripeChargeCreateOptions { Amount = (int)(model.Amount * 100), Currency = "usd", ReceiptEmail = customer.Email, CustomerId = customer.Id }; var chargeService = new StripeChargeService("sk_test_yPi2XADkAP3wiS1i6tkjErxZ"); var stripeCharge = chargeService.Create(myCharge); return stripeCharge.Id; }); }
private StripeCustomer GetCustomer(NewDonationModel model, StripePlan plan) { var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); var currentUser = manager.FindById(User.Identity.GetUserId()); string userEmail = currentUser.Email; var myCustomer = new StripeCustomerCreateOptions { Email = userEmail, Description = currentUser.FirstName + currentUser.LastName, Source = new StripeSourceOptions() { TokenId = model.Token } }; myCustomer.PlanId = plan.Id; var customerService = new StripeCustomerService("sk_test_yPi2XADkAP3wiS1i6tkjErxZ"); return customerService.Create(myCustomer); }
private async Task<string> OneTimeProcessPayment(NewDonationModel model) { return await Task.Run(() => { var myCharge = new StripeChargeCreateOptions { Amount = (int)(model.Amount * 100), Currency = "usd", ReceiptEmail = "*****@*****.**", Source = new StripeSourceOptions { TokenId = model.Token } }; var chargeService = new StripeChargeService("sk_test_yPi2XADkAP3wiS1i6tkjErxZ"); var stripeCharge = chargeService.Create(myCharge); return stripeCharge.Id; }); }
private StripePlan CreatePlan(NewDonationModel model) { var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); var currentUser = manager.FindById(User.Identity.GetUserId()); var myPlan = new StripePlanCreateOptions(); myPlan.Amount = (int)(model.Amount*100); myPlan.Currency = "usd"; myPlan.Interval = "month"; myPlan.Name = currentUser.FirstName + "-" + currentUser.LastName + "-" + model.Amount + "-" + model.Category; myPlan.Id = currentUser.FirstName + "-" + currentUser.LastName + "-" + model.Amount + "-" + model.Category; var planService = new StripePlanService("sk_test_yPi2XADkAP3wiS1i6tkjErxZ"); return planService.Create(myPlan); }