PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.17.0
GiveWP – Donation Plugin and Fundraising Platform v4.17.0
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
give / src / Donations / resources / hooks / useDonationRefund.ts

useDonationRefund.ts in GiveWP – Donation Plugin and Fundraising Platform 4.17.0, at src/Donations/resources/hooks/useDonationRefund.ts

85 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import apiFetch from '@wordpress/api-fetch';
2 import { useDispatch } from '@wordpress/data';
3 import { __ } from '@wordpress/i18n';
4 import { useMemo, useState } from 'react';
5 import { Donation } from '../admin/components/types';
6 import { getDonationOptionsWindowData } from '../utils';
7
8 /**
9 * @since 4.6.0
10 */
11 const canRefundDonation = (donation: Donation) => {
12 const { gateways } = getDonationOptionsWindowData();
13
14 //find the gateway in the gateways array
15 const gateway = gateways.find((gateway) => gateway.id === donation.gatewayId && gateway.enabled);
16
17 return gateway?.supportsRefund && donation.status === 'publish';
18 };
19
20 /**
21 * @since 4.6.0
22 */
23 const isResponseDonation = (response: unknown): response is Donation => {
24 return typeof response === 'object' && response !== null && 'id' in response;
25 };
26
27
28
29 /**
30 * @since 4.6.0
31 */
32 export default function useDonationRefund(donation: Donation) {
33 const [isRefunding, setIsRefunding] = useState(false);
34 const [isRefunded, setIsRefunded] = useState(false);
35 const dispatch = useDispatch('givewp/admin-details-page-notifications');
36 const canRefund = useMemo(() => (donation ? canRefundDonation(donation) : false), [donation?.gatewayId, donation?.status]);
37 const {invalidateResolution, invalidateResolutionForStore} = useDispatch('core');
38
39 /**
40 * TODO: This is a temporary solution to invalidate the donation cache.
41 * There is most likely a better way to do this.
42 */
43 const invalidateDonationCache = () => {
44 invalidateResolution('getEntityRecords', ['givewp', 'donation']);
45 invalidateResolutionForStore();
46 };
47
48 const refund = async () => {
49 setIsRefunding(true);
50 const response = await apiFetch({path: `/givewp/v3/donations/${donation.id}/refund`, method: 'POST'});
51
52 if (isResponseDonation(response) && response.status === 'refunded') {
53 setIsRefunding(false);
54 setIsRefunded(true);
55
56 invalidateDonationCache();
57
58 dispatch.addSnackbarNotice({
59 id: 'refund-donation',
60 content: __('Refund completed successfully', 'give'),
61 });
62
63 return response;
64 } else {
65 console.error('Failed to refund donation', response);
66 setIsRefunding(false);
67 setIsRefunded(false);
68
69 dispatch.addSnackbarNotice({
70 id: 'refund-donation',
71 content: __('Failed to refund donation', 'give'),
72 });
73
74 throw new Error('Failed to refund donation');
75 }
76 };
77
78 return {
79 isRefunding,
80 refund,
81 isRefunded,
82 canRefund,
83 };
84 }
85