How We Built a Cross‑Platform C++ Architecture for WeChat Pay on iOS & Android
This article explains the challenges of maintaining separate iOS and Android implementations for WeChat Pay, and details how a C++‑based cross‑platform framework—featuring abstracted business flows, a unified routing mechanism, managed network requests, and strict data‑transfer standards—dramatically improves code reuse, stability, and development productivity.
Background
WeChat Pay, as a critical business, faced many issues on client side, especially due to separate platform implementations:
Inconsistent iOS and Android implementations, leading to bugs and communication challenges.
Poor scalability and slow response to business needs.
Incomplete quality assurance and lack of unified automated testing.
Inconsistent user experience across platforms.
To address these core problems and technical debt, a C++ cross‑platform framework was built, covering the core payment flow.
Online Metrics
Crash rate remained stable after release, with no crashes observed in the cross‑platform payment flow. Code size for the core payment process dropped from 6,328 lines (native iOS) to 3,512 lines (cross‑platform), a reduction of about 45%.
Development effort for new features also decreased: the same feature that took 3 person‑days on iOS and Android separately was completed in 3 person‑days combined before the deadline, whereas native implementations required an additional week after the deadline.
What Is Software Architecture?
Software architecture can be defined as the components of a system and their relationships. In the context of WeChat Pay, it aligns with familiar patterns such as MVC and MVVM.
Why We Need Architecture
Complex business requirements introduce inherent complexity. Managing this complexity through separation of responsibilities, code reuse, and clear architectural boundaries improves productivity, much like library classification or organizational structures.
Building a Cross‑Platform Payment Architecture from Scratch
In the mobile client domain, C++ is used for business logic, but no mature architecture existed for UI and navigation. The solution involved four key steps:
1. Abstract Business Flow
Traditional MVC/MVP/MVVM patterns ignore the orchestration of business processes and screen transitions. We introduced a UseCase to encapsulate business flow and a UIPage to represent screens, allowing reuse and clearer separation.
2. Add Routing Mechanism
A unified routing mechanism was created to pass a data model describing the next action (e.g., open a UseCase, launch a UIPage, open a WebView, or invoke a mini‑program). The routing data model combines a route type with the required parameters, enabling consistent handling of special flows.
3. Manage Network Requests
Old implementations used a singleton network center, causing one‑to‑many communication issues and lifecycle bugs. The new design abstracts each network request as an independent object derived from BaseCgi, ties its lifecycle to the owning UseCase, and provides unified caching and encryption.
4. Standardize Data Transfer
Previous architectures relied on a shared mutable model, leading to data pollution and hard‑to‑trace bugs. The new approach uses immutable value types, one‑way data injection, and delegate patterns for necessary callbacks, eliminating public read/write data structures.
Conclusion
By abstracting business processes, introducing a routing mechanism, managing network requests, and enforcing strict data transfer rules, the cross‑platform architecture dramatically reduces code size, improves stability, and accelerates development for WeChat Pay on both iOS and Android. Ongoing evolution will continue to refine the architecture to meet emerging payment scenarios.
For those interested in building a C++ cross‑platform framework from scratch, the keynote from the 2019 QCon Guangzhou session can be downloaded from the provided GitHub link.
WeChat Client Technology Team
Official account of the WeChat mobile client development team, sharing development experience, cutting‑edge tech, and little‑known stories across Android, iOS, macOS, Windows Phone, and Windows.
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.
