Search code examples
javascriptreactjswebadyen

Adyen's Checkout SDK integration in react app


I have a react app and I want to set up adyen (payment processing API) in that. I want to use the checkout SDK (https://docs.adyen.com/developers/checkout/web-sdk )as its very simple, I have moved the js logic to componentDidMount, but having problem loading the sdk,

<script type="text/javascript" src="https://checkoutshopper-test.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.1.6.3.min.js"></script>

So I can use the below function from SDK:

chckt.hooks.beforeComplete = function(node, paymentData) {
   return false; // Indicates that you want to replace the default handling.
};

I have tried using react-script-tag, in my React component:

render() {
        return (
            <div className='checkout-warpper'>
               <ScriptTag type="text/javascript" src="https://checkoutshopper-test.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.1.9.2.min.js" />

                <div className="checkout" id="adyen-checkout">
                    {/* The checkout interface will be rendered here */}
                </div>
            </div>
        );
    }

but still get the error:

Uncaught ReferenceError: chckt is not defined.

Solution

  • Try window.chckt.hooks.beforeComplete = ... chckt is a global scope variable.

    The easiest way to load external script is by using react-async-script-loader

    import React from 'react';
    import scriptLoader from 'react-async-script-loader'
    
    class CheckoutSDK extends React.Component {
    
        componentWillReceiveProps({ isScriptLoaded, isScriptLoadSucceed }) {
            if (isScriptLoaded && !this.props.isScriptLoaded) { // load finished
                if (isScriptLoadSucceed) {
    
                    window.chckt.hooks.beforeComplete = function(node, paymentData) {
                        return false; // Indicates that you want to replace the default handling.
                    };
    
                }
            }
        }
    
        render() {
            return null
        }
    
    }
    
    export default scriptLoader('https://checkoutshopper-test.adyen.com/checkoutshopper/assets/js/sdk/checkoutSDK.1.6.3.min.js',)(CheckoutSDK)