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) 3.34 kB
import{createElement as t}from"../../external/preact/dist/preact.js";import{UIElement as e}from"../internal/UIElement/UIElement.js";import r from"./components/GiftcardComponent.js";import s from"../internal/PayButton/PayButton.js";import n from"../../core/Errors/AdyenCheckoutError.js";import{TxVariants as a}from"../tx-variants.js";function i(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}class o extends e{formatProps(t){return{...t?.configuration,...t}}formatData(){return{paymentMethod:{type:this.constructor.type,brand:this.props.brand,encryptedCardNumber:this.state.data?.encryptedCardNumber,encryptedSecurityCode:this.state.data?.encryptedSecurityCode}}}get isValid(){return!!this.state.isValid}get icon(){return this.props.brandsConfiguration[this.props.brand]?.icon||this.props.icon||this.resources.getImage()(this.props.brand)}get displayName(){return this.props.brandsConfiguration[this.props.brand]?.name||this.props.name}setBalanceCheckError(t){this.componentRef?.setBalanceCheckErrors?.(t)}balanceCheck(){return this.onBalanceCheck()}submit(){this.balanceCheck()}makeSubmitCall(){super.submit()}componentToRender(){return t(r,{ref:t=>{this.componentRef=t},...this.props,handleKeyPress:this.handleKeyPress,showPayButton:this.props.showPayButton,onChange:this.setState,makeBalanceCheck:()=>this.onBalanceCheck(),makePayment:()=>this.makeSubmitCall(),payButton:this.payButton,onSubmitAnalytics:this.submitAnalytics})}constructor(...e){super(...e),i(this,"componentRef",void 0),i(this,"handleBalanceCheck",t=>this.props.onBalanceCheck?new Promise((e,r)=>{this.props.onBalanceCheck(e,r,t)}):this.props.session?this.props.session.checkBalance(t):void 0),i(this,"onOrderRequest",t=>this.props.onOrderRequest?new Promise((e,r)=>{this.props.onOrderRequest(e,r,t)}):this.props.session?this.props.session.createOrder():void 0),i(this,"onBalanceCheck",()=>{if(!this.isValid)return void this.showValidation();if(!(this.props.session||this.props.onBalanceCheck))return super.submit();this.setStatus("loading"),this.handleBalanceCheck(this.formatData()).then(({balance:t,transactionLimit:e={}})=>{if(this.setBalanceCheckError(null),!t)throw new Error("card-error");if(t?.currency!==this.props.amount?.currency)throw new Error("currency-error");if(t?.value<=0)throw new Error("no-balance");return this.props.amount.value>t.value||this.props.amount.value>e.value?this.props.order?this.makeSubmitCall():this.onOrderRequest(this.data).then(t=>(this.setState({order:{orderData:t.orderData,pspReference:t.pspReference}}),this.makeSubmitCall())):this.handleOnRequiringConfirmation(t,e)}).catch(t=>{this.setBalanceCheckError(t?.message),t instanceof n?this.handleError(t):this.handleError(new n("ERROR",t))})}),i(this,"handleOnRequiringConfirmation",(t,e)=>{if(this.componentRef.setBalance({balance:t,transactionLimit:e}),this.setStatus("ready"),!this.props.showPayButton){if(!this.props.onRequiringConfirmation)throw new n("IMPLEMENTATION_ERROR","onRequiringConfirmation must be provided");return new Promise((t,e)=>{this.props.onRequiringConfirmation(t,e)}).then(()=>this.makeSubmitCall()).catch(()=>{})}}),i(this,"payButton",e=>t(s,e))}}i(o,"type",a.giftcard),i(o,"defaultProps",{brandsConfiguration:{}});export{o as GiftcardElement,o as default}; //# sourceMappingURL=Giftcard.js.map