text: import axios from 'axios'
  import {
      ADD_SUBSCRIPTION,
      SAVE_PAYMENT_METHOD,
      CLEAR_SUB_ITEM,
  
      ORDER_CREATE_REQUEST,
      ORDER_CREATE_SUCCESS, 
      ORDER_CREATE_FAIL,
  
      ORDER_DETAILS_REQUEST,
      ORDER_DETAILS_SUCCESS,
      ORDER_DETAILS_FAIL,
  
      ORDER_PAY_REQUEST,
      ORDER_PAY_SUCCESS,
      ORDER_PAY_FAIL ,
      ORDER_PAY_RESET,
  } from '../constants/orderConstants'
  
  
  export const addOrder = (id) => async (dispatch, getState) => {
      const { data } = await axios.get(`/api/subscriptions/${id}`)
  
  
      dispatch({
          type: ADD_SUBSCRIPTION,
          payload: {
              subscription: data._id,
              name: data.name,
              image: data.image,
              price: data.price,
              
          }
      })
      localStorage.setItem('subItem', JSON.stringify(getState().selectedSub.subItem))
  }
  
  
  export const savePaymentMethod = (data) => (dispatch) => {
      dispatch({
          type: SAVE_PAYMENT_METHOD,
          payload: data,
      })
  
      localStorage.setItem('paymentMethod', JSON.stringify(data))
  }
  
  export const createOrder = (subname,paymentMethod) => async (dispatch, getState) => {
      try {
          dispatch({
              type: ORDER_CREATE_REQUEST
          })
  
          const {
              userLogin: { userInfo },
          } = getState()
  
          const config = {
              headers: {
                  'Content-type': 'application/json',
                  Authorization: `Bearer ${userInfo.token}`
              }
          }
  
          const { data } = await axios.post(
              `/api/orders/add/`,
              {'plan':subname,'pm':paymentMethod},
              config
          )
  
          dispatch({
              type: ORDER_CREATE_SUCCESS,
              payload: data
          })
  
          dispatch({
              type: CLEAR_SUB_ITEM,
              payload: data
          })
  
          localStorage.removeItem('SubItem')
  
  
      } catch (error) {
          dispatch({
              type: ORDER_CREATE_FAIL,
              payload: error.response && error.response.data.detail
                  ? error.response.data.detail
                  : error.message,
          })
      }
  }
  
  export const getUserSubscription = (id) => async (dispatch, getState) => {
      try {
          dispatch({
              type: ORDER_DETAILS_REQUEST
          })
  
          const {
              userLogin: { userInfo },
          } = getState()
  
          const config = {
              headers: {
                  'Content-type': 'application/json',
                  Authorization: `Bearer ${userInfo.token}`
              }
          }
  
          const { data } = await axios.get(
              `/api/orders/${id}/`,
              config
          )
  
          dispatch({
              type: ORDER_DETAILS_SUCCESS,
              payload: data
          })
  
  
      } catch (error) {
          dispatch({
              type: ORDER_DETAILS_FAIL,
              payload: error.response && error.response.data.detail
                  ? error.response.data.detail
                  : error.message,
          })
      }
  }
  
  export const payOrder = (id,paymentResult) => async (dispatch, getState) => {
      try {
          dispatch({
              type: ORDER_PAY_REQUEST
          })
  
          const {
              userLogin: { userInfo },
          } = getState()
  
          const config = {
              headers: {
                  'Content-type': 'application/json',
                  Authorization: `Bearer ${userInfo.token}`
              }
          }
  
          const { data } = await axios.put(
              `/api/orders/${id}/pay/`,
              paymentResult,
              config 
          )
  
          dispatch({
              type: ORDER_PAY_SUCCESS,
              payload: data
          })
  
  
      } catch (error) {
          dispatch({
              type: ORDER_PAY_FAIL,
              payload: error.response && error.response.data.detail
                  ? error.response.data.detail
                  : error.message,
          })
      }
  }