Eloike David
Eloike David

Reputation: 175

How to call a function inside another function in Angular

I am building an online store, I am using paystack payment gateway, in the api, I am trying to run a function inside the payment gateway api but it's not working. Here is the code:

payWithPaystack(returnData, amount){
    const binded = this.service.completePayment.bind(this)
    var handler = PaystackPop.setup({
        key: '.......',
        email: this.details.email,
        amount: amount,
        currency: "NGN",
        ref: `ref-${Math.ceil(Math.random() * 10e13)}`, 
        metadata: {
            custom_fields: [{
                display_name: this.details.firstName+' '+this.details.lastName,
                variable_name: "mobile_number",
                value: this.details.phoneNumber
            }]
        },  
        callback(res){
            console.log(res)                
            binded(res);
        },
        onClose(){
            console.log('window closed');
            this.color='danger'
            this.payDisplay='Transaction closed'    
            this.payClick=false
        }
    });
    handler.openIframe();
}

in the service page here is the code

completePayment(sentData){
    console.log('enter')
    this.http.post(`${this.ApiURL}payAm.php`, sentData, {headers:this.headers})
    .subscribe(data=>console.log('ok'), error=>console.log(error))
}

Now the issue is that the function completePayment is calling partially, the console.log is running, but its not sending the http request, pls how can i solve this issue

Upvotes: 0

Views: 131

Answers (2)

georgeawg
georgeawg

Reputation: 48968

Write the payWithPaystack method so that it returns a promise:

payWithPaystack(amount){
    return new Promise( (resolve,reject) => {
        var handler = PaystackPop.setup({
            key: '.......',
            email: this.details.email,
            amount: amount,
            currency: "NGN",
            ref: `ref-${Math.ceil(Math.random() * 10e13)}`, 
            metadata: {
                custom_fields: [{
                    display_name: this.details.firstName+' '+this.details.lastName,
                    variable_name: "mobile_number",
                    value: this.details.phoneNumber
                }]
            },  
            callback(res){
                console.log(res)                
                //RESOLVE promise
                resolve(res);
            },
            onClose(){
                console.log('window closed');
                //REJECT promise
                reject('window closed');
            }
        });
        handler.openIframe();
    });
}

Then use the promise that it returns:

this.payWithPaystack(amount)
 .then( sentData => {
    console.log('enter', sentData);
    this.http.post(`${this.ApiURL}payAm.php`, sentData, {headers:this.headers})
    .subscribe(data=>console.log('ok'), error=>console.log(error))
}).catch( reason => {
    console.log("rejected: ", reason);
    this.color='danger'
    this.payDisplay='Transaction closed'    
    this.payClick=false;
});

Promises are the best way to chain asynchronous operations.

Upvotes: 1

Bálint Réthy
Bálint Réthy

Reputation: 433

Bind the syntax

...
payWithPaystack(returnData, amount){
    const binded = this.service.completePayment.bind(this);
    let handler = PaystackPop.setup({
    ....
    callback(res){
      binded(res);
    }
...

You can also try to bind the service ref.

Upvotes: 0

Related Questions