forked from lnbits/tpos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
356 lines (296 loc) · 11.2 KB
/
Copy pathmodels.py
File metadata and controls
356 lines (296 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from __future__ import annotations
from datetime import datetime
from time import time
from typing import Any, Literal
from fastapi import Query
from pydantic import BaseModel, Field, validator
class PayLnurlWData(BaseModel):
lnurl: str
class CreateWithdrawPay(BaseModel):
pay_link: str
class CreateTposInvoice(BaseModel):
amount: int = Query(..., ge=1)
memo: str | None = Query(None)
exchange_rate: float | None = Query(None, ge=0.0)
details: dict | None = Query(None)
notes: dict | None = Query(None)
inventory: InventorySale | None = Query(None)
tip_amount: int | None = Query(None, ge=1)
user_lnaddress: str | None = Query(None)
internal_memo: str | None = Query(None, max_length=512)
pay_in_fiat: bool = Query(False)
fiat_method: str | None = Query(None)
payment_method: str | None = Query(None)
amount_fiat: float | None = Query(None, ge=0.0)
tip_amount_fiat: float | None = Query(None, ge=0.0)
class InventorySaleItem(BaseModel):
id: str
quantity: int = Field(1, ge=1)
class InventorySale(BaseModel):
inventory_id: str
tags: list[str] = Field(default_factory=list)
items: list[InventorySaleItem] = Field(default_factory=list)
class CreateTposData(BaseModel):
wallet: str | None
name: str | None
currency: str | None
use_inventory: bool = Field(False)
inventory_id: str | None = None
inventory_tags: list[str] | None = None
inventory_omit_tags: list[str] | None = None
tax_inclusive: bool = Field(True)
tax_default: float | None = Field(0.0)
tip_options: str = Field("[]")
tip_wallet: str = Field("")
withdraw_time: int = Field(0)
withdraw_between: int = Field(10, ge=1)
withdraw_limit: int | None = Field(None, ge=1)
withdraw_time_option: str | None = Field(None)
withdraw_premium: float | None = Field(None)
lnaddress: bool = Field(False)
lnaddress_cut: int | None = Field(0)
enable_receipt_print: bool = Query(False)
enable_remote: bool = Query(False)
business_name: str | None
business_address: str | None
business_vat_id: str | None
only_show_sats_on_bitcoin: bool = Query(True)
allow_price_adjustment: bool = Field(True)
fiat_provider: str | None = Field(None)
stripe_card_payments: bool = False
stripe_reader_id: str | None = None
allow_cash_settlement: bool = Field(False)
onchain_enabled: bool = Field(False)
onchain_wallet_id: str | None = None
onchain_zero_conf: bool = Field(True)
@validator("tax_default", pre=True, always=True)
def default_tax_when_none(cls, v):
return 0.0 if v is None else v
class TposClean(BaseModel):
id: str
name: str
currency: str
tax_inclusive: bool
tax_default: float | None = None
withdraw_time: int
withdraw_between: int
withdraw_limit: int | None = None
withdraw_time_option: str | None = None
withdraw_premium: float | None = None
withdrawn_amount: int = 0
lnaddress: bool | None = None
lnaddress_cut: int = 0
items: str | None = None
use_inventory: bool = False
inventory_id: str | None = None
inventory_tags: str | None = None
inventory_omit_tags: str | None = None
tip_options: str | None = None
enable_receipt_print: bool
enable_remote: bool = False
business_name: str | None = None
business_address: str | None = None
business_vat_id: str | None = None
only_show_sats_on_bitcoin: bool = True
allow_price_adjustment: bool = True
fiat_provider: str | None = None
stripe_card_payments: bool = False
stripe_reader_id: str | None = None
allow_cash_settlement: bool = False
onchain_enabled: bool = False
onchain_wallet_id: str | None = None
onchain_zero_conf: bool = True
@property
def withdraw_maximum(self) -> int:
if not self.withdraw_limit:
return 0
return self.withdraw_limit - self.withdrawn_amount
@property
def can_withdraw(self) -> bool:
now = int(time())
seconds = (
self.withdraw_between * 60
if self.withdraw_time_option != "secs"
else self.withdraw_between
)
last_withdraw_time = self.withdraw_time - now
return last_withdraw_time < seconds
class Tpos(TposClean, BaseModel):
wallet: str
tip_wallet: str | None = None
class TposPayment(BaseModel):
id: str
tpos_id: str
payment_hash: str
amount: int = 0
paid: bool = False
payment_method: str | None = None
onchain_address: str | None = None
onchain_wallet_id: str | None = None
onchain_zero_conf: bool = True
mempool_endpoint: str | None = None
balance: int = 0
pending: int = 0
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: datetime = Field(default_factory=datetime.utcnow)
class TposInvoiceResponse(BaseModel):
payment_hash: str
bolt11: str
payment_request: str
tpos_payment_id: str
payment_options: list[str] = Field(default_factory=list)
onchain_address: str | None = None
onchain_amount_sat: int | None = None
payment_method: str | None = None
extra: dict[str, Any] = Field(default_factory=dict)
class LnurlCharge(BaseModel):
id: str
tpos_id: str
amount: int = 0
claimed: bool = False
class Item(BaseModel):
image: str | None
price: float
title: str
description: str | None
tax: float | None = Field(0, ge=0.0)
disabled: bool = False
categories: list[str] | None = []
@validator("tax", pre=True, always=True)
def set_default_tax(cls, v):
return v or 0
class CreateUpdateItemData(BaseModel):
items: list[Item]
class TapToPay(BaseModel):
type: str = "tap_to_pay"
payment_intent_id: str | None = None
client_secret: str | None = None
amount: int = 0
currency: str | None = None
tpos_id: str | None = None
payment_hash: str | None = None
paid: bool = False
class PrintReceiptRequest(BaseModel):
receipt_type: Literal["receipt", "order_receipt"] = "receipt"
class ReceiptItemData(BaseModel):
title: str = ""
note: str | None = None
quantity: int = 0
price: float = 0.0
class ReceiptDetailsData(BaseModel):
currency: str = "sats"
exchange_rate: float = 1.0
tax_value: float = 0.0
tax_included: bool = False
items: list[ReceiptItemData] = Field(default_factory=list)
class ReceiptExtraData(BaseModel):
amount: int = 0
paid_in_fiat: bool = False
fiat_method: str | None = None
fiat_payment_request: str | None = None
details: ReceiptDetailsData = Field(default_factory=ReceiptDetailsData)
class ReceiptData(BaseModel):
paid: bool = False
extra: ReceiptExtraData = Field(default_factory=ReceiptExtraData)
created_at: Any = None
business_name: str | None = None
business_address: str | None = None
business_vat_id: str | None = None
only_show_sats_on_bitcoin: bool = True
def paid_in_fiat(self) -> bool:
return bool(
self.extra.paid_in_fiat
or self.extra.fiat_method
or self.extra.fiat_payment_request
)
def show_bitcoin_details(self) -> bool:
return (not self.only_show_sats_on_bitcoin) or (not self.paid_in_fiat())
def subtotal(self) -> float:
if self.extra.details.items:
return sum(item.price * item.quantity for item in self.extra.details.items)
rate = self.extra.details.exchange_rate or 1.0
return self.extra.amount / rate
def total(self) -> float:
if not self.extra.details.items:
rate = self.extra.details.exchange_rate or 1.0
return self.extra.amount / rate
if self.extra.details.tax_included:
return self.subtotal()
return self.subtotal() + self.extra.details.tax_value
def format_money(self, amount: float) -> str:
return f"{amount:.2f} {self.extra.details.currency.upper()}"
def formatted_created_at(self) -> str | None:
if not self.created_at:
return None
if isinstance(self.created_at, datetime):
return self.created_at.strftime("%Y-%m-%d %H:%M")
value = str(self.created_at).strip()
if not value:
return None
try:
parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
return parsed.strftime("%Y-%m-%d %H:%M")
except ValueError:
return value
def render_text(
self, receipt_type: Literal["receipt", "order_receipt"] = "receipt"
) -> str:
lines: list[str] = []
lines.append("ORDER" if receipt_type == "order_receipt" else "RECEIPT")
formatted_created_at = self.formatted_created_at()
if formatted_created_at:
lines.append(formatted_created_at)
if self.show_bitcoin_details() and receipt_type != "order_receipt":
lines.append(
f"Rate (sat/{self.extra.details.currency}): "
f"{self.extra.details.exchange_rate:.2f}"
)
lines.append("")
for item in self.extra.details.items:
if item.title.strip():
lines.append(item.title.strip())
if receipt_type == "order_receipt":
lines.append(f"Qty: {item.quantity}")
else:
lines.append(f"{item.quantity} x {self.format_money(item.price)}")
if item.note and item.note.strip():
lines.append(item.note.strip())
lines.append("")
if receipt_type != "order_receipt":
lines.append(f"Subtotal: {self.format_money(self.subtotal())}")
lines.append(f"Tax: {self.format_money(self.extra.details.tax_value)}")
lines.append(f"Total: {self.format_money(self.total())}")
if self.show_bitcoin_details():
lines.append(f"Total (sats): {self.extra.amount}")
lines.append("")
lines.append("Thank you for your purchase!")
if receipt_type != "order_receipt":
if self.business_name:
lines.append(self.business_name)
if self.business_address:
lines.extend(
line for line in self.business_address.splitlines() if line.strip()
)
if self.business_vat_id:
lines.append(f"VAT: {self.business_vat_id}")
while lines and not lines[-1].strip():
lines.pop()
return "\n".join(lines)
def to_api_dict(self) -> dict[str, Any]:
data = self.dict()
details = data.get("extra", {}).get("details", {})
details["exchangeRate"] = details.pop("exchange_rate", 1.0)
details["taxValue"] = details.pop("tax_value", 0.0)
details["taxIncluded"] = details.pop("tax_included", False)
data["created_at"] = self.formatted_created_at()
data["print_text"] = self.render_text("receipt")
data["order_print_text"] = self.render_text("order_receipt")
return data
class ReceiptPrint(BaseModel):
type: str = "receipt_print"
tpos_id: str | None = None
payment_hash: str | None = None
receipt_type: Literal["receipt", "order_receipt"] = "receipt"
print_text: str = ""
receipt: dict[str, Any] = Field(default_factory=dict)
CreateTposInvoice.update_forward_refs(InventorySale=InventorySale)