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) 4.21 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";function o(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function s(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},a=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(a=a.concat(Object.getOwnPropertySymbols(n).filter((function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable})))),a.forEach((function(e){o(t,e,n[e])}))}return t}class r{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){var e;if(!!!(null===(e=t.profileData)||void 0===e?void 0:e.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:s({},t&&{fastlaneConfiguration:s({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(s({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(o(this,"clientKey",void 0),o(this,"checkoutShopperURL",void 0),o(this,"locale",void 0),o(this,"forceConsentDetails",void 0),o(this,"fastlaneSdk",void 0),o(this,"latestShopperDetails",void 0),o(this,"fastlaneSessionId",void 0),!(null==e?void 0:e.environment))throw new i("IMPLEMENTATION_ERROR","FastlaneSDK: 'environment' property is required");if(!(null==e?void 0: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{r as default}; //# sourceMappingURL=FastlaneSDK.js.map