@atlas-kitchen/adyen-web
Version:
3 lines (2 loc) • 3.81 kB
JavaScript
import{resolveEnvironments as t}from"../../core/Environment/Environment.js";import e from"./services/request-fastlane-token.js";import{convertAdyenLocaleToFastlaneLocale as n}from"./utils/convert-locale.js";import a from"../../utils/Script.js";import i from"../../core/Errors/AdyenCheckoutError.js";function s(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}class o{async initialize(){const t=await this.requestClientToken();return await this.fetchSdk(t.value,t.clientId),await this.initializeFastlaneInstance(),this}async authenticate(t){if(!this.fastlaneSdk)throw new i("IMPLEMENTATION_ERROR","authenticate(): Fastlane SDK is not initialized");const{customerContextId:e}=await this.fastlaneSdk.identity.lookupCustomerByEmail(t);return e?(this.latestShopperDetails={email:t,customerId:e},this.fastlaneSdk.identity.triggerAuthenticationFlow(e)):{authenticationState:"not_found",profileData:void 0}}async getComponentConfiguration(t){if(!t)throw new i("IMPLEMENTATION_ERROR","FastlaneSDK: you must pass the authentication result to get the component configuration");if("succeeded"===t.authenticationState){if(!!!t.profileData?.card)throw new i("ERROR","getComponentConfiguration(): There is no card associated with the authenticated profile");return{paymentType:"fastlane",configuration:{fastlaneSessionId:this.fastlaneSessionId,email:this.latestShopperDetails.email,tokenId:t.profileData.card.id,lastFour:t.profileData.card.paymentSource.card.lastDigits,brand:t.profileData.card.paymentSource.card.brand.toLowerCase()}}}{const t=await this.fetchConsentDetails();return{paymentType:"card",configuration:{...t&&{fastlaneConfiguration:{fastlaneSessionId:this.fastlaneSessionId,...t}}}}}}showShippingAddressSelector(){return this.fastlaneSdk.profile.showShippingAddressSelector()}async mountWatermark(t,e={includeAdditionalInfo:!0}){(await this.fastlaneSdk.FastlaneWatermarkComponent(e)).render(t)}requestClientToken(){return e(this.checkoutShopperURL,this.clientKey)}async fetchSdk(t,e){const n=new a(`https://www.paypal.com/sdk/js?client-id=${e}&components=buttons,fastlane`,"body",{},{sdkClientToken:t});await n.load()}async fetchSessionIdAsync(){try{const{sessionId:t}=await this.fastlaneSdk.identity.getSession();this.fastlaneSessionId=t}catch(t){console.warn("Fastlane SDK: Failed to fetch session ID",t)}}async fetchConsentDetails(){try{const t=await this.fastlaneSdk.ConsentComponent();return await t.getRenderState()}catch(t){throw new i("ERROR","fetchConsentDetails(): failed to fetch consent details",{cause:t})}}async initializeFastlaneInstance(){try{this.fastlaneSdk=await window.paypal.Fastlane({intendedExperience:"externalProcessorCustomConsent",...this.forceConsentDetails&&{metadata:{geoLocOverride:"US"}}}),this.fastlaneSdk.setLocale(this.locale),this.fetchSessionIdAsync()}catch(t){throw new i("ERROR","Fastlane SDK: Failed to initialize fastlane using the window.paypal.Fastlane constructor",{cause:t})}}constructor(e){if(s(this,"clientKey",void 0),s(this,"checkoutShopperURL",void 0),s(this,"locale",void 0),s(this,"forceConsentDetails",void 0),s(this,"fastlaneSdk",void 0),s(this,"latestShopperDetails",void 0),s(this,"fastlaneSessionId",void 0),!e?.environment)throw new i("IMPLEMENTATION_ERROR","FastlaneSDK: 'environment' property is required");if(!e?.clientKey)throw new i("IMPLEMENTATION_ERROR","FastlaneSDK: 'clientKey' property is required");e.forceConsentDetails&&e.environment.includes("live")&&console.warn("Fastlane SDK: 'forceConsentDetails' should not be used on 'live' environment");const{apiUrl:a}=t(e.environment);this.checkoutShopperURL=a,this.clientKey=e.clientKey,this.forceConsentDetails=e.forceConsentDetails||!1,this.locale=n(e.locale||"en-US")}}export{o as default};
//# sourceMappingURL=FastlaneSDK.js.map