import { RampWidget } from '@ramp-orchestrator/sdk';
class CryptoPurchaseManager {
private widget: RampWidget | null = null;
init(userId: string) {
this.widget = new RampWidget({
apiKey: process.env.RAMP_API_KEY,
projectId: process.env.RAMP_PROJECT_ID,
externalUserId: userId,
containerId: 'ramp-widget',
defaultCrypto: 'ETH',
});
this.setupEventHandlers();
this.widget.mount();
}
private setupEventHandlers() {
this.widget.on('ready', () => {
console.log('Widget ready');
});
this.widget.on('quoteGenerated', (quote) => {
analytics.track('Quote Generated', { amount: quote.sourceAmount });
});
this.widget.on('orderComplete', async (order) => {
await this.handleOrderComplete(order);
});
this.widget.on('error', (error) => {
errorReporter.capture(error);
});
}
private async handleOrderComplete(order) {
// Track conversion
analytics.track('Purchase Complete', {
orderId: order.id,
amount: order.sourceAmount,
crypto: order.targetCurrency,
});
// Update UI
showSuccessMessage('Purchase complete!');
}
destroy() {
this.widget?.unmount();
this.widget = null;
}
}