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) 6.89 kB
import{createElement as e}from"../../external/preact/dist/preact.js";import{UIElement as t}from"../internal/UIElement/UIElement.js";import o from"./GooglePayService.js";import n from"./components/GooglePayButton.js";import r from"./defaultProps.js";import{getGooglePayLocale as s,formatGooglePayContactToAdyenAddressFormat as i}from"./utils.js";import a from"../../utils/browserInfo.js";import l from"../../core/Errors/AdyenCheckoutError.js";import{TxVariants as d}from"../tx-variants.js";import{sanitizeResponse as h,verifyPaymentDidNotFail as p}from"../internal/UIElement/utils.js";import{AnalyticsInfoEvent as c,InfoEventType as y,UiTarget as u}from"../../core/Analytics/events/AnalyticsInfoEvent.js";import{mapGooglePayBrands as g}from"./utils/map-adyen-brands-to-googlepay-brands.js";function m(e,t,o){return t in e?Object.defineProperty(e,t,{value:o,enumerable:!0,configurable:!0,writable:!0}):e[t]=o,e}function P(e){for(var t=1;t<arguments.length;t++){var o=null!=arguments[t]?arguments[t]:{},n=Object.keys(o);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(o).filter(function(e){return Object.getOwnPropertyDescriptor(o,e).enumerable}))),n.forEach(function(t){m(e,t,o[t])})}return e}function b(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 o=Object.getOwnPropertySymbols(e);t.push.apply(t,o)}return t}(Object(t)).forEach(function(o){Object.defineProperty(e,o,Object.getOwnPropertyDescriptor(t,o))}),e}const f=["AMEX","DISCOVER","JCB","MASTERCARD","VISA"];class w extends t{getPaymentMethodFromPaymentMethodsResponse(e,t){return t?this.core.paymentMethodsResponse.findById(t):this.core.paymentMethodsResponse.find(e||this.constructor.type)||this.core.paymentMethodsResponse.find(d.paywithgoogle)}formatProps(e){var t,o,n;const r=null!==(t=e.buttonSizeMode)&&void 0!==t?t:e.isDropin?"fill":"static",i=s(null!==(o=e.buttonLocale)&&void 0!==o?o:null===(n=e.i18n)||void 0===n?void 0:n.locale),a=[...e.callbackIntents,"PAYMENT_AUTHORIZATION"],l=this.createAllowedCardNetworksValues({allowedCardNetworks:e.allowedCardNetworks,brands:e.brands});return b(P({},e),{allowedCardNetworks:l,configuration:e.configuration,buttonSizeMode:r,buttonLocale:i,callbackIntents:a})}formatData(){const{googlePayCardNetwork:e,googlePayToken:t,billingAddress:o,deliveryAddress:n}=this.state,{isExpress:r}=this.props;return P({paymentMethod:P({type:this.type,googlePayCardNetwork:e,googlePayToken:t},r&&{subtype:"express"}),browserInfo:this.browserInfo,origin:!!window&&window.location.origin},o&&{billingAddress:o},n&&{deliveryAddress:n})}createAllowedCardNetworksValues({allowedCardNetworks:e,brands:t}){return(null==e?void 0:e.length)>0?e:(null==t?void 0:t.length)>0?g(t):f}beforeRender(e){if(null==e?void 0:e.originalAction)return;const t=new c(P({type:y.rendered,component:this.type,configData:b(P({},e),{showPayButton:this.props.showPayButton})},(null==e?void 0:e.isExpress)&&{isExpress:e.isExpress},(null==e?void 0:e.expressPage)&&{expressPage:e.expressPage}));this.analytics.sendAnalytics(t)}showGooglePayPaymentSheet(){this.googlePay.initiatePayment(this.props,this.core.options.countryCode).catch(e=>{this.handleError(new l("CANCELED"===e.statusCode?"CANCEL":"ERROR",e.toString(),{cause:e}))})}async handleAuthorization(){return new Promise((e,t)=>{this.props.onAuthorized||e();const{authorizedEvent:o,billingAddress:n,deliveryAddress:r}=this.state;this.props.onAuthorized(P({authorizedEvent:o},n&&{billingAddress:n},r&&{deliveryAddress:r}),{resolve:e,reject:t})}).catch(e=>{const t={error:{googlePayError:e}};return Promise.reject(t)})}get isValid(){return!0}async isAvailable(){return this.isReadyToPay().then(e=>{if(!e.result)throw new l("ERROR","GooglePay is not available");if(!1===e.paymentMethodPresent)throw new l("ERROR","GooglePay - No paymentMethodPresent");return Promise.resolve()}).catch(e=>Promise.reject(e))}get browserInfo(){return a()}get icon(){var e;return null!==(e=this.props.icon)&&void 0!==e?e:this.resources.getImage()("googlepay")}componentToRender(){return this.props.showPayButton?e(n,{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(e,t){super(e,t),m(this,"googlePay",void 0),m(this,"submit",()=>{if(this.props.isInstantPayment){const e=new c({component:this.type,type:y.selected,target:u.instantPaymentButton});this.submitAnalytics(e)}new Promise((e,t)=>this.props.onClick(e,t)).then(this.showGooglePayPaymentSheet).catch(()=>{})}),m(this,"onPaymentAuthorized",async e=>{const t=i(e.paymentMethodData.info.billingAddress),o=i(e.shippingAddress,!0);return this.setState(P({authorizedEvent:e,googlePayToken:e.paymentMethodData.tokenizationData.token,googlePayCardNetwork:e.paymentMethodData.info.cardNetwork},t&&{billingAddress:t},o&&{deliveryAddress:o})),new Promise(e=>{this.handleAuthorization().then(this.makePaymentsCall).then(h).then(p).then(t=>(e({transactionState:"SUCCESS"}),t)).then(e=>{this.handleResponse(e)}).catch(t=>{var o;this.setElementStatus("ready");const n=null==t||null===(o=t.error)||void 0===o?void 0:o.googlePayError,r=this.props.i18n.get("error.subtitle.payment"),s="string"==typeof n?{intent:"PAYMENT_AUTHORIZATION",reason:"OTHER_ERROR",message:n||r}:{intent:(null==n?void 0:n.intent)||"PAYMENT_AUTHORIZATION",reason:(null==n?void 0:n.reason)||"OTHER_ERROR",message:(null==n?void 0:n.message)||r};e({transactionState:"ERROR",error:s});const i=b(P({},t),{error:{googlePayError:s}});this.handleFailedResult(i)})})}),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:n,paymentDataCallbacks:r}=this.props;if(!1===n&&(null==r?void 0: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,b(P({},n&&(null==r?void 0:r.onPaymentDataChanged)&&{onPaymentDataChanged:r.onPaymentDataChanged}),{onPaymentAuthorized:this.onPaymentAuthorized}))}}m(w,"type",d.googlepay),m(w,"txVariants",[d.googlepay,d.paywithgoogle]),m(w,"defaultProps",r);export{w as default}; //# sourceMappingURL=GooglePay.js.map