如何将PayPal express checkout集成到angular2 typescript项目中

loi*_*icb 10 paypal typescript angular

Paypal提供了一种简单的方法来集成其快速结账解决方案,但在使用打字稿编写的angular2项目中使用此解决方案的最佳解决方案是什么?

Rem*_*tec 7

我使用过这样的解决方案:

加载外部脚本的方法

  private loadExternalScript(scriptUrl: string) {
    return new Promise((resolve, reject) => {
      const scriptElement = document.createElement('script')
      scriptElement.src = scriptUrl
      scriptElement.onload = resolve
      document.body.appendChild(scriptElement)
  })
Run Code Online (Sandbox Code Playgroud)

组件代码

  ngAfterViewInit(): void {
    this.loadExternalScript("https://www.paypalobjects.com/api/checkout.js").then(() => {
      paypal.Button.render({
        env: 'sandbox',
        client: {
          production: '',
          sandbox: ''
        },
        commit: true,
        payment: function (data, actions) {
          return actions.payment.create({
            payment: {
              transactions: [
                {
                  amount: { total: '1.00', currency: 'USD' }
                }
              ]
            }
          })
        },
        onAuthorize: function(data, actions) {
          return actions.payment.execute().then(function(payment) {
            // TODO
          })
        }
      }, '#paypal-button');
    });
  }
Run Code Online (Sandbox Code Playgroud)

信用

Andrei Odri的答案在这里:加载模板dom时angular2模板/钩子中的脚本标记