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

Calling initialize Payment after axios response

Open deLTreeTech opened this issue 4 years ago • 6 comments

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

deLTreeTech avatar Nov 27 '21 15:11 deLTreeTech

@deLTreeTech , were you able to solve this?

Irene-24 avatar Jun 27 '22 11:06 Irene-24

Yes. I did this initially:

const initialConfig = { reference: null, email: userEmail, amount: 0, publicKey: REACT_APP_PAYSTACK_KEY, };

const [config, setConfig] = useState(initialConfig);

let initializePayment = usePaystackPayment(config);

Then when payment button is clicked, I set config state:

setConfig({ ...config, reference: response.data.data.cartReference, amount: cartTotalPrice() * 100, });

deLTreeTech avatar Jun 27 '22 16:06 deLTreeTech

Yes. I did this initially:

const initialConfig = { reference: null, email: userEmail, amount: 0, publicKey: REACT_APP_PAYSTACK_KEY, };

const [config, setConfig] = useState(initialConfig);

let initializePayment = usePaystackPayment(config);

Then when payment button is clicked, I set config state:

setConfig({ ...config, reference: response.data.data.cartReference, amount: cartTotalPrice() * 100, });

@deLTreeTech Thanks, I'll try it out

Irene-24 avatar Jun 27 '22 22:06 Irene-24

Yes. I did this initially:

const initialConfig = { reference: null, email: userEmail, amount: 0, publicKey: REACT_APP_PAYSTACK_KEY, };

const [config, setConfig] = useState(initialConfig);

let initializePayment = usePaystackPayment(config);

Then when payment button is clicked, I set config state:

setConfig({ ...config, reference: response.data.data.cartReference, amount: cartTotalPrice() * 100, });

does this actually work? Considering the asynchronous nature of state updates.

kbuika avatar Nov 20 '23 11:11 kbuika

Yes. I did this initially: const initialConfig = { reference: null, email: userEmail, amount: 0, publicKey: REACT_APP_PAYSTACK_KEY, }; const [config, setConfig] = useState(initialConfig); let initializePayment = usePaystackPayment(config); Then when payment button is clicked, I set config state: setConfig({ ...config, reference: response.data.data.cartReference, amount: cartTotalPrice() * 100, });

does this actually work? Considering the asynchronous nature of state updates.

Yes. It does

deLTreeTech avatar Nov 21 '23 12:11 deLTreeTech

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

kbuika avatar Nov 21 '23 14:11 kbuika