Commit graph

505 commits

Author SHA1 Message Date
18a9dfae4c Simplify Monero recovery procedures and remove automated health checks
- Remove 'Automated Health Checks' section - redundant with make commands
- Simplify recovery procedures to match Dogecoin documentation style
- Remove bash script examples and implementation details
- Keep focus on essential backup/recovery information
2025-09-30 22:28:44 -04:00
dc01348782 Remove address validation code from documentation
- Replace Python validation functions with simple descriptions
- Keep validation rules (D prefix, 34 chars for DOGE; 4 prefix, 95/106 chars for XMR)
- Documentation should describe what, not show implementation how
2025-09-30 22:22:32 -04:00
7c9796c56c Simplify Dogecoin setup to use Makefile targets
- Replace manual installation steps with make install-dogecoin
- Replace manual configuration with make dogecoin-config
- Remove raw dogecoin-cli commands in favor of make targets
- Keep documentation focused on what users need to do, not implementation details
2025-09-30 22:18:11 -04:00
cb0676e701 Remove broken multi-node setup from Monero documentation
- Multi-node setups would cause account index mismatches
- Monero wallets can't be sharded or load balanced
- Replace with honest scaling limitations and realistic options
- Single wallet file is a fundamental architectural constraint
2025-09-30 22:14:39 -04:00
51ec71a9f9 Update crypto documentation with accurate configuration and remove tutorials
- Document actual resource requirements from production systems
- Fix fee documentation: fee buffers are for quotes, not wallet reserves
- Clarify perfect accounting to quote amounts in atomic units
- Remove generic curl/API tutorials, keep Make commands
- Add dust prevention benefit of exact amount sweeping
- Update Makefile targets to match what actually exists
2025-09-30 22:03:08 -04:00
7c8a0065cf modified: docs/CRYPTO.rst
new file:   docs/CRYPTO_PERFORMANCE.rst
	modified:   docs/DOGECOIN.md
	modified:   docs/MONERO.rst
2025-09-30 21:39:35 -04:00
04583ace53 Clean up Makefile sweep commands and documentation
- Remove error messages from sweep/sweep-check Makefile targets
- Keep commands functional for developers/admins in local environments
- Simplify documentation to focus on automatic sweeping behavior
- Maintain separation between end-user docs and dev tools
2025-09-30 20:54:50 -04:00
ed98674c48 Remove untested sweep_to_cold script and references
- Delete make_post_sell/lib/sweep_to_cold.py (untested, risky for production)
- Remove sweep_to_cold console script entry from setup.py
- Update Makefile to deprecate sweep/sweep-check, recommend tested alternatives
- Update CRYPTO.rst documentation to promote sweep-all and sweep-all-doge
- Reduce technical debt by eliminating dangerous untested code path
- Users should use properly tested sweep-all/sweep-all-doge commands instead
2025-09-30 20:54:50 -04:00
f2227199c9 Delete MONERO_DEFECTS.rst 2025-10-01 00:41:12 +00:00
86fdab0f3a Delete CRYPTO_WATCHER_FLOW.md 2025-10-01 00:40:29 +00:00
29baa1bb73 Fix invoice deletion test for centralized logging
- Update test_handle_invoice_deletion_failure to mock centralized log instance
- Change from mocking logger.error to log.error_with_context
- Test now properly validates centralized logging system usage
2025-09-30 20:32:54 -04:00
001eedceed Format code with black 2025-09-30 20:27:25 -04:00
4063c1a9fa Remove RPC credential exposure from DogecoinClient._call
- Remove logging statements that exposed RPC username and password info
- Fix sweep restocking fee tests to work with centralized logging
- Update test mocking to use module-level log instance instead of class
- Maintain security by preventing credential leakage in logs
2025-09-30 20:22:47 -04:00
71f8f2b467 Extensively refactor crypto watcher logging and add comprehensive payment string representations
Infrastructure improvements:
- Add CryptoWatcherLogger class with centralized logging methods
- Add comprehensive __str__ and __repr__ methods to CryptoPayment model
- Include user display name and shop name in payment logs

Logging refactoring:
- Convert 101+ logger calls to use centralized patterns
- Replace repetitive f-string patterns with payment object logging
- Add standardized methods for state transitions, refunds, sweeps
- Eliminate duplicate payment context formatting

DRY improvements:
- Single point of control for payment log formatting
- Consistent error handling with payment context
- Standardized transaction and operation logging
- Enhanced debugging with rich payment information

Testing:
- Add 16 comprehensive unit tests for string representation methods
- Test coverage for all payment states and edge cases
- Maintain 100% functionality with improved maintainability
2025-09-30 20:11:01 -04:00
01c1d237e1 Prevent shop owners from clearing cold wallet addresses
Shop owners can no longer remove/clear their cryptocurrency cold wallet
addresses once set. They can only:
1. Replace the address with a new one
2. Disable the cryptocurrency entirely (address is preserved)

Changes:
- Added server-side validation in crypto_processor_settings view
- Check if processor exists with sweep_to_address before allowing empty submission
- Show clear error message if attempt to clear address
- Updated template to add 'required' attribute when address exists
- Added user-facing note explaining the restriction

This prevents accidental or intentional removal of cold wallet addresses
which could lead to funds being stuck in the hot wallet without a way
to sweep them to cold storage.
2025-09-30 16:31:47 -04:00
c255419f99 Fix invoice deletion integration tests and update documentation
Integration test fixes:
- Replace hardcoded status strings with proper CryptoPayment constants
- Fixed test_complete_refund_states_delete_invoice to use:
  - STATUS_LATEPAY_REFUNDED_COMPLETE
  - STATUS_UNDERPAID_REFUNDED_COMPLETE
  - STATUS_OUT_OF_STOCK_REFUNDED_COMPLETE
- Fixed test_no_refund_payment_deletes_invoice to use STATUS_LATEPAY_NOT_REFUNDED
- Updated all other status references to use constants instead of strings

The integration tests were failing because they used hardcoded strings like
"expired-refunded-complete" instead of the actual status constants from the model.
This caused the should_keep_invoice() logic to not work properly since the
status matching failed.

Documentation updates:
- Corrected "received" state description: detected in mempool (not blockchain)
- Added clarification about confirmations tracking via current_confirmations field
- Updated mempool detection rule explanation

All 11 invoice deletion integration tests now pass, confirming the invoice
deletion logic works correctly with real database operations.
2025-09-30 16:14:28 -04:00
6ceb9375de modified: Makefile
new file:   docs/state-machine.dot
	new file:   docs/state-machine.dot.svg
	renamed:    state-machine.svg -> docs/state-machine.svg
2025-09-30 15:43:54 -04:00
5030b47f4e Delete fix_crypto_payment_statuses.sql 2025-09-30 18:47:16 +00:00
0b5554e431 modified: docs/crypto-payments-state-machine.md
modified:   state-machine.svg
2025-09-30 14:46:47 -04:00
c7caae8d4a Fix crypto payment state machine logic and naming consistency
Major fixes:
1. Remove invalid received→expired transition
   - Payments that reach 'received' state cannot expire
   - Only pending payments can expire
   - Updated VALID_TRANSITIONS and tests accordingly

2. Rename STATUS_DOUBLEPAY_REFUND to STATUS_DOUBLEPAY_REFUNDED
   - Maintains consistency with all other past-tense status names
   - Updated all references across codebase and tests

3. Add INITIAL_WAITING_STATUSES semantic group
   - Groups states that are entry points: pending, latepay_refunded, doublepay_refunded
   - These states don't transition from 'received' - they represent initial states
   - Added is_initial_waiting_state() helper method

4. Update semantic groups
   - Removed latepay_refunded and doublepay_refunded from FAILED_PAYMENT_STATUSES
   - These are now in INITIAL_WAITING_STATUSES as they represent entry points

5. Fix mermaid diagram label
   - confirmed_overpay_not_refunded now correctly labeled as "Terminal Success (Not Refunded)"
   - Remove invalid received→expired transition from documentation

This improves the logical consistency of the state machine and aligns
the naming conventions across all status constants.
2025-09-30 14:34:11 -04:00
7625563f6a Fix invoice deletion test failures by adding missing mock setup
All invoice deletion tests were failing because they were missing the
mock setup for should_keep_invoice.return_value = False. This caused
the deletion logic to be skipped since the function would return early
when should_keep_invoice() returned the default MagicMock() value.

Changes:
- Added self.mock_crypto_payment.should_keep_invoice.return_value = False
  to test_handle_invoice_deletion_returns_false test
- All 11 invoice deletion tests now pass
- Completes the crypto payment test suite fixes (155 tests passing)

The invoice deletion logic properly checks should_keep_invoice() which
returns payment.is_successful_payment(), and failed payments should
have their invoices deleted while successful payments should keep them.
2025-09-30 14:13:35 -04:00
5686121ed5 Fix concurrent transaction detection for pending payments
When multiple transactions arrive simultaneously to a pending payment, treat ALL transactions as duplicates rather than processing the first one normally. This prevents potential attack scenarios where multiple payments are sent rapidly to the same quote.

- Modified duplicate detection logic to treat all concurrent transactions as suspicious
- Set new_sum=0 and new_txids=[] when all transactions become duplicates
- Removed recalculation logic that was processing first transaction
- Enhanced logging to reflect new behavior

Fixes test_double_spend_protection.py failures. All 130 crypto tests now pass.
2025-09-30 14:05:10 -04:00
5c67b9ca4c Fix all crypto payment test failures and status constant issues
- Fix duplicate detection logic to prevent amount accumulation when new transactions arrive to already-processed payments
- Fix status constant naming errors: STATUS_CONFIRMED_OVERPAID → STATUS_CONFIRMED_OVERPAY, STATUS_EXPIRED_REFUNDED → STATUS_LATEPAY_REFUNDED
- Fix SweepRestockingFeeTests mock setup: add missing refund_confirmations, get_coin_config patches, and proper XMR client mocks
- Correct overpayment test expectations: restocking fee sweep happens later after refund confirmation, not immediately
- Update test assertions to match actual implementation behavior (transfer vs sweep_all for XMR)
- Add comprehensive mock configurations for DOGE/XMR atomic units and balance methods

All 108 crypto payment tests now pass. Fixes critical duplicate payment processing bug and aligns test expectations with production behavior.
2025-09-30 13:56:42 -04:00
923bd873f2 Add comprehensive crypto payment state transition validation
* Implement complete state machine with 17 test scenarios
* Add priority-based processing order validation
* Include property-based testing for state transitions
* Add graph analysis for state machine integrity
* Validate all payment lifecycle paths
* Ensure robust error handling and logging
* Remove temporary debug files and consolidate test structure
2025-09-30 11:26:44 -04:00
73bbf04fd4 Fix payment priority order and refund processing
- Fix payment_priority() to include ALL refund statuses (EXPIRED_REFUNDED, UNDERPAID_REFUNDED, OUT_OF_STOCK_REFUNDED) not just DOUBLEPAY_REFUND
- Auto-sweep operations (CONFIRMED, CONFIRMED_OVERPAID, NO_REFUND) now have lowest priority (3)
- Refactor to use match/case statements for better readability
- Add comprehensive docstring explaining priority system
- Ensure refunds processed before auto-sweep for customer service priority

Priority order: 0=refunds, 1=incoming, 2=other (pending/terminals), 3=auto-sweep
2025-09-30 08:05:08 -04:00
e04e91075b modified: make_post_sell/models/stripe_user_shop.py
modified:   make_post_sell/views/billing.py
	modified:   make_post_sell/views/user_crypto_settings.py
2025-09-30 07:36:38 -04:00
49eb1eac9c Fix double spend protection tests and precision handling
Updated tests to reflect correct behavior where multiple transactions
to one quote are treated as duplicates rather than legitimate payments.

Key changes:
- Fixed test_concurrent_transactions_within_processing_window to expect
  duplicate detection behavior instead of transaction summing
- Fixed test_second_processing_cycle_rejects_duplicates to properly
  validate duplicate detection warnings and status
- Updated test_multiple_rapid_payments_scenario and test_zero_amount_transactions
  to handle duplicate payment scenarios correctly
- Removed int() casting from received_amount calculation to preserve
  precision in cryptocurrency amounts

The core insight is that there should never be legitimate multiple
transactions to one quote - any additional transactions are duplicates
that should be handled by the scanner for refunds.

All 22 tests in test_double_spend_protection.py now pass.
2025-09-29 19:53:46 -04:00
bad7a9fe39 Fix XMR duplicate detection: split multiple transactions into separate records
- Add detection for multiple transactions during initial pending payment processing
- Process first transaction normally, create DOUBLEPAY_REFUND records for duplicates
- Recalculate amounts to use only first transaction for original payment
- Prevent summing all transactions into single overpayment
- Maintain existing logic for already-processed payments with new transactions

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-29 18:53:58 -04:00
65176f7713 Fix double spend duplicate payment creation and processing
The issue was that duplicate payments were being created without proper
invoice references, causing the refund processing logic to fail when
looking up user information. This resulted in duplicate payments being
marked as 'no-refund' instead of 'doublepay-refund'.

Key changes:
- Modified _create_duplicate_payment() to preserve the original invoice
  reference instead of setting it to None
- Updated duplicate payment processing logic to use the invoice.user
  directly instead of trying to look up the "original payment"
- Explicitly set user_id and shop_id foreign keys to ensure proper
  database relationships
- Updated test assertions to expect duplicate payments to reference
  the same invoice as the original payment

This fixes the core architectural insight that duplicate payments
should reference the same invoice since they're all payments toward
the same original quote/order. The invoice-to-quote relationship
remains intact while allowing proper duplicate payment tracking.

Fixes:
- test_double_payment_creates_separate_refund_entry
- test_race_condition_double_refund_prevention
- test_three_payments_one_confirms_two_refund
2025-09-29 18:18:29 -04:00
523599c108 Fix crypto payment precision and order of operations issues
- Fix precision mismatches by using Decimal arithmetic throughout quote generation
- Replace int() truncation with math.ceil() for atomic unit conversions
- Update DOGE and XMR quote generation to avoid floating point errors
- Fix order of operations: process duplicate refunds BEFORE auto-sweep
- Add fund reservation logic to auto-sweep to protect pending refunds
- Fix duplicate payment queries to use address for DOGE, subaddress for XMR

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-29 16:45:33 -04:00
9211ba7697 Fix crypto precision issues and enhance refund email handling
• Fix precision rounding: Use math.ceil() for expected amounts and fees to prevent 1 koinu/piconero mismatches
• Complete refund email coverage: Handle all refund status variants including _COMPLETE statuses
• Add shop owner notifications: Send alerts when payments are forfeited due to missing refund addresses
• Auto-sweep no-refund payments: Transfer forfeited funds to shop owners automatically
• Add comprehensive precision tests: Protect against future floating-point precision regressions
• Improve duplicate payment processing: Better transaction deduplication and confirmation tracking
2025-09-29 16:17:00 -04:00
f01984e783 deleted: monero-sweep-all.sh 2025-09-29 14:43:52 -04:00
a403669e92 modified: make_post_sell/tests/test_crypto_watcher.py 2025-09-29 14:12:01 -04:00
a3c20609c0 Fix DOGE overpayment refund - Invalid amount precision error
* Fix "Invalid amount" error in DOGE refunds by rounding to 8 decimal places
* Add detailed RPC error logging to DogecoinClient for better debugging
* Add comment parameter to sendtoaddress calls for transaction labeling
* Identified root cause: Dogecoin daemon rejects amounts with >8 decimal precision
* Refund amount changed from 1.0385903528 to 1.03859035 DOGE

The system was correctly detecting overpayments (5.5 vs 4.358 DOGE expected)
but failing on the actual refund transaction due to precision requirements.
2025-09-29 13:52:37 -04:00
b5930694c7 Add is_finalized() method and make payment confirmation flow idempotent
* Add is_finalized() method to CryptoPayment model that checks:
  - Status in confirmed states (STATUS_CONFIRMED, STATUS_CONFIRMED_OVERPAID, STATUS_CONFIRMED_OVERPAID_REFUNDED)
  - Received amount >= expected amount (handles overpayments)
  - Current confirmations >= required confirmations

* Make process_confirmed_payment() idempotent and resumable:
  - Check is_finalized() before attempting to finalize invoice
  - Check refund_tx_hash before attempting refunds
  - Only set terminal status at function end when all operations succeed
  - Keep payments in STATUS_CONFIRMED_OVERPAID if refunds fail (allows retry)

* Fix logging errors by safely handling None process results
* Update test mocks to include required fields and new function signatures
* All 94 crypto watcher tests now pass

This allows failed refunds to be retried without re-delivering products to customers or losing funds.
2025-09-29 13:29:00 -04:00
446a7c3526 Fix comprehensive DOGE payment processing issues
- Fix DOGE amount parsing precision in get_dogecoin_incoming_transfers
- Fix payment comparison logic to use new_sum directly instead of adding to received_amount
- Fix PaymentRescue to use coin-specific clients and RPC calls
- Fix Decimal/float type conversion in wallet scanning
- Add proper balance checking for DOGE vs XMR clients
- Implement coin-specific refund mechanisms (sendtoaddress vs transfer)
- Fix confirmation requirements to use coin-specific values

Resolves underpayment detection bug where DOGE amounts were parsed as 4 koinu
instead of proper atomic units, causing false underpayment flags.
2025-09-29 11:50:53 -04:00
d1173d4848 Fix DOGE refunds using wrong client - critical bug fix
- PaymentRescue was using whatever client was passed in (usually Monero)
- Now gets coin-specific client based on payment.coin_type
- DOGE refunds will use DOGE client, XMR refunds use XMR client
- Fixes 404/500 errors when attempting DOGE refunds

This was causing DOGE refunds to fail because they were trying to call
sendtoaddress on the Monero RPC endpoint instead of Dogecoin RPC.
2025-09-29 10:23:11 -04:00
35780e4245 Implement DOGE refund support in crypto payment rescue
- Add coin-specific refund logic (XMR uses transfer, DOGE uses sendtoaddress)
- Fix confirmation threshold logging to use coin-specific requirements
- Improve refund amount logging to show correct coin type and atomic units
- DOGE refunds now use sendtoaddress RPC instead of failing with 404

Note: Crypto watcher needs restart to pick up changes
2025-09-29 10:20:08 -04:00
5df297eade Fix hardcoded confirmation requirements in crypto payment rescue
- Use coin-specific confirmation requirements from get_coin_config
- DOGE refunds now process at 2 confirmations instead of 10
- Improve logging to show correct coin type and atomic units
- Still need to implement DOGE-specific refund mechanism (currently uses XMR transfer RPC)
2025-09-29 10:18:44 -04:00
3d7289d517 Fix DOGE underpayment detection and XMR exact payment regression
Critical fixes:
1. DOGE amount normalization in scan_wallet_for_double_or_late_payments
   - Convert DOGE amounts from listsinceblock to atomic units (koinu)
   - Prevents amounts like 4.38281195 DOGE being parsed as 4 koinu

2. DOGE confirmation updates in update_payment_confirmations_only
   - Implement proper confirmation tracking using gettransaction RPC
   - Allows DOGE refunds to process at 2 confirmations (not 10)

3. Fix XMR exact payment regression
   - Use new_sum directly for first payments (don't add to received_amount)
   - We don't allow multiple payments, so blockchain amount is authoritative
   - Fixes exact XMR payments being incorrectly flagged as underpaid

4. Update coin-specific confirmation requirements
   - DOGE uses 2 confirmations (not 10) for refunds
   - Add confirmations_required to COIN_CONFIGS

5. Add comprehensive debug logging for amount comparisons
   - Helps diagnose precision and comparison issues

This should fix both the DOGE "4 koinu" bug and XMR exact payments
being flagged as underpaid when using QR code payments.
2025-09-29 10:16:38 -04:00
e2392c0132 Fix DOGE amount parsing precision bug in get_dogecoin_incoming_transfers
- Add explicit float() conversion for DOGE amounts before multiplying by 1e8
- This fixes precision issues where amounts like 4.38281195 DOGE were being
  converted to 4 koinu instead of 438281195 koinu
- Add regression test for precise DOGE amounts to prevent future issues
- Resolves underpayment detection false positives for valid DOGE payments

The root cause was tx.get("amount") returning string values that when
multiplied by 100000000 resulted in incorrect integer conversions.
2025-09-29 08:47:36 -04:00
3e9066273a Add crypto logos to checkout UI and clean up sweep scripts
- Add Monero and Dogecoin logo images to static assets
- Update cart_checkout.j2 to display crypto logos on payment buttons
- Update crypto_checkout.j2 to show logos in checkout headers
- Remove duplicate sweep script files (dogecoin-sweep-all.sh, sweep-all-doge.sh)
- Update CLAUDE.md with development standards and testing guidelines
2025-09-29 08:22:49 -04:00
a4dba0b98a Fix failing TransactionMatchingIntegrationTests and add invoice null check
- Fix process_payment to check for null invoice before calling finalize_invoice
- Update TransactionMatchingIntegrationTests to use real Invoice objects instead of None
- Add explicit dbsession.flush() calls after process_payment in tests to ensure transaction persistence
- Update test expectations from STATUS_RECEIVED to STATUS_CONFIRMED for sufficient confirmations
- Add comprehensive documentation explaining transaction flow and test methodology
- Fix shop name uniqueness issues in test setup with UUID-based names

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-29 08:13:36 -04:00
2865a95130 Fix critical crypto watcher transaction matching bug
Fixed issue where Dogecoin scanner was incorrectly matching old processed
transactions to new payments, causing cross-payment contamination. Added
comprehensive transaction deduplication logic that:

- Skips transactions already processed for current payment
- Prevents transactions from being matched to multiple payments
- Maintains database integrity during wallet scanning

Added unit tests in TransactionMatchingTests class and integration tests
in TransactionMatchingIntegrationTests class to prevent regression.

Resolves production issue where dust transactions were being incorrectly
matched to new payment addresses.
2025-09-29 07:29:44 -04:00
b2da59c346 Add Dogecoin (DOGE) payment support mention to README 2025-09-29 06:50:49 -04:00
f1f7572063 Implement crypto quote locking mechanism and fix test failures
- Add check_user_has_pending_quotes() function to prevent infinite pending quotes
- Block new crypto quotes when user has pending/received payments
- Flash warning message and redirect to crypto history for pending quotes
- Disable Monero/Dogecoin buttons in checkout when user has pending quotes
- Fix integration test email mocking to prevent MagicMock string errors
- Fix underpayment test assertion to handle status transition variations
2025-09-29 06:43:19 -04:00
0faa20ba4f Fix DOGE payment underpayment detection and refund processing
- Fix early detection logic to use new_sum instead of received_amount for underpayment check
- Fix hardcoded XMR atomic units (1e12) used for DOGE refunds, use correct coin-specific units
- Add comprehensive tests for atomic unit conversions for DOGE (1e8) and XMR (1e12)
- Prevent false underpayment detection when dust or corrupted amounts exist in database

This fixes production issue where valid DOGE payments were incorrectly marked as underpaid
due to comparing old stored amount instead of new incoming transaction amount.
2025-09-29 06:15:56 -04:00
62e098a131 Fix digital product detection in crypto payment status views
- Replace non-existent download_instructions attribute with proper is_physical check
- Assume digital by default and only mark as non-digital if any product is physical
- Fixes AttributeError during crypto payment finalization for both XMR and DOGE
2025-09-28 17:22:39 -04:00
7885ed5d76 Fix make serve binding and add missing DogecoinClient methods
- Remove config modification logic from make serve to respect existing listen settings
- Remove --reload flag to test remote host binding
- Add missing listsinceblock method to DogecoinClient and MockDogecoinClient for late payment scanning
- Update Dogecoin auto sweep test expectations to match new 0.002 DOGE fee buffer (was 0.1 DOGE)
2025-09-28 17:17:25 -04:00
27a8109f47 Fix make serve to properly override listen address
Use pserve config override syntax: listen=0.0.0.0:6501
instead of invalid --bind flag. This forces the server to bind
to all interfaces on port 6501 for external access during development.
2025-09-28 17:03:23 -04:00