public async Task <IActionResult> CheckoutPayment(CheckoutVM model) { //register the user and sign them in if they are not already signed in ApplicationUser user = await CurrentUser(); if (user == null) { var userresult = await CreateUser(model); if (userresult.HasErrors) { CheckoutErrorVM errorVM = new CheckoutErrorVM() { ErrorMessage = userresult.ErrorMessage }; return(RedirectToAction("CheckoutError", errorVM)); } else { //we now have a user user = userresult.User; } } //hook up with the stripe service and get the payment intent //add this to the model and pass it on model.Cart = _cart; PaymentIntentResult result = await _stripeService.CreatePaymentIntent((model.Cart.Total * 100), model.Email); //add the user to the courses but with the PaymentSucceeded set to false for now //this is set when the webhook handles the payment success event var useraddedresult = AddUserToCourses(user, result.PaymentIntentId); if (!useraddedresult.Succeeded) { //we've been able to create the user but unable to add them to any courses //this is highly unlikely but in this event we don't want them to go through //to the payment form where the payment will be asked to be collected //from stripe. So just send them to an error page CheckoutErrorVM errorVM = new CheckoutErrorVM() { ErrorMessage = "There was a problem adding the user to the course(s). This may be a system problem. Please try again later." }; return(RedirectToAction("CheckoutError", errorVM)); } model.Cart.ClientSecret = result.ClientSecret; model.PublishableKey = result.PublishableKey; return(View(model)); }
public async Task <PaymentIntentResult> PaymentIntentCreated(PaymentIntent paymentIntent) { _logger.LogInformation(GetLogMessage("Payment Intent: {0}"), paymentIntent.Id); var retVal = new PaymentIntentResult() { PaymentIntentId = paymentIntent.Id }; var pi = new StripePaymentIntent { Id = paymentIntent.Id, ObjectState = ObjectState.Added, CustomerId = paymentIntent.CustomerId, Amount = paymentIntent.Amount.GetValueOrDefault() / 100m, AmountCapturable = paymentIntent.AmountCapturable.GetValueOrDefault() / 100m, Created = paymentIntent.Created, Description = paymentIntent.Description, InvoiceId = paymentIntent.InvoiceId, AmountReceived = paymentIntent.AmountReceived.GetValueOrDefault() / 100m, TransferGroup = paymentIntent.TransferGroup, CancelledAt = paymentIntent.CanceledAt, Updated = DateTimeOffset.UtcNow, CaptureMethod = paymentIntent.CaptureMethod, ConfirmationMethod = paymentIntent.ConfirmationMethod }; var records = _paymentIntents.InsertOrUpdateGraph(pi, true); _logger.LogDebug(GetLogMessage("{0} Payment Intent Records Updated"), records); if (records > 0) { retVal.Succeeded = true; } return(await Task.FromResult(retVal)); }
public async Task <PaymentIntentResult> PaymentIntentUpdated(PaymentIntent paymentIntent) { _logger.LogInformation(GetLogMessage("Payment Intent: {0}"), paymentIntent.Id); var retVal = new PaymentIntentResult() { PaymentIntentId = paymentIntent.Id }; var entity = await _paymentIntents.Queryable().Where(x => x.Id == paymentIntent.Id) .FirstOrDefaultAsync(); if (entity != null) { entity.Amount = paymentIntent.Amount ?? paymentIntent.Amount.GetValueOrDefault() / 100m; entity.AmountCapturable = paymentIntent.AmountCapturable ?? paymentIntent.AmountCapturable.GetValueOrDefault() / 100m; entity.AmountReceived = paymentIntent.AmountReceived ?? paymentIntent.AmountReceived.GetValueOrDefault() / 100m; entity.CancelledAt = paymentIntent.CanceledAt; entity.ConfirmationMethod = paymentIntent.ConfirmationMethod; entity.CaptureMethod = paymentIntent.CaptureMethod; entity.Description = paymentIntent.Description; entity.CustomerId = paymentIntent.CustomerId; entity.InvoiceId = paymentIntent.InvoiceId; entity.TransferGroup = paymentIntent.TransferGroup; } var results = _paymentIntents.InsertOrUpdateGraph(entity, true); _logger.LogDebug(GetLogMessage("{0} updated payment intent records"), results); if (results > 0) { retVal.Succeeded = true; } return(retVal); }