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) 4.66 kB
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 s from"./defaultProps.js";import i from"../../core/Errors/AdyenCheckoutError.js";import{ERRORS as r}from"./constants.js";import{TxVariants as a}from"../tx-variants.js";import{formatPaypalOrderContactToAdyenFormat as o}from"./utils/format-paypal-order-contact-to-adyen-format.js";import{AnalyticsInfoEvent as h,InfoEventType as p}from"../../core/Analytics/events/AnalyticsInfoEvent.js";import{sanitizeResponse as l,verifyPaymentDidNotFail as d}from"../internal/UIElement/utils.js";import c from"../../core/Errors/CancelError.js";function m(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}class u extends t{formatProps(e){const t=e.configuration?.merchantId,n=e.configuration?.intent,s=0===e.amount?.value?"tokenize":e.intent||n,i="tokenize"===s||e.vault,r="continue"===e.userAction;return{...e,commit:!r&&e.commit,vault:i,configuration:{intent:s,merchantId:t}}}beforeRender(e){const t=new h({type:p.rendered,component:this.type,configData:{...e,showPayButton:this.props.showPayButton},...e?.isExpress&&{isExpress:e.isExpress},...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:u.type,userAction:t,subtype:e?"express":u.subtype}}}get isValid(){return!0}handleResolve(e){if(!this.resolve)return this.handleError(new i("ERROR",r.WRONG_INSTANCE));this.resolve(e)}handleReject(e){if(!this.reject)return this.handleError(new i("ERROR",r.WRONG_INSTANCE));this.reject(new Error(e))}handleSubmit(){return new Promise((e,t)=>{this.resolve=e,this.reject=t,this.makePaymentsCall().then(l).then(d).then(this.handleResponse).catch(e=>{if(e instanceof c)return void this.setElementStatus("ready");this.handleFailedResult(e);const t=e instanceof Error?e.message:JSON.stringify(e),n=e?`: ${t}`:"";this.handleReject(`${r.PAYMENT_FAILED}${n}`)})})}handleOnShippingAddressChange(e,t){const{onShippingAddressChange:n}=this.props;return n?n(e,t,this):Promise.resolve()}handleOnShippingOptionsChange(e,t){const{onShippingOptionsChange:n}=this.props;return n?n(e,t,this):Promise.resolve()}componentToRender(){if(!this.props.showPayButton)return null;const{onShippingAddressChange:t,onShippingOptionsChange:s,...r}=this.props;return e(n,{...r,setComponentRef:this.setComponentRef,...t&&{onShippingAddressChange:this.handleOnShippingAddressChange},...s&&{onShippingOptionsChange:this.handleOnShippingOptionsChange},onCancel:()=>this.handleError(new i("CANCEL")),onChange:this.setState,onApprove:this.handleOnApprove,onError:e=>{this.handleError(new i("ERROR",String(e),{cause:e}))},onScriptLoadFailure:e=>this.handleError(e),onSubmit:this.handleSubmit})}constructor(e,t){super(e,t),m(this,"paymentData",null),m(this,"resolve",null),m(this,"reject",null),m(this,"submit",()=>{this.handleError(new i("IMPLEMENTATION_ERROR",r.SUBMIT_NOT_SUPPORTED))}),m(this,"handleAction",e=>this.updateWithAction(e)),m(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(r.NO_TOKEN_PROVIDED),null}),m(this,"handleOnApprove",(e,t)=>{const{onAuthorized:n}=this.props,s={data:{details:e,paymentData:this.paymentData??void 0}};return n?t.order?t.order.get().then(e=>{const t=o(e?.payer),s=o(e?.purchase_units?.[0].shipping,!0);return this.setState({authorizedEvent:e,...t&&{billingAddress:t},...s&&{deliveryAddress:s}}),new Promise((i,r)=>n({authorizedEvent:e,...t&&{billingAddress:t},...s&&{deliveryAddress:s}},{resolve:i,reject:r}))}).then(()=>this.handleAdditionalDetails(s)).catch(e=>this.handleError(new i("ERROR","Something went wrong while parsing PayPal Order",{cause:e}))):(this.handleError(new i("ERROR","PayPal order actions are not available")),Promise.resolve()):(this.handleAdditionalDetails(s),Promise.resolve())}),this.handleSubmit=this.handleSubmit.bind(this),this.handleOnShippingAddressChange=this.handleOnShippingAddressChange.bind(this),this.handleOnShippingOptionsChange=this.handleOnShippingOptionsChange.bind(this)}}m(u,"type",a.paypal),m(u,"subtype","sdk"),m(u,"defaultProps",s);export{u as default}; //# sourceMappingURL=Paypal.js.map