How to integrate PayPal express checkout into angular2 typescript project

后端 未结 4 1428
说谎
说谎 2021-02-19 08:57

Paypal provides an easy way to integrate its express checkout solution but what is the best solution to use this solution in an angular2 project working written in typescript?

4条回答
  •  既然无缘
    2021-02-19 09:49

    I've used a solution like this:

    Method to load external scripts

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

    Component Code

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

    Credit

    Andrei Odri's answer here: script tag in angular2 template / hook when template dom is loaded

提交回复
热议问题