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.57 kB
import{createElement as t}from"../../external/preact/dist/preact.js";import{UIElement as e}from"../internal/UIElement/UIElement.js";import n from"./defaultProps.js";import{DropinComponent as o}from"./components/DropinComponent.js";import s from"./elements/createElements.js";import i from"./elements/createStoredElements.js";import a from"./elements/createInstantPaymentElements.js";import{hasOwnProperty as r}from"../../utils/hasOwnProperty.js";import h from"./elements/splitPaymentMethods.js";import{TxVariants as d}from"../tx-variants.js";function m(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}const p=["paywithgoogle","googlepay","applepay"];class l extends e{reportIntegrationFlavor(){this.analytics.sendFlavor("dropin")}storeElementRefOnCore(){this.core.storeElementReference(this)}formatProps(t){return{...super.formatProps(t),instantPaymentTypes:Array.from(new Set(t.instantPaymentTypes)).filter(t=>p.includes(t))}}get isValid(){return!!this.dropinRef&&!!this.dropinRef.state.activePaymentMethod&&!!this.dropinRef.state.activePaymentMethod.isValid}showValidation(){return this.dropinRef.state.activePaymentMethod&&this.dropinRef.state.activePaymentMethod.showValidation(),this}setStatus(t,e={}){return this.dropinRef?.setStatus(t,e),this}get activePaymentMethod(){return this.dropinRef?.state||this.dropinRef?.state.activePaymentMethod?this.dropinRef.state.activePaymentMethod:null}get data(){return this.activePaymentMethod?this.dropinRef.state.activePaymentMethod.data:null}displayFinalAnimation(t){this.props.disableFinalAnimation||this.dropinRef?.setStatus(t)}submit(){if(!this.activePaymentMethod)throw new Error("No active payment method.");this.activePaymentMethod.isValid||this.activePaymentMethod.showValidation(),this.activePaymentMethod.isInstantPayment&&this.closeActivePaymentMethod(),this.activePaymentMethod.submit()}updateAmount(t,e){this.props={...this.props,...t&&{amount:t},...e&&{secondaryAmount:e}},this.amountProviderRef.current?.update(t,e),this.paymentMethodElements.forEach(n=>{n.updateAmount(t,e)})}handleElementsCreated(t){t&&0!==t.length||(this.paymentMethodElements=[]),this.paymentMethodElements=t}handleAction(t,e={}){if(!t||!t.type){if(r(t,"action")&&r(t,"resultCode"))throw new Error('handleAction::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('handleAction::Invalid Action - the passed action object does not have a "type" property')}if("redirect"!==t.type&&this.activePaymentMethod?.updateWithAction)return this.activePaymentMethod.updateWithAction(t);this.elementRef instanceof l&&(e={...this.elementRef.activePaymentMethod?.props,...e});const n=this.core.createFromAction(t,{...e,elementRef:this.elementRef,isDropin:!0});return n?(this.setStatus(n.props.statusType,{component:n}),this.componentFromAction=n,this):null}closeActivePaymentMethod(){this.dropinRef.closeActivePaymentMethod()}handleKeyPress(t){if("Enter"===t.key||"Enter"===t.code){if("radio"===document?.activeElement?.getAttribute("role"))return;super.handleKeyPress(t)}}onEnterKeyPressed(t,e){const n=this.activePaymentMethod??e;this.activePaymentMethod?.onEnterKeyPressed(t,n)}componentToRender(){return t(o,{...this.props,core:this.core,elementRef:this.elementRef,onCreateElements:this.handleCreate,onElementsCreated:this.handleElementsCreated,ref:t=>{this.dropinRef=t}})}constructor(t,e){super(t,e),m(this,"dropinRef",null),m(this,"paymentMethodsConfiguration",void 0),m(this,"componentFromAction",void 0),m(this,"paymentMethodElements",[]),m(this,"handleCreate",()=>{const{paymentMethodsConfiguration:t,showStoredPaymentMethods:e,showPaymentMethods:n,instantPaymentTypes:o}=this.props,{paymentMethods:r,storedPaymentMethods:d,instantPaymentMethods:m,fastlanePaymentMethod:p}=h(this.core.paymentMethodsResponse,o),l={elementRef:this.elementRef,isDropin:!0},c=n?s(r,t,l,this.core):[],y=a(m,t,l,this.core);return[e?i(this.props.filterStoredPaymentMethods?.(d)??d,t,l,this.core):[],c,y,p?s([p],t,l,this.core):[]]}),m(this,"handleOrder",({order:t})=>{this.updateParent({order:t})}),this.submit=this.submit.bind(this),this.handleAction=this.handleAction.bind(this),this.handleElementsCreated=this.handleElementsCreated.bind(this),this.props.paymentMethodComponents.forEach(t=>this.core.register(t)),this.paymentMethodsConfiguration=this.props.paymentMethodsConfiguration||{}}}m(l,"type",d.dropin),m(l,"defaultProps",n);export{l as default}; //# sourceMappingURL=Dropin.js.map