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) 2.74 kB
import{AnalyticsLogEvent as n,LogEventSubtype as o,LogEventType as t}from"../../core/Analytics/events/AnalyticsLogEvent.js";function i(n,o,t){return o in n?Object.defineProperty(n,o,{value:t,enumerable:!0,configurable:!0,writable:!0}):n[o]=t,n}const a=3e3,e="Donation instantiation error:: You need to set donation.autoMount to false if you wish to display the Donation component in a different container.\nThe Donation component will be displayed in the default container.";class s{async initialise(){const[n]=await Promise.all([this.callSessionsDonationCampaigns(),new Promise(n=>setTimeout(n,this.delayMS))]);return n}async callSessionsDonationCampaigns(){const i=new n({component:s.type,type:t.apiRequest,subType:o.donationCampaigns,message:"Sessions flow: calling donationCampaigns endpoint"});this.core.modules.analytics.sendAnalytics(i);const a=await this.makeSessionsDonationCampaignsCall(),[e]=a?.donationCampaigns||[];return e?this.handleDonationCampaign(e):null}handleDonationCampaign(n){const{id:o,campaignName:t,...i}=n,a=i.donation.type,e={...i,onCancel:n=>{this.onDonationCompleted?.({didDonate:!1})},onDonate:(n,t)=>{const i={amount:n.data.amount,donationCampaignId:o,donationType:a};this.callSessionsDonations(i,t)},commercialTxAmount:this.commercialTxAmount};if("roundup"===a&&!this.commercialTxAmount)throw new Error('The donation type is "roundup" and the commercialTxAmount is not set.\nIt will not be possible to mount a Donation component.');return e}async callSessionsDonations(n,o){try{const t=await this.makeSessionDonationsCall(n);"Authorised"===t.resultCode?(o.setStatus("success"),this.onDonationCompleted?.({didDonate:!0})):(o.setStatus("error"),this.onDonationFailed?.(t.resultCode))}catch(n){o.setStatus("error"),this.onDonationFailed?.(n)}}async makeSessionsDonationCampaignsCall(){if(!this.core.session)throw new Error("DonationCampaignService requires a session to be configured");return await this.core.session.fetchDonationCampaigns()}async makeSessionDonationsCall(n){if(!this.core.session)throw new Error("DonationCampaignService requires a session to be configured");return await this.core.session.makeDonation(n)}constructor(n,o){i(this,"core",void 0),i(this,"commercialTxAmount",0),i(this,"onDonationCompleted",void 0),i(this,"onDonationFailed",void 0),i(this,"delayMS",void 0),this.core=n,this.onDonationCompleted=n.options.donation?.onDonationSuccess,this.onDonationFailed=n.options.donation?.onDonationFailure,this.commercialTxAmount=o.commercialTxAmount,this.delayMS=n.options.donation?.delay??3e3}}i(s,"type","donationCampaignService");export{a as DEFAULT_DONATION_AUTO_START_DELAY_MS,e as REPARENT_WITHOUT_AUTO_START_ERROR_MSG,s as default}; //# sourceMappingURL=DonationCampaignService.js.map