Пример #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 }));
        }
        public override async Task <ApiResult> CapturePaymentAsync(PaymentProviderContext <StripeCheckoutSettings> ctx)
        {
            // NOTE: Subscriptions aren't currently abled to be "authorized" so the capture
            // routine shouldn't be relevant for subscription payments at this point

            try
            {
                // We can only capture a payment intent, so make sure we have one
                // otherwise there is nothing we can do
                var paymentIntentId = ctx.Order.Properties["stripePaymentIntentId"];
                if (string.IsNullOrWhiteSpace(paymentIntentId))
                {
                    return(null);
                }

                var secretKey = ctx.Settings.TestMode ? ctx.Settings.TestSecretKey : ctx.Settings.LiveSecretKey;

                ConfigureStripe(secretKey);

                var paymentIntentService = new PaymentIntentService();
                var paymentIntentOptions = new PaymentIntentCaptureOptions
                {
                    AmountToCapture = AmountToMinorUnits(ctx.Order.TransactionInfo.AmountAuthorized.Value)
                };
                var paymentIntent = await paymentIntentService.CaptureAsync(paymentIntentId, paymentIntentOptions);

                return(new ApiResult()
                {
                    TransactionInfo = new TransactionInfoUpdate()
                    {
                        TransactionId = GetTransactionId(paymentIntent),
                        PaymentStatus = GetPaymentStatus(paymentIntent)
                    },
                    MetaData = new Dictionary <string, string>
                    {
                        { "stripeChargeId", GetTransactionId(paymentIntent) },
                        { "stripeCardCountry", paymentIntent.Charges?.Data?.FirstOrDefault()?.PaymentMethodDetails?.Card?.Country }
                    }
                });
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Stripe - CapturePayment");
            }

            return(ApiResult.Empty);
        }
Пример #3
0
        public async Task <bool> CapturePaymentIntentAsync(string paymentIntentId, MailAddress receiptEmail)
        {
            var paymentService = new PaymentIntentService();

            //add the receipt email
            var emailRes = await AddIntentReceiptEmail(paymentIntentId, receiptEmail);

            var options = new PaymentIntentCaptureOptions()
            {
                StatementDescriptorSuffix = "NowLeave.com"
            };

            try
            {
                var res = await paymentService.CaptureAsync(paymentIntentId, options, GetRequestOptions());

                return(res.Status.Equals("succeeded", StringComparison.InvariantCultureIgnoreCase));
            }
            catch (Exception e)
            {
                return(false);
            }
        }