Android App payment series (two): detailed guide for Alipay SDK access (with official payment demo)

Posted by shawjames on Sat, 15 Jun 2019 23:48:25 +0200

Links to the original text: http://blog.csdn.net/shenggaofei/article/details/52022206

Preface

A mobile Internet company, in the final analysis, payers are always required to make profits. It is obviously not wise for a company with limited resources to develop its own payment system. Ali is one of many mature mobile payment providers in China.  
Following Android App Payment Series (1): Detailed Guide to Wechat Payment Access Later, some of them came from Guo Lin's public address: guolin_blog readers and blog readers, hoping to write an access blog for Alipay app to pay sdk.  
The author summed up the Alipay under Ali. Android SDK payment access process for later reference.

This article is authorized WeChat The public number guolin_blog (Guo Lin) was released exclusively.

The access process is as follows:

1 sign up to become Alipay merchants

Signing address: https://b.alipay.com/, 
Only developers who become contracted merchants can have the qualifications to integrate Alipay app payment.  
Signing materials: 1) Business license 2) APP description document 3) Business operation information, business contacts and other information.
APP apk is also required for audit if necessary. Code integration can be implemented after audit.

After the approval is passed, the information such as the merchant number assigned by Alipay can be obtained.

2 Client Code Integration Preparation

2.1 Import jar package resources

The latest version of Alipay's jar package download address: http://download.csdn.net/detail/xiong_it/9566771 
After downloading, copy the libs directory and Eclipse automatically adds dependencies. Android Studio needs to add a line to app gradle

<code class="language-Groovy hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">compile <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">files</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'libs/alipaySdk-20160223.jar'</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

Click on the top right corner: Sync Now, wait a moment.

2.2 Modify the Android Manifest. XML List

Statement of necessary activities

  1. <activity  
  2.             android:name="com.alipay.sdk.app.H5PayActivity"  
  3.             android:configChanges="orientation|keyboardHidden|navigation"  
  4.             android:exported="false"  
  5.             android:screenOrientation="behind" >  
  6. </activity>  
  7. <activity  
  8.             android:name="com.alipay.sdk.auth.AuthActivity"  
  9.             android:configChanges="orientation|keyboardHidden|navigation"  
  10.             android:exported="false"  
  11.             android:screenOrientation="behind" >  
  12.  </activity>  

Add the necessary permissions

  1. <uses-permission android:name="android.permission.INTERNET" />  
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  3. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  4. <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  5. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  


2.3 add Alipay confusion rules

  1. -libraryjars libs/alipaySDK-20160223.jar  
  2.   
  3. -keep class com.alipay.android.app.IAlixPay{*;}  
  4. -keep class com.alipay.android.app.IAlixPay$Stub{*;}  
  5. -keep class com.alipay.android.app.IRemoteServiceCallback{*;}  
  6. -keep class com.alipay.android.app.IRemoteServiceCallback$Stub{*;}  
  7. -keep class com.alipay.sdk.app.PayTask{ public *;}  
  8. -keep class com.alipay.sdk.app.AuthTask{ public *;}  

3 Client Code Integration

Schematic diagram of Alipay interaction process

Interpretation of Flow Chart in Vernacular

  1. app carries the payment information to call the payment interface request, and the Alipay client transfers the payment interface.
  2. User operation, input password payment, payment success; directly return to cancel payment; error, payment failure; enter the payment interface, but enter password payment, payment to be confirmed;
  3. The Alipay client tells the app client the result of payment, and the merchant server notifies the app server of the payment result.
  4. app client handles payment results.
  5. The app server processes the payment results.

The payment process of Alipay is more than that of WeChat Payment flow chart is one step less than the generation of APP server-side orders, but in its demo code, it is recommended to use app server for sign ature process, so the author simply follows the process of micro-payment.( Wechat Payment Interactive Process ) De-integrated.

  • The client code obtains the information of the goods purchased by the user and passes it to the app server of the company. The parameters include, but are not limited to, the following:
  1. params.put("money", payMoney);//Amount of merchandise, unit: yuan  
  2.      params.put("goodsname", goodsName);//Name of commodity  


Note: Alipay pays RMB units and WeChat pays, and UnionPay payment is slightly different. The other two payment units are:
All other parameters can be handed over to our app server. For more details, please click: https://doc.open.alipay.com/doc2/detail?treeId=59&articleId=103663&docType=1

  • The app server refers to the link of the above detailed parameters, gets the sign field and returns the sign field to the mobile client.
  • The mobile client uses sign signature information to initiate payment client in non-UI thread for payment.
    User operation: input password to pay; return key to cancel payment; enter the payment interface, the user did not pay, the user returned, pending payment; network connectionless payment failure;
  • The client gets the payment result.
  • Alipay server asynchronously notifications our company app server payment result (the server's work has nothing to do with the client).

Benefits of doing so: Signature logic is done on the server, app does not need to expose public and private keys, and is safer. It is also recommended in demo.

For more detailed explanation of Alipay access interaction process, please click: https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.7WO30X&treeId=59&articleId=103658&docType=1

4 Client Code Example

Payment callup code (must be in sub-threads)

  1. new Thread() {  
  2.                @Override  
  3.                public void run() {  
  4.                    super.run();  
  5.                    PayTask payTask = new PayTask(mActivity);  
  6.                    String result = payTask.pay(signInfo, true);  
  7.                    Message message = mHandler.obtainMessage();  
  8.                    message.what = PAY_RESULT;  
  9.                    message.obj = result;  
  10.                    mHandler.sendMessage(message);  
  11.                }  
  12.            }.start();  


Payment Processing Code (UI Thread)

  1. /*Alipay pay result code*/  
  2.    private static final String PAY_OK = "9000";//Successful payment  
  3.    private static final String PAY_WAIT_CONFIRM = "8000";//Transactions to be confirmed  
  4.    private static final String PAY_NET_ERR = "6002";//Network error  
  5.    private static final String PAY_CANCLE = "6001";//Cancellation of transactions  
  6.    private static final String PAY_FAILED = "4000";//Transaction failure  
  7.   
  8.    /*Internal class, processing Alipay payment results*/  
  9.    static class AliPayHandler extends Handler {  
  10.        private SoftReference<PayActivity> activitySoftReference;//Use soft references to prevent memory leaks  
  11.   
  12.        public AliPayHandler(PayActivity activity) {  
  13.            activitySoftReference = new SoftReference<PayActivity>(activity);  
  14.        }  
  15.   
  16.        @Override  
  17.        public void handleMessage(Message msg) {  
  18.            super.handleMessage(msg);  
  19.            PayActivity activity = activitySoftReference.get();  
  20.   
  21.            AliPayResult payResult = new AliPayResult((String) msg.obj);  
  22.            String resutStatus = payResult.getResultStatus();  
  23.            Log.d(TAG, "statusCode = " + resutStatus);  
  24.   
  25.            if (resutStatus.equals(PAY_OK)) {  
  26.                activity.paySuccessed();  
  27.            } else if (resutStatus.equals(PAY_CANCLE)) {  
  28.                activity.payCanceled();  
  29.            } else if (resutStatus.equals(PAY_NET_ERR)) {  
  30.                activity.payFailed(NETWORK_ERR);  
  31.            } else if (resutStatus.equals(PAY_WAIT_CONFIRM)) {  
  32.                activity.payWaitConfirm();  
  33.            } else {  
  34.                activity.payFailed(UNKNOW_ERR);  
  35.            }  
  36.        }  
  37.    }  

Note: AliPay Result. Java PayResult. from Alipay demo Java 
At this point, the integration of Alipay SDK ends. I wish you all successful integration of Alipay payment!

Concluding remarks

The author of this paper: xiong_it, Links to this article: http://blog.csdn.net/xiong_it/article/details/51819559 
Attentions for Access to app Server: The official address given to fill in rsa public key is incorrect. Whatever prompt you fill in, the public key format is incorrect. Need to move to: Alipay public key to fill in the correct address fill in

udpate 20160714 
Recent login development platform, found that the revision, in the original address to fill in rsa public key. Please know.

According to convention, the official demo download link of Alipay SDK access is attached: https://doc.open.alipay.com/doc2/detail.htm?treeId=54&articleId=104509&docType=1

Topics: Android SDK Mobile network