Skip to content

Refactor subscription migration system to fix infinite loop, add state tracking and HPOS support #2133

@amans2k

Description

@amans2k

Problem

The current subscription migration system (class-angelleye-paypal-ppcp-migration.php) has critical issues:

  1. Infinite loop bug — Subscriptions with missing payment tokens (e.g., empty _payment_tokens_id, _ppec_billing_agreement_id) are reprocessed indefinitely since there's no mechanism to mark them as permanently failed.
  2. No state tracking — No lifecycle states for migration progress; impossible to distinguish pending vs completed vs failed subscriptions.
  3. Order note spam — Each failed retry adds duplicate notes, polluting subscription history with thousands of identical entries.
  4. No HPOS compatibility — Migration queries don't support WooCommerce High-Performance Order Storage.
  5. Monolithic architecture — All logic in a single 648-line class with no separation of concerns, no dependency injection, and no tests.

Solution

Refactor into a modular service-based architecture:

  • Migration_Controller — Facade/orchestrator with public API (start, stats, retry, reset)
  • Subscription_Migration_Service — Core migration logic with proper error handling
  • Action_Scheduler_Batch_Processor — Queue management with batch limits and delays
  • Payment_Token_Validator — Validates 4 token meta keys with format checking
  • Payment_Method_Updater — Handles payment method switching
  • HPOS_Migration_State_Storage — HPOS-compatible state persistence
  • Migration_Status enum — Categorized statuses: COMPLETED, FAILED_NO_TOKEN, FAILED_API_ERROR, FAILED_DATA_ERROR, SKIPPED_EXCLUDED, SKIPPED_MANUAL
  • DTOsMigration_Result, Batch_Result, Migration_Stats
  • Migration_Admin_Page — Dashboard with real-time progress, failure counts by type, retry/reset controls
  • Test suite — Unit and integration tests

Key behavioral changes

  • Subscriptions get lifecycle states: NOT_STARTED → IN_PROGRESS → COMPLETED/FAILED_*
  • Failed subscriptions are categorized and skipped on next run (no infinite loop)
  • Order notes only added on first failure attempt
  • HPOS auto-detection for compatible storage queries

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions