简体   繁体   English

ChargeBee Drop-in 结账 + 反应:错误

[英]ChargeBee Drop-in checkout + React: Error

I'm trying to add the drop-in checkout to a react SPA, following https://www.chargebee.com/checkout-portal-docs/drop-in-tutorial.html#implementation .我正在尝试按照https://www.chargebee.com/checkout-portal-docs/drop-in-tutorial.html#implementation将插入式结帐添加到 react SPA。 I'm dynamically adding chargebee.js after the rest of the view gets rendered, and then call registerAgain() .在视图的其余部分被渲染后,我动态添加了chargebee.js ,然后调用registerAgain()

componentDidMount() {
    const el = document.createElement('script');
    el.onload = () => {
        window.Chargebee.registerAgain();
        // this.setState({ chargebeeReady: true });
    };
    el.setAttribute('data-cb-site', 'derp-test');
    el.setAttribute('src', 'https://js.chargebee.com/v2/chargebee.js');
    document.body.appendChild(el);
}

render() {
    // [...]
    <a
        href="javascript:void(0)"
        data-cb-type="checkout"
        data-cb-plan-id="asdf-test"
    >
        Subscribe
    </a>
    // [...]
}

when clicking subscribe I get an error:单击subscribe时出现错误:

Uncaught TypeError: Cannot read property 'getCart' of null
    at t.a (event-binding.ts:24)
    at Function.value (chargebee.ts:46)
    at HTMLScriptElement.el.onload (Subscribe.js:23)

Chargebee instance will be created when DOMContentLoaded event is triggered.Since, you are loading it asynchronously, instance is not getting created. Chargebee 实例将在 DOMContentLoaded 事件被触发时创建。由于您是异步加载它,因此不会创建实例。 So you can create the instance using Chargebee.init().因此,您可以使用 Chargebee.init() 创建实例。

componentDidMount() {
    const el = document.createElement('script');
    el.onload = () => {
      window.Chargebee.init({
        "site": "derp-test"
      });
      window.Chargebee.registerAgain();
      // this.setState({ chargebeeReady: true });
    };
    el.setAttribute('src', 'https://js.chargebee.com/v2/chargebee.js');
    document.body.appendChild(el);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM