Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@types/pdfkit": "^0.17.5",
"@types/pg": "^8.16.0",
"@types/supertest": "^6.0.3",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^6.16.0",
"@typescript-eslint/parser": "^6.15.0",
"eslint": "^8.56.0",
Expand Down
24 changes: 24 additions & 0 deletions backend/src/controllers/paymentController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from 'express';
import { AnchorService } from '../services/anchorService.js';
import { Keypair } from '@stellar/stellar-sdk';
import { findConversionPaths, type PathfindRequest } from '../services/crossAssetPaymentService.js';

export class PaymentController {
/**
Expand Down Expand Up @@ -44,6 +45,29 @@ export class PaymentController {
}
}

/**
* POST /api/payments/pathfind
*/
static async findPaths(req: Request, res: Response) {
const { fromAsset, toAsset, amount } = req.body as PathfindRequest;

if (!fromAsset || !toAsset || !amount || amount <= 0) {
return res
.status(400)
.json({
error: 'Invalid pathfind request: fromAsset, toAsset, and positive amount required',
});
}

try {
const paths = await findConversionPaths({ fromAsset, toAsset, amount });
res.json({ paths });
} catch (error: any) {
console.error('Pathfinding error:', error);
res.status(500).json({ error: error.message });
}
}

/**
* GET /api/payments/sep31/status/:domain/:id
*/
Expand Down
195 changes: 177 additions & 18 deletions backend/src/controllers/webhook.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { WebhookService } from '../services/webhook.service.js';
import { WebhookService, WEBHOOK_EVENTS } from '../services/webhook.service.js';
import { z } from 'zod';

const subscribeSchema = z.object({
Expand All @@ -8,11 +8,33 @@ const subscribeSchema = z.object({
events: z.array(z.string()).default(['*']),
});

const updateSchema = z.object({
url: z.string().url().optional(),
secret: z.string().min(16).optional(),
events: z.array(z.string()).optional(),
is_active: z.boolean().optional(),
});

function getIdParam(params: Record<string, string | string[] | undefined>): string {
const id = params.id;
if (Array.isArray(id)) {
return id[0] || '';
}
return id || '';
}

export class WebhookController {
static async subscribe(req: Request, res: Response) {
try {
const organization_id = (req.user as any)?.organizationId;
if (!organization_id) {
res.status(401).json({ error: 'Organization not identified' });
return;
}

const validatedData = subscribeSchema.parse(req.body);
const subscription = await WebhookService.subscribe(
organization_id,
validatedData.url,
validatedData.secret,
validatedData.events
Expand All @@ -23,32 +45,169 @@ export class WebhookController {
res.status(400).json({ error: error.issues });
return;
}
console.error('Webhook subscription error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
}

static listSubscriptions(req: Request, res: Response) {
const subscriptions = WebhookService.listSubscriptions();
res.json(subscriptions);
static async update(req: Request, res: Response) {
try {
const organization_id = (req.user as any)?.organizationId;
if (!organization_id) {
res.status(401).json({ error: 'Organization not identified' });
return;
}

const id = getIdParam(req.params);
if (!id) {
res.status(400).json({ error: 'Subscription ID is required' });
return;
}

const validatedData = updateSchema.parse(req.body);

const subscription = await WebhookService.updateSubscription(
id,
organization_id,
validatedData
);
if (!subscription) {
res.status(404).json({ error: 'Subscription not found' });
return;
}
res.json(subscription);
} catch (error) {
if (error instanceof z.ZodError) {
res.status(400).json({ error: error.issues });
return;
}
console.error('Webhook update error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
}

static deleteSubscription(req: Request, res: Response) {
const { id } = req.params;
const success = WebhookService.deleteSubscription(id as string);
if (success) {
res.status(204).send();
return;
static async listSubscriptions(req: Request, res: Response) {
try {
const organization_id = (req.user as any)?.organizationId;
if (!organization_id) {
res.status(401).json({ error: 'Organization not identified' });
return;
}

const subscriptions = await WebhookService.listSubscriptions(organization_id);
res.json(subscriptions);
} catch (error) {
console.error('List subscriptions error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
res.status(404).json({ error: 'Subscription not found' });
}

// Debug endpoint to trigger a mock event
static async getSubscription(req: Request, res: Response) {
try {
const organization_id = (req.user as any)?.organizationId;
if (!organization_id) {
res.status(401).json({ error: 'Organization not identified' });
return;
}

const id = getIdParam(req.params);
if (!id) {
res.status(400).json({ error: 'Subscription ID is required' });
return;
}

const subscription = await WebhookService.getSubscriptionById(id, organization_id);
if (!subscription) {
res.status(404).json({ error: 'Subscription not found' });
return;
}
res.json(subscription);
} catch (error) {
console.error('Get subscription error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
}

static async deleteSubscription(req: Request, res: Response) {
try {
const organization_id = (req.user as any)?.organizationId;
if (!organization_id) {
res.status(401).json({ error: 'Organization not identified' });
return;
}

const id = getIdParam(req.params);
if (!id) {
res.status(400).json({ error: 'Subscription ID is required' });
return;
}

const success = await WebhookService.deleteSubscription(id, organization_id);
if (success) {
res.status(204).send();
return;
}
res.status(404).json({ error: 'Subscription not found' });
} catch (error) {
console.error('Delete subscription error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
}

static async getDeliveryLogs(req: Request, res: Response) {
try {
const organization_id = (req.user as any)?.organizationId;
if (!organization_id) {
res.status(401).json({ error: 'Organization not identified' });
return;
}

const id = getIdParam(req.params);
if (!id) {
res.status(400).json({ error: 'Subscription ID is required' });
return;
}

const subscription = await WebhookService.getSubscriptionById(id, organization_id);
if (!subscription) {
res.status(404).json({ error: 'Subscription not found' });
return;
}

const limit = req.query.limit ? parseInt(req.query.limit as string) : 20;
const logs = await WebhookService.getDeliveryLogs(id, limit);
res.json(logs);
} catch (error) {
console.error('Get delivery logs error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
}

static async getEvents(req: Request, res: Response) {
res.json({
events: Object.values(WEBHOOK_EVENTS),
description: 'Available webhook event types for subscription',
});
}

static async triggerMockEvent(req: Request, res: Response) {
const { event, payload } = req.body;
await WebhookService.dispatch(
(event as string) || 'payment.completed',
payload || { id: 'test_tx_123', amount: 100 }
);
res.json({ message: 'Mock event dispatched' });
try {
const organization_id = (req.user as any)?.organizationId;
if (!organization_id) {
res.status(401).json({ error: 'Organization not identified' });
return;
}

const { event, payload } = req.body;
await WebhookService.dispatch(
(event as string) || 'payment.completed',
organization_id,
payload || { id: 'test_tx_123', amount: 100 }
);
res.json({ message: 'Mock event dispatched' });
} catch (error) {
console.error('Trigger mock event error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
}
}
Loading