redoc01
redoc01

Reputation: 2297

Stripe - Payment goes through but get ERROR HERE "No such payment_intent: pm_1Gyj0uD3zt5RrIg0lSfDPKOO"

I have this in my javascript:

<script>

var stripe = Stripe('pk_test_51Gv0ngD3zt5RrIg0XQiKHaK7TOAqzju9yps8jJB2Gch6ksmG4FSnqgyWLv3Qld2EkNHgAb30PLKduYGBuYtZe71A0066dp27DB');
var elements = stripe.elements();
// Custom styling can be passed to options when creating an Element.
var style = {
    base: {
        // Add your base input styles here. For example:
        fontSize: '16px',
        color: '#32325d',
    },
};

// Create an instance of the card Element.
var card = elements.create('card', {
    hidePostalCode: true, style: style });

// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');

var form = document.getElementById('payment-form');

form.addEventListener('submit', function (event) {
    // We don't want to let default form submission happen here,
    // which would refresh the page.
    event.preventDefault();

    stripe.createPaymentMethod({
        type: 'card',
        card: card,
        billing_details: {
            // Include any additional collected billing details.
            name: 'Jenny Rosen',
        },
    }).then(stripePaymentMethodHandler);
});

function stripePaymentMethodHandler(result) {
    if (result.error) {
        // Show error in payment form
    } else {
        $.ajax({
            headers: { 'Content-Type': 'application/json' },
            method: 'POST',
            url: "/PayStripe",
            data: JSON.stringify({
                payment_method_id: result.paymentMethod.id,
            }),
            success: function (json) {
                handleServerResponse(json);
            }
        });
    }
}

function handleServerResponse(response) {
    if (response.error) {
        // Show error from server on payment form
    } else if (response.requires_action) {
        // Use Stripe.js to handle required card action
        stripe.handleCardAction(
            response.payment_intent_client_secret
        ).then(handleStripeJsResult);
    } else {
        // Show success message
    }
}

function handleStripeJsResult(result) {
    if (result.error) {
        // Show error in payment form
    } else {
        // The card action has been handled
        // The PaymentIntent can be confirmed again on the server
        fetch('/pay', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ payment_intent_id: result.paymentIntent.id })
        }).then(function (confirmResult) {
            return confirmResult.json();
        }).then(handleServerResponse);
    }
}


</script>

This is my HomeController:

 public ActionResult PayStripe(string payment_method_id)
    {

        StripeConfiguration.ApiKey = "sk_test_51Gv0ngD3zt5RrIg0KmTYo92QYmujb9Gp3dv8zz7fOJYjbLna3gRPOkHzZMSVMISHNgmPSrSncUtKL2DS86R4DEJI00mVv9GusU";
        var paymentIntentService = new PaymentIntentService();
        PaymentIntent paymentIntent = null;

        try
        {
            if (payment_method_id != "") { 
                // Create the PaymentIntent
                var createOptions = new PaymentIntentCreateOptions
                {
                    PaymentMethod = payment_method_id,
                    Amount = 1099,
                    Currency = "gbp",
                    ConfirmationMethod = "manual",
                    Confirm = true,
                };
                paymentIntent = paymentIntentService.Create(createOptions);
            }
            if (payment_method_id != "")
            {
                var confirmOptions = new PaymentIntentConfirmOptions { };
                paymentIntent = paymentIntentService.Confirm(
                    payment_method_id,
                    confirmOptions
                ); <-- ERROR HERE "No such payment_intent: pm_1Gyj0uD3zt5RrIg0lSfDPKOO"
            }
        }
        catch (StripeException e)
        {
            return Json(new { error = e.StripeError.Message });
        }
        return generatePaymentResponse(paymentIntent);
        

    }

ERROR HERE "No such payment_intent: pm_1Gyj0uD3zt5RrIg0lSfDPKOO"

Can any body see what i am missing here?

I created a Connected account and still get the same error.

Upvotes: 0

Views: 887

Answers (1)

koopajah
koopajah

Reputation: 25552

Your code is calling the PaymentIntent Confirm API but you're passing a PaymentMethod id (pm_123) as the first argument instead of the PaymentIntent id pi_123 which is why you're getting that error. Instead, you need to make sure you pass the PaymentMethod id inside confirmOptions and the PaymentIntent id as the first argument.

Relatedly, your code is creating a PaymentIntent but also passing Confirm = true which means you are already confirming it. And right after you are trying to re-confirm it which does't really make sense. You should pass the PaymentMethod id when you are confirming it.

If you want to create and confirm a PaymentIntent in one call you would do this instead:

var options = new PaymentIntentCreateOptions
{
    PaymentMethod = payment_method_id,
    Amount = 1099,
    Currency = "gbp",
    ConfirmationMethod = "manual",
    PaymentMethod = payment_method_id,
    Confirm = true,
};
var paymentIntent = paymentIntentService.Create(options);

Upvotes: 2

Related Questions