public IHttpActionResult CustomerPortal()
        {
            // Authenticate your user.
            var userManager = System.Web.HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>();
            var user        = userManager.FindById(User.Identity.GetUserId());

            var options = new Stripe.BillingPortal.SessionCreateOptions
            {
                Customer  = user.StripeCustomerId,
                ReturnUrl = DashboardUrl(),
            };
            var service = new Stripe.BillingPortal.SessionService();
            var session = service.Create(options);


            return(Created(session.Url, session));
        }
        public async Task <IActionResult> CustomerPortal([FromBody] CustomerPortalRequest req)
        {
            // This ID is typically stored with the authenticated
            // user in your database. For demonstration purposes, we're
            // pulling this value from environment variables.
            var stripeCustomerId = req.CustomerId;

            // This is the URL to which your customer will return after
            // they are done managing billing in the Customer Portal.
            var returnUrl = this.options.Value.Domain;

            var options = new Stripe.BillingPortal.SessionCreateOptions
            {
                Customer  = stripeCustomerId,
                ReturnUrl = returnUrl,
            };
            var service = new Stripe.BillingPortal.SessionService(this.client);
            var session = await service.CreateAsync(options);

            return(Ok(session));
        }
示例#3
0
        public async Task <IActionResult> CustomerPortal([FromBody] CustomerPortalRequest req)
        {
            // For demonstration purposes, we're using the Checkout session to retrieve the customer ID.
            // Typically this is stored alongside the authenticated user in your database.
            var checkoutSessionId = req.SessionId;
            var checkoutService   = new SessionService(this.client);
            var checkoutSession   = await checkoutService.GetAsync(checkoutSessionId);

            // This is the URL to which your customer will return after
            // they are done managing billing in the Customer Portal.
            var returnUrl = this.options.Value.Domain;

            var options = new Stripe.BillingPortal.SessionCreateOptions
            {
                Customer  = checkoutSession.CustomerId,
                ReturnUrl = returnUrl,
            };
            var service = new Stripe.BillingPortal.SessionService(this.client);
            var session = await service.CreateAsync(options);

            return(Ok(session));
        }
        public async Task <IActionResult> CustomerPortal([FromBody] CustomerPortalRequest req)
        {
            try
            {
                ClaimsPrincipal principal  = HttpContext.User as ClaimsPrincipal;
                var             claim      = principal.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname");
                var             userFromDb = await _userManager.FindByNameAsync(claim.Value);

                if (userFromDb == null)
                {
                    return(BadRequest());
                }
                var options = new Stripe.BillingPortal.SessionCreateOptions
                {
                    Customer  = userFromDb.CustomerId,
                    ReturnUrl = req.ReturnUrl,
                };
                var service = new Stripe.BillingPortal.SessionService();
                var session = await service.CreateAsync(options);

                return(Ok(new
                {
                    url = session.Url
                }));
            }
            catch (StripeException e)
            {
                Console.WriteLine(e.StripeError.Message);
                return(BadRequest(new ErrorResponse
                {
                    ErrorMessage = new ErrorMessage
                    {
                        Message = e.StripeError.Message,
                    }
                }));
            }
        }