@adyen/adyen-web
Version:
[](https://www.npmjs.com/package/@adyen/adyen-web)
3 lines (2 loc) • 4.4 kB
JavaScript
import{AnalyticsLogEvent as n,LogEventSubtype as o,LogEventType as t}from"../../core/Analytics/events/AnalyticsLogEvent.js";function e(n,o,t){return o in n?Object.defineProperty(n,o,{value:t,enumerable:!0,configurable:!0,writable:!0}):n[o]=t,n}function i(n,o){return o=null!=o?o:{},Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(o)):function(n){var o=Object.keys(n);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(n);o.push.apply(o,t)}return o}(Object(o)).forEach(function(t){Object.defineProperty(n,t,Object.getOwnPropertyDescriptor(o,t))}),n}function a(n,o){if(null==n)return{};var t,e,i,a={};if("undefined"!=typeof Reflect&&Reflect.ownKeys){for(t=Reflect.ownKeys(Object(n)),i=0;i<t.length;i++)e=t[i],o.indexOf(e)>=0||Object.prototype.propertyIsEnumerable.call(n,e)&&(a[e]=n[e]);return a}if(a=function(n,o){if(null==n)return{};var t,e,i={},a=Object.getOwnPropertyNames(n);for(e=0;e<a.length;e++)t=a[e],o.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(n,t)&&(i[t]=n[t]);return i}(n,o),Object.getOwnPropertySymbols)for(t=Object.getOwnPropertySymbols(n),i=0;i<t.length;i++)e=t[i],o.indexOf(e)>=0||Object.prototype.propertyIsEnumerable.call(n,e)&&(a[e]=n[e]);return a}const s=3e3,r="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 l{async initialise(){const[n]=await Promise.all([this.callSessionsDonationCampaigns(),new Promise(n=>setTimeout(n,this.delayMS))]);return n}async callSessionsDonationCampaigns(){const e=new n({component:l.type,type:t.apiRequest,subType:o.donationCampaigns,message:"Sessions flow: calling donationCampaigns endpoint"});this.core.modules.analytics.sendAnalytics(e);const i=await this.makeSessionsDonationCampaignsCall(),[a]=(null==i?void 0:i.donationCampaigns)||[];return a?this.handleDonationCampaign(a):null}handleDonationCampaign(n){const{id:o,campaignName:t}=n,s=a(n,["id","campaignName"]),r=s.donation.type,l=i(function(n){for(var o=1;o<arguments.length;o++){var t=null!=arguments[o]?arguments[o]:{},i=Object.keys(t);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(t).filter(function(n){return Object.getOwnPropertyDescriptor(t,n).enumerable}))),i.forEach(function(o){e(n,o,t[o])})}return n}({},s),{onCancel:n=>{var o;null===(o=this.onDonationCompleted)||void 0===o||o.call(this,{didDonate:!1})},onDonate:(n,t)=>{const e={amount:n.data.amount,donationCampaignId:o,donationType:r};this.callSessionsDonations(e,t)},commercialTxAmount:this.commercialTxAmount});if("roundup"===r&&!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 l}async callSessionsDonations(n,o){try{const i=await this.makeSessionDonationsCall(n);var t,e;if("Authorised"===i.resultCode)o.setStatus("success"),null===(t=this.onDonationCompleted)||void 0===t||t.call(this,{didDonate:!0});else o.setStatus("error"),null===(e=this.onDonationFailed)||void 0===e||e.call(this,i.resultCode)}catch(n){var i;o.setStatus("error"),null===(i=this.onDonationFailed)||void 0===i||i.call(this,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){var t,i,a,s;e(this,"core",void 0),e(this,"commercialTxAmount",0),e(this,"onDonationCompleted",void 0),e(this,"onDonationFailed",void 0),e(this,"delayMS",void 0),this.core=n,this.onDonationCompleted=null===(i=n.options.donation)||void 0===i?void 0:i.onDonationSuccess,this.onDonationFailed=null===(a=n.options.donation)||void 0===a?void 0:a.onDonationFailure,this.commercialTxAmount=o.commercialTxAmount,this.delayMS=null!==(t=null===(s=n.options.donation)||void 0===s?void 0:s.delay)&&void 0!==t?t:3e3}}e(l,"type","donationCampaignService");export{s as DEFAULT_DONATION_AUTO_START_DELAY_MS,r as REPARENT_WITHOUT_AUTO_START_ERROR_MSG,l as default};
//# sourceMappingURL=DonationCampaignService.js.map