Skip to content

Commit 81f9dde

Browse files
authored
Merge pull request #12 from lightsparkdev/feat/cancelinvoice
Add a cancel_invoice function.
2 parents 1bce829 + 85a8114 commit 81f9dde

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

examples/example.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@
222222
print(f"Simulated payment to the invoice done with ID = {payment.id}")
223223
print("")
224224

225+
# Create and cancel an invoice
226+
invoice = client.create_invoice(
227+
node_id=node_id,
228+
amount_msats=42000,
229+
memo="Cancelable Pizza!",
230+
)
231+
print(f"Invoice created from {node_name}: {invoice.id}")
232+
cancelled_invoice = client.cancel_invoice(invoice_id=invoice.id)
233+
print(f"Cancelled invoice {cancelled_invoice.id}")
234+
225235
# Pay invoice sample
226236
#
227237
test_invoice = client.create_test_mode_invoice(

lightspark/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from lightspark.objects.Balances import Balances
1818
from lightspark.objects.BitcoinNetwork import BitcoinNetwork
1919
from lightspark.objects.BlockchainBalance import BlockchainBalance
20+
from lightspark.objects.CancelInvoiceInput import CancelInvoiceInput
21+
from lightspark.objects.CancelInvoiceOutput import CancelInvoiceOutput
2022
from lightspark.objects.Channel import Channel
2123
from lightspark.objects.ChannelClosingTransaction import ChannelClosingTransaction
2224
from lightspark.objects.ChannelFees import ChannelFees

lightspark/lightspark_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
)
5959
from lightspark.requests.requester import Requester
6060
from lightspark.scripts.bitcoin_fee_estimate import BITCOIN_FEE_ESTIMATE_QUERY
61+
from lightspark.scripts.cancel_invoice import CANCEL_INVOICE_MUTATION
6162
from lightspark.scripts.claim_uma_invitation import (
6263
CLAIM_UMA_INVITATION_MUTATION,
6364
CLAIM_UMA_INVITATION_WITH_INCENTIVES_MUTATION,
@@ -210,6 +211,26 @@ def create_lnurl_invoice(
210211
self._requester, json["create_lnurl_invoice"]["invoice"]
211212
)
212213

214+
def cancel_invoice(
215+
self,
216+
invoice_id: str,
217+
) -> Invoice:
218+
"""Cancels an existing unpaid invoice and returns that invoice. Cancelled invoices cannot be paid.
219+
220+
Args:
221+
invoice_id (str): The ID of the invoice to cancel.
222+
223+
Returns:
224+
Invoice: An `Invoice` object representing the cancelled invoice.
225+
"""
226+
logger.info("Canceling an invoice with id %s.", invoice_id)
227+
json = self._requester.execute_graphql(
228+
CANCEL_INVOICE_MUTATION,
229+
{"invoice_id": invoice_id},
230+
)
231+
232+
return Invoice_from_json(self._requester, json["cancel_invoice"]["invoice"])
233+
213234
def create_node_wallet_address(
214235
self,
215236
node_id: str,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright ©, 2022-present, Lightspark Group, Inc. - All Rights Reserved
2+
3+
from dataclasses import dataclass
4+
from typing import Any, Mapping
5+
6+
7+
@dataclass
8+
class CancelInvoiceInput:
9+
invoice_id: str
10+
11+
def to_json(self) -> Mapping[str, Any]:
12+
return {
13+
"cancel_invoice_input_invoice_id": self.invoice_id,
14+
}
15+
16+
17+
def from_json(obj: Mapping[str, Any]) -> CancelInvoiceInput:
18+
return CancelInvoiceInput(
19+
invoice_id=obj["cancel_invoice_input_invoice_id"],
20+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright ©, 2022-present, Lightspark Group, Inc. - All Rights Reserved
2+
3+
from dataclasses import dataclass
4+
from typing import Any, Mapping
5+
6+
from lightspark.requests.requester import Requester
7+
8+
9+
@dataclass
10+
class CancelInvoiceOutput:
11+
requester: Requester
12+
13+
invoice_id: str
14+
15+
def to_json(self) -> Mapping[str, Any]:
16+
return {
17+
"cancel_invoice_output_invoice": {"id": self.invoice_id},
18+
}
19+
20+
21+
FRAGMENT = """
22+
fragment CancelInvoiceOutputFragment on CancelInvoiceOutput {
23+
__typename
24+
cancel_invoice_output_invoice: invoice {
25+
id
26+
}
27+
}
28+
"""
29+
30+
31+
def from_json(requester: Requester, obj: Mapping[str, Any]) -> CancelInvoiceOutput:
32+
return CancelInvoiceOutput(
33+
requester=requester,
34+
invoice_id=obj["cancel_invoice_output_invoice"]["id"],
35+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright ©, 2022-present, Lightspark Group, Inc. - All Rights Reserved
2+
3+
from lightspark.objects.Invoice import FRAGMENT as InvoiceFragment
4+
5+
CANCEL_INVOICE_MUTATION = f"""
6+
mutation CancelInvoice(
7+
$invoice_id: ID!
8+
) {{
9+
cancel_invoice(input: {{
10+
invoice_id: $invoice_id
11+
}}) {{
12+
invoice {{
13+
...InvoiceFragment
14+
}}
15+
}}
16+
}}
17+
18+
{InvoiceFragment}
19+
"""

0 commit comments

Comments
 (0)