【问题标题】:Adyen's Checkout SDK integration in react appAdyen 在 React 应用中的 Checkout SDK 集成
【发布时间】:2019-02-09 11:38:27
【问题描述】:

我有一个 react 应用程序,我想在其中设置 adyen(支付处理 API)。我想使用结帐 SDK (https://docs.adyen.com/developers/checkout/web-sdk) 非常简单, 我已将 js 逻辑移至 componentDidMount,但加载 sdk 时遇到问题,

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

所以我可以使用 SDK 中的以下功能:

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

我尝试在我的 React 组件中使用 react-script-tag

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>
        );
    }

但还是报错:

Uncaught ReferenceError: chckt is not defined.

【问题讨论】:

    标签: javascript reactjs web adyen


    【解决方案1】:

    试试window.chckt.hooks.beforeComplete = ... chckt 是一个全局范围变量。

    加载外部脚本最简单的方法是使用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)
    

    【讨论】:

    • 它不再维护了!
    • 看看react-load-script,无论哪种方式,您都只能从全局范围window.chckt...访问chckt
    【解决方案2】:

    您可以尝试使用 import ScriptLoader from 'react-script-loader-hoc';你可以在 window.chckt 上找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 2021-02-24
      • 2019-07-20
      • 2018-05-08
      • 2021-03-04
      • 2012-10-10
      • 2021-02-03
      相关资源
      最近更新 更多