Source: app/actions/resources/paymentActions.js

import { createSelector } from 'reselect';
import createAction from 'redux-actions/lib/createAction';
import WebHost from '../../data/enum/WebHost';
import { RESET_AUTH_QUERY } from '../../../server/util/AuthenticationHelper/constants';

export const REMOVE_PURCHASE_PAYMENT = 'paymentActions/REMOVE_PURCHASE_PAYMENT';

export const removePayment = createAction(REMOVE_PURCHASE_PAYMENT);

/**
 * When the payment is cancelled or failed we're storing the data like package,
 * voucherCode and checkout details (shop) in sessionStorage to recover it after
 * hard refresh and bring the form pre-filled with the last changes.
 */
export const savePurchaseInfo = (purchaseId, values) => {
  if (typeof sessionStorage !== 'undefined') {
    sessionStorage.setItem(purchaseId, JSON.stringify(values));
  }
};

export const purchaseInfoSelector = createSelector(
  state => (state.payment && state.payment.purchaseId) || null,
  purchaseId =>
    purchaseId && typeof sessionStorage !== 'undefined'
      ? JSON.parse(sessionStorage.getItem(purchaseId))
      : {},
);

export const deletePurchaseInfo = purchaseId => sessionStorage.removeItem(purchaseId);

/**
 * Start a payment using Adyen
 */
export const startPayment = (redirectOnPayment, purchaseId, service = WebHost.ACCOUNT) => (
  dispatch,
  getState,
) => {
  const state = getState();

  /**
   * before redirect to payment adyen page we delete the data/state related to the previous purchase
   */
  if (state.payment && state.payment.purchaseId) {
    deletePurchaseInfo(state.payment.purchaseId);
    dispatch(removePayment());
  }

  const paymentHost = state.config.environmentConfig.web.payment.host;
  const returnHost = state.config.environmentConfig.web[service].host;
  const paymentUrl = `${paymentHost}/purchases/${purchaseId}/pay/`;

  const tagResetAuth = redirectOnPayment.includes('?') ? '&' : '?';
  const paymentRedirectUrl = `${returnHost}${redirectOnPayment}${tagResetAuth}${RESET_AUTH_QUERY}=true&purchaseId=${purchaseId}`;

  window.location.href = `${paymentUrl}?returnUrl=${encodeURIComponent(paymentRedirectUrl)}`;

  /**
   * After redirecting when the page is still visible for a while, the page needs to keep its
   * current (submitting) state. When returning a Promise that does not resolve, it will cause
   * the form that has this action onSubmit to get { submitting: true }.
   */
  return new Promise(() => {});
};