Skip to content

fix: clear error when fiat exchange rate is unavailable - #89

Closed
dadofsambonzuki wants to merge 31 commits into
lnbits:mainfrom
dadofsambonzuki:fix/clear-error-on-unavailable-fiat-rate
Closed

dadofsambonzuki wants to merge 31 commits into
lnbits:mainfrom
dadofsambonzuki:fix/clear-error-on-unavailable-fiat-rate

Conversation

@dadofsambonzuki

Copy link
Copy Markdown

Summary

When no Bitcoin price provider responds (e.g. during a transient DNS/outbound-network outage), get_fiat_rate_satoshis() silently returns 0.0 instead of raising. Because api_charge_create computes data.amount = round(rate * data.currency_amount) without validating the rate, the charge amount becomes 0 and create_charge() fails with the generic:

Exception: Amount must be greater than 0

This generic failure propagates to checkout callers such as the events extension, which surface it as:

HTTP 400: Could not create payment for any enabled method.
Last error: SatsPay error: Unexpected error! ID: ...

That is misleading — the request was fine; the fiat→sats conversion was impossible at that moment.

Change

In views_api.py, after fetching the fiat rate, raise a clear HTTP 503 Service Unavailable when rate <= 0:

if rate <= 0:
    raise HTTPException(
        status_code=HTTPStatus.SERVICE_UNAVAILABLE,
        detail=(
            f"Could not fetch Bitcoin price for {data.currency}. "
            "Please try again later."
        ),
    )

Callers (and customers on the checkout page) now see an actionable, retryable message instead of the confusing "Amount must be greater than 0" / "Unexpected error!".

Note: get_fiat_rate_satoshis() already has a sibling helper (fiat_amount_as_satoshis()) that guards with if rate > 0 / raise ValueError(...) — this brings the API path in line with that behavior.

Testing

make checkruff / make checkblack for views_api.py report only pre-existing violations on main (existing E501/import-order, unchanged E501s elsewhere in the file); this diff adds no new lint findings.

dadofsambonzuki and others added 30 commits July 9, 2026 08:29
- Replaced standalone Fiat Configuration card and admin settings dialog
  with a single gear icon button that opens a combined modal
- Extension Settings section (admin only) with network, mempool URL,
  webhook method via lnbits-dynamic-fields
- Fiat Configuration section (all users) with per-provider toggles
  and password fields
- Dialog auto-closes after saving fiat config
- Add api_endpoint, api_version, location_id to FiatConfig model
- Add m017 migration for new fiat_configs columns
- Show provider-specific fields in settings UI (Secret Key for Stripe,
  Client ID/Secret for PayPal, API Secret Key for Revolut,
  Access Token/Location ID for Square) instead of generic labels
- Per-provider validation in canSaveFiatConfig (PayPal needs
  api_secret, Square needs location_id, others just api_key)
- Use structured fields in helpers.py instead of parsing extra JSON
…_providers

- Remove FiatConfig model, CRUD, API endpoints, UI, and migrations
- Charge creation validates fiat_provider against settings.get_fiat_providers_for_user()
- _create_*_checkout functions read credentials directly from core settings
- Webhook verification uses core settings for secrets
- Charge dialog fiat dropdown uses g.user.fiat_providers
- Settings modal no longer has fiat configuration section
…ix webhook double-encoding

- Gracefully handle missing register_fiat_webhook_handler in core
  (required until PR #4051 is merged).
- Send proper dict (charge.model_dump()) in webhook POST body instead
  of a pre-encoded JSON string (charge.json()) to avoid double-encoding.
Add settlement_method and settlement_proof columns to Charge (m017):
- lightning: stores preimage as proof
- onchain: stores txids as JSON proof
- fiat: stores provider name and checkout/order id

Record settlement in all payment detection paths (invoice listener,
onchain websocket, balance polling, both fiat webhook handlers, and
restart recovery). Expose both fields in the public charge payload and
websocket updates, and show 'Paid via <method>' with the proof on the
public paid screen.

Also add m018 to backfill settlement info for historical paid charges
from existing extra/fiat column data.
…ted hook)

Commit c424acf accidentally removed the closing braces and the created()
hook along with the fiat provider methods, leaving the file with a
syntax error so the SatsPay page never rendered in the browser.
…outs

Create fiat invoices via core create_payment_request(fiat_provider=...)
tagged with the charge id, instead of calling Stripe/PayPal/Revolut/
Square APIs directly. Core owns the provider checkout session, verifies
provider webhooks at its own callback endpoint, marks the internal
payment paid and notifies invoice listeners.

- on_invoice_paid settles charges with settlement_method/proof from
  payment.fiat_provider and extra.fiat_checking_id
- check_charge_balance also polls the fiat payment status
- drop /satspay/api/v1/fiat/webhook endpoint, handle_fiat_webhook_event
  and the PR #4051 dispatcher registration (not needed, per review)
- store the core payment_hash in fiat_payment_requests for polling
…unavailable

When no Bitcoin price provider responds (e.g. transient DNS/outbound
network issues), get_fiat_rate_satoshis() silently returns 0.0, so the
computed sats amount becomes 0 and create_charge() fails with the
generic 'Amount must be greater than 0' exception. That surfaces to
checkout flows (e.g. the events extension) as 'Could not create payment
for any enabled method'.

Raise a clear HTTP 503 with the failing currency instead, so callers and
users can distinguish a transient rate outage from a bad request.
@dadofsambonzuki

Copy link
Copy Markdown
Author

Closing this PR — it accidentally carried the full fork-main history (all changes already in #87). The currency-check change is being added to #87 and re-raised as a separate PR scoped to just that change.

@dadofsambonzuki
dadofsambonzuki deleted the fix/clear-error-on-unavailable-fiat-rate branch September 13, 2026 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant