fix: circular import on handle_fiat_payments (#3226)
This commit is contained in:
parent
26eb7a449c
commit
b4b37cd733
4 changed files with 105 additions and 98 deletions
|
|
@ -5,9 +5,106 @@ from typing import Optional
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.core.crud.payments import get_standalone_payment
|
from lnbits.core.crud import get_wallet
|
||||||
|
from lnbits.core.crud.payments import create_payment, get_standalone_payment
|
||||||
|
from lnbits.core.models import CreatePayment, Payment, PaymentState
|
||||||
from lnbits.core.models.misc import SimpleStatus
|
from lnbits.core.models.misc import SimpleStatus
|
||||||
|
from lnbits.db import Connection
|
||||||
from lnbits.fiat import get_fiat_provider
|
from lnbits.fiat import get_fiat_provider
|
||||||
|
from lnbits.settings import settings
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_fiat_payment_confirmation(
|
||||||
|
payment: Payment, conn: Optional[Connection] = None
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
await _credit_fiat_service_fee_wallet(payment, conn=conn)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(e)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await _debit_fiat_service_faucet_wallet(payment, conn=conn)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(e)
|
||||||
|
|
||||||
|
|
||||||
|
async def _credit_fiat_service_fee_wallet(
|
||||||
|
payment: Payment, conn: Optional[Connection] = None
|
||||||
|
):
|
||||||
|
fiat_provider_name = payment.fiat_provider
|
||||||
|
if not fiat_provider_name:
|
||||||
|
return
|
||||||
|
if payment.fee == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
limits = settings.get_fiat_provider_limits(fiat_provider_name)
|
||||||
|
if not limits:
|
||||||
|
return
|
||||||
|
|
||||||
|
if not limits.service_fee_wallet_id:
|
||||||
|
return
|
||||||
|
|
||||||
|
memo = (
|
||||||
|
f"Service fee for fiat payment of "
|
||||||
|
f"{abs(payment.sat)} sats. "
|
||||||
|
f"Provider: {fiat_provider_name}. "
|
||||||
|
f"Wallet: '{payment.wallet_id}'."
|
||||||
|
)
|
||||||
|
create_payment_model = CreatePayment(
|
||||||
|
wallet_id=limits.service_fee_wallet_id,
|
||||||
|
bolt11=payment.bolt11,
|
||||||
|
payment_hash=payment.payment_hash,
|
||||||
|
amount_msat=abs(payment.fee),
|
||||||
|
memo=memo,
|
||||||
|
)
|
||||||
|
await create_payment(
|
||||||
|
checking_id=f"service_fee_{payment.payment_hash}",
|
||||||
|
data=create_payment_model,
|
||||||
|
status=PaymentState.SUCCESS,
|
||||||
|
conn=conn,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def _debit_fiat_service_faucet_wallet(
|
||||||
|
payment: Payment, conn: Optional[Connection] = None
|
||||||
|
):
|
||||||
|
fiat_provider_name = payment.fiat_provider
|
||||||
|
if not fiat_provider_name:
|
||||||
|
return
|
||||||
|
|
||||||
|
limits = settings.get_fiat_provider_limits(fiat_provider_name)
|
||||||
|
if not limits:
|
||||||
|
return
|
||||||
|
|
||||||
|
if not limits.service_faucet_wallet_id:
|
||||||
|
return
|
||||||
|
|
||||||
|
faucet_wallet = await get_wallet(limits.service_faucet_wallet_id, conn=conn)
|
||||||
|
if not faucet_wallet:
|
||||||
|
raise ValueError(
|
||||||
|
f"Fiat provider '{fiat_provider_name}' faucet wallet not found."
|
||||||
|
)
|
||||||
|
|
||||||
|
memo = (
|
||||||
|
f"Faucet payment of {abs(payment.sat)} sats. "
|
||||||
|
f"Provider: {fiat_provider_name}. "
|
||||||
|
f"Wallet: '{payment.wallet_id}'."
|
||||||
|
)
|
||||||
|
create_payment_model = CreatePayment(
|
||||||
|
wallet_id=limits.service_faucet_wallet_id,
|
||||||
|
bolt11=payment.bolt11,
|
||||||
|
payment_hash=payment.payment_hash,
|
||||||
|
amount_msat=-abs(payment.amount),
|
||||||
|
memo=memo,
|
||||||
|
extra=payment.extra,
|
||||||
|
)
|
||||||
|
await create_payment(
|
||||||
|
checking_id=f"internal_fiat_{fiat_provider_name}_"
|
||||||
|
f"faucet_{payment.payment_hash}",
|
||||||
|
data=create_payment_model,
|
||||||
|
status=PaymentState.SUCCESS,
|
||||||
|
conn=conn,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def handle_stripe_event(event: dict):
|
async def handle_stripe_event(event: dict):
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ from lnbits.exceptions import InvoiceError, PaymentError
|
||||||
from lnbits.fiat import get_fiat_provider
|
from lnbits.fiat import get_fiat_provider
|
||||||
from lnbits.helpers import check_callback_url
|
from lnbits.helpers import check_callback_url
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
from lnbits.tasks import create_task, internal_invoice_queue_put
|
from lnbits.tasks import internal_invoice_queue_put
|
||||||
from lnbits.utils.crypto import fake_privkey, random_secret_and_hash
|
from lnbits.utils.crypto import fake_privkey, random_secret_and_hash
|
||||||
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis, satoshis_amount_as_fiat
|
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis, satoshis_amount_as_fiat
|
||||||
from lnbits.wallets import fake_wallet, get_funding_source
|
from lnbits.wallets import fake_wallet, get_funding_source
|
||||||
|
|
@ -587,20 +587,6 @@ async def get_payments_daily_stats(
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
async def handle_fiat_payment_confirmation(
|
|
||||||
payment: Payment, conn: Optional[Connection] = None
|
|
||||||
):
|
|
||||||
try:
|
|
||||||
await _credit_fiat_service_fee_wallet(payment, conn=conn)
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(e)
|
|
||||||
|
|
||||||
try:
|
|
||||||
await _debit_fiat_service_faucet_wallet(payment, conn=conn)
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(e)
|
|
||||||
|
|
||||||
|
|
||||||
async def _pay_invoice(
|
async def _pay_invoice(
|
||||||
wallet_id: str,
|
wallet_id: str,
|
||||||
create_payment_model: CreatePayment,
|
create_payment_model: CreatePayment,
|
||||||
|
|
@ -884,85 +870,6 @@ async def _credit_service_fee_wallet(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def _credit_fiat_service_fee_wallet(
|
|
||||||
payment: Payment, conn: Optional[Connection] = None
|
|
||||||
):
|
|
||||||
fiat_provider_name = payment.fiat_provider
|
|
||||||
if not fiat_provider_name:
|
|
||||||
return
|
|
||||||
if payment.fee == 0:
|
|
||||||
return
|
|
||||||
|
|
||||||
limits = settings.get_fiat_provider_limits(fiat_provider_name)
|
|
||||||
if not limits:
|
|
||||||
return
|
|
||||||
|
|
||||||
if not limits.service_fee_wallet_id:
|
|
||||||
return
|
|
||||||
|
|
||||||
memo = (
|
|
||||||
f"Service fee for fiat payment of "
|
|
||||||
f"{abs(payment.sat)} sats. "
|
|
||||||
f"Provider: {fiat_provider_name}. "
|
|
||||||
f"Wallet: '{payment.wallet_id}'."
|
|
||||||
)
|
|
||||||
create_payment_model = CreatePayment(
|
|
||||||
wallet_id=limits.service_fee_wallet_id,
|
|
||||||
bolt11=payment.bolt11,
|
|
||||||
payment_hash=payment.payment_hash,
|
|
||||||
amount_msat=abs(payment.fee),
|
|
||||||
memo=memo,
|
|
||||||
)
|
|
||||||
await create_payment(
|
|
||||||
checking_id=f"service_fee_{payment.payment_hash}",
|
|
||||||
data=create_payment_model,
|
|
||||||
status=PaymentState.SUCCESS,
|
|
||||||
conn=conn,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def _debit_fiat_service_faucet_wallet(
|
|
||||||
payment: Payment, conn: Optional[Connection] = None
|
|
||||||
):
|
|
||||||
fiat_provider_name = payment.fiat_provider
|
|
||||||
if not fiat_provider_name:
|
|
||||||
return
|
|
||||||
|
|
||||||
limits = settings.get_fiat_provider_limits(fiat_provider_name)
|
|
||||||
if not limits:
|
|
||||||
return
|
|
||||||
|
|
||||||
if not limits.service_faucet_wallet_id:
|
|
||||||
return
|
|
||||||
|
|
||||||
faucet_wallet = await get_wallet(limits.service_faucet_wallet_id, conn=conn)
|
|
||||||
if not faucet_wallet:
|
|
||||||
raise ValueError(
|
|
||||||
f"Fiat provider '{fiat_provider_name}' faucet wallet not found."
|
|
||||||
)
|
|
||||||
|
|
||||||
memo = (
|
|
||||||
f"Faucet payment of {abs(payment.sat)} sats. "
|
|
||||||
f"Provider: {fiat_provider_name}. "
|
|
||||||
f"Wallet: '{payment.wallet_id}'."
|
|
||||||
)
|
|
||||||
create_payment_model = CreatePayment(
|
|
||||||
wallet_id=limits.service_faucet_wallet_id,
|
|
||||||
bolt11=payment.bolt11,
|
|
||||||
payment_hash=payment.payment_hash,
|
|
||||||
amount_msat=-abs(payment.amount),
|
|
||||||
memo=memo,
|
|
||||||
extra=payment.extra,
|
|
||||||
)
|
|
||||||
await create_payment(
|
|
||||||
checking_id=f"internal_fiat_{fiat_provider_name}_"
|
|
||||||
f"faucet_{payment.payment_hash}",
|
|
||||||
data=create_payment_model,
|
|
||||||
status=PaymentState.SUCCESS,
|
|
||||||
conn=conn,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def _check_fiat_invoice_limits(
|
async def _check_fiat_invoice_limits(
|
||||||
amount_sat: int, fiat_provider_name: str, conn: Optional[Connection] = None
|
amount_sat: int, fiat_provider_name: str, conn: Optional[Connection] = None
|
||||||
):
|
):
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ from lnbits.core.crud import (
|
||||||
update_payment,
|
update_payment,
|
||||||
)
|
)
|
||||||
from lnbits.core.models import Payment, PaymentState
|
from lnbits.core.models import Payment, PaymentState
|
||||||
from lnbits.core.services.payments import handle_fiat_payment_confirmation
|
from lnbits.core.services.fiat_providers import handle_fiat_payment_confirmation
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
from lnbits.wallets import get_funding_source
|
from lnbits.wallets import get_funding_source
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,10 @@ from lnbits.core.models.payments import CreateInvoice, PaymentState
|
||||||
from lnbits.core.models.users import User
|
from lnbits.core.models.users import User
|
||||||
from lnbits.core.models.wallets import Wallet
|
from lnbits.core.models.wallets import Wallet
|
||||||
from lnbits.core.services import payments
|
from lnbits.core.services import payments
|
||||||
from lnbits.core.services.fiat_providers import check_stripe_signature
|
from lnbits.core.services.fiat_providers import (
|
||||||
|
check_stripe_signature,
|
||||||
|
handle_fiat_payment_confirmation,
|
||||||
|
)
|
||||||
from lnbits.core.services.users import create_user_account
|
from lnbits.core.services.users import create_user_account
|
||||||
from lnbits.fiat.base import FiatInvoiceResponse, FiatPaymentStatus
|
from lnbits.fiat.base import FiatInvoiceResponse, FiatPaymentStatus
|
||||||
from lnbits.settings import Settings
|
from lnbits.settings import Settings
|
||||||
|
|
@ -304,7 +307,7 @@ async def test_handle_fiat_payment_confirmation(
|
||||||
assert payment.status == PaymentState.PENDING
|
assert payment.status == PaymentState.PENDING
|
||||||
assert payment.amount == 10_000_000
|
assert payment.amount == 10_000_000
|
||||||
|
|
||||||
await payments.handle_fiat_payment_confirmation(payment)
|
await handle_fiat_payment_confirmation(payment)
|
||||||
# await asyncio.sleep(1) # Simulate async delay
|
# await asyncio.sleep(1) # Simulate async delay
|
||||||
|
|
||||||
service_fee_payments = await get_payments(wallet_id=service_fee_wallet.id)
|
service_fee_payments = await get_payments(wallet_id=service_fee_wallet.id)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue