UNPKG

breezi-js

Version:

Vanilla JavaScript SDK for Breezi PayPal integration

3 lines (2 loc) 4.64 kB
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).BreeziWidget={})}(this,function(e){"use strict";class r{constructor(e){this.sdkLoaded=!1,this.config=e,this.logger={debug:(e,r)=>{this.config.debug&&"undefined"!=typeof console&&console.debug&&console.debug(`[Breezi Debug] ${e}`,r||"")},error:(e,r)=>{"undefined"!=typeof console&&console.error&&console.error(`[Breezi Error] ${e}`,r||"")}},this.validateConfig()}validateConfig(){if(!this.config.environment)throw this.createError("Environment is required","CONFIG_ERROR","MISSING_ENVIRONMENT");if(!["sandbox","production"].includes(this.config.environment))throw this.createError('Environment must be "sandbox" or "production"',"CONFIG_ERROR","INVALID_ENVIRONMENT");if(!this.config.providers?.paypal?.clientId)throw this.createError("PayPal client ID is required in providers.paypal.clientId","CONFIG_ERROR","MISSING_PAYPAL_CLIENT_ID")}createError(e,r,t){const o=new Error(e);return o.type=r,o.code=t,o.retryable="NETWORK_ERROR"===r,o}async loadPayPalSDK(e){return new Promise((r,t)=>{if(window.paypal)return this.sdkLoaded=!0,void r();const o=document.createElement("script");o.src=`https://www.paypal.com/sdk/js?client-id=${this.config.providers.paypal.clientId}&currency=${e}&components=buttons`,o.onload=()=>{this.sdkLoaded=!0,this.logger.debug("PayPal SDK loaded successfully"),r()},o.onerror=()=>{const e=this.createError("Failed to load PayPal SDK","NETWORK_ERROR","SDK_LOAD_FAILED");this.logger.error("PayPal SDK loading failed",e),t(e)},document.body.appendChild(o)})}async renderPayPalButton(e,r){try{if(!e)throw this.createError("Container ID is required","VALIDATION_ERROR","MISSING_CONTAINER_ID");if(!r.amount||r.amount<=0)throw this.createError("Amount must be greater than 0","VALIDATION_ERROR","INVALID_AMOUNT");if(!r.currency||3!==r.currency.length)throw this.createError("Currency must be a valid 3-letter ISO code","VALIDATION_ERROR","INVALID_CURRENCY");const t=document.getElementById(e);if(!t)throw this.createError(`Container with ID "${e}" not found`,"VALIDATION_ERROR","CONTAINER_NOT_FOUND");this.sdkLoaded||await this.loadPayPalSDK(r.currency),t.innerHTML="",this.logger.debug("Rendering PayPal button",{containerId:e,options:r}),window.paypal.Buttons({createOrder:(e,t)=>(this.logger.debug("Creating PayPal order"),t.order.create({purchase_units:[{amount:{value:r.amount.toString(),currency_code:r.currency},description:r.description||`Payment of ${r.currency} ${r.amount}`}]})),onApprove:async(e,t)=>{try{this.logger.debug("Payment approved, capturing...");const e=await t.order.capture();this.logger.debug("Payment captured successfully:",e);const o={id:e.id,status:"completed",amount:r.amount,currency:r.currency,provider:"paypal",createdAt:(new Date).toISOString(),details:e};r.onSuccess?.(o)}catch(e){this.logger.error("Payment capture failed:",e);const t=this.createError("Payment capture failed","PROVIDER_ERROR","CAPTURE_FAILED");r.onError?.(t)}},onCancel:()=>{this.logger.debug("Payment cancelled by user"),r.onCancel?.()},onError:e=>{this.logger.error("PayPal SDK error:",e);const t=this.createError("PayPal error occurred","PROVIDER_ERROR","PAYPAL_ERROR");r.onError?.(t)},style:{color:"blue",shape:"rect",label:"paypal",height:40}}).render(t)}catch(e){throw this.logger.error("Failed to render PayPal button:",e),e}}createPaymentButton(e,r){const t=document.getElementById(e);if(!t)throw this.createError(`Container with ID "${e}" not found`,"VALIDATION_ERROR","CONTAINER_NOT_FOUND");const o=document.createElement("button");o.textContent=r.buttonText||`Pay ${r.currency} ${r.amount}`,Object.assign(o.style,{padding:"12px 24px",backgroundColor:"#0070f3",color:"white",border:"none",borderRadius:"6px",cursor:"pointer",fontSize:"16px",fontWeight:"500",transition:"all 0.2s ease",...r.buttonStyle}),o.addEventListener("click",()=>{const e=this.createError("Custom payment buttons require server-side implementation. Use renderPayPalButton() for direct PayPal integration.","CONFIG_ERROR","CUSTOM_BUTTON_NOT_IMPLEMENTED");r.onError?.(e)}),t.innerHTML="",t.appendChild(o)}getConfig(){return{...this.config}}updateConfig(e){this.config={...this.config,...e},this.validateConfig()}}function t(e){return new r(e)}if("undefined"!=typeof window&&(window.BreeziWidget=r,window.createBreeziWidget=t,window.BreeziConfig)){const e=new r(window.BreeziConfig);window.breezi=e}e.BreeziWidget=r,e.createBreeziWidget=t,e.default=r,Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=widget.min.js.map