Skip to content

Commit

Permalink
Merge pull request #151 from matjaz/invoice_has_expired
Browse files Browse the repository at this point in the history
feat: added invoice.hasExpired()
  • Loading branch information
rolznz authored Oct 20, 2024
2 parents 417e563 + a4d745b commit 1e56a58
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/invoice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ describe("Invoice", () => {
"2023-11-15T13:01:22.000Z",
);
expect(decodedInvoice.description).toBeNull();
expect(decodedInvoice.hasExpired()).toBe(true);
});

test("decode invoice without expiry", () => {
const decodedInvoice = new Invoice({ pr: paymentRequestWithoutExpiry });
expect(decodedInvoice.expiryDate).toBeUndefined();
expect(decodedInvoice.hasExpired()).toBe(false);
});

test("decode invoice with description", () => {
Expand All @@ -44,4 +46,10 @@ describe("Invoice", () => {
const decodedInvoice = new Invoice({ pr: signetPaymentRequest });
expect(decodedInvoice.satoshi).toBe(75831);
});

test("did not expired", () => {
const decodedInvoice = new Invoice({ pr: paymentRequestWithoutMemo });
decodedInvoice.expiryDate = new Date(Date.now() + 1000);
expect(decodedInvoice.hasExpired()).toBe(false);
});
});
8 changes: 8 additions & 0 deletions src/invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ export default class Invoice {

return json.settled;
}

hasExpired() {
const { expiryDate } = this;
if (expiryDate) {
return expiryDate.getTime() < Date.now();
}
return false;
}
}

0 comments on commit 1e56a58

Please sign in to comment.