2017-10-21 115 views
0

我可以在Angular 4應用中成功加載checkout.js,呈現PayPal按鈕,出現新窗口,我確認付款,付款已處理但我不知道如何設置onAuthorize回調來處理Angular 4中的響應(例如顯示成功並將支付記錄存儲到MongoDb)。這是我的代碼。我可以看到第一個包含付款響應的console.log,但我無法訪問組件的範圍,所以在執行第一個console.log之後沒有任何操作。我在控制檯中沒有錯誤。即使console.log(「成功!」)不處理。在Angular 4上處理PayPal onAuthorize回調

onAuthorize: (data, actions) => { 
      return actions.payment.execute().then(function(payment) { 
       console.log("response = " + JSON.stringify(payment)); 
       this._alertService.success("The extension of subscription was succesfull!"); 
       this.user.contracts.push({ 
         "acquired": new Date(payment.create_time), 
         "price": payment.transactions[0].amount.total, 
         "expires": new Date(this.user.expires.getFullYear()+this.selectedAccountExtensionOption.addedYears, this.user.expires.getMonth(), this.user.expires.getDate()) 
         }); 
       console.log("successful!");  

      }); 
     }, 
+0

小心分享您的集成? –

回答

0

問題是上下文(this)在回調中不可用。最好的解決辦法是結合thisanswer to this question

另一種解決方案是解釋發出貝寶回調自定義事件(文檔here

let event = new CustomEvent('dosomething', { data: "some data" });

,然後趕在組件此事件。文檔here

@HostListener('dosomething') onDoSomething() { // do what you need }

+0

自定義事件解決了問題! –