Skip to content
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

Open
deLTreeTech opened this issue Nov 27, 2021 · 6 comments
Open

Calling initialize Payment after axios response #49

deLTreeTech opened this issue Nov 27, 2021 · 6 comments

Comments

@deLTreeTech
Copy link

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

@Irene-24
Copy link

@deLTreeTech , were you able to solve this?

@deLTreeTech
Copy link
Author

deLTreeTech commented Jun 27, 2022

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, });

@Irene-24
Copy link

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

@kbuika
Copy link

kbuika commented Nov 20, 2023

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.

@deLTreeTech
Copy link
Author

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

@kbuika
Copy link

kbuika commented Nov 21, 2023

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

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

No branches or pull requests

3 participants