react-paystack icon indicating copy to clipboard operation
react-paystack copied to clipboard

OnSuccess & OnClose Function not working

Open multiman95 opened this issue 2 years ago • 9 comments

const config = {
  reference: (new Date()).getTime().toString(),
  email: "[email protected]",
  amount: Math.ceil(final_fees_to_pay) * 100, 
  currency: "GHS",
  channels: ["card", "bank", "mobile_money", "bank_transfer", "qr", "eft"],
  metadata: {
      "item_id": 'item-1003944'
  },
  publicKey: 'pk_test_4b43c',
};

// onSuccess, Verify Transaction in server and then redirect to Success Page
const onSuccess = (reference) => {


  // Implementation for whatever you want to do with reference and after success call.
  console.log('Transaction Success ', reference);
  alert(reference)

  router.replace('/success')
};


// Cancel transaction when user closes payment modal
const onClose = () => {

  // implementation for  whatever you want to do when the Paystack dialog closed.
  // alert('closed')
}


const PaystackHook = () => {

  const initializePayment = usePaystackPayment(config);


  return(
    <Button fullWidth color='primary' radius='full' size='lg' onClick={() => {initializePayment(onSuccess, onClose)}}> 
        <Image src='/images/default/paystack.jpg' width={30} height={30} className='rounded-full' alt='PayStack'/> Pay 
    </Button>
  )
}


return (
  <React.Fragment>


  
      <div className='mt-5'>
       <PaystackHook/>
      </div>
    


  </React.Fragment>
)
}

After payment success onSuccess does not work or throw any error and same as onClose

multiman95 avatar May 11 '24 20:05 multiman95

Same here.

iamefe avatar May 26 '24 11:05 iamefe

I am having the same issue. Have you been able to work around this @multiman95 @iamefe

kodervine avatar Jun 19 '24 05:06 kodervine

@kodervine Yes. Here's what worked for me:

  initializePayment({ onSuccess, onClose });

I wrapped both onSuccess and onClose in curly brackets before they were recognized. I think the documentation needs to be updated.

iamefe avatar Jun 19 '24 13:06 iamefe

@kodervine Yes. Here's what worked for me:

  initializePayment({ onSuccess, onClose });

I wrapped both onSuccess and onClose in curly brackets before they were recognized. I think the documentation needs to be updated.

That worked. Thank you so very much. Yes, definitely a documentation issue

kodervine avatar Jun 19 '24 13:06 kodervine

@kodervine Yes. Here's what worked for me:

  initializePayment({ onSuccess, onClose });

I wrapped both onSuccess and onClose in curly brackets before they were recognized. I think the documentation needs to be updated.

That worked. Thank you so very much. Yes, definitely a documentation issue

You are welcome. I just made a pull request for the edit. https://github.com/iamraphson/react-paystack/pull/106

iamefe avatar Jun 19 '24 15:06 iamefe

@kodervine Yes. Here's what worked for me:

  initializePayment({ onSuccess, onClose });

I wrapped both onSuccess and onClose in curly brackets before they were recognized. I think the documentation needs to be updated.

That worked. Thank you so very much. Yes, definitely a documentation issue

You are welcome. I just made a pull request for the edit. #106

Yeah hopefully. I know the older integration I'd had with paystack without the curly braces still work. But let's see what they resolve it

kodervine avatar Jun 19 '24 16:06 kodervine

Well.. Thanks guys, was facing the same difficulty for over 40mins now

daviddivinefavour avatar Jul 30 '24 08:07 daviddivinefavour

@kodervine Yes. Here's what worked for me:

  initializePayment({ onSuccess, onClose });

I wrapped both onSuccess and onClose in curly brackets before they were recognized. I think the documentation needs to be updated.

You are a life saver, ive been stuck on this for days. Thank you

mylesodey avatar Apr 23 '25 11:04 mylesodey

Submitted a fix in #127 that supports both positional arguments initializePayment(onSuccess, onClose) and the object signature initializePayment({ config, onSuccess, onClose }), keeping backwards compatibility while preventing zero-argument crashes.

DevMubarak1 avatar Oct 05 '26 09:10 DevMubarak1