Cross‑Platform Refactoring of WeChat Pay: Architecture, Design Patterns, and Performance Gains
The article describes how the WeChat Pay client was rebuilt using a C++‑based cross‑platform framework to unify iOS and Android implementations, improve code reuse, reduce bugs, and achieve measurable performance and productivity improvements while introducing a UseCase‑driven architecture, routing mechanism, and disciplined network request handling.
1. Introduction WeChat Pay, as a critical business feature, suffered from fragmented iOS and Android implementations that caused bugs, poor scalability, weak quality assurance, and inconsistent user experience.
2. Core Problems The main issues were: (1) inconsistent implementations across platforms; (2) low extensibility and slow response to business changes; (3) incomplete quality‑guarantee system; (4) divergent UI experiences.
3. Cross‑Platform Solution To address these problems, a C++‑based cross‑platform framework was built, allowing the same payment logic to run on iOS (from version 7.0.4) and Android (from version 7.0.7). The new architecture reduced the core payment code from 6,328 lines (native) to 3,512 lines (cross‑platform), a 45% reduction, and accelerated feature delivery.
4. What Is Software Architecture? Architecture is defined as the set of components and their communication relationships. For WeChat Pay, this translates to the classic MVC/MVP/MVVM patterns, but with a focus on managing complex business flows and UI transitions.
5. From 0 to 1: Building the Framework Existing C++‑based business code lacked a mature UI architecture. The solution introduced two new abstractions: UseCase (encapsulating business flow) and UIPage (representing screens). This decouples flow logic from view controllers and enables reuse.
5.1 Abstract Business Flow By moving flow logic into UseCase objects, code is no longer scattered across iOS ViewControllers and Android Activities. This improves maintainability and supports complex multi‑step processes such as transfers, risk control, and real‑name verification.
5.2 Routing Mechanism A routing layer was added to pass a unified data model (derived from network responses) to the appropriate UseCase or UIPage. The router parses the data and triggers actions such as opening a mini‑program, a WebView, or a dialog, eliminating ad‑hoc state handling.
5.3 Managing Network Requests The legacy design used a singleton network center, leading to one‑to‑many communication bugs and lifecycle mismatches. The new design treats each network request (CGI) as an independent command object bound to a single UseCase, ensuring clear ownership and proper cleanup.
5.4 Standardising Data Transfer The previous "blackboard" model caused data pollution and unpredictable flows. The refactor replaces shared mutable models with immutable value types, single‑direction injection, and delegate callbacks for necessary backward communication, resulting in traceable and safe data flow.
6. Results After the refactor, crash rates remained stable, code size decreased by ~45%, and development effort for new features dropped from a week to a few days. Special flows (e.g., opening a WebView) saw an 83% reduction in code. The architecture now provides a solid foundation for future feature expansion, automated reporting, and security cross‑cutting concerns.
7. Conclusion Good software architecture manages complexity inherent in business requirements. The WeChat Pay cross‑platform framework demonstrates how disciplined design—UseCase abstraction, routing, command‑based networking, and immutable data—can solve real‑world problems, improve productivity, and enable continuous evolution.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.