Skip to content

Passing payment options #78

Open
Open

Description

@peterayobami

I guess the pay stack hook can be better if we can pass in the payment options into whenever it called, as in initializePayment(config) as opposed to passing it into the hook directly when initializing it, as in const initializePayment = usePaystackPayment(config).

In practice you'll want to pass the payment options only when you're about to process the transaction because you don't know the email or amount to pay in advance.

export default function usePayStackPayment(): (options: PaystackProps, callback?: () => void, onClose?: () => void) => void {
    const [scriptLoaded, scriptError] = usePaystackScript();
    
    function initializePayment(options: PaystackProps, callback?: callback, onClose?: callback): void {

        const {
            publicKey,
            firstname,
            lastname,
            phone,
            email,
            amount,
            reference,
            metadata = {},
            currency = 'NGN',
            channels,
            label = '',
            plan = '',
            quantity = '',
            subaccount = '',
            transaction_charge = 0,
            bearer = 'account',
            split,
            split_code,
        } = options;

        if (scriptError) {
            throw new Error('Unable to load paystack inline script');
        }

        if (scriptLoaded) {
            const paystackArgs: Record<string, any> = {
                callback: callback ? callback : () => null,
                onClose: onClose ? onClose : () => null,
                key: publicKey,
                ref: reference,
                email,
                firstname,
                lastname,
                phone,
                amount,
                currency,
                plan,
                quantity,
                'data-custom-button': options['data-custom-button'] || '',
                channels,
                subaccount,
                transaction_charge,
                bearer,
                label,
                metadata,
                split,
                split_code,
            };
            callPaystackPop(paystackArgs);
        }
    }

    useEffect(() => {
        if (scriptError) {
            throw new Error('Unable to load paystack inline script');
        }
    }, [scriptError]);

    return initializePayment;
}

I hope this idea is considered.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions