storenest-commerce
Version:
Complete e-commerce SDK for Storenest platform with React components, multi-language support, secure checkout, and enterprise-grade security
111 lines (110 loc) • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StorenestCheckout = void 0;
const config_1 = require("./config");
const utils_1 = require("./utils");
// Client-side checkout class (safe for browsers)
class StorenestCheckout {
constructor(config = {}) {
this.config = {
theme: 'modern',
currency: 'EUR',
locale: 'en-US',
showOrderSummary: true,
enableGuestCheckout: true,
...config
};
this.apiBaseUrl = (0, config_1.getApiBaseUrl)();
}
// Initialize checkout session (calls server-side)
async initiateCheckout(cartId) {
const headers = await this.getHeaders();
const response = await fetch(`${this.apiBaseUrl}/checkout/init`, {
method: 'POST',
headers,
body: JSON.stringify({ cartId })
});
if (!response.ok) {
throw new Error('Failed to initiate checkout');
}
return await response.json();
}
// Get available shipping methods (non-sensitive)
async getShippingMethods(address) {
const headers = await this.getHeaders();
const response = await fetch(`${this.apiBaseUrl}/checkout/shipping-methods`, {
method: 'POST',
headers,
body: JSON.stringify({ address })
});
if (!response.ok) {
throw new Error('Failed to get shipping methods');
}
return await response.json();
}
// Get available payment methods (non-sensitive)
async getPaymentMethods() {
const headers = await this.getHeaders();
const response = await fetch(`${this.apiBaseUrl}/checkout/payment-methods`, {
method: 'GET',
headers
});
if (!response.ok) {
throw new Error('Failed to get payment methods');
}
return await response.json();
}
// Validate shipping address (non-sensitive)
async validateAddress(address) {
const headers = await this.getHeaders();
const response = await fetch(`${this.apiBaseUrl}/checkout/validate-address`, {
method: 'POST',
headers,
body: JSON.stringify({ address })
});
if (!response.ok) {
throw new Error('Failed to validate address');
}
return await response.json();
}
// Get checkout session status
async getCheckoutStatus(sessionId) {
const headers = await this.getHeaders();
const response = await fetch(`${this.apiBaseUrl}/checkout/status/${sessionId}`, {
method: 'GET',
headers
});
if (!response.ok) {
throw new Error('Failed to get checkout status');
}
return await response.json();
}
// Format price for display (client-side only)
formatPrice(amount) {
return new Intl.NumberFormat(this.config.locale, {
style: 'currency',
currency: this.config.currency
}).format(amount);
}
// Get configuration for UI components
getConfig() {
return { ...this.config };
}
async getHeaders() {
const apiKey = (0, config_1.getApiKey)();
if (!apiKey) {
throw new Error('API key not configured');
}
const { timestamp, signature, nonce } = await (0, utils_1.generateApiToken)(apiKey);
return {
'x-api-key': apiKey,
'x-timestamp': timestamp,
'x-signature': signature,
'x-nonce': nonce,
'Content-Type': 'application/json',
};
}
}
exports.StorenestCheckout = StorenestCheckout;
// Export the main checkout class
exports.default = StorenestCheckout;