@atlas-kitchen/adyen-web
Version:
3 lines (2 loc) • 6.43 kB
JavaScript
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 r from"./components/GooglePayButton.js";import n 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 h}from"../tx-variants.js";import{sanitizeResponse as p,verifyPaymentDidNotFail as c}from"../internal/UIElement/utils.js";import{ANALYTICS_RENDERED_STR as y,ANALYTICS_INSTANT_PAYMENT_BUTTON as d,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}function b(t){for(var e=1;e<arguments.length;e++){var o=null!=arguments[e]?arguments[e]:{},r=Object.keys(o);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(o).filter((function(t){return Object.getOwnPropertyDescriptor(o,t).enumerable})))),r.forEach((function(e){P(t,e,o[e])}))}return t}function f(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 o=Object.getOwnPropertySymbols(t);e.push.apply(e,o)}return e}(Object(e)).forEach((function(o){Object.defineProperty(t,o,Object.getOwnPropertyDescriptor(e,o))})),t}class E extends e{getPaymentMethodFromPaymentMethodsResponse(t){return this.core.paymentMethodsResponse.find(t||this.constructor.type)||this.core.paymentMethodsResponse.find(h.paywithgoogle)}formatProps(t){var e,o;const r=null!==(o=t.buttonSizeMode)&&void 0!==o?o:t.isDropin?"fill":"static";var n;const i=s(null!==(n=t.buttonLocale)&&void 0!==n?n:null===(e=t.i18n)||void 0===e?void 0:e.locale),a=[...t.callbackIntents,"PAYMENT_AUTHORIZATION"];return f(b({},t),{configuration:t.configuration,buttonSizeMode:r,buttonLocale:i,callbackIntents:a})}formatData(){const{googlePayCardNetwork:t,googlePayToken:e,billingAddress:o,deliveryAddress:r}=this.state,{isExpress:n}=this.props;return b({paymentMethod:b({type:this.type,googlePayCardNetwork:t,googlePayToken:e},n&&{subtype:"express"}),browserInfo:this.browserInfo,origin:!!window&&window.location.origin},o&&{billingAddress:o},r&&{deliveryAddress:r})}submitAnalytics(t){if(t instanceof m&&t.type===y){const{isExpress:e,expressPage:o}=this.props,r=o&&g.includes(o);"boolean"==typeof e&&(t.isExpress=e),!0===e&&r&&(t.expressPage=o)}super.submitAnalytics(t)}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:r,deliveryAddress:n}=this.state;this.props.onAuthorized(b({authorizedEvent:o},r&&{billingAddress:r},n&&{deliveryAddress:n}),{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 a()}get icon(){var t;return null!==(t=this.props.icon)&&void 0!==t?t:this.resources.getImage()("googlepay")}render(){return this.props.showPayButton?t(r,{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:d});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(b({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(c).then((e=>(t({transactionState:"SUCCESS"}),e))).then((t=>{this.handleResponse(t)})).catch((e=>{var o;this.setElementStatus("ready");const r=null==e||null===(o=e.error)||void 0===o?void 0:o.googlePayError,n=this.props.i18n.get("error.subtitle.payment"),s="string"==typeof r?{intent:"PAYMENT_AUTHORIZATION",reason:"OTHER_ERROR",message:r||n}:{intent:(null==r?void 0:r.intent)||"PAYMENT_AUTHORIZATION",reason:(null==r?void 0:r.reason)||"OTHER_ERROR",message:(null==r?void 0:r.message)||n};t({transactionState:"ERROR",error:s});const i=f(b({},e),{error:{googlePayError:s}});this.handleFailedResult(i)}))}))})),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:r,paymentDataCallbacks:n}=this.props;if(!1===r&&(null==n?void 0:n.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,f(b({},r&&(null==n?void 0:n.onPaymentDataChanged)&&{onPaymentDataChanged:n.onPaymentDataChanged}),{onPaymentAuthorized:this.onPaymentAuthorized}))}}P(E,"type",h.googlepay),P(E,"txVariants",[h.googlepay,h.paywithgoogle]),P(E,"defaultProps",n);export{E as default};
//# sourceMappingURL=GooglePay.js.map