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