UNPKG

@adyen/adyen-web

Version:

[![npm](https://img.shields.io/npm/v/@adyen/adyen-web.svg)](https://www.npmjs.com/package/@adyen/adyen-web)

3 lines (2 loc) 6.17 kB
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";import s from"../../core/Analytics/Analytics.js";import{InfoEventType as o,AnalyticsInfoEvent as r}from"../../core/Analytics/events/AnalyticsInfoEvent.js";import{AnalyticsEventQueue as c}from"../../core/Analytics/AnalyticsEventQueue.js";import{AnalyticsService as l}from"../../core/Analytics/AnalyticsService.js";function h(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}class d{async initialize(){this.analytics.setUp({checkoutStage:"precheckout"});const t=await this.requestClientToken();return await this.fetchSdk(t.value,t.clientId),await this.initializeFastlaneInstance(),this.trackEvent(o.Initialized),this}destroy(){document.removeEventListener("visibilitychange",this.handlePageVisibilityChanges),this.analytics.flush()}async authenticate(t){if(!this.fastlaneSdk)throw new i("IMPLEMENTATION_ERROR","authenticate(): Fastlane SDK is not initialized");this.trackEvent(o.LookupStarted);try{const{customerContextId:e}=await this.fastlaneSdk.identity.lookupCustomerByEmail(t);if(e){this.latestShopperDetails={email:t,customerId:e},this.trackEvent(o.OtpStarted);const n=await this.fastlaneSdk.identity.triggerAuthenticationFlow(e);return this.trackEvent(this.getOtpAnalyticsSubtype(n.authenticationState)),n}return this.trackEvent(o.LookupUserNotFound),{authenticationState:"not_found",profileData:void 0}}catch(t){throw new i("ERROR","Fastlane SDK: An error occurred during the authentication flow.",{cause:t})}}async getComponentConfiguration(t){if(!t)throw new i("IMPLEMENTATION_ERROR","FastlaneSDK: you must pass the authentication result to get the component configuration");this.analytics.flush();const e="succeeded"===t.authenticationState,n=!!t.profileData?.card,a=!!this.latestShopperDetails?.email;return e&&n&&a?this.createFastlaneComponentConfiguration(t):this.createCardComponentConfiguration()}async showShippingAddressSelector(){if(!this.fastlaneSdk)throw new i("IMPLEMENTATION_ERROR","showShippingAddressSelector(): Fastlane SDK is not initialized");this.trackEvent(o.AddressSelectorClicked);try{const t=await this.fastlaneSdk.profile.showShippingAddressSelector();return t.selectionChanged&&this.trackEvent(o.AddressChanged),this.trackEvent(o.AddressSelectorClosed),t}catch(t){throw new i("ERROR","Fastlane SDK: An error occurred when showing the shipping address selector",{cause:t})}}async mountWatermark(t,e={includeAdditionalInfo:!0}){if(!this.fastlaneSdk)throw new i("IMPLEMENTATION_ERROR","mountWatermark(): Fastlane SDK is not initialized");try{(await this.fastlaneSdk.FastlaneWatermarkComponent(e)).render(t)}catch(t){throw new i("ERROR","Fastlane SDK: An error occurred when rendering the watermark",{cause:t})}}requestClientToken(){return e(this.checkoutShopperURL,this.clientKey)}async fetchSdk(t,e){const n=new URL("https://www.paypal.com/sdk/js");n.searchParams.set("client-id",e),n.searchParams.set("components","buttons,fastlane");const i=new a({src:n.href,component:"fastlane",dataAttributes:{sdkClientToken:t},analytics:this.analytics});await i.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.fastlaneLocale),this.fetchSessionIdAsync()}catch(t){throw new i("ERROR","Fastlane SDK: Failed to initialize fastlane using the window.paypal.Fastlane constructor",{cause:t})}}createFastlaneComponentConfiguration(t){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()}}}async createCardComponentConfiguration(){const t=await this.fetchConsentDetails(),e={};return t&&(e.fastlaneConfiguration={fastlaneSessionId:this.fastlaneSessionId,...t}),{paymentType:"card",configuration:e}}trackEvent(t){const e=new r({type:t,component:"fastlane"});this.analytics.sendAnalytics(e)}getOtpAnalyticsSubtype(t){switch(t){case"succeeded":return o.OtpSucceeded;case"canceled":return o.OtpCanceled;case"failed":default:return o.OtpFailed;case"not_found":return o.LookupUserNotFound}}constructor(e){if(h(this,"clientKey",void 0),h(this,"checkoutShopperURL",void 0),h(this,"fastlaneLocale",void 0),h(this,"forceConsentDetails",void 0),h(this,"analytics",void 0),h(this,"fastlaneSdk",void 0),h(this,"latestShopperDetails",void 0),h(this,"fastlaneSessionId",void 0),h(this,"handlePageVisibilityChanges",()=>{"hidden"===document.visibilityState&&this.analytics.flush()}),!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,analyticsUrl:o}=t(e.environment);this.checkoutShopperURL=a,this.clientKey=e.clientKey,this.forceConsentDetails=e.forceConsentDetails||!1,this.fastlaneLocale=n(e.locale||"en-US"),this.analytics=new s({eventQueue:new c,service:new l({analyticsContext:o,clientKey:this.clientKey}),enabled:e.analytics?.enabled}),document.addEventListener("visibilitychange",this.handlePageVisibilityChanges)}}export{d as default}; //# sourceMappingURL=FastlaneSDK.js.map