I. Introduction to payment SDK

To use payment functionality in your APP, you must integrate some common third-party SDKS. Common payment SDKS are:

Alipay, wechat Pay, Baidu Wallet, e-bank (Yibao) and so on.

The third-party payment aggregator SDK has some common payment channels integrated like shareSDK: ping++, Bmob

We use ping++ because it is more professional.

How to integrate ping++ is only briefly explained here; refer to the ping++ official documentation for details. Create a new module, add jar packages, dynamic SO library, Assest resources, RES, Java source code, etc.

It is important to note that the library module manifest file does not need to write the Application node. The manifest file is not useful for library.

Whatever needs to be configured in the manifest file must be configured in the main project.

In Android Studio2.0, a new JIN folder generates a jni folder by default, but we need the jniLibs folder, which is simply in the main project build.gradle file: Add the following content to the configured Android node and click sync to automatically appear the jniLibs folder.

SourceSets {main {jniLibs. SrcDirs = ['libs']Copy the code

Finally, throw the jar package and so library into the liBS folder (Project view only has the LIBS folder).

Create an Android folder as follows:

2. Configure the main project manifest file, including permissions, activities, meta-data (APPKey), etc.

3. The above are common integration steps. Follow the official documents for subsequent steps.

How do we test our payment functionality? We don’t have to actually pay, the ping++SDK provides us with developer test mode, which allows us to test. When it is applied in practice, we only need to replace the APPKey we applied for with the APPKey of the company and verify it, so that the real payment function can be realized. The APPKey related to payment can only be applied by companies (with certain assets), which is quite strict and will not be mentioned here.

Ii. Payment process

As shown below:

The steps can be summarized as follows:

1. The APP accesses its own server API and submits the order. The important thing to note here is that the item information that the user needs to purchase is submitted via a JSON string, because a JSON string can pass a List object of indefinite length.

2. The server returns the payment voucher Charge to APP. (It is important to understand that the server side also needs to develop related payment.) The payment voucher Charge is a JSON string with a certain format.

3. After getting the payment voucher, the APP initiates a payment request by using the relevant API of the payment SDK. The code is explained in the official documentation as shown below:

** @param createOrderRespMsg */ private void pay(createOrderRespMsg createOrderRespMsg) {Intent intent = new Intent(); String packageName = getPackageName(); ComponentName componentName = new ComponentName(packageName, packageName + ".wxapi.WXPayEntryActivity"); intent.setComponent(componentName); intent.putExtra(PaymentActivity.EXTRA_CHARGE, JSONUtil.toJSON(createOrderRespMsg.getData().getCharge())); startActivityForResult(intent, Constants.REQUEST_CODE_PAYMENT); }Copy the code

4. The payment SDK returns the payment results (including success, failure, relevant payment APP not installed, and the user cancels payment) to the APP with the code as follows:

@param requestCode * @param resultCode * @param data */ @override protected void onActivityResult(int requestCode, int resultCode, REQUEST_CODE_PAYMENT) {if (requestCode == Constants.REQUEST_CODE_PAYMENT) {if (resultCode == Activity. String result = data.getExtras().getString("pay_result"); if (result.equals("success")) { changeOrderStatus(Constants.PAY_SUCCESS); Else if (result.equals("fail")) {changeOrderStatus(Constants.PAY_FAILED); } else if (result.equals("cancel")) { changeOrderStatus(Constants.PAY_CANCEL); } else if (result.equals("invalid")) { changeOrderStatus(Constants.PAY_INVALID); }}}}Copy the code

5. APP requests its own server according to the result returned by the payment SDK, and accordingly changes the order status (e.g. changes it to paid).

6. Finally, the server returns information to the client to indicate whether the operation is successful.

7. Empty your shopping cart of items you’ve already purchased.

If you feel that my words are helpful to you, welcome to pay attention to my public number:

My group welcomes everyone to come in and discuss all kinds of technical and non-technical topics. If you are interested, please add my wechat huannan88 and I will take you into our group.