@adyen/adyen-web
Version:
[](https://www.npmjs.com/package/@adyen/adyen-web)
3 lines (2 loc) • 7.75 kB
JavaScript
import{createElement as e}from"../../external/preact/dist/preact.js";import{UIElement as t}from"../internal/UIElement/UIElement.js";import r from"./components/ApplePayButton.js";import o from"./services/ApplePayService.js";import s from"../../utils/base64.js";import i from"./defaultProps.js";import{httpPost as n}from"../../core/Services/http.js";import{preparePaymentRequest as a}from"./utils/payment-request.js";import p from"../../core/Errors/AdyenCheckoutError.js";import{TxVariants as l}from"../tx-variants.js";import{sanitizeResponse as d,verifyPaymentDidNotFail as c}from"../internal/UIElement/utils.js";import{ANALYTICS_SELECTED_STR as h,ANALYTICS_INSTANT_PAYMENT_BUTTON as u}from"../../core/Analytics/constants.js";import{resolveSupportedVersion as y}from"./utils/resolve-supported-version.js";import{formatApplePayContactToAdyenAddressFormat as m}from"./utils/format-applepay-contact-to-adyen-format.js";import{mapBrands as b}from"./utils/map-adyen-brands-to-applepay-brands.js";import P from"./services/ApplePaySdkLoader.js";import{detectInIframe as f}from"../../utils/detectInIframe.js";function A(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function v(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{},o=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(r).filter((function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable})))),o.forEach((function(t){A(e,t,r[t])}))}return e}function g(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 r=Object.getOwnPropertySymbols(e);t.push.apply(t,r)}return t}(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))})),e}class S extends t{formatProps(e){var t,r,o;const s=(null===(t=e.brands)||void 0===t?void 0:t.length)?b(e.brands):e.supportedNetworks;var i,n;return g(v({},e),{configuration:e.configuration,supportedNetworks:s,buttonLocale:null!==(i=e.buttonLocale)&&void 0!==i?i:null===(r=e.i18n)||void 0===r?void 0:r.locale,totalPriceLabel:e.totalPriceLabel||(null===(o=e.configuration)||void 0===o?void 0:o.merchantName),renderApplePayCodeAs:null!==(n=e.renderApplePayCodeAs)&&void 0!==n?n:f()?"window":"modal"})}formatData(){const{applePayToken:e,billingAddress:t,deliveryAddress:r}=this.state,{isExpress:o}=this.props;return v({paymentMethod:v({type:S.type,applePayToken:e},o&&{subtype:"express"})},t&&{billingAddress:t},r&&{deliveryAddress:r})}submitAnalytics(e){super.submitAnalytics(v({},e),this.props)}get isValid(){return!0}async applePayCapabilities(e){const t=e||this.props.configuration.merchantId;try{var r;return await this.sdkLoader.isSdkLoaded(),await(null===(r=ApplePaySession)||void 0===r?void 0:r.applePayCapabilities(t))}catch(e){throw new p("ERROR","Apple Pay: Error when requesting applePayCapabilities()",{cause:e})}}async isAvailable(){if("https:"!==window.location.protocol)return Promise.reject(new p("IMPLEMENTATION_ERROR","Trying to start an Apple Pay session from an insecure document"));try{var e;return await this.sdkLoader.isSdkLoaded(),(null===(e=ApplePaySession)||void 0===e?void 0:e.canMakePayments())?Promise.resolve():Promise.reject(new p("ERROR","Apple Pay is not available on this device"))}catch(e){return Promise.reject(new p("ERROR","Apple Pay SDK failed to load",{cause:e}))}}defineApplePayVersionNumber(){"https:"===window.location.protocol&&(this.applePayVersionNumber=this.props.version||y(14))}configureApplePayWebOptions(){if(window.ApplePayWebOptions){const{renderApplePayCodeAs:e,onApplePayCodeClose:t}=this.props;window.ApplePayWebOptions.set(v({renderApplePayCodeAs:e},t&&{onApplePayCodeClose:t}))}}startSession(){const{onValidateMerchant:e,onPaymentMethodSelected:t,onShippingMethodSelected:r,onShippingContactSelected:s}=this.props,i=a(v({companyName:this.props.configuration.merchantName,countryCode:this.core.options.countryCode},this.props)),n=new o(i,{version:this.applePayVersionNumber,onError:e=>{this.handleError(new p("ERROR","ApplePay - Something went wrong on ApplePayService",{cause:e}))},onCancel:e=>{this.handleError(new p("CANCEL","ApplePay UI dismissed",{cause:e}))},onPaymentMethodSelected:t,onShippingMethodSelected:r,onShippingContactSelected:s,onValidateMerchant:e||this.validateMerchant,onPaymentAuthorized:(e,t,r)=>{const o=m(r.payment.billingContact),s=m(r.payment.shippingContact,!0);this.setState(v({applePayToken:btoa(JSON.stringify(r.payment.token.paymentData)),authorizedEvent:r},o&&{billingAddress:o},s&&{deliveryAddress:s})),this.handleAuthorization().then(this.makePaymentsCall).then(d).then(c).then(this.collectOrderTrackingDetailsIfNeeded).then((({paymentResponse:t,orderDetails:r})=>(e(v({status:ApplePaySession.STATUS_SUCCESS},r&&{orderDetails:r})),t))).then((e=>{this.handleResponse(e)})).catch((e=>{var r;const o=null==e||null===(r=e.error)||void 0===r?void 0:r.applePayError;t({status:ApplePaySession.STATUS_FAILURE,errors:o?Array.isArray(o)?o:[o]:void 0});const s=g(v({},e),{error:{applePayError:o}});this.handleFailedResult(s)}))}});return new Promise(((e,t)=>this.props.onClick(e,t))).then((()=>{n.begin()})).catch((()=>({})))}async handleAuthorization(){return new Promise(((e,t)=>{this.props.onAuthorized||e();const{authorizedEvent:r,billingAddress:o,deliveryAddress:s}=this.state;this.props.onAuthorized(v({authorizedEvent:r},o&&{billingAddress:o},s&&{deliveryAddress:s}),{resolve:e,reject:t})})).catch((e=>{const t={error:{applePayError:e}};return Promise.reject(t)}))}async collectOrderTrackingDetailsIfNeeded(e){return new Promise(((e,t)=>{if(!this.props.onOrderTrackingRequest)return e();this.props.onOrderTrackingRequest(e,t)})).then((t=>v({paymentResponse:e},t&&{orderDetails:t}))).catch((()=>({paymentResponse:e})))}async validateMerchant(e,t){const{hostname:r}=window.location,{clientKey:o,configuration:i,loadingContext:a,initiative:p}=this.props,{merchantName:l,merchantId:d}=i,c={loadingContext:a,path:`v1/applePay/sessions?clientKey=${o}`},h={displayName:l,domainName:r,initiative:p,merchantIdentifier:d};try{const r=await n(c,h),o=s.decode(r.data);if(o.success){e(JSON.parse(o.data))}else t("Could not decode Apple Pay session")}catch(e){t("Could not get Apple Pay session")}}render(){return this.props.showPayButton?e(r,{buttonStyle:this.props.buttonColor,buttonType:this.props.buttonType,buttonLocale:this.props.buttonLocale,onClick:this.submit}):null}constructor(e,t){super(e,t),A(this,"sdkLoader",void 0),A(this,"applePayVersionNumber",void 0),A(this,"submit",(()=>{this.props.isInstantPayment&&this.submitAnalytics({type:h,target:u}),this.startSession()}));const{isExpress:r,onShippingContactSelected:o,onShippingMethodSelected:s}=this.props;if(!1===r&&(o||s))throw new p("IMPLEMENTATION_ERROR",'ApplePay - You must set "isExpress" flag to "true" in order to use "onShippingContactSelected" and/or "onShippingMethodSelected" callbacks');this.startSession=this.startSession.bind(this),this.submit=this.submit.bind(this),this.validateMerchant=this.validateMerchant.bind(this),this.collectOrderTrackingDetailsIfNeeded=this.collectOrderTrackingDetailsIfNeeded.bind(this),this.handleAuthorization=this.handleAuthorization.bind(this),this.defineApplePayVersionNumber=this.defineApplePayVersionNumber.bind(this),this.configureApplePayWebOptions=this.configureApplePayWebOptions.bind(this),this.sdkLoader=new P,this.sdkLoader.load().then(this.defineApplePayVersionNumber).then(this.configureApplePayWebOptions).catch((e=>{this.handleError(e)}))}}A(S,"type",l.applepay),A(S,"defaultProps",i);export{S as default};
//# sourceMappingURL=ApplePay.js.map