@adyen/adyen-web
Version:
[](https://www.npmjs.com/package/@adyen/adyen-web)
3 lines (2 loc) • 7.4 kB
JavaScript
import{log as e,warn as i}from"../utilities/logger.js";import t from"./utils/createIframe.js";import{selectOne as s,removeAllChildren as r}from"../utilities/dom.js";import{on as n,off as o}from"../../../../../utils/listenerUtils.js";import a from"../CSF/utils/iframes/postMessageToIframe.js";import{originCheckPassed as l,isWebpackPostMsg as h,isChromeVoxPostMsg as f}from"../CSF/utils/iframes/postMessageValidation.js";import{ENCRYPTED_EXPIRY_DATE as c,ENCRYPTED_EXPIRY_MONTH as d,ENCRYPTED_EXPIRY_YEAR as m,DATE_POLICY_OPTIONAL as u,DATE_POLICY_HIDDEN as p,ENCRYPTED_SECURITY_CODE as y,CVC_POLICY_OPTIONAL as g,CVC_POLICY_HIDDEN as C}from"../constants.js";import{generateRandomNumber as b}from"../utilities/commonUtils.js";import E from"./AbstractSecuredField.js";import{reject as T}from"../../../../../utils/commonUtils.js";import{processAriaConfig as L}from"./utils/processAriaConfig.js";import{processPlaceholders as w}from"./utils/processPlaceholders.js";import{hasOwnProperty as k}from"../../../../../utils/hasOwnProperty.js";import{AnalyticsErrorEvent as F,ErrorEventType as _,ErrorEventCode as P}from"../../../../../core/Analytics/events/AnalyticsErrorEvent.js";class O extends E{init(e,i,r,o){const a=L(this.sfConfig.txVariant,this.sfConfig.fieldType,e,o);this.sfConfig.iframeUIConfig.ariaConfig=a,this.sfConfig.iframeUIConfig.placeholders=w(this.sfConfig.txVariant,this.sfConfig.fieldType,r);const l={src:i,title:a[this.sfConfig.fieldType].iframeTitle,policy:"origin"},h=t(l);this.holderEl.appendChild(h);const f=s(this.holderEl,".js-iframe");if(f){if(!f.contentWindow){console.error("ERROR: Trying to initialise a securedField iframe, but the iframe.contentWindow is undefined. Are you sure the element into which the Card component is being mounted already exists in the DOM *before* the Card component is mounted?");const e=new F({component:this.componentType,code:P.SECURED_FIELDS_IFRAME_CONTENT_WINDOW_NOT_FOUND,errorType:_.implementation,message:`Trying to initialise an iframe for ${this.sfConfig.fieldType}, but the iframe.contentWindow is undefined`});this.submitAnalytics?.(e)}this.iframeContentWindow=f.contentWindow,this.iframeOnLoadListener=this.iframeOnLoadListenerFn,n(f,"load",this.iframeOnLoadListener,!1)}return this.iframeRef=f,this}iframeOnLoadListenerFn(){this.postMessageListener=this.postMessageListenerFn,n(window,"message",this.postMessageListener,!1);const e={...this.sfConfig,numKey:this.numKey};window._b$dl&&console.log("### SecuredField:::: onIframeLoaded:: created configObj=",e),a(e,this.iframeContentWindow,this.loadingContext),this.onIframeLoadedCallback()}postMessageListenerFn(t){if(!l(t,this.loadingContext,this.sfConfig.showWarnings))return;let s;try{s=JSON.parse(t.data)}catch(i){return h(t)?void(this.sfConfig.showWarnings&&e("### SecuredField::postMessageListenerFn:: PARSE FAIL - WEBPACK")):f(t)?void(this.sfConfig.showWarnings&&e("### SecuredField::postMessageListenerFn:: PARSE FAIL - CHROMEVOX")):void(this.sfConfig.showWarnings&&e("### SecuredField::postMessageListenerFn:: PARSE FAIL - UNKNOWN REASON: event.data=",t.data))}if(k(s,"action")&&k(s,"numKey"))if(this.numKey===s.numKey)switch(s.action){case"encryption":this.isValid=!0,this.onEncryptionCallback(s);break;case"config":window._b$dl&&console.log("### SecuredField::postMessageListenerFn:: configured - calling onConfigCallback",s.fieldType),this.onConfigCallback(s);break;case"focus":this.onFocusCallback(s);break;case"binValue":this.onBinValueCallback(s);break;case"touch":this.onTouchstartCallback(s);break;case"shifttab":this.onShiftTabCallback(s);break;case"autoComplete":this.onAutoCompleteCallback(s);break;case"enterKeyPressed":this.onKeyPressedCallback(s);break;case"encryptionError":{const e=new F({component:this.componentType,code:s.code,errorType:_.internal,message:`${s.error}. Field= ${this.sfConfig.fieldType}`});this.submitAnalytics?.(e);break}default:this.isValid=!1,this.onValidationCallback(s)}else this.sfConfig.showWarnings&&i("WARNING SecuredField :: postMessage listener for iframe :: data mismatch! (Probably a message from an unrelated securedField)");else this.sfConfig.showWarnings&&i("WARNING SecuredField :: postMessage listener for iframe :: data mismatch!")}destroy(){o(window,"message",this.postMessageListener,!1),o(this.iframeRef,"load",this.iframeOnLoadListener,!1),this.iframeContentWindow=null,r(this.holderEl)}isOptionalOrHidden(){if(this.sfConfig.fieldType===c||this.sfConfig.fieldType===d||this.sfConfig.fieldType===m)switch(this.expiryDatePolicy){case p:return!0;case u:return!this.hasError;default:return!1}if(this.sfConfig.fieldType===y)switch(this.cvcPolicy){case C:return!0;case g:return!this.hasError;default:return!1}return!1}onIframeLoaded(e){return this.onIframeLoadedCallback=e,this}onEncryption(e){return this.onEncryptionCallback=e,this}onValidation(e){return this.onValidationCallback=e,this}onConfig(e){return this.onConfigCallback=e,this}onFocus(e){return this.onFocusCallback=e,this}onBinValue(e){return this.onBinValueCallback=e,this}onTouchstart(e){return this.onTouchstartCallback=e,this}onShiftTab(e){return this.onShiftTabCallback=e,this}onAutoComplete(e){return this.onAutoCompleteCallback=e,this}onKeyPressed(e){return this.onKeyPressedCallback=e,this}get errorType(){return this._errorType}set errorType(e){this._errorType=e}get hasError(){return this._hasError}set hasError(e){this._hasError=e}get isValid(){if(this.sfConfig.fieldType===y)switch(this.cvcPolicy){case C:return!0;case g:return!this.hasError;default:return this._isValid}if(this.sfConfig.fieldType===c||this.sfConfig.fieldType===d||this.sfConfig.fieldType===m)switch(this.expiryDatePolicy){case p:return!0;case u:return!this.hasError;default:return this._isValid}return this._isValid}set isValid(e){this._isValid=e}get cvcPolicy(){return this._cvcPolicy}set cvcPolicy(e){this.sfConfig.fieldType===y&&e!==this.cvcPolicy&&(this._cvcPolicy=e,this.hasError&&"isValidated"===this.errorType&&(this.hasError=!1))}get expiryDatePolicy(){return this._expiryDatePolicy}set expiryDatePolicy(e){this.sfConfig.fieldType!==c&&this.sfConfig.fieldType!==d&&this.sfConfig.fieldType!==m||e!==this.expiryDatePolicy&&(this._expiryDatePolicy=e,this.hasError&&"isValidated"===this.errorType&&(this.hasError=!1))}get iframeContentWindow(){return this._iframeContentWindow}set iframeContentWindow(e){this._iframeContentWindow=e}get isEncrypted(){return this._isEncrypted}set isEncrypted(e){this._isEncrypted=e}get numKey(){return this._numKey}set numKey(e){this._numKey=e}get iframeOnLoadListener(){return this._iframeOnLoadListener}set iframeOnLoadListener(e){this._iframeOnLoadListener=e.bind(this)}get postMessageListener(){return this._postMessageListener}set postMessageListener(e){this._postMessageListener=e.bind(this)}constructor(e,i){super();const t=T(["loadingContext","holderEl","iframeSrc","showContextualElement","placeholders"]).from(e);this.sfConfig={...t,iframeUIConfig:{...t.iframeUIConfig}};const{iframeSrc:s,placeholders:r,showContextualElement:n}=e;return this.loadingContext=e.loadingContext,this.holderEl=e.holderEl,this.submitAnalytics=e.submitAnalytics,this.componentType=e.componentType,this.isValid=!1,this.iframeContentWindow=null,this.numKey=b(),this.isEncrypted=!1,this.hasError=!1,this.errorType="",this.cvcPolicy=e.cvcPolicy,this.expiryDatePolicy=e.expiryDatePolicy,this.init(i,s,r,n)}}export{O as default};
//# sourceMappingURL=SecuredField.js.map