@commercelayer/react-components 4.11.2-beta.2 → 4.11.2-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
1
  "use client";
2
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.setPlaceOrder=exports.placeOrderPermitted=exports.setPlaceOrderErrors=exports.placeOrderInitialState=exports.setButtonRef=void 0;const tslib_1=require("tslib"),baseReducer_1=tslib_1.__importDefault(require("../utils/baseReducer")),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),shipments_1=require("../utils/shipments"),customerOrderOptions_1=require("../utils/customerOrderOptions"),getSdk_1=tslib_1.__importDefault(require("../utils/getSdk")),getErrors_1=tslib_1.__importDefault(require("../utils/getErrors")),isGuestToken_1=require("../utils/isGuestToken"),localStorage_1=require("../utils/localStorage"),hasSubscriptions_1=require("../utils/hasSubscriptions");function setButtonRef(ref,dispatch){ref?.current!=null&&dispatch({type:"setButtonRef",payload:{placeOrderButtonRef:ref}})}exports.setButtonRef=setButtonRef,exports.placeOrderInitialState={errors:[],isPermitted:!1};function setPlaceOrderErrors(errors,dispatch){dispatch&&dispatch({type:"setErrors",payload:{errors}})}exports.setPlaceOrderErrors=setPlaceOrderErrors;function placeOrderPermitted({config,order,dispatch,options}){var _a;if(order&&config){let isPermitted=!0;order.privacy_url&&order.terms_url&&(isPermitted=localStorage.getItem("privacy-terms")==="true");const billingAddress=order.billing_address,shippingAddress=order.shipping_address,doNotShip=(0,shipments_1.isDoNotShip)(order.line_items),shipments=order.shipments,shipment=shipments&&(0,shipments_1.shipmentsFilled)(shipments),paymentMethod=order.payment_method,paymentSource=order.payment_source;order.total_amount_with_taxes_cents!==0&&(0,isEmpty_1.default)(paymentMethod?.id)&&(isPermitted=!1),(0,isEmpty_1.default)(billingAddress)&&(isPermitted=!1),(0,isEmpty_1.default)(shippingAddress)&&!doNotShip&&(isPermitted=!1),!(0,isEmpty_1.default)(shipments)&&!shipment&&(isPermitted=!1),paymentSource?.mismatched_amounts&&(isPermitted=!1),dispatch({type:"setPlaceOrderPermitted",payload:{isPermitted,paymentType:paymentMethod?.payment_source_type,paymentSecret:paymentSource?.client_secret,paymentId:(_a=paymentSource?.options)===null||_a===void 0?void 0:_a.id,paymentSource,options}})}}exports.placeOrderPermitted=placeOrderPermitted;function setPlaceOrder({state,order,config,setOrderErrors,paymentSource,setOrder,include,currentCustomerPaymentSourceId}){var _a,_b,_c,_d,_e,_f,_g,_h;return tslib_1.__awaiter(this,void 0,void 0,function*(){const response={placed:!1};if(state&&config&&order){const sdk=(0,getSdk_1.default)(config),{options,paymentType}=state;try{if(paymentType==="paypal_payments"&&paymentSource?.type==="paypal_payments"){if(!options?.paypalPayerId&&paymentSource?.approval_url)return window.location.href=paymentSource?.approval_url,response;yield sdk[paymentType].update({id:paymentSource.id,paypal_payer_id:options?.paypalPayerId})}if(paymentType==="checkout_com_payments"&&paymentSource&&(!((_a=options?.checkoutCom)===null||_a===void 0)&&_a.session_id)){const payment=yield sdk[paymentType].update({id:paymentSource.id,_details:!0,session_id:(_b=options?.checkoutCom)===null||_b===void 0?void 0:_b.session_id});if(((_c=payment?.payment_response)===null||_c===void 0?void 0:_c.status)!=="Authorized"){const[action]=((_d=payment?.payment_response)===null||_d===void 0?void 0:_d.actions)||[""];throw{errors:[{code:"PAYMENT_NOT_APPROVED_FOR_EXECUTION",message:action?.response_summary,resource:"orders",field:"checkout_com_payments"}]}}}const updateAttributes={id:order.id,_place:!0};if((0,customerOrderOptions_1.saveBillingAddress)()&&(yield sdk.orders.update({id:order.id,_save_billing_address_to_customer_address_book:!0})),(0,customerOrderOptions_1.saveShippingAddress)()&&(yield sdk.orders.update({id:order.id,_save_shipping_address_to_customer_address_book:!0})),paymentType==="stripe_payments"&&paymentSource!=null){const ps=paymentSource,currentUrl=window.location.href,returnUrl=(_e=ps?.options)===null||_e===void 0?void 0:_e.return_url;currentUrl!==returnUrl&&(yield sdk[paymentType].update({id:paymentSource.id,options:{return_url:currentUrl}}))}switch((((_f=order.line_items)===null||_f===void 0?void 0:_f.some(item=>{var _a2;return item.frequency&&((_a2=item.frequency)===null||_a2===void 0?void 0:_a2.length)>0}))||order?.subscription_created_at!=null||!1)&&config?.accessToken!=null&&!(0,isGuestToken_1.isGuestToken)(config.accessToken)&&currentCustomerPaymentSourceId==null&&(0,localStorage_1.setCustomerOrderParam)("_save_payment_source_to_customer_wallet","true"),paymentType){case"braintree_payments":{const total=(_g=order?.total_amount_cents)!==null&&_g!==void 0?_g:0;(0,customerOrderOptions_1.saveToWallet)()&&total>0&&(yield sdk.orders.update({id:order.id,_save_payment_source_to_customer_wallet:!0}));const orderUpdated=yield sdk.orders.update(updateAttributes,{include});return setOrder&&setOrder(orderUpdated),setOrderErrors&&setOrderErrors([]),(0,hasSubscriptions_1.updateSubscriptionCustomerPaymentSource)(orderUpdated,sdk),{placed:!0,order:orderUpdated}}default:{const orderUpdated=yield sdk.orders.update(updateAttributes,{include}),total=(_h=orderUpdated?.total_amount_cents)!==null&&_h!==void 0?_h:0;return setOrder&&setOrder(orderUpdated),(0,customerOrderOptions_1.saveToWallet)()&&total>0&&sdk.orders.update({id:order.id,_save_payment_source_to_customer_wallet:!0}).catch(error=>{const errors=(0,getErrors_1.default)({error,resource:"orders",field:paymentType});setOrderErrors&&setOrderErrors(errors)}),setOrderErrors&&setOrderErrors([]),(0,hasSubscriptions_1.updateSubscriptionCustomerPaymentSource)(orderUpdated,sdk),{placed:!0,order:orderUpdated}}}}catch(error){const errors=(0,getErrors_1.default)({error,resource:"orders",field:paymentType});return setOrderErrors&&setOrderErrors(errors),Object.assign(Object.assign({},response),{errors})}}return response})}exports.setPlaceOrder=setPlaceOrder;const type=["setErrors","setPlaceOrderPermitted","setButtonRef"],placeOrderReducer=(state,reducer)=>(0,baseReducer_1.default)(state,reducer,type);exports.default=placeOrderReducer;
2
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.setPlaceOrder=exports.placeOrderPermitted=exports.setPlaceOrderErrors=exports.placeOrderInitialState=exports.setButtonRef=void 0;const tslib_1=require("tslib"),baseReducer_1=tslib_1.__importDefault(require("../utils/baseReducer")),isEmpty_1=tslib_1.__importDefault(require("lodash/isEmpty")),shipments_1=require("../utils/shipments"),customerOrderOptions_1=require("../utils/customerOrderOptions"),getSdk_1=tslib_1.__importDefault(require("../utils/getSdk")),getErrors_1=tslib_1.__importDefault(require("../utils/getErrors")),isGuestToken_1=require("../utils/isGuestToken"),localStorage_1=require("../utils/localStorage"),hasSubscriptions_1=require("../utils/hasSubscriptions"),updateOrderSubscriptionCustomerPaymentSource_1=require("../utils/updateOrderSubscriptionCustomerPaymentSource");function setButtonRef(ref,dispatch){ref?.current!=null&&dispatch({type:"setButtonRef",payload:{placeOrderButtonRef:ref}})}exports.setButtonRef=setButtonRef,exports.placeOrderInitialState={errors:[],isPermitted:!1};function setPlaceOrderErrors(errors,dispatch){dispatch&&dispatch({type:"setErrors",payload:{errors}})}exports.setPlaceOrderErrors=setPlaceOrderErrors;function placeOrderPermitted({config,order,dispatch,options}){var _a;if(order&&config){let isPermitted=!0;order.privacy_url&&order.terms_url&&(isPermitted=localStorage.getItem("privacy-terms")==="true");const billingAddress=order.billing_address,shippingAddress=order.shipping_address,doNotShip=(0,shipments_1.isDoNotShip)(order.line_items),shipments=order.shipments,shipment=shipments&&(0,shipments_1.shipmentsFilled)(shipments),paymentMethod=order.payment_method,paymentSource=order.payment_source;order.total_amount_with_taxes_cents!==0&&(0,isEmpty_1.default)(paymentMethod?.id)&&(isPermitted=!1),(0,isEmpty_1.default)(billingAddress)&&(isPermitted=!1),(0,isEmpty_1.default)(shippingAddress)&&!doNotShip&&(isPermitted=!1),!(0,isEmpty_1.default)(shipments)&&!shipment&&(isPermitted=!1),paymentSource?.mismatched_amounts&&(isPermitted=!1),dispatch({type:"setPlaceOrderPermitted",payload:{isPermitted,paymentType:paymentMethod?.payment_source_type,paymentSecret:paymentSource?.client_secret,paymentId:(_a=paymentSource?.options)===null||_a===void 0?void 0:_a.id,paymentSource,options}})}}exports.placeOrderPermitted=placeOrderPermitted;function setPlaceOrder({state,order,config,setOrderErrors,paymentSource,setOrder,include,currentCustomerPaymentSourceId}){var _a,_b,_c,_d,_e,_f,_g;return tslib_1.__awaiter(this,void 0,void 0,function*(){const response={placed:!1};if(state&&config&&order){const sdk=(0,getSdk_1.default)(config),{options,paymentType}=state;try{if(paymentType==="paypal_payments"&&paymentSource?.type==="paypal_payments"){if(!options?.paypalPayerId&&paymentSource?.approval_url)return window.location.href=paymentSource?.approval_url,response;yield sdk[paymentType].update({id:paymentSource.id,paypal_payer_id:options?.paypalPayerId})}if(paymentType==="checkout_com_payments"&&paymentSource&&(!((_a=options?.checkoutCom)===null||_a===void 0)&&_a.session_id)){const payment=yield sdk[paymentType].update({id:paymentSource.id,_details:!0,session_id:(_b=options?.checkoutCom)===null||_b===void 0?void 0:_b.session_id});if(((_c=payment?.payment_response)===null||_c===void 0?void 0:_c.status)!=="Authorized"){const[action]=((_d=payment?.payment_response)===null||_d===void 0?void 0:_d.actions)||[""];throw{errors:[{code:"PAYMENT_NOT_APPROVED_FOR_EXECUTION",message:action?.response_summary,resource:"orders",field:"checkout_com_payments"}]}}}const updateAttributes={id:order.id,_place:!0};if((0,customerOrderOptions_1.saveBillingAddress)()&&(yield sdk.orders.update({id:order.id,_save_billing_address_to_customer_address_book:!0})),(0,customerOrderOptions_1.saveShippingAddress)()&&(yield sdk.orders.update({id:order.id,_save_shipping_address_to_customer_address_book:!0})),paymentType==="stripe_payments"&&paymentSource!=null){const ps=paymentSource,currentUrl=window.location.href,returnUrl=(_e=ps?.options)===null||_e===void 0?void 0:_e.return_url;currentUrl!==returnUrl&&(yield sdk[paymentType].update({id:paymentSource.id,options:{return_url:currentUrl}}))}switch((0,hasSubscriptions_1.hasSubscriptions)(order)&&config?.accessToken!=null&&!(0,isGuestToken_1.isGuestToken)(config.accessToken)&&currentCustomerPaymentSourceId==null&&(0,localStorage_1.setCustomerOrderParam)("_save_payment_source_to_customer_wallet","true"),paymentType){case"braintree_payments":{const total=(_f=order?.total_amount_cents)!==null&&_f!==void 0?_f:0,promises=[sdk.orders.update(updateAttributes,{include}),(0,customerOrderOptions_1.saveToWallet)()&&total>0&&sdk.orders.update({id:order.id,_save_payment_source_to_customer_wallet:!0})],orderUpdated=yield Promise.all(promises).then(res=>res[0]);return setOrder&&setOrder(orderUpdated),setOrderErrors&&setOrderErrors([]),(0,updateOrderSubscriptionCustomerPaymentSource_1.updateOrderSubscriptionCustomerPaymentSource)(orderUpdated,sdk),{placed:!0,order:orderUpdated}}default:{const orderUpdated=yield sdk.orders.update(updateAttributes,{include}),total=(_g=orderUpdated?.total_amount_cents)!==null&&_g!==void 0?_g:0;setOrder&&setOrder(orderUpdated);const promises=[(0,customerOrderOptions_1.saveToWallet)()&&total>0&&sdk.orders.update({id:order.id,_save_payment_source_to_customer_wallet:!0}).catch(error=>{const errors=(0,getErrors_1.default)({error,resource:"orders",field:paymentType});setOrderErrors&&setOrderErrors(errors)})];return setOrderErrors&&setOrderErrors([]),yield Promise.all(promises).then(()=>{(0,updateOrderSubscriptionCustomerPaymentSource_1.updateOrderSubscriptionCustomerPaymentSource)(orderUpdated,sdk)}),{placed:!0,order:orderUpdated}}}}catch(error){const errors=(0,getErrors_1.default)({error,resource:"orders",field:paymentType});return setOrderErrors&&setOrderErrors(errors),Object.assign(Object.assign({},response),{errors})}}return response})}exports.setPlaceOrder=setPlaceOrder;const type=["setErrors","setPlaceOrderPermitted","setButtonRef"],placeOrderReducer=(state,reducer)=>(0,baseReducer_1.default)(state,reducer,type);exports.default=placeOrderReducer;
@@ -1,13 +1,7 @@
1
- import { type CommerceLayerClient, type Order } from '@commercelayer/sdk';
1
+ import { type Order } from '@commercelayer/sdk';
2
2
  /**
3
- * Check if the order has subscriptions reading the frequency attribute of the line items
3
+ * Check if a given `order` has subscriptions by checking the `frequency` attribute of the `line_items` (in case of brand new `order_subscription`) or the `subscription_created_at` attribute of the `order` itself (in case `order_subscription` is already existing)
4
4
  * @param order Order
5
5
  * @returns boolean
6
6
  */
7
7
  export declare function hasSubscriptions(order: Order): boolean;
8
- /**
9
- * Check if the given `order` has a linked `order_subscription` to replace its `customer_payment_source` with the updated `order`'s `payment_source`.
10
- * @param order Order
11
- * @returns void
12
- */
13
- export declare function updateSubscriptionCustomerPaymentSource(order: Order, sdk: CommerceLayerClient): void;
@@ -1,2 +1,2 @@
1
1
  "use client";
2
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.updateSubscriptionCustomerPaymentSource=exports.hasSubscriptions=void 0;function hasSubscriptions(order){var _a;return((_a=order?.line_items)===null||_a===void 0?void 0:_a.some(li=>li.frequency!=null))!=null}exports.hasSubscriptions=hasSubscriptions;function updateSubscriptionCustomerPaymentSource(order,sdk){order.subscription_created_at!=null&&sdk.orders.retrieve(order.id,{include:["order_subscription","order_subscription.customer_payment_source"]}).then(order2=>{order2.payment_source_details!=null&&order2.order_subscription!=null&&sdk.customer_payment_sources.list({pageSize:25}).then(customerPaymentSources2=>{var _a;if(customerPaymentSources2.length>0&&order2.payment_source_details!=null&&order2.order_subscription!=null){const details=order2.payment_source_details,customerPaymentSource=customerPaymentSources2.find(cps=>cps.payment_source_token===details.payment_method_id);customerPaymentSource!=null&&sdk.order_subscriptions.update({id:(_a=order2.order_subscription)===null||_a===void 0?void 0:_a.id,customer_payment_source:sdk.customer_payment_sources.relationship(customerPaymentSource?.id)})}})})}exports.updateSubscriptionCustomerPaymentSource=updateSubscriptionCustomerPaymentSource;
2
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.hasSubscriptions=void 0;function hasSubscriptions(order){var _a;return((_a=order.line_items)===null||_a===void 0?void 0:_a.some(item=>{var _a2;return item.frequency&&((_a2=item.frequency)===null||_a2===void 0?void 0:_a2.length)>0}))||order?.subscription_created_at!=null||!1}exports.hasSubscriptions=hasSubscriptions;
@@ -0,0 +1,7 @@
1
+ import { type CommerceLayerClient, type Order } from '@commercelayer/sdk';
2
+ /**
3
+ * Check if a given `order` has a linked `order_subscription` to replace its `customer_payment_source` with the `order`'s `payment_source`.
4
+ * @param order Order
5
+ * @returns void
6
+ */
7
+ export declare function updateOrderSubscriptionCustomerPaymentSource(order: Order, sdk: CommerceLayerClient): void;
@@ -0,0 +1,2 @@
1
+ "use client";
2
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.updateOrderSubscriptionCustomerPaymentSource=void 0;function updateOrderSubscriptionCustomerPaymentSource(order,sdk){order.subscription_created_at!=null&&sdk.orders.retrieve(order.id,{include:["order_subscription","order_subscription.customer_payment_source"]}).then(order2=>{order2.payment_source_details!=null&&order2.order_subscription!=null&&sdk.customer_payment_sources.list({filters:{payment_source_token_eq:order2.payment_source_details.payment_method_id}}).then(customerPaymentSources2=>{var _a;if(customerPaymentSources2.length>0&&order2.order_subscription!=null){const customerPaymentSource=customerPaymentSources2[0];customerPaymentSource!=null&&sdk.order_subscriptions.update({id:(_a=order2.order_subscription)===null||_a===void 0?void 0:_a.id,customer_payment_source:sdk.customer_payment_sources.relationship(customerPaymentSource?.id)})}})})}exports.updateOrderSubscriptionCustomerPaymentSource=updateOrderSubscriptionCustomerPaymentSource;
@@ -1,2 +1,2 @@
1
1
  "use client";
2
- import baseReducer from"../utils/baseReducer";import isEmpty from"lodash/isEmpty";import{isDoNotShip,shipmentsFilled}from"../utils/shipments";import{saveBillingAddress,saveShippingAddress,saveToWallet}from"../utils/customerOrderOptions";import getSdk from"../utils/getSdk";import getErrors from"../utils/getErrors";import{isGuestToken}from"../utils/isGuestToken";import{setCustomerOrderParam}from"../utils/localStorage";import{updateSubscriptionCustomerPaymentSource}from"../utils/hasSubscriptions";export function setButtonRef(ref,dispatch){ref?.current!=null&&dispatch({type:"setButtonRef",payload:{placeOrderButtonRef:ref}})}export const placeOrderInitialState={errors:[],isPermitted:!1};export function setPlaceOrderErrors(errors,dispatch){dispatch&&dispatch({type:"setErrors",payload:{errors}})}export function placeOrderPermitted({config,order,dispatch,options}){if(order&&config){let isPermitted=!0;order.privacy_url&&order.terms_url&&(isPermitted=localStorage.getItem("privacy-terms")==="true");const billingAddress=order.billing_address,shippingAddress=order.shipping_address,doNotShip=isDoNotShip(order.line_items),shipments=order.shipments,shipment=shipments&&shipmentsFilled(shipments),paymentMethod=order.payment_method,paymentSource=order.payment_source;order.total_amount_with_taxes_cents!==0&&isEmpty(paymentMethod?.id)&&(isPermitted=!1),isEmpty(billingAddress)&&(isPermitted=!1),isEmpty(shippingAddress)&&!doNotShip&&(isPermitted=!1),!isEmpty(shipments)&&!shipment&&(isPermitted=!1),paymentSource?.mismatched_amounts&&(isPermitted=!1),dispatch({type:"setPlaceOrderPermitted",payload:{isPermitted,paymentType:paymentMethod?.payment_source_type,paymentSecret:paymentSource?.client_secret,paymentId:paymentSource?.options?.id,paymentSource,options}})}}export async function setPlaceOrder({state,order,config,setOrderErrors,paymentSource,setOrder,include,currentCustomerPaymentSourceId}){const response={placed:!1};if(state&&config&&order){const sdk=getSdk(config),{options,paymentType}=state;try{if(paymentType==="paypal_payments"&&paymentSource?.type==="paypal_payments"){if(!options?.paypalPayerId&&paymentSource?.approval_url)return window.location.href=paymentSource?.approval_url,response;await sdk[paymentType].update({id:paymentSource.id,paypal_payer_id:options?.paypalPayerId})}if(paymentType==="checkout_com_payments"&&paymentSource&&options?.checkoutCom?.session_id){const payment=await sdk[paymentType].update({id:paymentSource.id,_details:!0,session_id:options?.checkoutCom?.session_id});if(payment?.payment_response?.status!=="Authorized"){const[action]=payment?.payment_response?.actions||[""];throw{errors:[{code:"PAYMENT_NOT_APPROVED_FOR_EXECUTION",message:action?.response_summary,resource:"orders",field:"checkout_com_payments"}]}}}const updateAttributes={id:order.id,_place:!0};if(saveBillingAddress()&&await sdk.orders.update({id:order.id,_save_billing_address_to_customer_address_book:!0}),saveShippingAddress()&&await sdk.orders.update({id:order.id,_save_shipping_address_to_customer_address_book:!0}),paymentType==="stripe_payments"&&paymentSource!=null){const ps=paymentSource,currentUrl=window.location.href,returnUrl=ps?.options?.return_url;currentUrl!==returnUrl&&await sdk[paymentType].update({id:paymentSource.id,options:{return_url:currentUrl}})}switch((order.line_items?.some(item=>item.frequency&&item.frequency?.length>0)||order?.subscription_created_at!=null||!1)&&config?.accessToken!=null&&!isGuestToken(config.accessToken)&&currentCustomerPaymentSourceId==null&&setCustomerOrderParam("_save_payment_source_to_customer_wallet","true"),paymentType){case"braintree_payments":{const total=order?.total_amount_cents??0;saveToWallet()&&total>0&&await sdk.orders.update({id:order.id,_save_payment_source_to_customer_wallet:!0});const orderUpdated=await sdk.orders.update(updateAttributes,{include});return setOrder&&setOrder(orderUpdated),setOrderErrors&&setOrderErrors([]),updateSubscriptionCustomerPaymentSource(orderUpdated,sdk),{placed:!0,order:orderUpdated}}default:{const orderUpdated=await sdk.orders.update(updateAttributes,{include}),total=orderUpdated?.total_amount_cents??0;return setOrder&&setOrder(orderUpdated),saveToWallet()&&total>0&&sdk.orders.update({id:order.id,_save_payment_source_to_customer_wallet:!0}).catch(error=>{const errors=getErrors({error,resource:"orders",field:paymentType});setOrderErrors&&setOrderErrors(errors)}),setOrderErrors&&setOrderErrors([]),updateSubscriptionCustomerPaymentSource(orderUpdated,sdk),{placed:!0,order:orderUpdated}}}}catch(error){const errors=getErrors({error,resource:"orders",field:paymentType});return setOrderErrors&&setOrderErrors(errors),{...response,errors}}}return response}const type=["setErrors","setPlaceOrderPermitted","setButtonRef"],placeOrderReducer=(state,reducer)=>baseReducer(state,reducer,type);export default placeOrderReducer;
2
+ import baseReducer from"../utils/baseReducer";import isEmpty from"lodash/isEmpty";import{isDoNotShip,shipmentsFilled}from"../utils/shipments";import{saveBillingAddress,saveShippingAddress,saveToWallet}from"../utils/customerOrderOptions";import getSdk from"../utils/getSdk";import getErrors from"../utils/getErrors";import{isGuestToken}from"../utils/isGuestToken";import{setCustomerOrderParam}from"../utils/localStorage";import{hasSubscriptions}from"../utils/hasSubscriptions";import{updateOrderSubscriptionCustomerPaymentSource}from"../utils/updateOrderSubscriptionCustomerPaymentSource";export function setButtonRef(ref,dispatch){ref?.current!=null&&dispatch({type:"setButtonRef",payload:{placeOrderButtonRef:ref}})}export const placeOrderInitialState={errors:[],isPermitted:!1};export function setPlaceOrderErrors(errors,dispatch){dispatch&&dispatch({type:"setErrors",payload:{errors}})}export function placeOrderPermitted({config,order,dispatch,options}){if(order&&config){let isPermitted=!0;order.privacy_url&&order.terms_url&&(isPermitted=localStorage.getItem("privacy-terms")==="true");const billingAddress=order.billing_address,shippingAddress=order.shipping_address,doNotShip=isDoNotShip(order.line_items),shipments=order.shipments,shipment=shipments&&shipmentsFilled(shipments),paymentMethod=order.payment_method,paymentSource=order.payment_source;order.total_amount_with_taxes_cents!==0&&isEmpty(paymentMethod?.id)&&(isPermitted=!1),isEmpty(billingAddress)&&(isPermitted=!1),isEmpty(shippingAddress)&&!doNotShip&&(isPermitted=!1),!isEmpty(shipments)&&!shipment&&(isPermitted=!1),paymentSource?.mismatched_amounts&&(isPermitted=!1),dispatch({type:"setPlaceOrderPermitted",payload:{isPermitted,paymentType:paymentMethod?.payment_source_type,paymentSecret:paymentSource?.client_secret,paymentId:paymentSource?.options?.id,paymentSource,options}})}}export async function setPlaceOrder({state,order,config,setOrderErrors,paymentSource,setOrder,include,currentCustomerPaymentSourceId}){const response={placed:!1};if(state&&config&&order){const sdk=getSdk(config),{options,paymentType}=state;try{if(paymentType==="paypal_payments"&&paymentSource?.type==="paypal_payments"){if(!options?.paypalPayerId&&paymentSource?.approval_url)return window.location.href=paymentSource?.approval_url,response;await sdk[paymentType].update({id:paymentSource.id,paypal_payer_id:options?.paypalPayerId})}if(paymentType==="checkout_com_payments"&&paymentSource&&options?.checkoutCom?.session_id){const payment=await sdk[paymentType].update({id:paymentSource.id,_details:!0,session_id:options?.checkoutCom?.session_id});if(payment?.payment_response?.status!=="Authorized"){const[action]=payment?.payment_response?.actions||[""];throw{errors:[{code:"PAYMENT_NOT_APPROVED_FOR_EXECUTION",message:action?.response_summary,resource:"orders",field:"checkout_com_payments"}]}}}const updateAttributes={id:order.id,_place:!0};if(saveBillingAddress()&&await sdk.orders.update({id:order.id,_save_billing_address_to_customer_address_book:!0}),saveShippingAddress()&&await sdk.orders.update({id:order.id,_save_shipping_address_to_customer_address_book:!0}),paymentType==="stripe_payments"&&paymentSource!=null){const ps=paymentSource,currentUrl=window.location.href,returnUrl=ps?.options?.return_url;currentUrl!==returnUrl&&await sdk[paymentType].update({id:paymentSource.id,options:{return_url:currentUrl}})}switch(hasSubscriptions(order)&&config?.accessToken!=null&&!isGuestToken(config.accessToken)&&currentCustomerPaymentSourceId==null&&setCustomerOrderParam("_save_payment_source_to_customer_wallet","true"),paymentType){case"braintree_payments":{const total=order?.total_amount_cents??0,promises=[sdk.orders.update(updateAttributes,{include}),saveToWallet()&&total>0&&sdk.orders.update({id:order.id,_save_payment_source_to_customer_wallet:!0})],orderUpdated=await Promise.all(promises).then(res=>res[0]);return setOrder&&setOrder(orderUpdated),setOrderErrors&&setOrderErrors([]),updateOrderSubscriptionCustomerPaymentSource(orderUpdated,sdk),{placed:!0,order:orderUpdated}}default:{const orderUpdated=await sdk.orders.update(updateAttributes,{include}),total=orderUpdated?.total_amount_cents??0;setOrder&&setOrder(orderUpdated);const promises=[saveToWallet()&&total>0&&sdk.orders.update({id:order.id,_save_payment_source_to_customer_wallet:!0}).catch(error=>{const errors=getErrors({error,resource:"orders",field:paymentType});setOrderErrors&&setOrderErrors(errors)})];return setOrderErrors&&setOrderErrors([]),await Promise.all(promises).then(()=>{updateOrderSubscriptionCustomerPaymentSource(orderUpdated,sdk)}),{placed:!0,order:orderUpdated}}}}catch(error){const errors=getErrors({error,resource:"orders",field:paymentType});return setOrderErrors&&setOrderErrors(errors),{...response,errors}}}return response}const type=["setErrors","setPlaceOrderPermitted","setButtonRef"],placeOrderReducer=(state,reducer)=>baseReducer(state,reducer,type);export default placeOrderReducer;
@@ -1,13 +1,7 @@
1
- import { type CommerceLayerClient, type Order } from '@commercelayer/sdk';
1
+ import { type Order } from '@commercelayer/sdk';
2
2
  /**
3
- * Check if the order has subscriptions reading the frequency attribute of the line items
3
+ * Check if a given `order` has subscriptions by checking the `frequency` attribute of the `line_items` (in case of brand new `order_subscription`) or the `subscription_created_at` attribute of the `order` itself (in case `order_subscription` is already existing)
4
4
  * @param order Order
5
5
  * @returns boolean
6
6
  */
7
7
  export declare function hasSubscriptions(order: Order): boolean;
8
- /**
9
- * Check if the given `order` has a linked `order_subscription` to replace its `customer_payment_source` with the updated `order`'s `payment_source`.
10
- * @param order Order
11
- * @returns void
12
- */
13
- export declare function updateSubscriptionCustomerPaymentSource(order: Order, sdk: CommerceLayerClient): void;
@@ -1,2 +1,2 @@
1
1
  "use client";
2
- export function hasSubscriptions(order){return order?.line_items?.some(li=>li.frequency!=null)!=null}export function updateSubscriptionCustomerPaymentSource(order,sdk){order.subscription_created_at!=null&&sdk.orders.retrieve(order.id,{include:["order_subscription","order_subscription.customer_payment_source"]}).then(order2=>{order2.payment_source_details!=null&&order2.order_subscription!=null&&sdk.customer_payment_sources.list({pageSize:25}).then(customerPaymentSources2=>{if(customerPaymentSources2.length>0&&order2.payment_source_details!=null&&order2.order_subscription!=null){const details=order2.payment_source_details,customerPaymentSource=customerPaymentSources2.find(cps=>cps.payment_source_token===details.payment_method_id);customerPaymentSource!=null&&sdk.order_subscriptions.update({id:order2.order_subscription?.id,customer_payment_source:sdk.customer_payment_sources.relationship(customerPaymentSource?.id)})}})})}
2
+ export function hasSubscriptions(order){return order.line_items?.some(item=>item.frequency&&item.frequency?.length>0)||order?.subscription_created_at!=null||!1}
@@ -0,0 +1,7 @@
1
+ import { type CommerceLayerClient, type Order } from '@commercelayer/sdk';
2
+ /**
3
+ * Check if a given `order` has a linked `order_subscription` to replace its `customer_payment_source` with the `order`'s `payment_source`.
4
+ * @param order Order
5
+ * @returns void
6
+ */
7
+ export declare function updateOrderSubscriptionCustomerPaymentSource(order: Order, sdk: CommerceLayerClient): void;
@@ -0,0 +1,2 @@
1
+ "use client";
2
+ export function updateOrderSubscriptionCustomerPaymentSource(order,sdk){order.subscription_created_at!=null&&sdk.orders.retrieve(order.id,{include:["order_subscription","order_subscription.customer_payment_source"]}).then(order2=>{order2.payment_source_details!=null&&order2.order_subscription!=null&&sdk.customer_payment_sources.list({filters:{payment_source_token_eq:order2.payment_source_details.payment_method_id}}).then(customerPaymentSources2=>{if(customerPaymentSources2.length>0&&order2.order_subscription!=null){const customerPaymentSource=customerPaymentSources2[0];customerPaymentSource!=null&&sdk.order_subscriptions.update({id:order2.order_subscription?.id,customer_payment_source:sdk.customer_payment_sources.relationship(customerPaymentSource?.id)})}})})}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercelayer/react-components",
3
- "version": "4.11.2-beta.2",
3
+ "version": "4.11.2-beta.4",
4
4
  "description": "The Official Commerce Layer React Components",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/esm/index.js",