0

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

4
  • Please read Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. Commented Nov 21, 2019 at 12:31
  • what's the issue your facing? Commented Nov 21, 2019 at 12:34
  • Could you provide some logs? How do you decide that it does not work? Maybe you've got some errors or...? Commented Nov 21, 2019 at 12:34
  • the issue is that the this.completePayment(res) is not running Commented Nov 21, 2019 at 12:35

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

it says bind does not exist on type 'void'
Okay, my bad I edideted the answer.
its not fully working, here is the function in service 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)) }
Have you tried bind the service ref as well? const binded = this.service.completePayment.bind(this.service);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.