Wechat payment cross platform software architecture

Time:2021-8-23

background

As an important business, wechat payment faces various problems on the client. The core problem is the problem caused by sub platform implementation. Common problems are:

  1. The implementation of IOS and Android is inconsistent: it is easy to bug and the quality cannot be guaranteed through communication;
  2. Poor scalability, unable to quickly respond to business requirements: long iteration cycle of requirements change and incomplete data reporting;
  3. Imperfect quality assurance system: lack of precipitation of business and design knowledge, loose protocol management and lack of unified automatic testing;
  4. Inconsistent user experience: for example, the following figure shows the cashier before Android and IOS were not unified.

Wechat payment cross platform software architecture
In order to solve the core problem of multiple platform implementation and solve the previous technical debt. We have established a set of cross platform framework based on C + +, and reconstructed the core payment process. Wechat payment cross platform is fully covered from IOS version 7.0.4 and Android version 7.0.7.

target

Taking the current situation of IOS running online as an example, wechat payment cross platform has basically achieved the following indicators:

Crash

The crash rate remained stable before and after the launch, which did not affect the stability of wechat. There was no need to present crash for cross platform payment, so that users could switch without perception.
For example, you can send a red envelope through wechat. The cashier and payment process are driven by cross platform code written based on C + +.

Efficiency improvement

Wechat payment cross platform software architecture
Taking the core payment process code as an example, 3512 lines are required for cross platform and 6328 lines are required for IOS native. Reduced code by nearly 45%.

Take the development of new requirements as an example:

  • Cross platform implementation: IOS + Android has a total of 3 person days, which shall be completed before the board closing time
  • Native implementation: IOS and Android were basically completed one week after the board was closed
  • Cross platform implementation: IOS + Android has a total of 5 person days, which shall be completed before the board closing time
  • Native implementation: IOS and Android were basically completed one week after the board was closed

So how can payment cross platform software architecture effectively guarantee quality and improve productivity? Look at the architecture diagram below.
Wechat payment cross platform software architecture
Students interested in how to build a cross platform framework from zero to one based on C + + can check building a cross platform development framework for wechat clients based on C + +.

What is software architecture

What is software architecture? As Ivar Jacobson (the father of UML) said, find five people to answer this question, and five people may have different answers.

There are many ways to define an architecture, from code specification to publishing process.

According to the business characteristics of wechat payment, the definition of architecture here is: architecture is the component of the system and its relationship (communication mode). This is more in line with our programmers’ understanding of architecture when writing business code, that is, MVC, MVP, MVVM, etc. in a popular sense.

As early as 1986, when discussing the complexity of software, the author of man month myth said that the essential complexity of software exists in complex business requirements.

The most fundamental means of managing complexity is the separation of responsibilities. In order to realize the separation of responsibilities and code reuse, the architecture is slowly reproduced. The essence of architecture is managing complexity.

Without architecture, all our codes are coupled together. The human mental model is not good at dealing with this complexity. The establishment of architecture is the same as the book classification of the library and the organization division of the company. Is to manage complexity to achieve higher productivity.

Building payment cross platform software architecture from zero to one

In the field of mobile client, the industry writes business code based on C + +, and there is no mature architecture. Even if the business logic is written in C + +, it does not involve the UI or the jump process of the interface.

Since the industry does not have a mature architecture to learn from, is it good to simply apply the common architecture in the industry?

1. Abstract business process

Now MVC, MVP and MVVM are commonly used in the industry. These are familiar software architectures. However, there is a problem with these software architectures: they do not handle the business process and interface transition well. There are many wechat payment processes. The process is composed of one interface (view controller, activity) and related business logic.

The above MV (x) mode ignores a very important point, that is, who is responsible for the transition of business process and interface. That is, who maintains the relationship between viewcontroller and viewcontroller and where the logic of business process is written. If you still follow the traditional MVC mode, the viewcontroller is responsible for communicating with different viewcontrollers. Then the viewcontroller cannot be reused. What’s more fatal is that the code of the business process is very unclear. The code of the business process is scattered into each controller, and a controller may be coupled with the code of multiple businesses.

For example, an ordinary transfer process may involve risk control interception, real name verification, cashier, card binding, payment success page, etc. If it is based on MVC architecture, the code will soon become difficult to maintain.
Wechat payment cross platform software architecture
Therefore, in order to adapt to the characteristics of many wechat payment processes and complex interface jump. The first step of architecture abstraction is to abstract the business process into an independent role usecase. At the same time, the interface is abstracted as uipage. A large business process can be decomposed into small business processes.
Wechat payment cross platform software architecture
Compared with the previous architecture based on MVC chaos, usecase has the following advantages:

  • The business process code can be aggregated into usecase instead of scattered into the original IOS and Android viewcontrollers and activities.
  • Business processes and interfaces are reused.
  • It is in line with the multi process of wechat payment and the complex business characteristics of interface jump.

2. Join routing

Now that the process has been abstracted, we need to think more about the business process. When developing payment business processes, developers cannot bypass the following problems:
1. Circulation between processes and pages.

Wechat payment cross platform software architecture
For example, we need to transfer money to a friend, enter the amount, confirm the payment, and trigger CGI. The next process is changeable. It is possible that the user needs to go to the real name, the user may want to enter a secure intercepted WebView, or pull up the cashier normally.

In the past, there was no unified processing mechanism when IOS and Android were implemented separately. It is either judged by a field of the network packet back, or it is to maintain some local states to decide what process to go next, which is very cumbersome and error prone.

2. Handling of special processes
Wechat payment cross platform software architecture
There is also a special place in the payment business process, that is, in the middle of the normal process, it is often necessary to insert some special processes. For example, some places need to jump to WebView, some places need to jump to applet, some places need to pop up windows to inform users of risks, or terminate the current process, etc. We often need to repeatedly add such processing in business code.

It is precisely because of these problems that we need to consider the routing mechanism of wechat payment. First, learn about the routing mechanism.
Wechat payment cross platform software architecture
The core idea of routing mechanism is to pass data to the route, and then the route parses the data and responds. Combined with the closely related characteristics of wechat payment and network, we innovatively take the payment domain model as the transmitted data.
Wechat payment cross platform software architecture
So how to build this payment domain model?
Modeling is to establish mapping. Domain knowledge + modeling method = domain modeling. The domain knowledge here is the understanding of payment business process. Modeling method, I use UML modeling. Finally, it will be implemented as proto protocol for the client and background to use together.
Wechat payment cross platform software architecture
First of all, wechat payment business is closely related to the network. Processes and pages are often connected by CGI. Therefore, when building the model, the outermost layer is network packet return. For the routing mechanism, we only care about the routing data model.

The routing data model is composed of routing types and the information required by each routing type.

The routing type clearly defines the behavior to be triggered. Whether to open a usecase or an interface, or a web page, applet, pop-up window, etc.

Then there is the data needed for these behaviors. For example, the parameters required to open the applet and pop up the window.
Wechat payment cross platform software architecture
After establishing the payment domain model, our route analysis becomes very clear. After route resolution, different actions will be triggered according to the route type. For example, the process and interface flow will be handed over to usecase for processing. Special processes, such as opening applet, WebView and pop-up window, will be handled uniformly.

In the first step, we abstract the business process as usecase. The second step is to add a routing mechanism. After adding the routing mechanism, the cross platform software architecture of payment evolves like this.
Wechat payment cross platform software architecture
After adding the routing mechanism, compared with the original old architectures of IOS and Android, the new architecture has the following advantages:

  • Unified process, page flow. Clear and easy to maintain.
  • It unifies the processing of special processes and reduces repetitive work.
  • When adding the routing mechanism, combined with the closely related characteristics of wechat payment and network, the payment domain is modeled. The core idea of payment background protocol reconfiguration 2.0 also revolves around this routing mechanism.

Wechat payment cross platform software architecture
Let’s take another look at the productivity improvement after adding the routing mechanism. Take the WebView applet opened in the payment process as an example, reducing the code by nearly 83%. More importantly, the special processes here are handled uniformly in the routing mechanism, are not coupled to the business code, and are reusable.

3. Manage network requests

First, let’s take a look at the defects of IOS in processing payment network requests. The following is a schematic diagram.
Wechat payment cross platform software architecture
The original payment request is initiated through a single instance network center, and then after receiving the packet return, it is recalled to the business party by throwing a notice or calling a closure. For the above structure, there are such problems:
1. CGI one to many communication problem.
Take a previous problem.

Wechat payment cross platform software architecture
Then the CGI packet sent by the wallet will overwrite the data on the collection and payment page. Previously, IOS can only be solved by patching, increasing the scene value and adding some marker bits. Maybe a new pit will appear one day.

  • After entering the wallet page, a CGI is launched
  • Then enter the collection and payment page and launch the same CGI
  • If the subcontracting initiated by collection and payment arrives first
  • Then return the package on the front page of the wallet.

2. CGI life cycle problem.
Wechat payment cross platform software architecture
From time to time, users will give feedback about why they didn’t do anything, and suddenly a network error will pop up. The reason is that CGI’s life cycle has problems. After the business is completed, CGI’s packet return is still processed.

Solution

1. Abstract CGI as an independent object
In terms of architecture design, the old architecture is an intensive API implemented through singleton mode, while our new architecture is a discrete API implemented through command mode. That is, CGI is encapsulated as a stand-alone object. We cohesive CGI related attributes and capabilities. When developing business, simply inherit basecgi and set parameters.

2. Divide responsibilities and clarify the life cycle
As for who initiated CGI, there was no unified approach between Android and IOS before. Some people will put activity, viewcontroller and UI code together.

Therefore, in the cross platform software architecture, we are uniformly initiated by the business process usecase. And the life cycle is one-to-one. A CGI will only be processed by one usecase. After the usecase is destroyed, the CGI will also be destroyed.

Wechat payment cross platform software architecture
Compared with the old architecture, the new architecture has the following advantages:

  • Eliminate the bug caused by one to many communication.
  • The life cycle is bound with the business logic. The business will not end. The action will be triggered after CGI returns.
  • High cohesion, low coupling. The CGI related data can be processed in a centralized manner, and the service side does not need to perceive it.
  • Provide unified cache and encryption capability.

Wechat payment cross platform software architecture
In the first and second steps, we abstract the business process and add the routing mechanism.
Wechat payment cross platform software architecture
After the third step of managing network requests, our software architecture evolved like this.
Wechat payment cross platform software architecture

4. Standardize data transmission

The old architectures of IOS and Android have problems of improper information transmission and data pollution. This problem is the most serious. IOS and Android have had a lot of bugs.

First of all, let’s take a look at the problems that have occurred on the current network recently: before IOS appeared, many internal colleagues and external users were feeding back: after the change page, the blank box will pop up for no reason. The payment is related to money, causing users’ panic.

After investigation, the specific reasons are:

  • When entering the payment home page, the background returns the data, which is then written to a public model
  • Then go to the wallet page, and then go to the change page. The public model is passed all the way.
  • Then the change page reads the data of the public model, but the code can’t handle it, resulting in this problem that makes users panic.

In addition, there were many change display errors on Android and IOS, such as wallet page. At the time of payment. Bank card failure and so on. These problems are diverse. It seems that the places and scenes are different. Every time I encounter such problems, I can only repair them. However, if you go deeper, you will find that the real reason is the problem in the software architecture:
Wechat payment cross platform software architecture
The old payment architecture adopts the blackboard mode, although it facilitates data reading and writing. However, the problems and benefits are not directly proportional:

  • There are data types for public reading and writing. The data type passed by Android is a dictionary, while IOS is a model object. All interfaces and business logic share one data.
  • Disordered data flow. The flow of data is not traceable, and data modification can occur anywhere public data is used.

In view of the above problems, the payment cross platform software architecture. In order to eliminate such problems, the following architecture improvements are made.
Wechat payment cross platform software architecture

  1. Remove public read / write data types
  2. The data of value type is transferred. When the data is modified in the later process, the previous process will not be affected.
  3. One way transfer of data, relying only on the injection of necessary data.
  4. If the data modification needs to be notified to the pre process, the agent mode communication is used.

After standardizing data transmission. Compared with the old architecture:

  1. The architecture fundamentally solves the problem of data pollution that has plagued wechat payment for a long time.
  2. The flow of data becomes unidirectional and traceable.

In the first three steps, we abstract the business process, add the routing mechanism, and uniformly manage the network requests. The architecture is as follows.
Wechat payment cross platform software architecture
After standardizing data transmission, our software architecture will evolve like this.
Wechat payment cross platform software architecture

summary

The essential complexity of software exists in complex business requirements. The essence of software architecture is to manage complexity. Therefore, a really good architecture is repeatedly refined and summarized from complex business requirements to solve real business problems, not empty talk.

Software architecture is the cornerstone of our business development in addition to cleaning up the defects of the old historical architecture. It can also empower the business and bring value to the business. On the basis of establishing the software architecture, the cross platform automatic data reporting mechanism of wechat payment is also established around the software architecture, so as to prevent repeated payment, secure crosscutting and other capabilities to bring huge business benefits. If you have the opportunity, you will further write relevant articles to communicate and discuss with you later.

Architecture is an evolving process. With the continuous writing of new payment services based on cross platform software architecture, I will continue to update and iterate this architecture. Make this software architecture more suitable for wechat payment, more robust and complete.

Reference: building wechat client cross platform development framework based on C + +