Skip to content

Commit 2d32a6b

Browse files
committed
feat: Add MAX_PAYMENTS_PER_INVOICE
1 parent 4796ad6 commit 2d32a6b

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export const INVOICE_MIN_AMOUNT = 0.00001;
33
export const WEBHOOK_DELIVERY_TIMEOUT = 15000; // 15 seconds
44
export const INVOICE_EXPIRATION = 1000 * 60 * 10; // 10 minutes
55
export const MAX_WEBSOCKET_SESSIONS_PER_PAYMENT_NOTIFIER = 10;
6+
export const MAX_PAYMENTS_PER_INVOICE = 10;

src/durable/payment-listener.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import NanoWebsocket, { SendEvent } from '../nano/websocket';
33
import { Invoice, MessageBody, Payment, Service, Webhook } from '../types';
44
import { rawToNano } from '../utils';
55
import { logger } from '../logger';
6-
import { INVOICE_MIN_AMOUNT } from '../constants';
6+
import { INVOICE_MIN_AMOUNT, MAX_PAYMENTS_PER_INVOICE } from '../constants';
77
import { PaymentNotifier } from './payment-notifier';
88

99
export class PaymentListener extends DurableObject<Env> {
@@ -116,6 +116,12 @@ export class PaymentListener extends DurableObject<Env> {
116116
if (paid_total >= invoice.price) {
117117
await this.removePendingInvoice(invoice.id, invoice.pay_address);
118118
await this.paymentReceiver(payments, invoice);
119+
} else if (payments.length >= MAX_PAYMENTS_PER_INVOICE) {
120+
await this.removePendingInvoice(invoice.id, invoice.pay_address);
121+
logger.warn(`Max payments reached for invoice: ${invoice.id}`, {
122+
invoice,
123+
payments,
124+
});
119125
}
120126
}
121127

src/durable/payment-notifier.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DurableObject } from 'cloudflare:workers';
22
import { logger } from '../logger';
3-
import { MAX_WEBSOCKET_SESSIONS_PER_PAYMENT_NOTIFIER } from '../constants';
3+
import { MAX_PAYMENTS_PER_INVOICE, MAX_WEBSOCKET_SESSIONS_PER_PAYMENT_NOTIFIER } from '../constants';
44

55
export type PaymentNotification = {
66
from: string;
@@ -84,7 +84,7 @@ export class PaymentNotifier extends DurableObject<Env> {
8484

8585
// Load the last 10 payments from the history stored on disk, and send them to the
8686
// client.
87-
const payments = await this.storage.list<Record<any, any>>({ reverse: true, limit: 10, prefix: 'payment_' });
87+
const payments = await this.storage.list<Record<any, any>>({ reverse: true, limit: MAX_PAYMENTS_PER_INVOICE, prefix: 'payment_' });
8888
[...payments.values()].forEach((value) => {
8989
webSocket.send(JSON.stringify(value));
9090
});

0 commit comments

Comments
 (0)