mirror of
https://github.com/lnbits/lnbits.git
synced 2025-06-28 01:32:38 +02:00
Merge pull request #1068 from lnbits/improvement-splitpayments
Improvement to payment handling in splitpayments extension
This commit is contained in:
commit
1ae8527d29
@ -4,10 +4,10 @@ import json
|
|||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.core import db as core_db
|
from lnbits.core import db as core_db
|
||||||
from lnbits.core.crud import create_payment
|
|
||||||
from lnbits.core.models import Payment
|
from lnbits.core.models import Payment
|
||||||
from lnbits.helpers import get_current_extension_name, urlsafe_short_hash
|
from lnbits.core.services import create_invoice, pay_invoice
|
||||||
from lnbits.tasks import internal_invoice_listener, register_invoice_listener
|
from lnbits.helpers import get_current_extension_name
|
||||||
|
from lnbits.tasks import register_invoice_listener
|
||||||
|
|
||||||
from .crud import get_livestream_by_track, get_producer, get_track
|
from .crud import get_livestream_by_track, get_producer, get_track
|
||||||
|
|
||||||
@ -44,44 +44,20 @@ async def on_invoice_paid(payment: Payment) -> None:
|
|||||||
# now we make a special kind of internal transfer
|
# now we make a special kind of internal transfer
|
||||||
amount = int(payment.amount * (100 - ls.fee_pct) / 100)
|
amount = int(payment.amount * (100 - ls.fee_pct) / 100)
|
||||||
|
|
||||||
# mark the original payment with two extra keys, "shared_with" and "received"
|
payment_hash, payment_request = await create_invoice(
|
||||||
# (this prevents us from doing this process again and it's informative)
|
wallet_id=tpos.tip_wallet,
|
||||||
# and reduce it by the amount we're going to send to the producer
|
amount=amount, # sats
|
||||||
await core_db.execute(
|
internal=True,
|
||||||
"""
|
|
||||||
UPDATE apipayments
|
|
||||||
SET extra = ?, amount = ?
|
|
||||||
WHERE hash = ?
|
|
||||||
AND checking_id NOT LIKE 'internal_%'
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
json.dumps(
|
|
||||||
dict(
|
|
||||||
**payment.extra,
|
|
||||||
shared_with=[producer.name, producer.id],
|
|
||||||
received=payment.amount,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
payment.amount - amount,
|
|
||||||
payment.payment_hash,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# perform an internal transfer using the same payment_hash to the producer wallet
|
|
||||||
internal_checking_id = f"internal_{urlsafe_short_hash()}"
|
|
||||||
await create_payment(
|
|
||||||
wallet_id=producer.wallet,
|
|
||||||
checking_id=internal_checking_id,
|
|
||||||
payment_request="",
|
|
||||||
payment_hash=payment.payment_hash,
|
|
||||||
amount=amount,
|
|
||||||
memo=f"Revenue from '{track.name}'.",
|
memo=f"Revenue from '{track.name}'.",
|
||||||
pending=False,
|
|
||||||
)
|
)
|
||||||
|
logger.debug(f"livestream: producer invoice created: {payment_hash}")
|
||||||
|
|
||||||
# manually send this for now
|
checking_id = await pay_invoice(
|
||||||
# await internal_invoice_paid.send(internal_checking_id)
|
payment_request=payment_request,
|
||||||
await internal_invoice_listener.put(internal_checking_id)
|
wallet_id=payment.wallet_id,
|
||||||
|
extra={"tag": "livestream"},
|
||||||
|
)
|
||||||
|
logger.debug(f"livestream: producer invoice paid: {checking_id}")
|
||||||
|
|
||||||
# so the flow is the following:
|
# so the flow is the following:
|
||||||
# - we receive, say, 1000 satoshis
|
# - we receive, say, 1000 satoshis
|
||||||
|
@ -14,7 +14,7 @@ class Target(BaseModel):
|
|||||||
class TargetPutList(BaseModel):
|
class TargetPutList(BaseModel):
|
||||||
wallet: str = Query(...)
|
wallet: str = Query(...)
|
||||||
alias: str = Query("")
|
alias: str = Query("")
|
||||||
percent: float = Query(..., ge=0.01)
|
percent: float = Query(..., ge=0.01, lt=100)
|
||||||
|
|
||||||
|
|
||||||
class TargetPut(BaseModel):
|
class TargetPut(BaseModel):
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.core import db as core_db
|
|
||||||
from lnbits.core.crud import create_payment
|
|
||||||
from lnbits.core.models import Payment
|
from lnbits.core.models import Payment
|
||||||
from lnbits.helpers import get_current_extension_name, urlsafe_short_hash
|
from lnbits.core.services import create_invoice, pay_invoice
|
||||||
from lnbits.tasks import internal_invoice_queue, register_invoice_listener
|
from lnbits.helpers import get_current_extension_name
|
||||||
|
from lnbits.tasks import register_invoice_listener
|
||||||
|
|
||||||
from .crud import get_targets
|
from .crud import get_targets
|
||||||
|
|
||||||
@ -22,60 +20,36 @@ async def wait_for_paid_invoices():
|
|||||||
|
|
||||||
|
|
||||||
async def on_invoice_paid(payment: Payment) -> None:
|
async def on_invoice_paid(payment: Payment) -> None:
|
||||||
if payment.extra.get("tag") == "splitpayments" or payment.extra.get("splitted"):
|
if payment.extra.get("tag") == "splitpayments":
|
||||||
# already splitted, ignore
|
# already a splitted payment, ignore
|
||||||
return
|
return
|
||||||
|
|
||||||
# now we make some special internal transfers (from no one to the receiver)
|
|
||||||
targets = await get_targets(payment.wallet_id)
|
targets = await get_targets(payment.wallet_id)
|
||||||
|
|
||||||
if not targets:
|
if not targets:
|
||||||
return
|
return
|
||||||
|
|
||||||
transfers = [
|
total_percent = sum([target.percent for target in targets])
|
||||||
(target.wallet, int(target.percent * payment.amount / 100))
|
|
||||||
for target in targets
|
|
||||||
]
|
|
||||||
transfers = [(wallet, amount) for wallet, amount in transfers if amount > 0]
|
|
||||||
amount_left = payment.amount - sum([amount for _, amount in transfers])
|
|
||||||
|
|
||||||
if amount_left < 0:
|
if total_percent > 100:
|
||||||
logger.error(
|
logger.error("splitpayment failure: total percent adds up to more than 100%")
|
||||||
"splitpayments failure: amount_left is negative.", payment.payment_hash
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# mark the original payment with one extra key, "splitted"
|
logger.debug(f"performing split payments to {len(targets)} targets")
|
||||||
# (this prevents us from doing this process again and it's informative)
|
for target in targets:
|
||||||
# and reduce it by the amount we're going to send to the producer
|
amount = int(payment.amount * target.percent / 100) # msats
|
||||||
await core_db.execute(
|
payment_hash, payment_request = await create_invoice(
|
||||||
"""
|
wallet_id=target.wallet,
|
||||||
UPDATE apipayments
|
amount=int(amount / 1000), # sats
|
||||||
SET extra = ?, amount = ?
|
internal=True,
|
||||||
WHERE hash = ?
|
memo=f"split payment: {target.percent}% for {target.alias or target.wallet}",
|
||||||
AND checking_id NOT LIKE 'internal_%'
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
json.dumps(dict(**payment.extra, splitted=True)),
|
|
||||||
amount_left,
|
|
||||||
payment.payment_hash,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# perform the internal transfer using the same payment_hash
|
|
||||||
for wallet, amount in transfers:
|
|
||||||
internal_checking_id = f"internal_{urlsafe_short_hash()}"
|
|
||||||
await create_payment(
|
|
||||||
wallet_id=wallet,
|
|
||||||
checking_id=internal_checking_id,
|
|
||||||
payment_request="",
|
|
||||||
payment_hash=payment.payment_hash,
|
|
||||||
amount=amount,
|
|
||||||
memo=payment.memo,
|
|
||||||
pending=False,
|
|
||||||
extra={"tag": "splitpayments"},
|
extra={"tag": "splitpayments"},
|
||||||
)
|
)
|
||||||
|
logger.debug(f"created split invoice: {payment_hash}")
|
||||||
|
|
||||||
# manually send this for now
|
checking_id = await pay_invoice(
|
||||||
await internal_invoice_queue.put(internal_checking_id)
|
payment_request=payment_request,
|
||||||
return
|
wallet_id=payment.wallet_id,
|
||||||
|
extra={"tag": "splitpayments"},
|
||||||
|
)
|
||||||
|
logger.debug(f"paid split invoice: {checking_id}")
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
|
||||||
|
|
||||||
from lnbits.core import db as core_db
|
from loguru import logger
|
||||||
from lnbits.core.crud import create_payment
|
|
||||||
from lnbits.core.models import Payment
|
from lnbits.core.models import Payment
|
||||||
from lnbits.helpers import get_current_extension_name, urlsafe_short_hash
|
from lnbits.core.services import create_invoice, pay_invoice
|
||||||
from lnbits.tasks import internal_invoice_queue, register_invoice_listener
|
from lnbits.helpers import get_current_extension_name
|
||||||
|
from lnbits.tasks import register_invoice_listener
|
||||||
|
|
||||||
from .crud import get_tpos
|
from .crud import get_tpos
|
||||||
|
|
||||||
@ -20,11 +20,9 @@ async def wait_for_paid_invoices():
|
|||||||
|
|
||||||
|
|
||||||
async def on_invoice_paid(payment: Payment) -> None:
|
async def on_invoice_paid(payment: Payment) -> None:
|
||||||
if payment.extra.get("tag") == "tpos" and payment.extra.get("tipSplitted"):
|
if payment.extra.get("tag") != "tpos":
|
||||||
# already splitted, ignore
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# now we make some special internal transfers (from no one to the receiver)
|
|
||||||
tpos = await get_tpos(payment.extra.get("tposId"))
|
tpos = await get_tpos(payment.extra.get("tposId"))
|
||||||
tipAmount = payment.extra.get("tipAmount")
|
tipAmount = payment.extra.get("tipAmount")
|
||||||
|
|
||||||
@ -32,39 +30,17 @@ async def on_invoice_paid(payment: Payment) -> None:
|
|||||||
# no tip amount
|
# no tip amount
|
||||||
return
|
return
|
||||||
|
|
||||||
tipAmount = tipAmount * 1000
|
payment_hash, payment_request = await create_invoice(
|
||||||
amount = payment.amount - tipAmount
|
|
||||||
|
|
||||||
# mark the original payment with one extra key, "splitted"
|
|
||||||
# (this prevents us from doing this process again and it's informative)
|
|
||||||
# and reduce it by the amount we're going to send to the producer
|
|
||||||
await core_db.execute(
|
|
||||||
"""
|
|
||||||
UPDATE apipayments
|
|
||||||
SET extra = ?, amount = ?
|
|
||||||
WHERE hash = ?
|
|
||||||
AND checking_id NOT LIKE 'internal_%'
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
json.dumps(dict(**payment.extra, tipSplitted=True)),
|
|
||||||
amount,
|
|
||||||
payment.payment_hash,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# perform the internal transfer using the same payment_hash
|
|
||||||
internal_checking_id = f"internal_{urlsafe_short_hash()}"
|
|
||||||
await create_payment(
|
|
||||||
wallet_id=tpos.tip_wallet,
|
wallet_id=tpos.tip_wallet,
|
||||||
checking_id=internal_checking_id,
|
amount=int(tipAmount), # sats
|
||||||
payment_request="",
|
internal=True,
|
||||||
payment_hash=payment.payment_hash,
|
memo=f"tpos tip",
|
||||||
amount=tipAmount,
|
|
||||||
memo=f"Tip for {payment.memo}",
|
|
||||||
pending=False,
|
|
||||||
extra={"tipSplitted": True},
|
|
||||||
)
|
)
|
||||||
|
logger.debug(f"tpos: tip invoice created: {payment_hash}")
|
||||||
|
|
||||||
# manually send this for now
|
checking_id = await pay_invoice(
|
||||||
await internal_invoice_queue.put(internal_checking_id)
|
payment_request=payment_request,
|
||||||
return
|
wallet_id=payment.wallet_id,
|
||||||
|
extra={"tag": "tpos"},
|
||||||
|
)
|
||||||
|
logger.debug(f"tpos: tip invoice paid: {checking_id}")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user