@adyen/adyen-web
Version:
[](https://www.npmjs.com/package/@adyen/adyen-web)
3 lines (2 loc) • 4.31 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 n}from"../../core/Context/CoreProvider.js";import o from"../internal/PayButton/PayButton.js";import s 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}function p(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter((function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable})))),n.forEach((function(e){a(t,e,r[e])}))}return t}function c(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 r=Object.getOwnPropertySymbols(t);e.push.apply(e,r)}return e}(Object(e)).forEach((function(r){Object.defineProperty(t,r,Object.getOwnPropertyDescriptor(e,r))})),t}class h extends e{formatProps(t){return p({},null==t?void 0:t.configuration,t)}formatData(){var t,e;return{paymentMethod:{type:this.constructor.type,brand:this.props.brand,encryptedCardNumber:null===(t=this.state.data)||void 0===t?void 0:t.encryptedCardNumber,encryptedSecurityCode:null===(e=this.state.data)||void 0===e?void 0:e.encryptedSecurityCode}}}get isValid(){return!!this.state.isValid}get icon(){var t;return(null===(t=this.props.brandsConfiguration[this.props.brand])||void 0===t?void 0:t.icon)||this.props.icon||this.resources.getImage()(this.props.brand)}get displayName(){var t;return(null===(t=this.props.brandsConfiguration[this.props.brand])||void 0===t?void 0:t.name)||this.props.name}balanceCheck(){return this.onBalanceCheck()}submit(){if(!this.isValid)return this.showValidation(),!1;this.balanceCheck()}makeSubmitCall(){super.submit()}render(){return t(n,{i18n:this.props.i18n,loadingContext:this.props.loadingContext,resources:this.resources},t(r,c(p({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={}})=>{var r;if(!t)throw new Error("card-error");if((null==t?void 0:t.currency)!==(null===(r=this.props.amount)||void 0===r?void 0:r.currency))throw new Error("currency-error");if((null==t?void 0: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((null==t?void 0:t.message)||"error"),this.props.onError&&(t instanceof s?this.handleError(t):this.handleError(new s("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 s("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(o,e)))}}a(h,"type",i.giftcard),a(h,"defaultProps",{brandsConfiguration:{}});export{h as GiftcardElement,h as default};
//# sourceMappingURL=Giftcard.js.map