@adyen/adyen-web
Version:
[](https://www.npmjs.com/package/@adyen/adyen-web)
3 lines (2 loc) • 10.9 kB
JavaScript
import{Language as t}from"../language/Language.js";import e from"./RiskModule/RiskModule.js";import o from"./ProcessResponse/PaymentMethods/PaymentMethods.js";import n from"./ProcessResponse/PaymentAction/PaymentAction.js";import i from"./Analytics/Analytics.js";import{processGlobalOptions as s,assertConfigurationPropertiesAreValid as r}from"./utils.js";import a from"./CheckoutSession/CheckoutSession.js";import{hasOwnProperty as l}from"../utils/hasOwnProperty.js";import{Resources as c}from"./Context/Resources.js";import{SRPanel as p}from"./Errors/SRPanel.js";import h from"./core.registry.js";import{sanitizeResponse as d,verifyPaymentDidNotFail as m,cleanupFinalResult as u}from"../components/internal/UIElement/utils.js";import y,{IMPLEMENTATION_ERROR as f}from"./Errors/AdyenCheckoutError.js";import{THREEDS2_FULL as v}from"../components/ThreeDS2/constants.js";import{DEFAULT_LOCALE as g}from"../language/constants.js";import b from"./Services/get-translations.js";import{defaultProps as w}from"./core.defaultProps.js";import{formatLocale as j,formatCustomTranslations as C}from"../language/utils.js";import{resolveEnvironments as A}from"./Environment/Environment.js";import{LIBRARY_BUNDLE_TYPE as O,LIBRARY_VERSION as P}from"./config.js";import{AnalyticsLogEvent as E,LogEventType as M}from"./Analytics/events/AnalyticsLogEvent.js";import R from"./Errors/CancelError.js";import{AnalyticsService as I}from"./Analytics/AnalyticsService.js";import{AnalyticsEventQueue as T}from"./Analytics/AnalyticsEventQueue.js";import{isAmountValid as x}from"../utils/amount-util.js";function k(t,e,o){return e in t?Object.defineProperty(t,e,{value:o,enumerable:!0,configurable:!0,writable:!0}):t[e]=o,t}function D(t){for(var e=1;e<arguments.length;e++){var o=null!=arguments[e]?arguments[e]:{},n=Object.keys(o);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(o).filter(function(t){return Object.getOwnPropertyDescriptor(o,t).enumerable}))),n.forEach(function(e){k(t,e,o[e])})}return t}function U(t,e){return e=null!=e?e:{},Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(e)):function(t){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(t);e.push.apply(e,o)}return e}(Object(e)).forEach(function(o){Object.defineProperty(t,o,Object.getOwnPropertyDescriptor(e,o))}),t}function S(t,e){if(null==t)return{};var o,n,i,s={};if("undefined"!=typeof Reflect&&Reflect.ownKeys){for(o=Reflect.ownKeys(t),i=0;i<o.length;i++)n=o[i],e.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(t,n)&&(s[n]=t[n]);return s}if(s=function(t,e){if(null==t)return{};var o,n,i={},s=Object.getOwnPropertyNames(t);for(n=0;n<s.length;n++)o=s[n],e.indexOf(o)>=0||Object.prototype.propertyIsEnumerable.call(t,o)&&(i[o]=t[o]);return i}(t,e),Object.getOwnPropertySymbols)for(o=Object.getOwnPropertySymbols(t),i=0;i<o.length;i++)n=o[i],e.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(t,n)&&(s[n]=t[n]);return s}class F{static setBundleType(t){F.metadata.bundleType=t}static register(...t){h.add(...t)}register(...t){h.add(...t)}getComponent(t){return h.getComponent(t)}async initialize(){return await this.initializeCore(),this.validateCoreConfiguration(),await this.createCoreModules(),await this.requestAnalyticsAttemptId(),this}async initializeCore(){return this.session?this.session.setupSession(this.options).then(t=>{const{amount:e,shopperLocale:o,countryCode:n,paymentMethods:i}=t,s=S(t,["amount","shopperLocale","countryCode","paymentMethods"]);return this.setOptions(U(D({},s),{amount:this.options.order?this.options.order.remainingAmount:e,locale:this.options.locale||o,countryCode:this.options.countryCode||n})),this.createPaymentMethodsList(i),this}).catch(t=>(this.options.onError&&this.options.onError(t),Promise.reject(t))):(this.createPaymentMethodsList(),Promise.resolve(this))}async fetchLocaleTranslations(){try{return await b(this.cdnTranslationsUrl,F.metadata.version,this.options.locale)}catch(i){var t,e,o,n;i instanceof y?null===(t=(e=this.options).onError)||void 0===t||t.call(e,i):null===(o=(n=this.options).onError)||void 0===o||o.call(n,new y("ERROR","Failed to fetch translation",{cause:i}))}}validateCoreConfiguration(){if(this.options.paymentMethodsConfiguration&&console.warn('WARNING: "paymentMethodsConfiguration" is supported only by Drop-in.'),!this.options.countryCode)throw new y(f,"You must specify a countryCode when initializing checkout.");this.options.locale||this.setOptions({locale:g}),this.options.locale=j(this.options.locale),this.options.translations=C(this.options.translations)}submitDetails(t){let e=null;var o,n;(this.options.onAdditionalDetails&&(e=new Promise((e,o)=>{this.options.onAdditionalDetails({data:t},void 0,{resolve:e,reject:o})})),this.session&&(e=this.session.submitDetails(t).catch(t=>{var e,o;return null===(e=(o=this.options).onError)||void 0===e||e.call(o,t),Promise.reject(t)})),e)?e.then(d).then(m).then(this.afterAdditionalDetails).then(t=>{var e,o;u(t),null===(e=(o=this.options).onPaymentCompleted)||void 0===e||e.call(o,t)}).catch(t=>{var e,o;t instanceof R||(u(t),null===(e=(o=this.options).onPaymentFailed)||void 0===e||e.call(o,t))}):null===(o=(n=this.options).onError)||void 0===o||o.call(n,new y("IMPLEMENTATION_ERROR",'It can not submit the details. The callback "onAdditionalDetails" or the Session is not setup correctly.'))}createFromAction(t,e={}){if(!t||!t.type){if(l(t,"action")&&l(t,"resultCode"))throw new Error('createFromAction::Invalid Action - the passed action object itself has an "action" property and a "resultCode": have you passed in the whole response object by mistake?');throw new Error('createFromAction::Invalid Action - the passed action object does not have a "type" property')}if(t.type){const o=t.type===v?`${t.type}${t.subtype}`:t.paymentMethodType,i=new E({type:M.action,subType:E.getSubtypeFromActionType(t.type),message:`${o} action was handled by the SDK`,component:o});this.modules.analytics.sendAnalytics(i);const s=D({},this.getCorePropsForComponent(),e);return n(this,h,t,s)}return this.handleCreateError()}update(t={},{shouldReinitializeCheckout:e=!0}={}){if(e)return this.setOptions(t),this.initialize().then(()=>(this.components.forEach(e=>{var o;const n=D({},(null===(o=t.order)||void 0===o?void 0:o.remainingAmount)?{amount:t.order.remainingAmount}:{amount:this.options.amount},this.session&&{session:this.session},t);e.update(n)}),this));const{amount:o,secondaryAmount:n}=t;return(o||n)&&this.triggerAmountUpdate(o,n),Promise.resolve(this)}triggerAmountUpdate(t,e){x(t)?!e||x(e)?(this.setOptions(D({amount:t},e&&{secondaryAmount:e})),this.components.forEach(o=>{o.updateAmount(t,e)})):console.warn("Core update(): Update canceled. Invalid secondary amount object"):console.warn("Core update(): Update canceled. Invalid amount object")}getCorePropsForComponent(){return U(D({},s(this.options)),{core:this,i18n:this.modules.i18n,modules:this.modules,session:this.session,loadingContext:this.loadingContext,cdnContext:this.cdnImagesUrl,createFromAction:this.createFromAction})}storeElementReference(t){t&&this.components.push(t)}handleCreateError(t){var e;const o=null!==(e=null==t?void 0:t.name)&&void 0!==e?e:"The passed payment method",n=t?`${o} is not a valid Checkout Component. What was passed as a txVariant was: ${JSON.stringify(t)}. Check if this payment method is configured in the Backoffice or if the txVariant is a valid one`:"No Payment Method component was passed";throw new Error(n)}createPaymentMethodsList(t){this.paymentMethodsResponse=new o(this.options.paymentMethodsResponse||t,this.options)}async createCoreModules(){var o,n;if(this.modules)return;const s=await this.fetchLocaleTranslations();this.modules=Object.freeze({risk:new e(this,U(D({},this.options),{loadingContext:this.loadingContext})),analytics:new i({eventQueue:new T,service:new I({analyticsContext:this.analyticsContext,clientKey:this.options.clientKey}),enabled:null===(o=this.options.analytics)||void 0===o?void 0:o.enabled,analyticsData:null===(n=this.options.analytics)||void 0===n?void 0:n.analyticsData}),resources:new c(this.cdnImagesUrl),i18n:new t({locale:this.options.locale,translations:s,customTranslations:this.options.translations}),srPanel:new p(this,D({},this.options.srConfig))})}async requestAnalyticsAttemptId(){var t;await this.modules.analytics.setUp(D({locale:this.options.locale},(null===(t=this.session)||void 0===t?void 0:t.id)&&{sessionId:this.session.id}))}constructor(t){var e;k(this,"session",void 0),k(this,"paymentMethodsResponse",void 0),k(this,"modules",void 0),k(this,"options",void 0),k(this,"analyticsContext",void 0),k(this,"loadingContext",void 0),k(this,"cdnImagesUrl",void 0),k(this,"cdnTranslationsUrl",void 0),k(this,"components",[]),k(this,"afterAdditionalDetails",t=>{if(this.options.afterAdditionalDetails&&(null==t?void 0:t.action)){const e=this.createFromAction(t.action);return this.options.afterAdditionalDetails(e),Promise.reject(new R("Handled by afterAdditionalDetails"))}return Promise.resolve(t)}),k(this,"remove",t=>(this.components=this.components.filter(e=>e._id!==t._id),t.unmount(),this)),k(this,"setOptions",t=>{var e,o;this.options=U(D({},this.options,t),{locale:(null==t?void 0:t.locale)||(null===(e=this.options)||void 0===e?void 0:e.locale),environment:String.prototype.toLowerCase.apply((null==t?void 0:t.environment)||(null===(o=this.options)||void 0===o?void 0:o.environment))})}),r(t),this.createFromAction=this.createFromAction.bind(this),this.update=this.update.bind(this),this.setOptions(D({},w,t));const{apiUrl:o,analyticsUrl:n,cdnImagesUrl:i,cdnTranslationsUrl:s}=A(this.options.environment,this.options._environmentUrls);this.loadingContext=o,this.analyticsContext=n,this.cdnImagesUrl=i,this.cdnTranslationsUrl=s,this.session=this.options.session&&new a(this.options.session,this.options.clientKey,this.loadingContext);const l=null===(e=this.options.clientKey)||void 0===e?void 0:e.substring(0,4);var c,p;if(("test"===l||"live"===l)&&!this.loadingContext.includes(l))throw new y("IMPLEMENTATION_ERROR",`Error: you are using a ${l} clientKey against the ${(null===(c=this.options._environmentUrls)||void 0===c?void 0:c.api)||this.options.environment} environment`);"pub."===l&&console.debug(`The value you are passing as your "clientKey" looks like an originKey (${null===(p=this.options.clientKey)||void 0===p?void 0:p.substring(0,12)}..). Although this is supported it is not the recommended way to integrate. To generate a clientKey, see the documentation (https://docs.adyen.com/development-resources/client-side-authentication/migrate-from-origin-key-to-client-key/) for more details.`);this.options.exposeLibraryMetadata&&(window.AdyenWebMetadata=F.metadata)}}k(F,"metadata",{version:P,bundleType:O}),k(F,"registry",h);export{F as default};
//# sourceMappingURL=core.js.map