コード例 #1
0
        public async Task <bool> ReleasePaymentIntentAsync(string paymentIntentId)
        {
            var paymentService = new PaymentIntentService();
            var options        = new PaymentIntentCancelOptions()
            {
                CancellationReason = "abandoned"
            };
            var res = await paymentService.CancelAsync(paymentIntentId, options, GetRequestOptions());

            return(res.Status.Equals("canceled"));
        }
        public override async Task <ApiResult> CancelPaymentAsync(PaymentProviderContext <StripeCheckoutSettings> ctx)
        {
            // NOTE: Subscriptions aren't currently abled to be "authorized" so the cancel
            // routine shouldn't be relevant for subscription payments at this point

            try
            {
                // See if there is a payment intent to cancel
                var stripePaymentIntentId = ctx.Order.Properties["stripePaymentIntentId"];
                if (!string.IsNullOrWhiteSpace(stripePaymentIntentId))
                {
                    var secretKey = ctx.Settings.TestMode ? ctx.Settings.TestSecretKey : ctx.Settings.LiveSecretKey;

                    ConfigureStripe(secretKey);

                    var paymentIntentService = new PaymentIntentService();
                    var intent = await paymentIntentService.CancelAsync(stripePaymentIntentId);

                    return(new ApiResult()
                    {
                        TransactionInfo = new TransactionInfoUpdate()
                        {
                            TransactionId = GetTransactionId(intent),
                            PaymentStatus = GetPaymentStatus(intent)
                        }
                    });
                }

                // If there is a charge, then it's too late to cancel
                // so we attempt to refund it instead
                var chargeId = ctx.Order.Properties["stripeChargeId"];
                if (chargeId != null)
                {
                    return(await RefundPaymentAsync(ctx));
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Stripe - CancelPayment");
            }

            return(ApiResult.Empty);
        }