/* eslint-disable */
import axios from "axios";

export default {
  namespaced: true,
  state: {
    payments: [],
    payment: {}
  },
  mutations: {
    setPayment(state, item) {
      state.payment = item;
    },
    setPayments(state, items) {
      state.payments = items;
    }
  },
  getters: {},
  actions: {
    getPayment({ commit }, id) {
      axios
        .get(`/api/payment/${id}`)
        .then(result => commit("setPayment", result.data))
        .catch(ex => console.log(ex.response));
    },
    getPayments({ commit }) {
      axios
        .get("/api/payment")
        .then(result => commit("setPayments", result.data))
        .catch(ex => console.log(ex.response));
    },
    async addPayment({ commit }, payment) {
      const response = await axios.post("/api/payment", payment);
      if (response.status === 200) {
        var respObj = {};
        response.data.split("&").forEach(element => {
          var name = "";
          var value = "";
          name = element.split("=")[0];
          value = element.split("=")[1];
          if (name === "PAYGATE_ID") {
            respObj.PAYGATE_ID = value;
          } else if (name === "PAY_REQUEST_ID") {
            respObj.PAY_REQUEST_ID = value;
          } else if (name === "REFERENCE") {
            respObj.REFERENCE = value;
          } else if (name === "CHECKSUM") {
            respObj.CHECKSUM = value;
          }
        });
        return Promise.resolve(respObj);
      } else {
        return Promise.reject(response.status);
      }
    }
  }
};
