UNPKG

@atlas-kitchen/adyen-web

Version:
3 lines (2 loc) 6.85 kB
import{createCheckoutPayloadBasedOnScheme as e,createShopperCardsList as t,CTP_IFRAME_NAME as i}from"./utils.js";import o from"./sdks/SrciError.js";import{SchemeNames as s}from"./sdks/utils.js";import r from"../../../../utils/uuid.js";import n from"../../../../core/Errors/AdyenCheckoutError.js";import{isRejected as a,isFulfilled as d}from"../../../../utils/promise-util.js";import c from"../errors/TimeoutError.js";import{executeWithTimeout as l}from"./execute-with-timeout.js";function h(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}function m(e){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{},o=Object.keys(i);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(i).filter((function(e){return Object.getOwnPropertyDescriptor(i,e).enumerable})))),o.forEach((function(t){h(e,t,i[t])}))}return e}function u(e,t){return t=null!=t?t:{},Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):function(e){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);t.push.apply(t,i)}return t}(Object(t)).forEach((function(i){Object.defineProperty(e,i,Object.getOwnPropertyDescriptor(t,i))})),e}var p=function(e){return e.Idle="Idle",e.Loading="Loading",e.ShopperIdentified="ShopperIdentified",e.OneTimePassword="OneTimePassword",e.Ready="Ready",e.Login="Login",e.NotAvailable="NotAvailable",e}({});class S{get shopperAccountFound(){return["Ready","ShopperIdentified"].includes(this.state)}get schemes(){return this.sdkLoader.schemes}updateStoreCookiesConsent(e){this.storeCookies=e}async initialize(){this.setState("Loading");try{this.sdks=await this.sdkLoader.load(this.environment),await this.initiateSdks();const{recognized:e=!1,idTokens:t=null}=await this.verifyIfShopperIsRecognized();if(e)return await this.getShopperProfile(t),void this.setState("Ready");if(!this.shopperIdentity)return void this.setState("NotAvailable");const{isEnrolled:i}=await this.verifyIfShopperIsEnrolled(this.shopperIdentity);if(i)return void this.setState("ShopperIdentified");this.setState("NotAvailable")}catch(e){e instanceof o&&"REQUEST_TIMEOUT"===(null==e?void 0:e.reason)||e instanceof c?this.handleTimeout(e):e instanceof o?console.warn(`Error at ClickToPayService # init: ${e.toString()}`):console.warn(e),this.setState("NotAvailable")}}subscribeOnStateChange(e){this.stateSubscriber=e}async startIdentityValidation(){if(!this.validationSchemeSdk)throw Error("startIdentityValidation: No ValidationSDK set for the validation process");try{const{maskedValidationChannel:e}=await this.validationSchemeSdk.initiateIdentityValidation();this.identityValidationData={maskedShopperContact:e.replace(/\*/g,"•"),selectedNetwork:s[this.validationSchemeSdk.schemeName]},this.setState("OneTimePassword")}catch(e){throw this.validationSchemeSdk=null,e}}async finishIdentityValidation(e){if(!this.validationSchemeSdk)throw Error("finishIdentityValidation: No ValidationSDK set for the validation process");const t=await this.validationSchemeSdk.completeIdentityValidation(e);await this.getShopperProfile([t.idToken]),this.setState("Ready"),this.validationSchemeSdk=null}async checkout(t){if(!t)throw Error("ClickToPayService # checkout: Missing card data");const o=this.sdks.find((e=>e.schemeName===t.scheme)),s=await o.checkout(m({srcDigitalCardId:t.srcDigitalCardId,srcCorrelationId:t.srcCorrelationId},t.isDcfPopupEmbedded&&{windowRef:window.frames[i]},this.storeCookies&&{complianceSettings:{complianceResources:[{complianceType:"REMEMBER_ME",uri:""}]}}));if("COMPLETE"!==s.dcfActionCode)throw new n("ERROR",`Checkout through Scheme DCF did not complete. DCF Action code received: ${s.dcfActionCode}`);return e(t,s,this.environment)}async logout(){if(!this.sdks)throw new n("ERROR","ClickToPayService is not initialized");try{const e=this.sdks.map((e=>e.unbindAppInstance()));await Promise.all(e)}catch(e){e instanceof o?console.warn(`Error at ClickToPayService # logout: ${e.toString()}`):console.warn(e)}this.shopperCards=null,this.identityValidationData=null,this.validationSchemeSdk=null,this.setState("Login")}verifyIfShopperIsEnrolled(e){const{shopperEmail:t}=e;return new Promise(((e,i)=>{const o=this.sdks.map((o=>l((()=>o.identityLookup({identityValue:t,type:"email"})),5e3,new c({source:"identityLookup",scheme:o.schemeName,isTimeoutTriggeredBySchemeSdk:!1})).then((t=>{t.consumerPresent&&!this.validationSchemeSdk&&(this.setSdkForPerformingShopperIdentityValidation(o),e({isEnrolled:!0}))})).catch((e=>{i(e)}))));Promise.allSettled(o).then((()=>{e({isEnrolled:!1})}))}))}setState(e){var t;this.state=e,null===(t=this.stateSubscriber)||void 0===t||t.call(this,this.state)}setSdkForPerformingShopperIdentityValidation(e){this.validationSchemeSdk=e}handleTimeout(e){var t;const i=e instanceof o?new c({source:e.source,scheme:e.scheme,isTimeoutTriggeredBySchemeSdk:!0}):e;var s,r,n,a,d,l;"visa"===i.scheme&&(i.setCorrelationId(null===(s=window.VISA_SDK)||void 0===s?void 0:s.correlationId),(null===(r=window.VISA_SDK)||void 0===r?void 0:r.correlationId)?null===(a=window.VISA_SDK)||void 0===a||null===(n=a.buildClientProfile)||void 0===n||n.call(a):null===(l=window.VISA_SDK)||void 0===l||null===(d=l.buildClientProfile)||void 0===d||d.call(l,this.schemesConfig.visa.srciDpaId));null===(t=this.onTimeout)||void 0===t||t.call(this,i)}async getShopperProfile(e){return new Promise(((i,o)=>{const s=this.sdks.map((t=>t.getSrcProfile(e)));Promise.allSettled(s).then((e=>{e.every(a)&&o(e[0].reason);const s=e.map(((e,t)=>d(e)&&u(m({},e.value),{scheme:this.sdks[t].schemeName}))).filter((e=>!!e));this.shopperCards=t(s),i()}))}))}verifyIfShopperIsRecognized(){return new Promise(((e,t)=>{const i=this.sdks.map((i=>l((()=>i.isRecognized()),5e3,new c({source:"isRecognized",scheme:i.schemeName,isTimeoutTriggeredBySchemeSdk:!1})).then((t=>{t.recognized&&e(t)})).catch((e=>{t(e)}))));Promise.allSettled(i).then((()=>{e({recognized:!1})}))}))}initiateSdks(){const e=this.sdks.map((e=>{const t=this.schemesConfig[e.schemeName];return l((()=>e.init(t,this.srciTransactionId)),5e3,new c({source:"init",scheme:e.schemeName,isTimeoutTriggeredBySchemeSdk:!1}))}));return Promise.all(e)}constructor(e,t,i,o,s){h(this,"sdkLoader",void 0),h(this,"schemesConfig",void 0),h(this,"shopperIdentity",void 0),h(this,"environment",void 0),h(this,"onTimeout",void 0),h(this,"srciTransactionId",r()),h(this,"sdks",void 0),h(this,"validationSchemeSdk",null),h(this,"stateSubscriber",void 0),h(this,"state","Idle"),h(this,"shopperCards",null),h(this,"identityValidationData",null),h(this,"storeCookies",!1),this.sdkLoader=t,this.schemesConfig=e,this.shopperIdentity=o,this.environment=i,this.onTimeout=s}}export{p as CtpState,S as default}; //# sourceMappingURL=ClickToPayService.js.map