Faran Khan
Faran Khan

Reputation: 1565

Stripe Payment element show saved card

I am using laravel with stripe payment element. I am trying to show the saved cards for the customers that we already have. I have followed the stripe docs and found how I can show it on checkout. But the problem is that I am not getting the saved cards for the customer. And instead I am facing an error on my console as:

enter image description here

When authenticating with an ephemeral key, you must set the Stripe-Version header to an explicit API version, such as 2020-08-27

I have checked and changed lot of versions from here:

$ephemeralKey = \Stripe\EphemeralKey::create(
                    ['customer' => "$user->stripe_customer_id"],
                    ['stripe_version' => '2019-11-05']
                );

I changed the version to different version that I can see on my stripe dashboard:

enter image description here

This is my Js Initialize function:

    // Fetches a payment intent and captures the client secret
async function initialize() {
    // Customize the appearance of Elements using the Appearance API.
    const appearance = { /* ... */ };

    // Enable the skeleton loader UI for the optimal loading experience.
    const loader = 'auto';
    const { clientSecret, customerOptions } = await fetch("{{ route("user-create-stripe-element-payment") }}", {
        method: "POST",
        headers: {
            "Content-Type" : "application/json",
            "accept" : "application/json",
            'X-CSRF-TOKEN': "{{ csrf_token() }}",
            'stripe_version':"2019-11-05"
        },
        body: JSON.stringify({ totalCharge:total }),
    }).then((r) => r.json());

    elements = stripe.elements({
        clientSecret,
        appearance,
        loader,
        customerOptions
    });

    const paymentElement = elements.create("payment");
    paymentElement.mount("#payment-element");
}

And I am also using the betas which is given in the documentation:

    const stripe = Stripe("{{env('STRIPE_KEY')}}", {
    betas: ['elements_customers_beta_1'],
});

But this error is not going away. And its not even populating the Payment element.

Please help me debug this or if someone has any suggestion to check what is going on here.

Thanks in advance.

Upvotes: 3

Views: 1323

Answers (1)

Suleman Waheed
Suleman Waheed

Reputation: 36

You are not providing an API version in your JS here

const stripe = Stripe("{{env('STRIPE_KEY')}}", {
    betas: ['elements_customers_beta_1'],
});

change the above code to

const stripe = Stripe("{{env('STRIPE_KEY')}}", {
    betas: ['elements_customers_beta_1'],
    apiVersion: 'Your Version Here'
});

In your case, it should be something like this

const stripe = Stripe("{{env('STRIPE_KEY')}}", {
    betas: ['elements_customers_beta_1'],
    apiVersion: '2019-11-05'
});

You can read more here. https://stripe.com/docs/api/versioning?lang=node It is for nodejs but the API version override will work in the same way.

Upvotes: 2

Related Questions