stripe-js: [BUG]: Issue: Stripe Integration Errors in Magento 1.9

What happened?

I am currently working with Magento 1.9 and encountered issues related to Stripe integration after recent changes. Here are the specific errors I am facing:

  1. Error in prototype.js: prototype.js:828 Uncaught TypeError: Cannot read properties of undefined (reading ‘length’) at v3/:1:154936 at prototype.js:865:29 at prototype.js:825:18 at Array.forEach (<anonymous>) at Array.each (prototype.js:824:12) at Array.collect (prototype.js:864:10) at 3534 (v3/:1:154903) at e (v3/:1:119) at 4200 (v3/:1:157974) at e (v3/:1:119)

  2. Error in stripe_payments.js: stripe_payments.js:136 Uncaught ReferenceError: Stripe is not defined at Object.onLoadStripeJsV3 (stripe_payments.js:136:13) at stripeJsV3.onload (stripe_payments.js:109:20)

Steps to Reproduce

  1. Integrate Stripe with Magento 1.9.
  2. Load the checkout page to trigger Stripe’s JavaScript.

Expected Behavior

Stripe integration should work without any JavaScript errors, allowing smooth payment processing.

Actual Behavior

The errors mentioned above are triggered, causing the Stripe payment processing to fail.

Additional Information

I believe these issues are due to the recent changes made by Stripe. Please provide guidance or a solution to resolve these errors.

Thank you.

Environment

  • Magento Version: 1.9
  • Stripe Integration: Latest version

Environment

No response

Reproduction

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

I have temporarily fixed the issue by defining the Stripe file before prototype.js in the head section:

Example:

<head>
    <!-- Stripe.js -->
    <script src="https://js.stripe.com/v3/"></script>
    <!-- Prototype.js -->
    <script src="path/to/prototype.js"></script>
</head>

If anyone needs further assistance or has any questions, feel free to reach out to me directly on WhatsApp: Click here to message me …

Hello @all

I have temporarily fixed the issue by defining the Stripe file before prototype.js in the head section:

Example:

<head> <script src="https://js.stripe.com/v3/"></script> <script src="path/to/prototype.js"></script> </head>

Above solution will not work if you have custom module for js merge so I have fixed it as well.

If anyone want solution mail me on : mageexpert92@gmail.com

The provided solution only fixes the first of the two errors and loads the script even on non-checkout pages. To fix the source of the problem and keep Stripe script loading asynchronous, you need to save the native Array.map before it gets overwritten by underscore library that is used by Magento 1 and then reassign it before loading the Stripe v3 script. The script can then initialize with the native map. When the script is loaded, you can revert the reassignment.

Change your header.phtml:

...
<script>const __ArrayMapNative = Array.prototype.map;</script> // <-- added
<?php echo $this->getCssJsHtml(); ?>
...

Change your stripe_payments.js:

...
loadStripeJsV3: function(callback)
    {
        var script = document.getElementsByTagName('script')[0];
        var stripeJsV3 = document.createElement('script');
        var __ArrayMapMagento = Array.prototype.map; // <-- added
        Array.prototype.map = __ArrayMapNative || __ArrayMapMagento // <-- added
        stripeJsV3.src = "https://js.stripe.com/v3/";
        stripeJsV3.onload = function()
        {
            Array.prototype.map = __ArrayMapMagento; // <-- added
            stripe.onLoadStripeJsV3();
            if (typeof callback === 'function') {
                callback();
            }
        };
        stripeJsV3.onerror = function(evt) {
            console.warn("Stripe.js v3 could not be loaded");
            console.error(evt);
        };
        // Do this on the next cycle so that stripe.onLoadStripeJsV2() finishes first
        script.parentNode.insertBefore(stripeJsV3, script);
    },
...