shora-ai-payment-sdk
Version:
The first open-source payment SDK designed specifically for AI agents and chatbots - ACP Compatible
145 lines (144 loc) • 6.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateEncryptionKey = exports.createSecurityEnhancement = void 0;
const customAxios_1 = require("./customAxios");
const payments_1 = require("./payments");
const auth_1 = require("./auth");
const error_handling_1 = require("./error-handling");
const security_enhance_1 = require("./security_enhance");
Object.defineProperty(exports, "createSecurityEnhancement", { enumerable: true, get: function () { return security_enhance_1.createSecurityEnhancement; } });
Object.defineProperty(exports, "generateEncryptionKey", { enumerable: true, get: function () { return security_enhance_1.generateEncryptionKey; } });
const pkg = require('../package.json');
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 = (0, customAxios_1.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 (0, error_handling_1.parseError)(error);
});
this.payments = new payments_1.PaymentService(this.client);
this.auth = {
createMandate: (request) => new auth_1.AuthService(this.client).createMandate(request),
generateToken: (request) => new auth_1.AuthService(this.client).generateToken(request),
pay: (request) => new auth_1.AuthService(this.client).pay(request),
verifyTrust: (request) => new auth_1.AuthService(this.client).verifyTrust(request),
getTrustStatus: () => new auth_1.AuthService(this.client).getTrustStatus(),
};
this.security = (0, security_enhance_1.createSecurityEnhancement)({
encryptionKey: this.config.encryptionKey || (0, security_enhance_1.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,
};
}
}
exports.default = ShoraSDK;