Skip to content

OnSuccess & OnClose Function not working #101

Open

Description

@multiman95
const config = {
  reference: (new Date()).getTime().toString(),
  email: "demo@gmail.com",
  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

Activity

  1. iamefe commented on May 26, 2024

    @iamefe

    Same here.

  2. kodervine commented on Jun 19, 2024

    @kodervine

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

  3. iamefe commented on Jun 19, 2024

    @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.

  4. kodervine commented on Jun 19, 2024

    @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

  5. iamefe commented on Jun 19, 2024

    @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

  6. kodervine commented on Jun 19, 2024

    @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. #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

  7. daviddivinefavour commented on Jul 30, 2024

    @daviddivinefavour

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

  8. mylesodey commented on Apr 23, 2025

    @mylesodey

    @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

  9. DevMubarak1 commented on Oct 5, 2026

    @DevMubarak1

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions