在Angular 4中处理Paypal onAuthorize回调



我可以在Angular 4应用程序中成功加载checkout.js,呈现Paypal按钮,出现新窗口,我确认付款,付款已处理,但我不知道如何设置onAuthorize回调来处理Angular 4中的响应(比如显示成功并将付款记录存储到MongoDb)。这是我的密码。我可以看到包含付款响应的第一个console.log,但我无法访问组件的作用域,因此在第一个console.log之后什么都不执行。控制台中没有错误。甚至console.log("successful!")也不会被处理。

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!");     
});
},

问题是上下文(this)在回调中不可用。最好的解决方案是结合this,正如回答这个问题时所解释的那样

另一种解决方案是在PayPal回调中发出自定义事件(此处为文档)

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

然后在组件中捕获此事件。此处提供文档。

@HostListener('dosomething') 
onDoSomething(data) {
console.log(data)
// your rest of the implementation
}

最新更新