UNPKG

@commercelayer/react-components

Version:
2 lines 4.59 kB
"use client"; import baseReducer from"../utils/baseReducer";import getErrors,{setErrors}from"../utils/getErrors";import getSdk from"../utils/getSdk";import{snakeToCamelCase}from"../utils/snakeToCamelCase";import{replace}from"../utils/replace";import{pick}from"../utils/pick";export function setLoading({loading,dispatch}){dispatch&&dispatch({type:"setLoading",payload:{loading}})}export const setPaymentRef=({ref,dispatch})=>{ref&&dispatch&&dispatch({type:"setPaymentRef",payload:{currentPaymentMethodRef:ref}})},paymentMethodInitialState={errors:[],paymentMethods:void 0},setPaymentMethodErrors=(errors,dispatch)=>{dispatch&&dispatch({type:"setErrors",payload:{errors}})},getPaymentMethods=async({order,dispatch})=>{const paymentMethods=order.available_payment_methods,paymentMethod=order.payment_method,paymentSource=order.payment_source;dispatch({type:"setPaymentMethods",payload:{paymentMethods,currentPaymentMethodId:paymentMethod?.id,currentPaymentMethodType:paymentMethod?.payment_source_type,paymentSource}})};export async function setPaymentMethod({config,dispatch,order,paymentMethodId,updateOrder,setOrderErrors,paymentResource}){let response={success:!1};try{if(config&&order&&dispatch&&paymentResource){localStorage.removeItem("_save_payment_source_to_customer_wallet");const attributes={payment_method:getSdk(config).payment_methods.relationship(paymentMethodId)};updateOrder!=null&&(response=await updateOrder({id:order.id,attributes})),dispatch({type:"setPaymentMethods",payload:{currentPaymentMethodId:paymentMethodId,currentPaymentMethodType:paymentResource,errors:[]}}),setOrderErrors&&setOrderErrors([])}return response}catch(error){const errors=getErrors({error,resource:"orders",field:paymentResource});return console.error("Set payment method",errors),response}}export async function setPaymentSource({config,dispatch,getOrder,attributes,order,paymentResource,customerPaymentSourceId,paymentSourceId,updateOrder,errors:currentErrors}){try{const isAlreadyPlaced=order?.status==="placed";if(config&&order&&!isAlreadyPlaced){let paymentSource;const sdk=getSdk(config);if(!customerPaymentSourceId){if(paymentSourceId){const attrs={id:paymentSourceId,...attributes};paymentSource=attributes!=null?await sdk[paymentResource].update(attrs):await sdk[paymentResource].retrieve(paymentSourceId)}else{const attrs={...attributes,order:sdk.orders.relationship(order.id)};paymentSource=await sdk[paymentResource].create(attrs)}return getOrder&&await getOrder(order.id),dispatch&&dispatch({type:"setPaymentSource",payload:{paymentSource,errors:[],currentCustomerPaymentSourceId:null}}),paymentSource}if(updateOrder!=null){const{order:orderUpdated}=await updateOrder({id:order.id,attributes:{_customer_payment_source_id:customerPaymentSourceId}});dispatch!=null&&orderUpdated!=null&&dispatch({type:"setPaymentSource",payload:{paymentSource:orderUpdated.payment_source,currentCustomerPaymentSourceId:orderUpdated.payment_source?.id}})}}}catch(error){const errors=getErrors({error,resource:"payment_methods",field:paymentResource});if(console.error("Set payment source:",errors),errors!=null&&errors?.length>0){const[error2]=errors;if(error2?.status==="401"&&getOrder!=null&&order!=null){const currentOrder=await getOrder(order?.id);currentOrder?.status!=null&&!["placed","approved"].includes(currentOrder.status)&&(console.error("Set payment source:",errors),setErrors({currentErrors,newErrors:errors,dispatch}))}else setErrors({currentErrors,newErrors:errors,dispatch})}else setErrors({currentErrors,newErrors:errors,dispatch})}}export const updatePaymentSource=async({id,attributes,config,dispatch,paymentResource})=>{if(config)try{const paymentSource=await getSdk(config)[paymentResource].update({id,...attributes});dispatch&&dispatch({type:"setPaymentSource",payload:{paymentSource}})}catch(err){console.error("Update payment source:",err)}},destroyPaymentSource=async({paymentSourceId,paymentResource,dispatch})=>{paymentSourceId&&paymentResource&&dispatch&&dispatch({type:"setPaymentSource",payload:{paymentSource:void 0}})},setPaymentMethodConfig=(config,dispatch)=>{dispatch({type:"setPaymentMethodConfig",payload:{config}})};export function getPaymentConfig(paymentResource,config){const resourceKeys=replace(replace(paymentResource,"payments","payment"),"transfers","transfer"),resource=snakeToCamelCase(resourceKeys);return pick(config,[resource])}const type=["setErrors","setPaymentMethodConfig","setPaymentMethods","setPaymentSource","setPaymentRef","setLoading"],paymentMethodReducer=(state,reducer)=>baseReducer(state,reducer,type);export default paymentMethodReducer;