在Angular 4上处理PayPal onAuthorize回调
问题描述:
我可以在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!");
});
},
答
问题是上下文(this
)在回调中不可用。最好的解决办法是结合this
在answer to this question
另一种解决方案是解释发出贝宝回调自定义事件(文档here)
let event = new CustomEvent('dosomething', { data: "some data" });
,然后赶在组件此事件。文档here。
@HostListener('dosomething') onDoSomething() { // do what you need }
+0
自定义事件解决了问题! –
小心分享您的集成? –