ngx-paypal-with-intent
Version:
Paypal integration for Angular 6, 7 and beyond
2 lines • 6.92 kB
JavaScript
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("rxjs"),require("rxjs/operators")):"function"==typeof define&&define.amd?define("ngx-paypal",["exports","@angular/core","rxjs","rxjs/operators"],t):t(n["ngx-paypal"]={},n.ng.core,n.rxjs,n.rxjs.operators)}(this,function(n,t,e,o){"use strict";var i={Card:0,Credit:1,Elv:2};i[i.Card]="Card",i[i.Credit]="Credit",i[i.Elv]="Elv";var a={ClientSideREST:0,ServerSideREST:1};a[a.ClientSideREST]="ClientSideREST",a[a.ServerSideREST]="ServerSideREST";var r=function(){function n(){this.useGlobalConfig=!1,this.registerPayPalScriptWhenContainerIsReady=!1,this.defaultPollInterval=50,this.maximumPollWaitTime=5e3,this.paypalWindowName="paypal",this.paypalWindowScriptInitiated="ngx-paypal-script-initiated",this.paypalScriptUrl="https://www.paypalobjects.com/api/checkout.js",this.payPalButtonContainerIdPrefix="ngx-paypal-button-container-",this.ngUnsubscribe=new e.Subject}return Object.defineProperty(n.prototype,"payPalButtonContainerElem",{set:function(n){n&&(this._payPalButtonContainerElem=n)},enumerable:!0,configurable:!0}),n.prototype.ngOnChanges=function(n){this.config&&this.initPayPal()},n.prototype.ngAfterViewInit=function(){this.registerPayPalScriptWhenContainerIsReady&&this._payPalButtonContainerElem&&(this.setupScript(),this.registerPayPalScriptWhenContainerIsReady=!1)},n.prototype.ngOnDestroy=function(){this.ngUnsubscribe.next(),this.ngUnsubscribe.complete()},n.prototype.initPayPal=function(){this.payPalButtonContainerId=""+this.payPalButtonContainerIdPrefix+this.getPseudoUniqueNumber(),window[this.paypalWindowName]?this.handleScriptRegistering():!0===window[this.paypalWindowScriptInitiated]?this.pollUntilScriptAvailable():(window[this.paypalWindowScriptInitiated]=!0,this.addPayPalScriptToPage())},n.prototype.getPseudoUniqueNumber=function(){return(new Date).valueOf()},n.prototype.pollUntilScriptAvailable=function(){var t=this,i=e.interval(this.defaultPollInterval).pipe(o.takeUntil(this.ngUnsubscribe),o.map(function(n){if(n>=t.maximumPollWaitTime)return console.warn("PayPal script was not loaded after '"+t.maximumPollWaitTime+"' maximum polling time."),void i.unsubscribe();window[t.paypalWindowName]&&(t.handleScriptRegistering(),i.unsubscribe())})).subscribe()},n.prototype.addPayPalScriptToPage=function(){var n=this,t=document.createElement("script");t.innerHTML="",t.src=this.paypalScriptUrl,t.onload=function(){return n.handleScriptRegistering()},t.async=!0,t.defer=!0,this.paypalScriptElem.nativeElement.appendChild(t)},n.prototype.handleScriptRegistering=function(){this._payPalButtonContainerElem&&this._payPalButtonContainerElem.nativeElement&&this._payPalButtonContainerElem.nativeElement.id===this.payPalButtonContainerId?this.setupScript():this.registerPayPalScriptWhenContainerIsReady=!0},n.prototype.setupScript=function(){var e=this;if(!this._payPalButtonContainerElem)throw Error("Cannot setup script because paypal button container with id '"+this.payPalButtonContainerId+"' is not yet ready");if(this._payPalButtonContainerElem.nativeElement.innerHTML="",!window[this.paypalWindowName])throw Error("PayPal script is not available");window[this.paypalWindowName].Button.render({env:this.config.environment.toString(),commit:this.config.commit,client:this.getClient(),style:this.config.button,funding:this.getFunding(),payment:function(n,t){if(e.config.integrationType===a.ServerSideREST){if(!e.config.payment)throw Error("You need set up a create payment method and return\n PayPal's payment id when using server side integration");return e.config.payment().toPromise().then(function(n){return n})}if(e.config.integrationType===a.ClientSideREST){if(!e.config.transactions||!Array.isArray(e.config.transactions)||e.config.transactions.length<=0)throw Error("You need to provide at least 1 transaction for client side integration");var i=e.config.experience;return t.payment.create({intent:e.config.intent?e.config.intent:"sale",payment:{transactions:e.config.transactions},experience:{input_fields:{no_shipping:i&&i.noShipping?1:0},presentation:{brand_name:i&&i.brandName?i.brandName:null,logo_image:i&&i.logoImage?i.logoImage:null,locale_code:i&&i.localeCode?i.localeCode:null}}})}},onAuthorize:function(n,t){if(e.config.integrationType===a.ServerSideREST){if(!e.config.onAuthorize)throw Error("You need set up an execute method when using server side integration");return e.config.onAuthorize(n,t).toPromise()}if(e.config.integrationType===a.ClientSideREST)return t.payment.execute().then(function(){if(!e.config.onPaymentComplete)throw Error("You should provide some callback when payment is finished when using client side integration");e.config.onPaymentComplete(n,t)})},onError:function(n){e.config.onError&&e.config.onError(n)},onCancel:function(n,t){e.config.onCancel&&e.config.onCancel(n,t)},onClick:function(){e.config.onClick&&e.config.onClick()},validate:function(n){e.config.validate&&e.config.validate(n)}},"#"+this.payPalButtonContainerId)},n.prototype.getClient=function(){if(this.config.integrationType!==a.ClientSideREST)return undefined;if(!this.config.client)throw Error("You need to setup client information when using client side integration");return{production:this.config.client.production,sandbox:this.config.client.sandbox}},n.prototype.getFunding=function(){var t=this;if(!this.config.funding)return undefined;var i=[],e=[];return this.config.funding.allowed&&this.config.funding.allowed.forEach(function(n){i.push(t.mapFundingType(n))}),this.config.funding.disallowed&&this.config.funding.disallowed.forEach(function(n){e.push(t.mapFundingType(n))}),{allowed:i,disallowed:e}},n.prototype.mapFundingType=function(n){if(n===i.Card)return paypal.FUNDING.CARD;if(n===i.Credit)return paypal.FUNDING.CREDIT;if(n===i.Elv)return paypal.FUNDING.ELV;throw Error("Unsupported funding type '"+n+"'")},n.decorators=[{type:t.Component,args:[{changeDetection:t.ChangeDetectionStrategy.OnPush,selector:"ngx-paypal",template:'\n <div #payPalScriptElem></div>\n <div #payPalButtonContainerElem [id]="payPalButtonContainerId"></div>\n '}]}],n.ctorParameters=function(){return[]},n.propDecorators={config:[{type:t.Input}],useGlobalConfig:[{type:t.Input}],paypalScriptElem:[{type:t.ViewChild,args:["payPalScriptElem"]}],payPalButtonContainerElem:[{type:t.ViewChild,args:["payPalButtonContainerElem"]}]},n}(),p=function(){function n(){}return n.decorators=[{type:t.NgModule,args:[{imports:[],declarations:[r],exports:[r]}]}],n}(),l=function s(n,t,i){this.integrationType=n,this.environment=t,this.commit=!0,this.intent="sale",Object.assign(this,i)};n.NgxPayPalModule=p,n.PayPalEnvironment={Sandbox:"sandbox",Production:"production"},n.PayPalFunding=i,n.PayPalIntegrationType=a,n.PayPalConfig=l,n.NgxPaypalComponent=r,n.ɵa=r,Object.defineProperty(n,"__esModule",{value:!0})});
//# sourceMappingURL=ngx-paypal.umd.min.js.map