Processing Paypal onAuthorize callback in Angular 4

泄露秘密 提交于 2020-01-03 04:45:05

问题


I can successfully load checkout.js in Angular 4 app, render the Paypal button, the new window appears, I confirm the payment, the payment is processed but I do not know how to setup onAuthorize callback to process the response in Angular 4 (like to show the success and store a payment record to MongoDb). Here is my code. I can see the first console.log containing the payment response but I cannot access the component's scope, so nothing after this first console.log is executed. I get no error in console. Even the console.log("successful!") is not processed.

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

            });
        },

回答1:


Issue is that context (this) is not available in callback. The best solution would be to bind this as explained in answer to this question

Alternative solution is to emit custom event in PayPal callback (documentation here)

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

and then catch this event in your component. Documentation here.

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



来源:https://stackoverflow.com/questions/46861004/processing-paypal-onauthorize-callback-in-angular-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!