-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling initialize Payment after axios response #49
Comments
@deLTreeTech , were you able to solve this? |
Yes. I did this initially:
Then when payment button is clicked, I set config state:
|
@deLTreeTech Thanks, I'll try it out |
does this actually work? Considering the asynchronous nature of state updates. |
Yes. It does |
@deLTreeTech Okay, nice. That did not work for me. I had to create a custom hook and return a method that would allow me to update the config and trigger a payment. I haven't had any side-effect with it import { useEffect, useState } from 'react';
import { usePaystackPayment } from 'react-paystack';
import { errorToast } from '@/lib/utils';
import { PaystackProps } from "react-paystack/dist/types";
import { callback } from '@/types';
interface CustomPaymentProps {
config: PaystackProps,
onSuccess: callback,
onClose: () => void
}
// TODO: Post-mortem this solution
const useCustomPaystackPayment = ({config, onSuccess, onClose}: CustomPaymentProps) => {
const [paymentReference, setPaymentReference] = useState(config.reference);
const initializePayment = usePaystackPayment({
...config,
reference: paymentReference,
});
useEffect(() => {
if (paymentReference) {
initializePayment(onSuccess, onClose);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [paymentReference]);
const handlePayment = (newReference: string) => {
if (newReference) {
setPaymentReference(newReference);
} else {
errorToast('Invalid payment reference.');
}
};
return {
handlePayment,
};
};
export default useCustomPaystackPayment; |
I have a need to first get reference from axios call, set the reference in config then make call to make payment on axios response. Is there anyway I can use this library to achieve this. Thanks
The text was updated successfully, but these errors were encountered: