/**
* All functions in this module are action creators and the return value should be passed to the
* redux store dispatch() function.
*
* IMPORTANT: This file contains imports that should only be bundled when doing a 'node' build.
* This action file should never be imported in the web bundle
* @module
*/
import debugLib from 'debug';
import querystring from 'querystring';
import createAction from 'redux-actions/lib/createAction';
import { verifyReponseData } from '../util/responseQueryUtils';
const debug = debugLib('SlimmingWorld:paymentResponseActions');
const SET_PAYMENT_RESPONSE = 'paymentResponseActions/SET_PAYMENT_RESPONSE';
/**
* Sets the payment response as decoded from the query string
* @function setPaymentResponse
* @param query The parsed query string. This will be decoded into an object
*/
export const setPaymentResponse = createAction(SET_PAYMENT_RESPONSE, query =>
querystring.parse(query),
);
/**
* Parse the payment response on the query parameters, if it is present.
* If the payment response is found, it will be decoded and validated using
* the signature in the query string. If a valid response is present, it will
* dispatch the setPaymentResponse action to pass the response to the redux
* state. This will be read from routeRequirements on the payment callback route.
* @param query The query object as parsed by react-router
* @param environmentConfig A configuration string from the environmentConfig
*/
export const parsePaymentResponseQuery = (query, environmentConfig) => async dispatch => {
if (query.result) {
const paymentResult = await verifyReponseData(query.result, environmentConfig);
if (paymentResult) {
debug(`Found payment response on query: ${paymentResult}`);
return dispatch(setPaymentResponse(paymentResult));
}
}
return Promise.resolve();
};