UNPKG

shora-ai-payment-sdk

Version:

The first open-source payment SDK designed specifically for AI agents and chatbots - ACP Compatible

141 lines (140 loc) 5.84 kB
import { createAxiosInstance } from './customAxios'; import { PaymentService } from './payments'; import { AuthService } from './auth'; import { parseError } from './error-handling'; import { createSecurityEnhancement, generateEncryptionKey } from './security_enhance'; const pkg = require('../package.json'); export { createSecurityEnhancement, generateEncryptionKey }; class ShoraSDK { constructor(config, httpClient) { const DEFAULT_BASE_URLS = { sandbox: 'https://shora-core.onrender.com', staging: 'https://shora-core.onrender.com', production: 'https://api.shora.cloud', }; const environment = config.environment ?? 'production'; const envBase = process.env.SHORA_API_BASE_URL; let resolvedBase; if (config.baseUrl) { resolvedBase = config.baseUrl; } else if (envBase) { resolvedBase = envBase; } else if (environment === 'production') { resolvedBase = DEFAULT_BASE_URLS.production; } else { throw new Error(`Environment '${environment}' requires explicit baseUrl configuration. ` + `Provide config.baseUrl or set SHORA_API_BASE_URL environment variable. ` + `This prevents accidental calls to production endpoints.`); } if (!resolvedBase) { throw new Error('No baseUrl configured. Provide config.baseUrl, set SHORA_API_BASE_URL, or use environment=production.'); } this.config = { baseUrl: resolvedBase, timeout: 30000, ...config }; if (httpClient) { this.client = httpClient; } else { this.client = createAxiosInstance(resolvedBase, this.config.apiKey); this.client.defaults.headers.common['Content-Type'] = 'application/json'; if (this.config.tenantId) { this.client.defaults.headers.common['X-Tenant-ID'] = this.config.tenantId; } } this.client.interceptors.response.use((response) => response, (error) => { throw parseError(error); }); this.payments = new PaymentService(this.client); this.auth = { createMandate: (request) => new AuthService(this.client).createMandate(request), generateToken: (request) => new AuthService(this.client).generateToken(request), pay: (request) => new AuthService(this.client).pay(request), verifyTrust: (request) => new AuthService(this.client).verifyTrust(request), getTrustStatus: () => new AuthService(this.client).getTrustStatus(), }; this.security = createSecurityEnhancement({ encryptionKey: this.config.encryptionKey || generateEncryptionKey(), auditLogEndpoint: this.config.auditLogEndpoint, enableAuditLogging: this.config.enableAuditLogging || false, tenantId: this.config.tenantId || 'default', sdkVersion: pkg.version, }); } async healthCheck() { return this.payments.healthCheck(); } async createACPCheckout(request) { return this.payments.createACPCheckout(request); } async createPaymentSession(request, options) { return this.payments.createPaymentSession(request, options); } async processPayment(sessionId, paymentMethod, cardToken) { return this.payments.processPayment(sessionId, paymentMethod, cardToken); } async createCheckoutIntent(request, options) { return this.payments.createCheckoutIntent(request, options); } async confirmCheckout(request, options) { return this.payments.confirmCheckout(request, options); } async getPaymentSession(sessionId) { return this.payments.getPaymentSession(sessionId); } async getReceipt(receiptId) { return this.payments.getReceipt(receiptId); } async getSupportedMethods() { return this.payments.getSupportedMethods(); } encryptToken(token, additionalData) { return this.security.encryptToken(token, additionalData); } decryptToken(encryptedToken) { return this.security.decryptToken(encryptedToken); } generateSecurePaymentToken(paymentData) { return this.security.generateSecurePaymentToken(paymentData); } validatePaymentToken(encryptedToken) { return this.security.validatePaymentToken(encryptedToken); } setRequestContext(context) { this.security.setRequestContext(context); } logAudit(action, message, entityId, userId, agentId, amount, currency, status, metadata) { this.security.logAudit(action, message, entityId, userId, agentId, amount, currency, status, metadata); } getAuditLogs(startDate, endDate, action) { return this.security.getAuditLogs(startDate, endDate, action); } async payWithACP(request) { const checkout = await this.createACPCheckout({ amount: request.amount, currency: request.currency, description: `WooCommerce Order #${request.order_id}`, customer: { email: request.customer_email, metadata: { woo_product_id: request.woo_product_id, order_id: request.order_id, ...request.metadata, }, }, metadata: { source: 'woocommerce', product_id: request.woo_product_id, order_id: request.order_id, }, }); return { checkout_id: checkout.checkout_id, checkout_url: checkout.checkout_url, status: checkout.status, expires_at: checkout.expires_at, }; } } export default ShoraSDK;