HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux WebLive 5.15.0-79-generic #86-Ubuntu SMP Mon Jul 10 16:07:21 UTC 2023 x86_64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/wpmuhibbah/wp-content/plugins/give/src/Donations/resources/hooks/useDonationRefund.ts
import apiFetch from '@wordpress/api-fetch';
import { useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { useMemo, useState } from 'react';
import { Donation } from '../admin/components/types';
import { getDonationOptionsWindowData } from '../utils';

/**
 * @since 4.6.0
 */
const canRefundDonation = (donation: Donation) => {
    const { gateways } = getDonationOptionsWindowData();

    //find the gateway in the gateways array
    const gateway = gateways.find((gateway) => gateway.id === donation.gatewayId && gateway.enabled);

    return gateway?.supportsRefund && donation.status === 'publish';
};

/**
 * @since 4.6.0
 */
const isResponseDonation = (response: unknown): response is Donation => {
    return typeof response === 'object' && response !== null && 'id' in response;
};



/**
 * @since 4.6.0
 */
export default function useDonationRefund(donation: Donation) {
    const [isRefunding, setIsRefunding] = useState(false);
    const [isRefunded, setIsRefunded] = useState(false);
    const dispatch = useDispatch('givewp/admin-details-page-notifications');
    const canRefund = useMemo(() => (donation ? canRefundDonation(donation) : false), [donation?.gatewayId, donation?.status]);
    const {invalidateResolution, invalidateResolutionForStore} = useDispatch('core');

    /**
     * TODO: This is a temporary solution to invalidate the donation cache.
     * There is most likely a better way to do this.
     */
    const invalidateDonationCache = () => {
        invalidateResolution('getEntityRecords', ['givewp', 'donation']);
        invalidateResolutionForStore();
    };

    const refund = async () => {
        setIsRefunding(true);
        const response = await apiFetch({path: `/givewp/v3/donations/${donation.id}/refund`, method: 'POST'});

        if (isResponseDonation(response) && response.status === 'refunded') {
            setIsRefunding(false);
            setIsRefunded(true);

            invalidateDonationCache();

            dispatch.addSnackbarNotice({
                id: 'refund-donation',
                content: __('Refund completed successfully', 'give'),
            });

            return response;
        } else {
            console.error('Failed to refund donation', response);
            setIsRefunding(false);
            setIsRefunded(false);

            dispatch.addSnackbarNotice({
                id: 'refund-donation',
                content: __('Failed to refund donation', 'give'),
            });

            throw new Error('Failed to refund donation');
        }
    };

    return {
        isRefunding,
        refund,
        isRefunded,
        canRefund,
    };
}