I have called a function inside a function but inner function is not working

I have called a function inside a function but inner function is not working please help me out
below my code

pay() {
    console.log(this.total_ammount)
    var options = {
      description: 'Payment To View Seller Profile',
      image: 'url',
      currency: 'INR',
      key: 'rzp_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',// this key need to chage for every app
      amount: this.total_ammount,
      name: 'appname',
      prefill: {
        email: this.email,
        contact: this.mobile,
        name: this.name
      },
      theme: {
        color: '#F37254'
      },
      modal: {
        ondismiss: function() {
          alert('dismissed')
        }
      }
    };

    var successCallback = function(payment_id) {
      alert(payment_id);
**here i want to call a function**
      
      let postParams ={
        userid:'1',
        ammount:'100',
        payment_valid:'5',
    }
    alert(JSON.stringify(postParams));
 
    };
    var cancelCallback = function(error) {
      alert(error.description + ' (Error ' + error.code + ')');
    };
    RazorpayCheckout.open(options,successCallback,cancelCallback);
  }

I want to call a function inside the successCallback payment id show in alert(payment_id); box
when while i called a function this function is not working or fire
please help me out
Thanks in advace

1 Like

Didn’t completely get it. Can you a show demo code of how you are trying to call a function and why functions inside a function which is actually a callback to be called in the first place?

Thanks for your response i past my full code please check at once

pay() {

    var options = {
      description: 'Payment To View Seller Profile',
      image: 'url/assets/images/logo.png',
      currency: 'INR',
      key: 'rzp_live_xxxxxxxxxx',// this key need to chage for every app
      amount: this.total_ammount,
      name: 'onlySpare',
      prefill: {
        email: this.email,
        contact: this.mobile,
        name: this.name
      },
      theme: {
        color: '#F37254'
      },
      modal: {
        ondismiss: function() {
          alert('dismissed')
        }
      }
    };
    var successCallback = function(success) {
      alert('payment_id: ' + success.razorpay_payment_id)
      var orderId = success.razorpay_order_id
      var signature = success.razorpay_signature
     // this.paymentsave(); here i want to this function
    }
    var cancelCallback = function(error) {
      alert(error.description + ' (Error ' + error.code + ')');
    };
    RazorpayCheckout.on('payment.success', successCallback)
    RazorpayCheckout.on('payment.cancel', cancelCallback)
    RazorpayCheckout.open(options)
  }
paymentsave(){
  alert('its working');
  let paymentparams={
    'userid':this.userid,
    'pay':this.total_ammount,
    'valid':this.validate_time
  }
  this.restApi.paymentdetailssaved(paymentparams).subscribe((data:any)=>{
    console.log(JSON.stringify(data))
  },(error:any)=>{
    console.log(JSON.stringify(error))
  })
}

I want to call this function paymentsave() inside the successCallback
like this

    var successCallback = function(success) {
      alert('payment_id: ' + success.razorpay_payment_id)
      this.paymentsave()

    }

RazorpayCheckout.on should be giving some way to do that. Can you share it’s docs link?

1 Like

Sure please check this blow repo link

Also you can check this one

  • Ok, I read the docs but there seems to be no way of detecting whether the checkout went through success or the user cancelled it.

  • You could just copy paste code inside paymentsave() to successCallback, but the context of this will be lost.

  • However, can you check with whether the RazorpayCheckout uses any storage of ionic? If yes, then probably, you could just add some key as success in storage and then check that in your current file and call paymentsave() accordingly. Something like below:

var successCallback = function(success) {
      alert('payment_id: ' + success.razorpay_payment_id)
      var orderId = success.razorpay_order_id
      var signature = success.razorpay_signature
      this.storage.set('payment_success',true); // here, storage must be an instance variable of RazorpayCheckout
}

and then after RazorpayCheckout.open(options), you could something like below:

RazorpayCheckout.open(options);
// here, storage is your current class's instance variable
this.storage.get('payment_success').then((has_paid) => {
   if(has_paid !== null && has_paid === true){
       this.paymentsave();
  }
});
1 Like

Thanks for you effort i have solved just i use arrow functions like this

var successCallback = (success) =>{
      alert('payment_id: ' + success.razorpay_payment_id)
      var orderId = success.razorpay_order_id
      var signature = success.razorpay_signature
      this.storage.set('payment_success',true); // here, storage must be an instance variable of RazorpayCheckout
}

its working fine
really i appreciate for your effors
Thanks again

1 Like

You are welcome. So, does RazorpayCheckout provide storage functionality? Did you achieve this via storage?

No i just use arrow functions below is my code

pay() {

    var options = {
      description: 'Payment To View Seller Profile',
      image: 'url',
      currency: 'INR',
      key: 'rzp_live_xxxxxxxxxxxxxxxxx',// this key need to chage for every app
      amount: this.total_ammount,
      name: 'onlySpare',
      prefill: {
        email: this.email,
        contact: this.mobile,
        name: this.name
      },
      theme: {
        color: '#F37254'
      },
      modal: {
        ondismiss: function() {
          alert('dismissed')
        }
      }
    };
    var successCallback = (success)=> {
      alert('payment_id: ' + success.razorpay_payment_id)
      var orderId = success.razorpay_order_id
      var signature = success.razorpay_signature
     this.paymentsave(); 
    }
    var cancelCallback = (error)=>{
      alert(error.description + ' (Error ' + error.code + ')');
    };
    RazorpayCheckout.on('payment.success', successCallback)
    RazorpayCheckout.on('payment.cancel', cancelCallback)
    RazorpayCheckout.open(options)
  }
paymentsave(){
  alert('its working');
  let paymentparams={
    'userid':this.userid,
    'pay':this.total_ammount,
    'valid':this.payment_valid
  }
  this.restApi.paymentdetailssaved(paymentparams).subscribe((data:any)=>{
    console.log(JSON.stringify(data))
  },(error:any)=>{
    console.log(JSON.stringify(error))
  })
}

Please check at once
Thanks

3 Likes

Yes, this should work with the use of arrow functions itself. I too was concerned with the code properly referring to the type of this. Hence, I went with storage. It(arrow functions) properly binds this with it’s enclosing scope as explained in the MDN docs.

1 Like

Hi ,

does it work for you when build and run apk file into mobile ?

for me after applying arrow function it does work perfect into the browser but after build the apk file it does not work into the phone

Thank buddy, it works…thanks…||

payWithRazorpay() {

var options = {

  description: 'Credits towards consultation',

  image: 'https://i.imgur.com/3g7nmJC.png',

  currency: "INR", // your 3 letter currency code

  key: "rzp_test_1DP5mmOlF5G5ag", // your Key Id from Razorpay dashboard

  amount: 100, // Payment amount in smallest denomiation e.g. cents for USD

  name: 'Razorpay',

  prefill: {

    email: 'test@razorpay.com',

    contact: '9990009991',

    name: 'Razorpay'

  },

  theme: {

    color: '#F37254'

  },

  modal: {

    ondismiss: function () {

      alert('dismissed')

    }

  }

};

var successCallback = function (payment_id)

 {

  alert('payment_id: ' + payment_id);

  this.navCtrl.push(OrderConfirmPage) // i want call this function but its fails

       

}

@flycoders_sourav muchas gracias, funciona!!!