give
/
src
/
DonorDashboards
/
resources
/
js
/
app
/
components
/
subscription-manager
/
utils
/
index.js
give
/
src
/
DonorDashboards
/
resources
/
js
/
app
/
components
/
subscription-manager
/
utils
Last commit date
index.js
1 year ago
index.js
71 lines
| 1 | import {__} from '@wordpress/i18n'; |
| 2 | import {store} from '../../../tabs/recurring-donations/store'; |
| 3 | import {donorDashboardApi} from '../../../utils'; |
| 4 | import {fetchSubscriptionsDataFromAPI} from '../../../tabs/recurring-donations/utils'; |
| 5 | import {setError} from '../../../tabs/recurring-donations/store/actions'; |
| 6 | |
| 7 | export const updateSubscriptionWithAPI = async ({id, amount, paymentMethod}) => { |
| 8 | const {dispatch} = store; |
| 9 | |
| 10 | try { |
| 11 | const response = await donorDashboardApi.post( |
| 12 | 'recurring-donations/subscription/update', |
| 13 | { |
| 14 | id, |
| 15 | amount, |
| 16 | payment_method: paymentMethod, |
| 17 | }, |
| 18 | {} |
| 19 | ); |
| 20 | |
| 21 | await fetchSubscriptionsDataFromAPI(); |
| 22 | |
| 23 | return response; |
| 24 | } catch (error) { |
| 25 | if (error.response.status === 500) { |
| 26 | dispatch( |
| 27 | setError( |
| 28 | __( |
| 29 | 'An error occurred while processing your request. Please try again later, or contact support if the issue persists.', |
| 30 | 'give' |
| 31 | ) |
| 32 | ) |
| 33 | ); |
| 34 | } else { |
| 35 | dispatch(setError(error.response.data.message)); |
| 36 | } |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | export const managePausingSubscriptionWithAPI = async ({id, action = 'pause', intervalInMonths = null}) => { |
| 41 | const {dispatch} = store; |
| 42 | try { |
| 43 | const response = await donorDashboardApi.post( |
| 44 | 'recurring-donations/subscription/manage-pausing', |
| 45 | { |
| 46 | id, |
| 47 | action, |
| 48 | interval_in_months: intervalInMonths, |
| 49 | }, |
| 50 | {} |
| 51 | ); |
| 52 | |
| 53 | await fetchSubscriptionsDataFromAPI(); |
| 54 | |
| 55 | return response; |
| 56 | } catch (error) { |
| 57 | if (error.response.status === 500) { |
| 58 | dispatch( |
| 59 | setError( |
| 60 | __( |
| 61 | 'An error occurred while processing your request. Please try again later, or contact support if the issue persists.', |
| 62 | 'give' |
| 63 | ) |
| 64 | ) |
| 65 | ); |
| 66 | } else { |
| 67 | dispatch(setError(error.response.data.message)); |
| 68 | } |
| 69 | } |
| 70 | }; |
| 71 |