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) 10.6 kB
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 h}from"./Errors/SRPanel.js";import d from"./core.registry.js";import{sanitizeResponse as p,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{defaultProps as g}from"./core.defaultProps.js";import{resolveEnvironments as b}from"./Environment/Environment.js";import{LIBRARY_BUNDLE_TYPE as w,LIBRARY_VERSION as C}from"./config.js";import{AnalyticsLogEvent as j,LogEventType as A}from"./Analytics/events/AnalyticsLogEvent.js";import O from"./Errors/CancelError.js";import{AnalyticsService as P}from"./Analytics/AnalyticsService.js";import{AnalyticsEventQueue as E}from"./Analytics/AnalyticsEventQueue.js";import{isAmountValid as T}from"../utils/amount-util.js";import{LanguageService as M}from"../language/LanguageService.js";function I(t,e,o){return e in t?Object.defineProperty(t,e,{value:o,enumerable:!0,configurable:!0,writable:!0}):t[e]=o,t}function x(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){I(t,e,o[e])})}return t}function R(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 k(t,e){if(null==t)return{};var o,n,i,s={};if("undefined"!=typeof Reflect&&Reflect.ownKeys){for(o=Reflect.ownKeys(Object(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 U{static setBundleType(t){U.metadata.bundleType=t}static register(...t){d.add(...t)}register(...t){d.add(...t)}getComponent(t){return d.getComponent(t)}async initialize(){return await this.initializeCore(),this.validateCoreConfiguration(),this.createCoreModules(),this.assignLocaleToCore(),this.requestAnalyticsAttemptId(),await this.requestTranslations(),this}async initializeCore(){return this.session?this.session.setupSession(this.options).then(t=>{const{amount:e,shopperLocale:o,countryCode:n,paymentMethods:i}=t,s=k(t,["amount","shopperLocale","countryCode","paymentMethods"]);return this.setOptions(R(x({},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))}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.")}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(p).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 O||(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 j({type:A.action,subType:j.getSubtypeFromActionType(t.type),message:`${o} action was handled by the SDK`,component:o});this.modules.analytics.sendAnalytics(i);const s=x({},this.getCorePropsForComponent(),e);return n(this,d,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=x({},(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){T(t)?!e||T(e)?(this.setOptions(x({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 R(x({},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)}createCoreModules(){var o,n,s;this.modules||(this.modules=Object.freeze({risk:new e(this,R(x({},this.options),{loadingContext:this.loadingContext})),analytics:new i({eventQueue:new E,service:new P({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,customTranslations:this.options.translations,service:new M({cdnUrl:this.cdnTranslationsUrl,sdkVersion:U.metadata.version}),onError:null===(s=this.options)||void 0===s?void 0:s.onError}),srPanel:new h(this,x({},this.options.srConfig))}))}assignLocaleToCore(){this.options.locale=this.modules.i18n.locale}async requestTranslations(){try{await this.modules.i18n.requestTranslations()}catch(t){console.warn("Core - requestTranslations(): Something went wrong.",t)}}async requestAnalyticsAttemptId(){var t;await this.modules.analytics.setUp(x({locale:this.options.locale},(null===(t=this.session)||void 0===t?void 0:t.id)&&{sessionId:this.session.id}))}constructor(t){var e;I(this,"session",void 0),I(this,"paymentMethodsResponse",void 0),I(this,"modules",void 0),I(this,"options",void 0),I(this,"analyticsContext",void 0),I(this,"loadingContext",void 0),I(this,"cdnImagesUrl",void 0),I(this,"cdnTranslationsUrl",void 0),I(this,"components",[]),I(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 O("Handled by afterAdditionalDetails"))}return Promise.resolve(t)}),I(this,"remove",t=>(this.components=this.components.filter(e=>e._id!==t._id),t.unmount(),this)),I(this,"setOptions",t=>{var e,o;this.options=R(x({},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(x({},g,t));const{apiUrl:o,analyticsUrl:n,cdnImagesUrl:i,cdnTranslationsUrl:s}=b(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,h;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===(h=this.options.clientKey)||void 0===h?void 0:h.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=U.metadata)}}I(U,"metadata",{version:C,bundleType:w}),I(U,"registry",d);export{U as default}; //# sourceMappingURL=core.js.map