Building a Cross‑Platform Payment Architecture with C++ for WeChat Pay
This article describes how a C++‑based cross‑platform framework was designed and implemented to unify iOS and Android payment flows in WeChat Pay, covering architectural motivation, business‑process abstraction, routing mechanisms, network request management, and standardized data transmission to improve quality and productivity.
Background
WeChat Pay suffers from platform‑specific implementations that cause bugs, poor extensibility, incomplete quality assurance, and inconsistent user experience across iOS and Android.
Inconsistent implementations lead to bugs and communication overhead.
Low extensibility results in long iteration cycles and incomplete data reporting.
Quality assurance is weak due to lack of design knowledge, loose protocol management, and missing automated tests.
User experience differs, as shown by the pre‑unification checkout screens.
To address these issues, a C++ cross‑platform framework was built and the core payment flow was refactored. The framework has been fully deployed on iOS 7.0.4+ and Android 7.0.7+.
Online Effect Metrics
After launch, crash rates remained stable, and no new crashes were observed during the switch. Code size for the core payment flow dropped from 6,328 lines (native) to 3,512 lines (cross‑platform), a 45% reduction. Development effort for new features decreased from ~1 week (native) to 3 person‑days (cross‑platform).
What Is Software Architecture?
Software architecture is defined here as the components of a system and their relationships, often expressed through patterns such as MVC or MVVM .
Why Architecture Is Needed
Complex business requirements create inherent complexity; architecture manages this complexity by separating responsibilities, enabling reuse, and improving productivity.
Building the Cross‑Platform Payment Architecture from Scratch
1. Abstract Business Process
Traditional MVC/MVP/MVVM focus on UI but ignore business flow and page transitions. We introduce a UseCase to encapsulate a business process and a UIPage to represent a screen, allowing reuse and clear separation.
2. Add Routing Mechanism
A routing layer receives a data model (derived from network responses) and dispatches actions such as opening a UseCase , a UI page, a WebView, a mini‑program, or a dialog. The routing model is defined with protobuf and generated for both client and server.
3. Manage Network Requests
Old implementations used a singleton network center, causing one‑to‑many communication issues and lifecycle bugs. The new design treats each network request ( CGI ) as an independent command object derived from BaseCgi , with a one‑to‑one relationship to its owning UseCase , ensuring proper lifecycle management.
4. Standardize Data Transmission
We replace shared mutable models with immutable value types passed via dependency injection. Data flows in a single direction; when upstream components need updates, a delegate pattern is used. This eliminates data pollution and makes data flow traceable.
Resulting Architecture
The final architecture consists of:
Business‑process abstraction ( UseCase ).
Unified routing handling both normal and special flows.
Command‑style network request objects with clear lifecycles.
One‑way, type‑safe data propagation.
These improvements have reduced code duplication (up to 83% for special flows), lowered bug rates, and increased development efficiency.
Summary
Good software architecture manages complexity arising from demanding business scenarios. By refactoring WeChat Pay’s client code into a C++‑based cross‑platform framework with clear abstractions, routing, network management, and data flow, the team achieved higher quality, faster feature delivery, and a solid foundation for future enhancements.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.