我有一个反应应用程序,我想在其中设置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.
尝试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-script-loader-hoc'导入ScriptLoader;你可以在window.chckt上找到。