EasyPay GitHub source link

Vision

Become the most convenient and easy-to-use payment framework for Android platform


Introduction

EasyPay integrates and highly encapsulates Wechat Pay on the Android platform, while Alipay pays by UnionPay. Using this library, developers can implement payment functions using simple, easy and convenient apis, saving a lot of time in integration configuration.

This is a library for Android Developers easily to use Alipay, WechatPay and UnionPay in Android project.


Why do we have this library?

If you don’t want to hear my three or four paragraphs of nonsense, you can jump to the following Usage.

  1. There are many payment platforms. Wechat, Alipay, UnionPay, JINGdong, Meituan, Baidu, etc., need to read various documents, consuming a lot of time and energy.
  2. The integration steps are cumbersome. Integrated WeChat pay all know, must be configured in the package name ${applicationId}. Wxapi. WXPayEntryActivity…
  3. The Api calls are inconsistent. For the same function, the code varies greatly, increasing the cost of understanding.
  4. The payment result receiving logic is decentralized. The calling method is not in the same place as the code that receives the result, the logic is scattered, and the readability and maintainability are poor.

Think deeply

Can we have a multi-in-one library, which can save complicated integration steps, with a unified integration method, unified Api call, unified callback receiving logic, and at the same time, the ability to easily expand the new payment method, to meet the payment function requirements.

My answer is EasyPay. EasyPay has been born for three years, with over 3000+ apps using its payment function, and its extremely simple Api has been recognized by developers for realizing a variety of payment functions.

How to solve the problem

  1. EasyPay cleverly eliminates the need to configure Wxapi.wxPayActivity for wechat Pay. You don’t even need to configure wechat AppId on the client
  2. EasyPay encapsulates wechat Pay, Alipay payment and UnionPay respectively. It is a separate library and integrated on demand. All library integration is completed in one code
  3. EasyPay unified payment call interface, unified payment result receive callback, call code and receive result code centralized
  4. EasyPay reserves the interface to extend other payment methods
  5. EasyPay even found time to use reflection to solve the memory leak problem caused by the UnionPay SDK holding static Context (this problem was not solved after I reported it three years ago… So you have to do it yourself.)
  6. EasyPay will save you time to spend with your girlfriend, hahahahahahaha.

Some people will say, No Picture, you say a XX. OK, understood, arranged!

Screenshot (Payment effect)


Usage

Use the steps are very simple, a total of two steps: 1. 2. Related payment Api calls.

Using Step 1, integrate the dependency libraries

You can use either of the following integration modes:

Remote dependent library integration Or download source code as Module import integration;

Remote dependency library integration approach

Add the following dependencies to the Dependencies block of build.gradle in the main App module of Project:

1) EasyPay Payment base Library (required) :

Note: This step must be added because the library is the EasyPay base class library

Implementation 'com. XGR. Easypay: easypay: 2.0.0'Copy the code

2) Integrate wechat Pay, Alipay and UnionPay as required

Note: The following three libraries can be added or deleted according to actual needs

1) wechat Payment integration (optional) :
Implementation 'com. XGR. Easypay: wechatpay: 2.0.0'Copy the code
2) Alipay Payment integration (optional) :
Implementation 'com. XGR. Easypay: alipay: 2.0.0'Copy the code
3) UnionPay payment integration (optional) :
Implementation 'com. XGR. Easypay: unionpay: 2.0.0'Copy the code

This ends the remote dependency integration approach.

Download the source and import it as module:

1) Integrate base class dependency libraries (mandatory) :

implementation project(':easypay')
Copy the code

2) Integrate other payment dependency libraries as required

1) wechat Payment integration (optional) :
implementation project(':wechatpay')
Copy the code
2) Alipay integration (optional) :
implementation project(':alipay')
Copy the code
3) UnionPay payment integration (optional) :
implementation project(':unionpay')
Copy the code

This is the end of downloading the source code as Module import integration.


Use step 2, the related payment Api call

Wechat Pay (two steps)

1) configuration

In the android{} block ->defaultConfig{} block of the build.gradle file of the main App module, configure applicationId as follows:

        manifestPlaceholders = [
                APPLICATION_ID: applicationId,
        ]
Copy the code
2) API calls
    private void wxpay(a){
        // Instantiate the wechat Payment policy
        WXPay wxPay = WXPay.getInstance();
        // Create a wechat order entity. It is usually returned directly by the server.
        WXPayInfoImpli wxPayInfoImpli = new WXPayInfoImpli();
        wxPayInfoImpli.setTimestamp("");
        wxPayInfoImpli.setSign("");
        wxPayInfoImpli.setPrepayId("");
        wxPayInfoImpli.setPartnerid("");
        wxPayInfoImpli.setAppid("");
        wxPayInfoImpli.setNonceStr("");
        wxPayInfoImpli.setPackageValue("");
        // The policy scenario class invokes the payment method to start the payment and receives the callback.
        EasyPay.pay(wxPay, this, wxPayInfoImpli, new IPayCallback() {
            @Override
            public void success(a) {
                toast("Payment successful");
            }

            @Override
            public void failed(int code, String msg) {
                toast("Payment failure");
            }

            @Override
            public void cancel(a) {
                toast("Payment cancelled"); }}); }Copy the code

This is the end of wechat Pay

Alipay (One step)

    private void alipay(a){
        // Instantiate the Alipay payment policy
        AliPay aliPay = new AliPay();
        // Create alipay order entity. It is usually returned directly by the server.
        AlipayInfoImpli alipayInfoImpli = new AlipayInfoImpli();
        alipayInfoImpli.setOrderInfo("");
        // The policy scenario class invokes the payment method to start the payment and receives the callback.
        EasyPay.pay(aliPay, this, alipayInfoImpli, new IPayCallback() {
            @Override
            public void success(a) {
                toast("Payment successful");
            }

            @Override
            public void failed(int code, String msg) {
                toast("Payment failure");
            }

            @Override
            public void cancel(a) {
                toast("Payment cancelled"); }}); }Copy the code

This is the end of alipay

Unionpay (One step)

    private void unionpay(a){
        // Instantiate the UnionPay payment policy
        UnionPay unionPay = new UnionPay();
        // Create unionPay order entity. It is usually returned directly by the server. You can use mode. TEST for testing and mode. RELEASE for publishing.
        UnionPayInfoImpli unionPayInfoImpli = new UnionPayInfoImpli();
        unionPayInfoImpli.setTn("814144587819703061900");
        unionPayInfoImpli.setMode(Mode.TEST);
        // The policy scenario class invokes the payment method to start the payment and receives the callback.
        EasyPay.pay(unionPay, this, unionPayInfoImpli, new IPayCallback() {
            @Override
            public void success(a) {
                toast("Payment successful");
            }

            @Override
            public void failed(int code, String msg) {
                toast("Payment failure");
            }

            @Override
            public void cancel(a) {
                toast("Payment cancelled"); }}); }Copy the code

This is the end of UnionPay


Introduction to Project Implementation

The implementation of easy payment coding follows the six principles of design pattern, and uses singleton and policy pattern to realize the whole library, which has good scalability and can be easily extended to other payment methods.

├ ─ ─ the activity │ ├ ─ ─ UnionPayAssistActivity. Java// UnionPay auxiliary Activity, responsible for initiating unionPay payment interface and receiving callback. The client doesn't need to care.│ └ ─ ─ WXPayEntryBaseActivity. Java// wechat Pay callback Activity encapsulation. The client inherits the Activity and implements the getAppId() method.├─ Alipay │ ├─ Alipay. Java// Alipay payment API encapsulation, implementation of IPayStrategy interface│ ├─ ├─ ├─ ├.java ├─ ├.java ├─ ├.java │ ├─ ├.java │ ├─ ├.java │ ├─ ├.java// Easy pay Payment information base class interface│ └ ─ ─ IPayStrategy. Java// Easy pay payment policy base class interface├─ Callback │ ├─ ├.java// Easy pay unified callback interface├ ─ ─ EasyPay. Java// Easy to pay scenario class, client caller├── Unionpay │ ├─ Java │ ├─ Unionpay// UnionPay API encapsulation, implementation of IPayStrategy interface│ ├─ ├─ ├─ ├.java │ ├─ ├.java │ ├─ ├.java// wechat pay API encapsulation, implementation of IPayStrategy interface└ ─ ─ WXPayInfoImpli. Java └ ─ ─ WXErrCodeEx. JavaCopy the code

Framework extension to new payment platforms (meituan, JD, etc.)

EasyPay has fully considered code expansibility, enabled policy mode, adopted interface-oriented programming and followed the dependency inversion design principle since the beginning of project establishment. It is easy to extend new payments from the payments base class. Just three steps. The following steps are for reference. For more details, please refer to alipay, wechat or UnionPay in the project.

1) IPayInfo interface is realized for payment order information class

public class XXpayInfoImpli implements IPayInfo {
    public xxType xxField = xxx;
    publicyyTYpe xxFiled = yyy; . other Field }Copy the code

2) Payment strategy class to implement IPayStrategy.

Pass the payment entity class from step 1 into the generic. The original intention of the payment strategy is to centrally encapsulate all operations of a certain payment, and call this class wherever the business needs to use the payment.

public class XXPay implements IPayStrategy<XXpayInfoImpli> {
    private AlipayInfoImpli alipayInfoImpli;
    private static IPayCallback sPayCallback;

    @Override
    public void pay(Activity activity, AlipayInfoImpli payInfo, IPayCallback payCallback) {
        this.mActivity = activity;
        this.alipayInfoImpli = payInfo;
        sPayCallback = payCallback;
    }
    
    ...other method
}
Copy the code

After completing the above two steps, it can be called where necessary according to the business. It should be noted that when the payment callback of a payment platform is scattered, the call can be forwarded to the above payment class in the corresponding place. In this way, the logic can be concentrated into a single class. If you do not understand this paragraph, you can look at the onActivityResult() method in UnionPay to pay assistActivity, and then transfer the logic to unionpay.handleresult (this,data).

3) Call the Api

        // Instantiate the payment policy
        XXpay xxPay = new XXPay();
        // Create alipay order entity. It is usually returned directly by the server.
        XXpayInfoImpli xxpayInfoImpli = newXXpayInfoImpli(); xxpayInfoImpli.setXXFiled(); .// The policy scenario class invokes the payment method to start the payment and receives the callback.
        EasyPay.pay(xxPay, this, xxpayInfoImpli, new IPayCallback() {
            @Override
            public void success(a) {
                toast("Payment successful");
            }

            @Override
            public void failed(int code, String message) {
                toast("Payment failure");
            }

            @Override
            public void cancel(a) {
                toast("Payment cancelled"); }});Copy the code

This concludes the extension introduction


(ChangeLog) Update logs

V2.0.0 update (2019/10/27)

  1. Simplify the integration and use steps of wechat pay, and update the SDK of wechat Pay
  2. Update alipay SDK
  3. Update unionPay SDK
  4. The payment callback fail method returns code and message

To contact me

1) QuestionsIssues. Welcome to exchange ideas.

2) Email (Email: [email protected])

3) Add QQ group for free for a limited time (group number: 797559567)


License

MIT License


Copyright (c) 2017 kingofglory


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:


The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Copy the code