UNPKG

@atlas-kitchen/adyen-web

Version:
3 lines (2 loc) 5.47 kB
import{createElement as t}from"../../external/preact/dist/preact.js";import{UIElement as e}from"../internal/UIElement/UIElement.js";import o from"./GooglePayService.js";import s from"./components/GooglePayButton.js";import r from"./defaultProps.js";import{getGooglePayLocale as n,formatGooglePayContactToAdyenAddressFormat as i}from"./utils.js";import a from"../../utils/browserInfo.js";import h from"../../core/Errors/AdyenCheckoutError.js";import{TxVariants as l}from"../tx-variants.js";import{sanitizeResponse as p,verifyPaymentDidNotFail as d}from"../internal/UIElement/utils.js";import{ANALYTICS_RENDERED_STR as y,ANALYTICS_INSTANT_PAYMENT_BUTTON as c,ANALYTICS_SELECTED_STR as u,ANALYTICS_EXPRESS_PAGES_ARRAY as g}from"../../core/Analytics/constants.js";import{AnalyticsInfoEvent as m}from"../../core/Analytics/AnalyticsInfoEvent.js";function P(t,e,o){return e in t?Object.defineProperty(t,e,{value:o,enumerable:!0,configurable:!0,writable:!0}):t[e]=o,t}class b extends e{getPaymentMethodFromPaymentMethodsResponse(t){return this.core.paymentMethodsResponse.find(t||this.constructor.type)||this.core.paymentMethodsResponse.find(l.paywithgoogle)}formatProps(t){const e=t.buttonSizeMode??(t.isDropin?"fill":"static"),o=n(t.buttonLocale??t.i18n?.locale),s=[...t.callbackIntents,"PAYMENT_AUTHORIZATION"];return{...t,configuration:t.configuration,buttonSizeMode:e,buttonLocale:o,callbackIntents:s}}formatData(){const{googlePayCardNetwork:t,googlePayToken:e,billingAddress:o,deliveryAddress:s}=this.state,{isExpress:r}=this.props;return{paymentMethod:{type:this.type,googlePayCardNetwork:t,googlePayToken:e,...r&&{subtype:"express"}},browserInfo:this.browserInfo,origin:!!window&&window.location.origin,...o&&{billingAddress:o},...s&&{deliveryAddress:s}}}submitAnalytics(t){if(t instanceof m&&t.type===y){const{isExpress:e,expressPage:o}=this.props,s=o&&g.includes(o);"boolean"==typeof e&&(t.isExpress=e),!0===e&&s&&(t.expressPage=o)}super.submitAnalytics(t)}showGooglePayPaymentSheet(){this.googlePay.initiatePayment(this.props,this.core.options.countryCode).catch((t=>{this.handleError(new h("CANCELED"===t.statusCode?"CANCEL":"ERROR",t.toString(),{cause:t}))}))}async handleAuthorization(){return new Promise(((t,e)=>{this.props.onAuthorized||t();const{authorizedEvent:o,billingAddress:s,deliveryAddress:r}=this.state;this.props.onAuthorized({authorizedEvent:o,...s&&{billingAddress:s},...r&&{deliveryAddress:r}},{resolve:t,reject:e})})).catch((t=>{const e={error:{googlePayError:t}};return Promise.reject(e)}))}get isValid(){return!0}async isAvailable(){return this.isReadyToPay().then((t=>{if(!t.result)throw new h("ERROR","GooglePay is not available");if(!1===t.paymentMethodPresent)throw new h("ERROR","GooglePay - No paymentMethodPresent");return Promise.resolve()})).catch((t=>Promise.reject(t)))}get browserInfo(){return a()}get icon(){return this.props.icon??this.resources.getImage()("googlepay")}render(){return this.props.showPayButton?t(s,{buttonColor:this.props.buttonColor,buttonType:this.props.buttonType,buttonSizeMode:this.props.buttonSizeMode,buttonLocale:this.props.buttonLocale,buttonRootNode:this.props.buttonRootNode,buttonRadius:this.props.buttonRadius,paymentsClient:this.googlePay.paymentsClient,onClick:this.submit}):null}constructor(t,e){super(t,e),P(this,"googlePay",void 0),P(this,"submit",(()=>{if(this.props.isInstantPayment){const t=new m({type:u,target:c});this.submitAnalytics(t)}new Promise(((t,e)=>this.props.onClick(t,e))).then(this.showGooglePayPaymentSheet).catch((()=>{}))})),P(this,"onPaymentAuthorized",(async t=>{const e=i(t.paymentMethodData.info.billingAddress),o=i(t.shippingAddress,!0);return this.setState({authorizedEvent:t,googlePayToken:t.paymentMethodData.tokenizationData.token,googlePayCardNetwork:t.paymentMethodData.info.cardNetwork,...e&&{billingAddress:e},...o&&{deliveryAddress:o}}),new Promise((t=>{this.handleAuthorization().then(this.makePaymentsCall).then(p).then(d).then((e=>(t({transactionState:"SUCCESS"}),e))).then((t=>{this.handleResponse(t)})).catch((e=>{this.setElementStatus("ready");const o=e?.error?.googlePayError,s=this.props.i18n.get("error.subtitle.payment"),r="string"==typeof o?{intent:"PAYMENT_AUTHORIZATION",reason:"OTHER_ERROR",message:o||s}:{intent:o?.intent||"PAYMENT_AUTHORIZATION",reason:o?.reason||"OTHER_ERROR",message:o?.message||s};t({transactionState:"ERROR",error:r});const n={...e,error:{googlePayError:r}};this.handleFailedResult(n)}))}))})),P(this,"isReadyToPay",(()=>this.googlePay.isReadyToPay(this.props))),P(this,"prefetch",(()=>this.googlePay.prefetchPaymentData(this.props,this.core.options.countryCode))),this.handleAuthorization=this.handleAuthorization.bind(this),this.showGooglePayPaymentSheet=this.showGooglePayPaymentSheet.bind(this);const{isExpress:s,paymentDataCallbacks:r}=this.props;if(!1===s&&r?.onPaymentDataChanged)throw new h("IMPLEMENTATION_ERROR",'GooglePay - You must set "isExpress" flag to "true" in order to use "onPaymentDataChanged" callback');if(!this.props.configuration.merchantId)throw new h("IMPLEMENTATION_ERROR","GooglePay - Missing merchantId. Please ensure that it is correctly configured in your customer area.");this.googlePay=new o(this.props.environment,{...s&&r?.onPaymentDataChanged&&{onPaymentDataChanged:r.onPaymentDataChanged},onPaymentAuthorized:this.onPaymentAuthorized})}}P(b,"type",l.googlepay),P(b,"txVariants",[l.googlepay,l.paywithgoogle]),P(b,"defaultProps",r);export{b as default}; //# sourceMappingURL=GooglePay.js.map