【问题标题】:Calling External Functions Once PayPal Approve / Finish the Transaction [duplicate]PayPal批准/完成交易后调用外部函数[重复]
【发布时间】:2020-04-22 18:40:54
【问题描述】:

我正在为我的结帐流程集成 PayPal 智能按钮 下面是呈现按钮的代码

     LoadPaypalButton(orderid :string , link : string,token : string , applicationbaseurl:string)
  {
    this.addPaypalScript().then(()=>{paypal.Buttons({enableStandardCardFields: true,
      style: {
        shape: 'rect',
        color: 'blue',
        layout: 'vertical',
        label: 'paypal',
    },
      createOrder: function() {
        return orderid
       },
       onApprove : function(data , actions) {
        return fetch(link, {
          method: 'post',
          headers: {
            'Authorization': token,
            'content-type': 'application/json'
          },
          body: JSON.stringify({
            OrderID: data.orderID
          })
        }).then(function(res) {
          return res.json();
        }).then(function(details) {
          console.log( details);
          console.log(details);
          actions.redirect(applicationbaseurl+'OrderHistory/'+token);

        })
       }
     }
    ).render('#paypal-button-container')});


  }

一切正常,我想在事务完成后调用外部函数来处理我的 UI,而不将用户带到任何其他页面。如何在脚本中调用如下函数

callsuccess()
 {
   // Some Other work....
   console.log("Something ..!");

 }

我使用 Angular 8.0 作为我的前端。这是我迄今为止在 OnApprove 中尝试过的:

   onApprove : function(data , actions) {
    return fetch(link, {
      method: 'post',
      headers: {
        'Authorization': token,
        'content-type': 'application/json'
      },
      body: JSON.stringify({
        OrderID: data.orderID
      })
    }).then(function(res) {
      return res.json();
    }).then(function(details) {

      this.callsuccess(); //  This does not work
      actions.redirect(applicationbaseurl+'OrderHistory/'+token);

    })
   }

给我下面的错误

zone-evergreen.js:172 Uncaught TypeError: Cannot read property 'callsuccess' of undefined
at http://localhost:4200/main.js:1430:30
at ZoneDelegate.invoke (http://localhost:4200/polyfills.js:3365:26)
at Zone.run (http://localhost:4200/polyfills.js:3130:43)
at http://localhost:4200/polyfills.js:3861:36
at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:3397:31)
at Zone.runTask (http://localhost:4200/polyfills.js:3174:47)
at drainMicroTaskQueue (http://localhost:4200/polyfills.js:3565:35)

有没有人有任何建议来实现这一点..?

【问题讨论】:

  • 将所有函数改为粗箭头:function (arg) { 改为 (arg) => {。看看有没有帮助。

标签: node.js angular paypal


【解决方案1】:

尝试使用箭头函数:

onApprove : (data , actions) => {
    return fetch(link, {
      method: 'post',
      headers: {
        'Authorization': token,
        'content-type': 'application/json'
      },
      body: JSON.stringify({
        OrderID: data.orderID
      })
    }).then((res) => {
      return res.json();
    }).then((details) => {

      this.callsuccess(details);


   actions.redirect(applicationbaseurl+'OrderHistory/'+token);

    })
   }

注意function(data , actions) { 是如何变成(data, actions) => { 的,promise 的then 函数也是如此。

这应该保留this,因为箭头函数具有this 关键字的词法范围。该函数也称为lambda functions

【讨论】:

  • 不,它没有工作..!谢谢 。我在 stackoverflow stackoverflow.com/questions/57090296/… 上找到了这个,你知道如何将它翻译成角度。我从来没有使用过窗口对象
  • 对不起,我忘了改then函数,它们应该是箭头函数。我已经更新了答案。它现在应该可以工作了。
  • 还是一样,它给了我下面的错误
  • 实际上只是意识到我没有从功能(数据,动作)部分中删除功能。在做了几项研究后 2 天后发现。你的答案是正确的答案,我只是因为错误而浪费了几天
  • 谢谢@jeprubio,它对我有用,你救了我的 1 头头痛。
猜你喜欢
  • 2021-01-14
  • 2020-10-01
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 2012-08-03
  • 2020-08-06
  • 1970-01-01
  • 2014-10-05
相关资源
最近更新 更多