%PDF- %PDF-
Direktori : /home/emergentqj/actioncivile/pre-auth/.4cc3a2e1590a4463a5f392ddcc26929e/static/js/sagas/ |
Current File : /home/emergentqj/actioncivile/pre-auth/.4cc3a2e1590a4463a5f392ddcc26929e/static/js/sagas/getBusinessPaymentMethods.js |
/* eslint-disable max-len */ import { put, call, takeEvery } from "redux-saga/effects"; import configFunc from "config/config"; import LIST_ACTIONS from "../constants/action_types"; import { getDataAction, postDataAction } from "../common/actions"; const config = configFunc(); function* runGetBusinessPaymentMethods(dispatch, action) { const getBusinessPaymentMethodsUrl = `${config.b2bApiUrl}billinggroup/paymentmethods?registration=true&companyid=${action.customerId}`; try { const payload = yield call( getDataAction(getBusinessPaymentMethodsUrl, null, true), ); yield put({ type: LIST_ACTIONS.GET_BUSINESS_PAYMENT_METHODS_SUCCESS, payload, }); } catch (e) { yield put({ type: LIST_ACTIONS.GET_BUSINESS_PAYMENT_METHODS_ERROR, e, }); } } export function* getBusinessPaymentMethods(dispatch) { yield takeEvery( LIST_ACTIONS.GET_BUSINESS_PAYMENT_METHODS_REQUEST, runGetBusinessPaymentMethods, dispatch, ); } // Special case with Sepa: there's no need to call the BE endpoint // because no information (links or API endpoints) is needed from the BE to display Sepa form fields // However, since displaying any payment method is dependent on 2 global state fields, // the following action is artificially dispatched for Sepa function* runSetBusinessPaymentMethod(dispatch, action) { if (action.data.paymentMethod?.paymentMethod === "SEPA") { yield put({ type: LIST_ACTIONS.SET_BUSINESS_PAYMENT_METHOD_SUCCESS, payload: "Sepa", }); return; } // Same thing with Afterpay if (action.data.paymentMethod?.paymentMethod === "AFTER_PAY_DIRECT_DEBIT") { yield put({ type: LIST_ACTIONS.SET_BUSINESS_PAYMENT_METHOD_SUCCESS, payload: "Afterpay direct debit", }); return; } let setBusinessPaymentMethodUrl = `${config.b2bApiUrl}billinggroup/${action.data.billingAccountId}/registerPaymentDevice?paymentMethodId=${action.data.paymentMethodId}&companyid=${action.data.customerId}®istration=true&returnUrl=${window.location.href}&language=${action.data.language}`; if (action.data?.paymentMethod.paymentMethod === "ADYEN") { setBusinessPaymentMethodUrl = `${config.b2bApiUrl}billinggroup/${action.data.billingAccountId}/registerAdyenPaymentDevice?paymentMethodId=${action.data.paymentMethodId}&companyid=${action.data.customerId}®istration=true&language=${action.data.language}`; } if (action.data?.paymentMethod.paymentMethod === "PAYPAL") { const returnUrl = action.data.isMigration ? `${window.location.href.split("?")[0]}/success` : `${window.location.href.split("?")[0]}/final?params=${action.data?.paymentMethod.paymentMethod}:${action.data?.paymentMethod.name.replace(/ /g, "_")}`; setBusinessPaymentMethodUrl = `${config.b2bApiUrl}billinggroup/${action.data.billingAccountId}/registerPaymentDevice?paymentMethodId=${action.data.paymentMethodId}&companyid=${action.data.customerId}®istration=true&returnUrl=${returnUrl}&language=${action.data.language}`; } try { const payload = yield call( postDataAction(setBusinessPaymentMethodUrl, null, true), ); if ( /^([a-z|A-Z])+?:\/\/([^\/]+[.])?(easyparksystem[.]net)?(\/.*)?$/.test( payload.headers.location, ) === true || /^([a-z|A-Z])+?:\/\/([^\/]+[.])?(epinternal[.]net)?(\/.*)?$/.test( payload.headers.location, ) === true || /^([a-z|A-Z])+?:\/\/([^\/]+[.])?(paypal[.]com)?(\/.*)?$/.test( payload.headers.location, ) === true ) { yield put({ type: LIST_ACTIONS.SET_BUSINESS_PAYMENT_METHOD_SUCCESS, payload: payload.headers.location, }); } else { yield put({ type: LIST_ACTIONS.SET_BUSINESS_PAYMENT_METHOD_ERROR, function() { return "Url is not whitelisted"; }, }); } } catch (e) { yield put({ type: LIST_ACTIONS.SET_BUSINESS_PAYMENT_METHOD_ERROR, e, }); } } export function* setBusinessPaymentMethod(dispatch) { yield takeEvery( LIST_ACTIONS.SET_BUSINESS_PAYMENT_METHOD_REQUEST, runSetBusinessPaymentMethod, dispatch, ); }