UNPKG

commerce-vuex

Version:
66 lines (63 loc) 2.12 kB
import { snakeCase } from 'lodash' import humps from 'lodash-humps' import createHumps from 'lodash-humps/lib/createHumps' import { getField, updateField } from 'vuex-map-fields' import stripeCard from './stripe_card.service' const { doPostPaymentMethod, doAttachPaymentMethodToCustomer } = stripeCard const snakes = createHumps(snakeCase) export default { namespaced: true, state: { one: null, inProgress: false }, getters: { getField, }, mutations: { updateField, setOne(state, one) { state.one = one; }, inProgress(state, yesOrNo) { state.inProgress = yesOrNo }, }, actions: { new({ commit }) { commit('inProgress', false); commit('setOne', { number: '', expMonth: '', expYear: '', cvc: '', name: '' }); }, // post new payment method and attached to customer saveStripePaymentMethod: async ({ commit }, { stripeCustomerId, type, card, billingDetails }) => { commit('inProgress', true); try { const paymentMethod = await doPostPaymentMethod({ type, card: snakes(card), billingDetails: snakes(billingDetails) }); if (paymentMethod) { // commit('setOne', humps(paymentMethod)); let attachedPaymentMethod = await doAttachPaymentMethodToCustomer({ paymentMethodId: paymentMethod.id, stripeCustomerId }); commit('inProgress', false) return humps(attachedPaymentMethod) } else { commit('inProgress', false) commit('setOne', null); } } // eslint-disable-next-line no-useless-catch catch (err) { commit('inProgress', false) commit('setOne', null); throw err } finally { commit('inProgress', false) } } }, }