Ejemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync(
            string paymentId,
            [FromServices] CreateOrder createOrder,
            [FromServices] GetCart getCart,
            [FromServices] PaymentIntentService paymentIntentService,
            [FromServices] ILogger <Payment> logger)
        {
            var userId = User.GetUserId();
            var cartId = await getCart.Id(userId);

            if (cartId == 0)
            {
                logger.LogWarning($"Cart not found for {userId} with payment id {paymentId}");
                return(RedirectToPage("/Checkout/Error"));
            }

            var payment = await paymentIntentService.CaptureAsync(paymentId);

            if (payment == null)
            {
                logger.LogWarning($"Payment Intent not found {paymentId}");
                return(RedirectToPage("/Checkout/Error"));
            }

            var order = new Domain.Models.Order
            {
                StripeReference = paymentId,
                CartId          = cartId,
            };
            await createOrder.Do(order);

            return(RedirectToPage("/Checkout/Success", new { orderId = order.Id }));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnPostAsync(
            string paymentId,
            [FromServices] CreateOrder createOrder,
            [FromServices] GetCart getCart)
        {
            var order = new Domain.Models.Order
            {
                StripeReference = paymentId,
                CartId          = await getCart.Id(User.GetUserId()),
            };
            await createOrder.Do(order);

            return(RedirectToPage("/Checkout/Success", new { orderId = order.Id }));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> OnPostAsync(
            string paymentId,
            [FromServices] CreateOrder createOrder,
            [FromServices] GetCart getCart,
            [FromServices] PaymentIntentService paymentIntentService)
        {
            var payment = await paymentIntentService.GetAsync(paymentId);

            if (payment == null)
            {
                //todo do some kinda notification that this went wrong.
                return(RedirectToPage());
            }

            var order = new Domain.Models.Order
            {
                StripeReference = paymentId,
                CartId          = await getCart.Id(User.GetUserId()),
            };
            await createOrder.Do(order);

            return(RedirectToPage("/Checkout/Success", new { orderId = order.Id }));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Index(
            [FromServices] IOptionsMonitor <StripeSettings> optionsMonitor,
            [FromServices] CreateOrder createOrder,
            [FromServices] GetCart getCart)
        {
            StripeConfiguration.ApiKey = optionsMonitor.CurrentValue.SecretKey;
            var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

            try
            {
                var stripeEvent = EventUtility.ConstructEvent(json,
                                                              Request.Headers["Stripe-Signature"],
                                                              optionsMonitor.CurrentValue.SigningSecret);

                _logger.LogInformation("received stripe event {0} {1}", Environment.NewLine, json);

                if (stripeEvent.Type == Events.PaymentIntentCreated)
                {
                    var paymentMethod = stripeEvent.Data.Object as PaymentIntent;
                }
                else if (stripeEvent.Type == Events.ChargeSucceeded)
                {
                    var paymentMethod = stripeEvent.Data.Object as Charge;
                }
                else if (stripeEvent.Type == Events.PaymentMethodAttached)
                {
                    var paymentMethod = stripeEvent.Data.Object as PaymentMethod;
                }
                else if (stripeEvent.Type == Events.PaymentIntentSucceeded)
                {
                    var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                }
                else if (stripeEvent.Type == Events.CustomerCreated)
                {
                    var paymentMethod = stripeEvent.Data.Object as Customer;
                }
                else if (stripeEvent.Type == Events.CheckoutSessionCompleted)
                {
                    var checkoutSession = stripeEvent.Data.Object as Session;

                    var order = new Domain.Models.Order
                    {
                        StripeReference = checkoutSession.Id,
                        CartId          = await getCart.Id(checkoutSession.Metadata["user_id"]),
                    };
                    await createOrder.Do(order);

                    _logger.LogInformation("created order {0},{1}: {2}", order.Id, Environment.NewLine,
                                           JsonConvert.SerializeObject(order));
                }
                else
                {
                    return(BadRequest());
                }

                return(Ok());
            }
            catch (StripeException e)
            {
                return(BadRequest(e.Message));
            }
        }