"use strict";(globalThis.webpackChunkwebpackWcBlocksPaymentMethodExtensionJsonp=globalThis.webpackChunkwebpackWcBlocksPaymentMethodExtensionJsonp||[]).push([[484],{484:(e,t,r)=>{r.r(t),r.d(t,{default:()=>l});var a=r(87),n=r(774),o=r(703),c=r(143),i=r(455),d=r.n(i),u=r(723),s=r(790);const l=({clientId:e,components:t,disableFunding:r,enableFunding:i,currency:l,intent:p,merchantId:h,partnerAttributionId:w,pageType:y,isProductPage:m,appSwitchRequestOrigin:_})=>{const[g,k]=(0,a.useState)(""),[b,v]=(0,a.useState)(""),[S,f]=(0,a.useState)({id:"",quantity:""}),F=(0,o.getPaymentMethodData)("paypal",{}),P={clientId:e||"",components:t||"",disableFunding:r||"",enableFunding:i||"",currency:l||"",intent:p||"",merchantId:h||"","data-partner-attribution-id":w||"","data-page-type":y||""};return(0,s.jsx)(n.ag,{options:P,children:(0,s.jsx)(n.JV,{appSwitchWhenAvailable:!0,createOrder:async e=>{let t;try{if(m&&!await(async()=>{var e;let t=document.querySelector('[name="add-to-cart"]')?.value;const r=document.querySelector('[name="variation_id"]')?.value;if(r&&(t=r),!t)return!1;const a=document.querySelector('[name="quantity"]'),n=null!==(e=a?.value)&&void 0!==e?e:"1";if(""===n)return!1;if(b&&S.id===t&&S.quantity===n)return!0;try{const e=await window.wp.apiFetch({method:"DELETE",path:"/wc/store/v1/cart/items"});if(!e||0!==e.length)throw new Error("Failed to empty cart");const r=await window.wp.apiFetch({method:"POST",path:"/wc/store/v1/cart/items",data:{id:t,quantity:n}});if(!r||!r.key)throw new Error("Failed to add product to cart")}catch(e){return!1}return f({id:t,quantity:n}),!0})())return null;if(t=await d()({method:"GET",path:"/wc/store/v1/checkout",headers:{Nonce:F.wc_store_api_nonce}}),!t.order_id||!t.order_key)return console.error("Failed to create WooCommerce order",t),null;const r=await d()({method:"POST",path:"/wc/v3/paypal-buttons/create-order",headers:{Nonce:F.create_order_nonce},data:{order_id:t.order_id,order_key:t.order_key,payment_source:e.paymentSource||"",app_switch_request_origin:_}});return v(r.order_id),k(r.return_url),r.paypal_order_id}catch(e){return console.error("Failed to create order",e),null}},onApprove:()=>{g&&(window.location.href=g)},onCancel:async e=>{let t=b;if(!t){const e=new URLSearchParams(window.location.search).get("order_id");e&&(v(e),t=e)}if(t)try{await d()({method:"POST",path:"/wc/v3/paypal-buttons/cancel-payment",headers:{Nonce:F.cancel_payment_nonce},data:{order_id:t,paypal_order_id:e.orderID}}),k("")}catch(e){console.error("Failed to cancel PayPal payment",e)}},onError:e=>{const t=e.message||(0,u.__)("An unknown error occurred","woocommerce");(0,c.dispatch)("core/notices").createErrorNotice(t,{context:"checkout"===y?"wc/checkout":"wc/cart"})}})})}}}]);