learndash.paypal_card_manager=learndash.paypal_card_manager||{},((e,a,d,r,l,n)=>{const t=a.querySelector(".ld-paypal-checkout-sdk");n.cardFields=null,n.init=()=>{t&&void 0!==d&&n.setupCardFields()},n.showNotice=d=>{const l=a.querySelector("#btn-add-card");l.textContent=r.__("Save New Card","learndash"),l.removeAttribute("disabled"),e.alert(d)},n.createSetupToken=()=>fetch(n.endpoints.setup_token,{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":n.nonce,"Learndash-Experimental-Rest-Api":"Allow"},body:JSON.stringify({user_id:n.user_id,is_sandbox:n.is_sandbox,use_card_fields:!0})}).then((e=>e.json())).then((e=>e.success?e.data.setup_token.id:(n.showNotice(e.message),""))),n.createPaymentToken=a=>fetch(n.endpoints.payment_token,{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":n.nonce,"Learndash-Experimental-Rest-Api":"Allow"},body:JSON.stringify({token_id:a.vaultSetupToken,user_id:n.user_id,is_sandbox:n.is_sandbox})}).then((e=>e.json())).then((a=>{a.success?e.location.reload():n.showNotice(a.message)})),n.showCardFieldsNotice=e=>{"p is not a function"===e&&(e="unknown_error");const a={unknown_error:r.__("An invalid card was provided. Please review the card details and try again.","learndash"),ineligible_card_vendor:r.__("The card vendor is not supported.","learndash"),invalid_name:r.__("Please enter a valid card holder name.","learndash"),invalid_number:r.__("Please enter a valid card number.","learndash"),invalid_expiry:r.__("Please enter a valid card expiry date.","learndash"),invalid_cvv:r.__("Please enter a valid card security code.","learndash")},d=e.toLowerCase(),l=a[d];return l?n.showNotice(l):n.showNotice(d)},n.setupCardFields=()=>{const e={style:{input:{border:"1px solid #bfbfbf","border-radius":"0","font-size":"0.875rem",padding:"10px"},":focus":{"box-shadow":"none",outline:"1px solid #1d1d1d"}},onError:e=>n.showNotice(e),createVaultSetupToken:()=>n.createSetupToken(),onApprove:e=>n.createPaymentToken(e)};if(n.cardFields=d.CardFields(e),!n.cardFields.isEligible())return;const a=n.cardFields.NameField(),r=n.cardFields.NumberField(),l=n.cardFields.ExpiryField(),t=n.cardFields.CVVField();a.render("#ld-paypal-checkout__card-name-field"),r.render("#ld-paypal-checkout__card-number-field"),l.render("#ld-paypal-checkout__card-expiry-field"),t.render("#ld-paypal-checkout__card-cvv-field")},n.validateBillingAddress=()=>{if(!a.querySelector("#ld-paypal-checkout__billing-address-1").value)return n.showNotice(r.__("Please enter a valid billing address.","learndash")),!1;if(!a.querySelector("#ld-paypal-checkout__billing-postal").value)return n.showNotice(r.__("Please enter a valid postal/zip code.","learndash")),!1;const e=a.querySelector("#ld-paypal-checkout__billing-country");return!(!e.value||""===e.value)||(n.showNotice(r.__("Please select a country.","learndash")),!1)},n.handleFormSubmission=()=>{const e=a.querySelector("#btn-add-card");if(e.textContent=r.__("Saving card…","learndash"),e.setAttribute("disabled","disabled"),!n.validateBillingAddress())return e.textContent=r.__("Save New Card","learndash"),void e.removeAttribute("disabled");n.cardFields.submit({billingAddress:{addressLine1:a.querySelector("#ld-paypal-checkout__billing-address-1").value,addressLine2:a.querySelector("#ld-paypal-checkout__billing-address-2").value,adminArea1:a.querySelector("#ld-paypal-checkout__billing-city").value,countryCode:a.querySelector("#ld-paypal-checkout__billing-country").value.toUpperCase(),postalCode:a.querySelector("#ld-paypal-checkout__billing-postal").value}}).then((()=>{e.textContent=r.__("Processing…","learndash")})).catch((a=>{e.textContent=r.__("Save New Card","learndash"),e.removeAttribute("disabled"),n.showCardFieldsNotice(a.message)}))},l.addAction("learndash_profile_add_card_form_content_loaded","learndash/paypal-checkout/card-manager",(()=>{n.init()})),l.addAction("learndash_dialog_closed","learndash/paypal-checkout/card-manager",(a=>{"add-new-card-dialog"===a.dialogId&&e.location.reload()})),l.addAction("learndash_profile_add_card_submit","learndash/paypal-checkout/card-manager",(()=>{n.handleFormSubmission()}))})(window,document,window.paypal,window.wp.i18n,window.wp.hooks,learndash.paypal_card_manager);