index.js
1 lines
| 1 | import{state as checkoutState}from"@store/checkout";import{state as processorsState}from"@store/processors";import{addQueryArgs,getQueryArg}from"@wordpress/url";import apiFetch from"../../functions/fetch";import{__}from"@wordpress/i18n";export const baseUrl="surecart/v1/checkouts/";export const expand=["line_items","line_item.price","line_item.fees","price.product","customer","customer.shipping_address","payment_intent","discount","discount.promotion","recommended_bumps","bump.price","discount.coupon","shipping_address","staged_payment_intents","tax_identifier","manual_payment_method","shipping_choices","shipping_choice.shipping_method"];export const withDefaultData=(t={})=>{var e,o,a,d;return{live_mode:"test"!==checkoutState.mode,group_key:checkoutState.groupId,abandoned_checkout_enabled:checkoutState.abandonedCheckoutEnabled,metadata:{...(null==t?void 0:t.metadata)||{},...(null===(e=null===window||void 0===window?void 0:window.scData)||void 0===e?void 0:e.page_id)&&{page_id:null===(o=null===window||void 0===window?void 0:window.scData)||void 0===o?void 0:o.page_id},...(null===(a=null==checkoutState?void 0:checkoutState.product)||void 0===a?void 0:a.id)&&{buy_page_product_id:null===(d=null==checkoutState?void 0:checkoutState.product)||void 0===d?void 0:d.id},page_url:window.location.href},...t}};export const withDefaultQuery=(t={})=>{var e,o;return{...!!(null==checkoutState?void 0:checkoutState.formId)&&{form_id:null==checkoutState?void 0:checkoutState.formId},...!!(null===(e=null==checkoutState?void 0:checkoutState.product)||void 0===e?void 0:e.id)&&{product_id:null===(o=null==checkoutState?void 0:checkoutState.product)||void 0===o?void 0:o.id},...!!processorsState.config.stripe.paymentElement&&{stage_processor_type:"stripe"},...t}};export const findInitialCheckoutId=()=>{var a,d;return getQueryArg(window.location.href,"checkout_id")||((null===(t=null==checkoutState?void 0:checkoutState.checkout)||void 0===t?void 0:t.id)?null===(e=null==checkoutState?void 0:checkoutState.checkout)||void 0===e?void 0:e.id:null)};export const parsePath=(t,e="")=>{let o=t?`${baseUrl}${t}`:baseUrl;return o=`${o}${e}`,addQueryArgs(o,{expand:expand})};export const fetchCheckout=async({id:t,query:e={}})=>await apiFetch({path:addQueryArgs(parsePath(t),withDefaultQuery(e))});export const createOrUpdateCheckout=async({id:t=null,data:e={},query:o={}})=>{return t=t||getQueryArg(window.location.href,"checkout_id")||((null===(a=null==checkoutState?void 0:checkoutState.checkout)||void 0===a?void 0:a.id)?null===(d=null==checkoutState?void 0:checkoutState.checkout)||void 0===d?void 0:d.id:null),await apiFetch({method:t?"PATCH":"POST",path:addQueryArgs(parsePath(t),withDefaultQuery(o)),data:withDefaultData(e)});var a,d};export const updateCheckout=async({id:t,data:e={},query:o={}})=>await apiFetch({method:"PATCH",path:addQueryArgs(parsePath(t),withDefaultQuery(o)),data:withDefaultData(e)});export const finalizeCheckout=async({id:t,data:e={},query:o={},processor:a})=>await apiFetch({method:"POST",path:addQueryArgs(parsePath(t,"/finalize"),withDefaultQuery({...(null==a?void 0:a.manual)?{manual_payment:!0,manual_payment_method_id:null==a?void 0:a.id}:{processor_type:null==a?void 0:a.id},...o})),data:withDefaultData(e)});export const addLineItem=async({checkout:t,data:e,live_mode:o=!1})=>{if(!(null==t?void 0:t.id))return await apiFetch({method:"POST",path:addQueryArgs(parsePath(null)),data:{line_items:[e],live_mode:o}});const a=await apiFetch({path:addQueryArgs("surecart/v1/line_items",{consolidate:!0,expand:[...(expand||[]).map((t=>t.includes(".")?t:`checkout.${t}`)),"checkout"]}),method:"POST",data:{...e,checkout:t.id}});return null==a?void 0:a.checkout};export const removeLineItem=async({checkoutId:t,itemId:e})=>{const{deleted:o}=await apiFetch({path:`surecart/v1/line_items/${e}`,method:"DELETE"});if(!o)throw{code:"error",message:__("Failed to delete","surecart")};return await fetchCheckout({id:t})};export const updateLineItem=async({id:t,data:e})=>{const o=await apiFetch({path:addQueryArgs(`surecart/v1/line_items/${t}`,{expand:[...(expand||[]).map((t=>t.includes(".")?t:`checkout.${t}`)),"checkout"]}),method:"PATCH",data:e});return null==o?void 0:o.checkout}; |