Skip to content

fix: support positional callbacks in initializePayment - #127

Open
DevMubarak1 wants to merge 1 commit into
iamraphson:masterfrom
DevMubarak1:fix/support-positional-callbacks
Open

DevMubarak1 wants to merge 1 commit into
iamraphson:masterfrom
DevMubarak1:fix/support-positional-callbacks

Conversation

@DevMubarak1

Copy link
Copy Markdown

Resolves #101.

Context

In use-paystack.ts, initializePayment currently destructures { config, onSuccess, onClose } directly from the first argument.

The README and earlier versions of this library show callers passing positional arguments:

initializePayment(onSuccess, onClose);

When called this way, onSuccess is passed as the first parameter (the options object), and destructuring { config, onSuccess, onClose } yields undefined for both callbacks. The payment modal opens, but neither callback ever fires. Calling initializePayment() without arguments also throws a TypeError when destructuring undefined.

What this PR does

  • Checks whether the first parameter is a callback function or an options object.
  • If a function is passed, it treats arguments positionally: (onSuccess, onClose, config).
  • If an object is passed, it continues reading { config, onSuccess, onClose }.
  • If called with no arguments (initializePayment()), it handles it cleanly without throwing.
  • Overloads the InitializePayment type in libs/types.ts so TypeScript users get accurate typing and autocomplete for both patterns.
  • Adds test cases in libs/test/use-paystack.test.tsx covering positional callbacks and zero-argument calls.

Both styles work as expected now and existing code using the object syntax won't break.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OnSuccess & OnClose Function not working

1 participant