File: /home/swifizcd/pinnacleforge.com.ng/wp-content/plugins/woocommerce/assets/client/blocks/278.js
"use strict";(globalThis.webpackChunkwebpackWcBlocksPaymentMethodExtensionJsonp=globalThis.webpackChunkwebpackWcBlocksPaymentMethodExtensionJsonp||[]).push([[278],{278:(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),s=r(723),u=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,S]=(0,a.useState)(""),[v,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,u.jsx)(n.ag,{options:P,children:(0,u.jsx)(n.JV,{appSwitchWhenAvailable:!0,createOrder:async e=>{let t;try{if(m&&!await(async()=>{let e=document.querySelector('[name="add-to-cart"]')?.value;const t=document.querySelector('[name="variation_id"]')?.value;if(t&&(e=t),!e)return!1;const r=document.querySelector('[name="quantity"]'),a=r?.value??"1";if(""===a)return!1;if(b&&v.id===e&&v.quantity===a)return!0;try{const t=await window.wp.apiFetch({method:"DELETE",path:"/wc/store/v1/cart/items"});if(!t||0!==t.length)throw new Error("Failed to empty cart");const r=await window.wp.apiFetch({method:"POST",path:"/wc/store/v1/cart/items",data:{id:e,quantity:a}});if(!r||!r.key)throw new Error("Failed to add product to cart")}catch(e){return!1}return f({id:e,quantity:a}),!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 S(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&&(S(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,s.__)("An unknown error occurred","woocommerce");(0,c.dispatch)("core/notices").createErrorNotice(t,{context:"checkout"===y?"wc/checkout":"wc/cart"})}})})}}}]);