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) 5.87 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 a}from"./utils.js";import i from"../../utils/browserInfo.js";import l from"../../core/Errors/AdyenCheckoutError.js";import{TxVariants as h}from"../tx-variants.js";import{sanitizeResponse as d,verifyPaymentDidNotFail as p}from"../internal/UIElement/utils.js";import{AnalyticsInfoEvent as y,InfoEventType as c,UiTarget as g}from"../../core/Analytics/events/AnalyticsInfoEvent.js";import{mapGooglePayBrands as u}from"./utils/map-adyen-brands-to-googlepay-brands.js";function m(t,e,o){return e in t?Object.defineProperty(t,e,{value:o,enumerable:!0,configurable:!0,writable:!0}):t[e]=o,t}const P=["AMEX","DISCOVER","JCB","MASTERCARD","VISA"];class b extends e{getPaymentMethodFromPaymentMethodsResponse(t,e){return e?this.core.paymentMethodsResponse.findById(e):this.core.paymentMethodsResponse.find(t||this.constructor.type)||this.core.paymentMethodsResponse.find(h.paywithgoogle)}formatProps(t){const e=t.buttonSizeMode??(t.isDropin?"fill":"static"),o=n(t.buttonLocale??t.i18n?.locale),s=[...t.callbackIntents,"PAYMENT_AUTHORIZATION"],r=this.createAllowedCardNetworksValues({allowedCardNetworks:t.allowedCardNetworks,brands:t.brands});return{...t,allowedCardNetworks:r,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}}}createAllowedCardNetworksValues({allowedCardNetworks:t,brands:e}){return t?.length>0?t:e?.length>0?u(e):P}beforeRender(t){if(t?.originalAction)return;const e=new y({type:c.rendered,component:this.type,configData:{...t,showPayButton:this.props.showPayButton},...t?.isExpress&&{isExpress:t.isExpress},...t?.expressPage&&{expressPage:t.expressPage}});this.analytics.sendAnalytics(e)}showGooglePayPaymentSheet(){this.googlePay.initiatePayment(this.props,this.core.options.countryCode).catch(t=>{this.handleError(new l("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 l("ERROR","GooglePay is not available");if(!1===t.paymentMethodPresent)throw new l("ERROR","GooglePay - No paymentMethodPresent");return Promise.resolve()}).catch(t=>Promise.reject(t))}get browserInfo(){return i()}get icon(){return this.props.icon??this.resources.getImage()("googlepay")}componentToRender(){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),m(this,"googlePay",void 0),m(this,"submit",()=>{if(this.props.isInstantPayment){const t=new y({component:this.type,type:c.selected,target:g.instantPaymentButton});this.submitAnalytics(t)}new Promise((t,e)=>this.props.onClick(t,e)).then(this.showGooglePayPaymentSheet).catch(()=>{})}),m(this,"onPaymentAuthorized",async t=>{const e=a(t.paymentMethodData.info.billingAddress),o=a(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(d).then(p).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)})})}),m(this,"isReadyToPay",()=>this.googlePay.isReadyToPay(this.props)),m(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 l("IMPLEMENTATION_ERROR",'GooglePay - You must set "isExpress" flag to "true" in order to use "onPaymentDataChanged" callback');if(!this.props.configuration.merchantId)throw new l("IMPLEMENTATION_ERROR","GooglePay - Missing merchantId. Please ensure that it is correctly configured in your customer area.");this.googlePay=new o(this.props.environment,this.analytics,{...s&&r?.onPaymentDataChanged&&{onPaymentDataChanged:r.onPaymentDataChanged},onPaymentAuthorized:this.onPaymentAuthorized})}}m(b,"type",h.googlepay),m(b,"txVariants",[h.googlepay,h.paywithgoogle]),m(b,"defaultProps",r);export{b as default}; //# sourceMappingURL=GooglePay.js.map