@adyen/adyen-web
Version:
[](https://www.npmjs.com/package/@adyen/adyen-web)
3 lines (2 loc) • 5.63 kB
JavaScript
import{createElement as e}from"../../external/preact/dist/preact.js";import{UIElement as t}from"../internal/UIElement/UIElement.js";import n from"./components/PaypalComponent.js";import r from"./defaultProps.js";import i from"../../core/Errors/AdyenCheckoutError.js";import{ERRORS as o}from"./constants.js";import{TxVariants as s}from"../tx-variants.js";import{formatPaypalOrderContactToAdyenFormat as a}from"./utils/format-paypal-order-contact-to-adyen-format.js";import{AnalyticsInfoEvent as p,InfoEventType as h}from"../../core/Analytics/events/AnalyticsInfoEvent.js";function l(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function d(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),r.forEach(function(t){l(e,t,n[t])})}return e}function c(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 n=Object.getOwnPropertySymbols(e);t.push.apply(t,n)}return t}(Object(t)).forEach(function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}),e}function u(e,t){if(null==e)return{};var n,r,i,o={};if("undefined"!=typeof Reflect&&Reflect.ownKeys){for(n=Reflect.ownKeys(e),i=0;i<n.length;i++)r=n[i],t.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(e,r)&&(o[r]=e[r]);return o}if(o=function(e,t){if(null==e)return{};var n,r,i={},o=Object.getOwnPropertyNames(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n]);return i}(e,t),Object.getOwnPropertySymbols)for(n=Object.getOwnPropertySymbols(e),i=0;i<n.length;i++)r=n[i],t.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(e,r)&&(o[r]=e[r]);return o}class y extends t{formatProps(e){var t,n,r;const i=null===(t=e.configuration)||void 0===t?void 0:t.merchantId,o=null===(n=e.configuration)||void 0===n?void 0:n.intent,s=0===(null===(r=e.amount)||void 0===r?void 0:r.value)?"tokenize":e.intent||o,a="tokenize"===s||e.vault,p="continue"===e.userAction;return c(d({},e),{commit:!p&&e.commit,vault:a,configuration:{intent:s,merchantId:i}})}beforeRender(e){const t=new p(d({type:h.rendered,component:this.type,configData:c(d({},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)}updatePaymentData(e){e||console.warn("PayPal - Updating payment data with an invalid value"),this.paymentData=e}formatData(){const{isExpress:e,userAction:t}=this.props;return{paymentMethod:{type:y.type,userAction:t,subtype:e?"express":y.subtype}}}get isValid(){return!0}handleResolve(e){if(!this.resolve)return this.handleError(new i("ERROR",o.WRONG_INSTANCE));this.resolve(e)}handleReject(e){if(!this.reject)return this.handleError(new i("ERROR",o.WRONG_INSTANCE));this.reject(new Error(e))}handleSubmit(){return super.submit(),new Promise((e,t)=>{this.resolve=e,this.reject=t})}handleOnShippingAddressChange(e,t){return this.props.onShippingAddressChange(e,t,this)}handleOnShippingOptionsChange(e,t){return this.props.onShippingOptionsChange(e,t,this)}componentToRender(){if(!this.props.showPayButton)return null;const t=this.props,{onShippingAddressChange:r,onShippingOptionsChange:o}=t,s=u(t,["onShippingAddressChange","onShippingOptionsChange"]);return e(n,c(d({ref:e=>{this.componentRef=e}},s,r&&{onShippingAddressChange:this.handleOnShippingAddressChange},o&&{onShippingOptionsChange:this.handleOnShippingOptionsChange}),{onCancel:()=>this.handleError(new i("CANCEL")),onChange:this.setState,onApprove:this.handleOnApprove,onError:e=>{this.handleError(new i("ERROR",e.toString(),{cause:e}))},onScriptLoadFailure:e=>this.handleError(e),onSubmit:this.handleSubmit}))}constructor(e,t){super(e,t),l(this,"paymentData",null),l(this,"resolve",null),l(this,"reject",null),l(this,"submit",()=>{this.handleError(new i("IMPLEMENTATION_ERROR",o.SUBMIT_NOT_SUPPORTED))}),l(this,"handleAction",e=>this.updateWithAction(e)),l(this,"updateWithAction",e=>{if(e.paymentMethodType!==this.type)throw new Error("Invalid Action");return e.paymentData&&(this.paymentData=e.paymentData),e.sdkData&&e.sdkData.token?(this.onActionHandled({componentType:this.type,actionDescription:"sdk-loaded",originalAction:e}),this.handleResolve(e.sdkData.token)):this.handleReject(o.NO_TOKEN_PROVIDED),null}),l(this,"handleOnApprove",(e,t)=>{const{onAuthorized:n}=this.props,r={data:{details:e,paymentData:this.paymentData}};if(n)return t.order.get().then(e=>{var t;const r=a(null==e?void 0:e.payer),i=a(null==e||null===(t=e.purchase_units)||void 0===t?void 0:t[0].shipping,!0);return this.setState(d({authorizedEvent:e},r&&{billingAddress:r},i&&{deliveryAddress:i})),new Promise((t,o)=>n(d({authorizedEvent:e},r&&{billingAddress:r},i&&{deliveryAddress:i}),{resolve:t,reject:o}))}).then(()=>this.handleAdditionalDetails(r)).catch(e=>this.handleError(new i("ERROR","Something went wrong while parsing PayPal Order",{cause:e})));this.handleAdditionalDetails(r)}),this.handleSubmit=this.handleSubmit.bind(this),this.handleOnShippingAddressChange=this.handleOnShippingAddressChange.bind(this),this.handleOnShippingOptionsChange=this.handleOnShippingOptionsChange.bind(this)}}l(y,"type",s.paypal),l(y,"subtype","sdk"),l(y,"defaultProps",r);export{y as default};
//# sourceMappingURL=Paypal.js.map