lab.nexedi.com will be down from Thursday, 20 March 2025, 07:30:00 UTC for a duration of approximately 2 hours

actions.js 7.15 KB
Newer Older
1
import Api from 'ee/api';
2
import { PAYMENT_FORM_ID } from 'ee/subscriptions/constants';
3
import { GENERAL_ERROR_MESSAGE } from 'ee/vue_shared/purchase_flow/constants';
4
import activateNextStepMutation from 'ee/vue_shared/purchase_flow/graphql/mutations/activate_next_step.mutation.graphql';
5
import createFlash from '~/flash';
6
import { redirectTo } from '~/lib/utils/url_utility';
7
import { sprintf, s__ } from '~/locale';
8
import { trackCheckout, trackTransaction } from '~/google_tag_manager';
9
import defaultClient from '../graphql';
10
import * as types from './mutation_types';
11

12
export const updateSelectedPlan = ({ commit, getters }, selectedPlan) => {
13
  commit(types.UPDATE_SELECTED_PLAN, selectedPlan);
14
  trackCheckout(selectedPlan, getters.confirmOrderParams?.subscription?.quantity);
15 16
};

17
export const updateSelectedGroup = ({ commit }, selectedGroup) => {
18 19 20 21
  commit(types.UPDATE_SELECTED_GROUP, selectedGroup);
  commit(types.UPDATE_ORGANIZATION_NAME, null);
};

22 23 24 25
export const toggleIsSetupForCompany = ({ state, commit }) => {
  commit(types.UPDATE_IS_SETUP_FOR_COMPANY, !state.isSetupForCompany);
};

26
export const updateNumberOfUsers = ({ commit, getters }, numberOfUsers) => {
27
  commit(types.UPDATE_NUMBER_OF_USERS, numberOfUsers || 0);
28
  trackCheckout(getters.selectedPlanDetails?.value, numberOfUsers);
29 30 31 32 33
};

export const updateOrganizationName = ({ commit }, organizationName) => {
  commit(types.UPDATE_ORGANIZATION_NAME, organizationName);
};
34

Alex Buijs's avatar
Alex Buijs committed
35
export const fetchCountries = ({ dispatch }) =>
36
  Api.fetchCountries()
37 38 39 40
    .then(({ data }) => dispatch('fetchCountriesSuccess', data))
    .catch(() => dispatch('fetchCountriesError'));

export const fetchCountriesSuccess = ({ commit }, data = []) => {
41
  const countries = data.map((country) => ({ text: country[0], value: country[1] }));
42 43 44 45 46

  commit(types.UPDATE_COUNTRY_OPTIONS, countries);
};

export const fetchCountriesError = () => {
47 48 49
  createFlash({
    message: s__('Checkout|Failed to load countries. Please try again.'),
  });
50 51 52 53 54 55 56 57 58
};

export const fetchStates = ({ state, dispatch }) => {
  dispatch('resetStates');

  if (!state.country) {
    return;
  }

59
  Api.fetchStates(state.country)
60 61 62 63 64
    .then(({ data }) => dispatch('fetchStatesSuccess', data))
    .catch(() => dispatch('fetchStatesError'));
};

export const fetchStatesSuccess = ({ commit }, data = {}) => {
65
  const states = Object.keys(data).map((state) => ({ text: state, value: data[state] }));
66 67 68 69 70

  commit(types.UPDATE_STATE_OPTIONS, states);
};

export const fetchStatesError = () => {
71 72 73
  createFlash({
    message: s__('Checkout|Failed to load states. Please try again.'),
  });
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
};

export const resetStates = ({ commit }) => {
  commit(types.UPDATE_COUNTRY_STATE, null);
  commit(types.UPDATE_STATE_OPTIONS, []);
};

export const updateCountry = ({ commit }, country) => {
  commit(types.UPDATE_COUNTRY, country);
};

export const updateStreetAddressLine1 = ({ commit }, streetAddressLine1) => {
  commit(types.UPDATE_STREET_ADDRESS_LINE_ONE, streetAddressLine1);
};

export const updateStreetAddressLine2 = ({ commit }, streetAddressLine2) => {
  commit(types.UPDATE_STREET_ADDRESS_LINE_TWO, streetAddressLine2);
};

export const updateCity = ({ commit }, city) => {
  commit(types.UPDATE_CITY, city);
};

export const updateCountryState = ({ commit }, countryState) => {
  commit(types.UPDATE_COUNTRY_STATE, countryState);
};

export const updateZipCode = ({ commit }, zipCode) => {
  commit(types.UPDATE_ZIP_CODE, zipCode);
};
104

Alex Buijs's avatar
Alex Buijs committed
105 106 107 108
export const startLoadingZuoraScript = ({ commit }) =>
  commit(types.UPDATE_IS_LOADING_PAYMENT_METHOD, true);

export const fetchPaymentFormParams = ({ dispatch }) =>
109
  Api.fetchPaymentFormParams(PAYMENT_FORM_ID)
110 111 112 113 114
    .then(({ data }) => dispatch('fetchPaymentFormParamsSuccess', data))
    .catch(() => dispatch('fetchPaymentFormParamsError'));

export const fetchPaymentFormParamsSuccess = ({ commit }, data) => {
  if (data.errors) {
115 116
    createFlash({
      message: sprintf(
117 118 119 120 121 122
        s__('Checkout|Credit card form failed to load: %{message}'),
        {
          message: data.errors,
        },
        false,
      ),
123
    });
124 125 126 127 128 129
  } else {
    commit(types.UPDATE_PAYMENT_FORM_PARAMS, data);
  }
};

export const fetchPaymentFormParamsError = () => {
130 131 132
  createFlash({
    message: s__('Checkout|Credit card form failed to load. Please try again.'),
  });
133 134
};

Alex Buijs's avatar
Alex Buijs committed
135 136 137 138
export const zuoraIframeRendered = ({ commit }) =>
  commit(types.UPDATE_IS_LOADING_PAYMENT_METHOD, false);

export const paymentFormSubmitted = ({ dispatch, commit }, response) => {
139
  if (response.success) {
Alex Buijs's avatar
Alex Buijs committed
140 141
    commit(types.UPDATE_IS_LOADING_PAYMENT_METHOD, true);

142 143 144 145 146 147 148 149 150 151 152 153 154
    dispatch('paymentFormSubmittedSuccess', response.refId);
  } else {
    dispatch('paymentFormSubmittedError', response);
  }
};

export const paymentFormSubmittedSuccess = ({ commit, dispatch }, paymentMethodId) => {
  commit(types.UPDATE_PAYMENT_METHOD_ID, paymentMethodId);

  dispatch('fetchPaymentMethodDetails');
};

export const paymentFormSubmittedError = (_, response) => {
155 156
  createFlash({
    message: sprintf(
157 158 159 160
      s__(
        'Checkout|Submitting the credit card form failed with code %{errorCode}: %{errorMessage}',
      ),
      response,
161
      false,
162
    ),
163
  });
164 165
};

Alex Buijs's avatar
Alex Buijs committed
166
export const fetchPaymentMethodDetails = ({ state, dispatch, commit }) =>
167
  Api.fetchPaymentMethodDetails(state.paymentMethodId)
168
    .then(({ data }) => dispatch('fetchPaymentMethodDetailsSuccess', data))
Alex Buijs's avatar
Alex Buijs committed
169 170
    .catch(() => dispatch('fetchPaymentMethodDetailsError'))
    .finally(() => commit(types.UPDATE_IS_LOADING_PAYMENT_METHOD, false));
171

172
export const fetchPaymentMethodDetailsSuccess = ({ commit }, creditCardDetails) => {
173 174
  commit(types.UPDATE_CREDIT_CARD_DETAILS, creditCardDetails);

175 176 177 178 179 180 181
  defaultClient
    .mutate({
      mutation: activateNextStepMutation,
    })
    .catch((error) => {
      createFlash({ message: GENERAL_ERROR_MESSAGE, error, captureError: true });
    });
182 183 184
};

export const fetchPaymentMethodDetailsError = () => {
185 186 187
  createFlash({
    message: s__('Checkout|Failed to register credit card. Please try again.'),
  });
188
};
189 190 191 192 193 194

export const confirmOrder = ({ getters, dispatch, commit }) => {
  commit(types.UPDATE_IS_CONFIRMING_ORDER, true);

  Api.confirmOrder(getters.confirmOrderParams)
    .then(({ data }) => {
195
      if (data.location) {
196 197 198 199 200
        const transactionDetails = {
          paymentOption: getters.confirmOrderParams?.subscription?.payment_method_id,
          revenue: getters.totalExVat,
          tax: getters.vat,
          selectedPlan: getters.selectedPlanDetails?.value,
201
          quantity: getters.confirmOrderParams?.subscription?.quantity,
202 203 204 205
        };

        trackTransaction(transactionDetails);

206 207 208 209 210 211
        dispatch('confirmOrderSuccess', {
          location: data.location,
        });
      } else {
        dispatch('confirmOrderError', JSON.stringify(data.errors));
      }
212 213 214 215
    })
    .catch(() => dispatch('confirmOrderError'));
};

216
export const confirmOrderSuccess = (_, { location }) => {
217 218 219 220 221 222 223 224 225 226
  redirectTo(location);
};

export const confirmOrderError = ({ commit }, message = null) => {
  commit(types.UPDATE_IS_CONFIRMING_ORDER, false);

  const errorString = message
    ? s__('Checkout|Failed to confirm your order: %{message}. Please try again.')
    : s__('Checkout|Failed to confirm your order! Please try again.');

227 228 229
  createFlash({
    message: sprintf(errorString, { message }, false),
  });
230
};