@adyen/adyen-web
Version:
[](https://www.npmjs.com/package/@adyen/adyen-web)
3 lines (2 loc) • 3.36 kB
JavaScript
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{CoreProvider as s}from"../../core/Context/CoreProvider.js";import n from"../internal/PayButton/PayButton.js";import o from"../../core/Errors/AdyenCheckoutError.js";import{TxVariants as i}from"../tx-variants.js";function a(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}class h 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}balanceCheck(){return this.onBalanceCheck()}submit(){if(!this.isValid)return this.showValidation(),!1;this.balanceCheck()}makeSubmitCall(){super.submit()}render(){return t(s,{i18n:this.props.i18n,loadingContext:this.props.loadingContext,resources:this.resources},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}))}constructor(...e){super(...e),a(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)),a(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)),a(this,"onBalanceCheck",(()=>{if(!(this.props.session||this.props.onBalanceCheck))return super.submit();this.setStatus("loading"),this.handleBalanceCheck(this.formatData()).then((({balance:t,transactionLimit:e={}})=>{if(!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.setStatus(t?.message||"error"),this.props.onError&&(t instanceof o?this.handleError(t):this.handleError(new o("ERROR",t)))}))})),a(this,"handleOnRequiringConfirmation",((t,e)=>{if(this.componentRef.setBalance({balance:t,transactionLimit:e}),this.setStatus("ready"),!this.props.showPayButton){if(!this.props.onRequiringConfirmation)throw new o("IMPLEMENTATION_ERROR","onRequiringConfirmation must be provided");return new Promise(((t,e)=>{this.props.onRequiringConfirmation(t,e)})).then((()=>this.makeSubmitCall())).catch((()=>{}))}})),a(this,"payButton",(e=>t(n,e)))}}a(h,"type",i.giftcard),a(h,"defaultProps",{brandsConfiguration:{}});export{h as GiftcardElement,h as default};
//# sourceMappingURL=Giftcard.js.map