public async Task <IActionResult> Index(string stripePaymentFormId, [FromQuery] Dictionary <string, string> metadata)
        {
            var stripePaymentForm = await _contentManager.GetAsync(stripePaymentFormId);

            //payment part
            var paymentPart = stripePaymentForm.As <PaymentPart>();
            var cost        = (long)(paymentPart.Cost * 100);
            var currency    = paymentPart.Currency.Text;

            //stripe payment form part
            var stripePaymentFormPart = stripePaymentForm.As <StripePaymentFormPart>();
            var StripeSecretKey       = stripePaymentFormPart.StripeSecretKey.Text;
            var stripePublishableKey  = stripePaymentFormPart.StripePublishableKey.Text;
            var paymentIntent         = await _stripePaymentService.CreatePaymentIntent(StripeSecretKey, cost, currency, metadata);

            //Should be using shapes but need a way to pass in custom metadata to an existing form
            var shape = await _contentDisplay.BuildDisplayAsync(stripePaymentForm, _updateModelAccessor.ModelUpdater);

            var stripePaymentFormViewModel = new StripePaymentFormViewModel()
            {
                PaymentIntentClientSecret = paymentIntent.ClientSecret,
                StripePublishableAPIKey   = stripePublishableKey,
                Shape = shape
            };

            return(View(stripePaymentFormViewModel));
        }
        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 override async Task <IDisplayResult> DisplayAsync(StripePaymentFormPart stripePaymentFormPart, BuildPartDisplayContext context)
        {
            var paymentPart   = stripePaymentFormPart.ContentItem.Get <PaymentPart>("PaymentPart");
            var cost          = (long)(paymentPart.Cost * 100);
            var currency      = paymentPart.Currency.Text;
            var paymentIntent = await _stripePaymentService.CreatePaymentIntent(stripePaymentFormPart.StripeSecretKey.Text, cost, currency, new Dictionary <string, string>());

            return(Initialize <StripePaymentFormPartViewModel>("StripePaymentFormPart", m =>
            {
                m.PublishableKey = stripePaymentFormPart.StripePublishableKey.Text;
                m.IntentClientSecret = paymentIntent.ClientSecret;
            })
                   .Location("Detail", "Content:5")
                   .Location("Summary", "Content:5"));
        }
        public override async Task <IDisplayResult> DisplayAsync(StripePaymentFormPart stripePaymentFormPart, BuildPartDisplayContext context)
        {
            var paymentPart = stripePaymentFormPart.ContentItem.As <PaymentPart>();

            paymentPart = new PaymentPart()
            {
                Cost = 200
            };

            var paymentIntent = await _stripePaymentService.CreatePaymentIntent(paymentPart);

            return(Initialize <StripePaymentFormPartViewModel>("StripePaymentFormPart", m =>
            {
                m.IntentCleintSecret = paymentIntent.ClientSecret;
            })
                   .Location("Detail", "Content:5")
                   .Location("Summary", "Content:5"));
        }