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

onSuccess not accepting props

Open ayocodes opened this issue 2 years ago • 3 comments

i am getting error Types of property 'onSuccess' are incompatible. Type '(reference: any) => void' is not assignable to type 'callback'.

some code snippet onSuccess: (reference:any) => { handleSuccess(reference)}, const handleSuccess = (reference: any) =>{ axios.put( https://relayed-service.herokuapp.com/user/verifyTransaction, { UPAddress: account, reference: reference.reference, message: reference.message, } );

}

ayocodes avatar Sep 14 '22 07:09 ayocodes

a workaround is to extract the payment logic to a child component in JavaScript instead of a typescript component, as the issue is from incorrect typings from the package.

Parent Component named PaymentPage.tsx name an be called anything

import PaymentWithPaystack from './components/PaymentWithPaystack'
  interface PaymentRef {
    message: string;
    redirecturl: string;
    reference: number;
    status: string;
    trans: number;
    transaction: number;
    trxref: number;
  }
const PaymentPage= () =>{
  const paymentResponse = (ref: PaymentRef )=>{  
      console.log(ref);
    }

    return (
    <>
    //....Other components
    <PaymentWithPaystack amount={300000} paymentResponse={paymentResponse}/>
    </>
    )
}
export default PaymentPage;

Child Component named PaymentWithPaystack.js name can be called anything


import { usePaystackPayment } from 'react-paystack';

const PaymentWithPaystack = ({amount=0, paymentResponse }) => {
  const config = {
    reference: (new Date()).getTime().toString(),
    email: "[email protected]",
    amount: amount,
    publicKey: 'pk_test_.........',
  };
  
  // you can call this function anything
  const handleSuccess = (ref) => {
  // Implementation for whatever you want to do with reference and after success call.
  //  console.log(ref);
   paymentResponse(ref)
   
  };
  
  // you can call this function anything
  const onClose = () => {
  // implementation for  whatever you want to do when the Paystack dialog closed.
  console.log('closed')
  }
  
  const PaystackHookExample = () => {
    const initializePayment = usePaystackPayment(config);
    return (
      <div>
          <button onClick={() => {
              initializePayment(handleSuccess, onClose)
          }}>Paystack Hooks Implementation</button>
      </div>
    );
  };
  return (
    <div>
      
      <PaystackHookExample />
    </div>
    
  )
}

export default PaymentWithPaystack

somteacodes avatar Sep 25 '22 19:09 somteacodes

interface PaymentRef {
    message: string;
    redirecturl: string;
    reference: number;
    status: string;
    trans: number;
    transaction: number;
    trxref: number;
  }

I think reference could be either number or string reference: number | string;

dhatGuy avatar Oct 12 '22 09:10 dhatGuy

See PR #67

kduprey avatar Dec 03 '22 05:12 kduprey