Android App Payment Series (ii): Alipay SDK Access detailed guide (with official payment demo) _android

Source: Internet
Author: User
Tags static class stub

A mobile internet company, in the final analysis, to make a profit always need to pay users, the development of their own payment system for the limited resources of the company is clearly unwise, there are many mature mobile payment providers, Ali is one of them.

The author summarizes the next Ali's payment treasure Android SDK payment access process, for later reference.

The access process is as follows:

1 Sign up to become Alipay merchant

Signed Address: https://b.alipay.com/,

Only the developer who is a contracted merchant can have the eligibility to pay for the integrated Alipay app.

Contract Information: 1 Business License 2) app description document 3 merchant business information, merchant contact information

If necessary, you will also need to provide app apk for review. After approval, the code can be integrated.

After the audit through, you can get Alipay distribution of the merchant number and other information.

2 Client Code Integration Preparation

2.1 Importing JAR Package Resources

Currently the latest new payment treasure Development jar package Download Address: Http://xiazai.jb51.net/201611/yuanma/alipaySdk_jb51.jar

After downloading the Libs directory, Eclipse automatically adds dependencies, and Android studio needs to add a line to the app's Gradle

Compile files (' Libs/alipaysdk-20160223.jar ')

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

2.2 Modify Androidmanifest.xml List

Declaring necessary activity

<activity
      android:name= "com.alipay.sdk.app.H5PayActivity"
      android:configchanges= "orientation| Keyboardhidden|navigation "
      android:exported=" false "
      android:screenorientation=" behind ">
</ activity>
<activity
      android:name= "com.alipay.sdk.auth.AuthActivity"
      android:configchanges= " Orientation|keyboardhidden|navigation "
      android:exported=" false "
      android:screenorientation=" behind " >
 </activity>

Add necessary permissions

 <uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE "/>
<uses-permission android:name=" android.permission.ACCESS_ Wifi_state "/>
<uses-permission android:name= android.permission.READ_PHONE_STATE"/>
< Uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

2.3 Add Alipay obfuscation rules

-libraryjars Libs/alipaysdk-20160223.jar
-keep class com.alipay.android.app.ialixpay{*;}
-keep class com.alipay.android.app.ialixpay$stub{*;}
-keep class com.alipay.android.app.iremoteservicecallback{*;}
-keep class com.alipay.android.app.iremoteservicecallback$stub{*;}
-keep class com.alipay.sdk.app.paytask{public *;}
-keep class com.alipay.sdk.app.authtask{public *;}

3 Client Code Integration

Payment Treasure Interactive Process diagram

Interpretation of the vernacular version of the flowchart

1.app Carry payment information call Payment interface request Alipay Client to adjust the payment interface;

2. User operation, input password payment, payment success, direct return cancellation payment, error, payment failure, enter the payment interface, but enter the password to pay, pay to confirm;

3. Alipay client will pay the results to inform the app client, merchant Server Notify app server payment results;

4.app client processing payment results;

The 5.app server handles payment results.

Alipay's payment process is a step less than the micro-letter payment flowchart. App server-side order generation, but in its demo code is recommended to use the app server for the sign signature process, so the author simply according to the micro-letter payment process (micro-credit payment interaction process) to integrate.

• Client code gets the product information purchased by the user and passes it to his or her company's app server, which includes but is not limited to the following:

Params.put ("Money", Paymoney);//Commodity amount, Unit: Yuan
params.put ("Goodsname", goodsname);//Product Name

Note: Alipay paid RMB unit and micro-letter payment, UnionPay pay slightly different, need to: Yuan as a payment unit. The other two payment units are: cent
Other parameters are given to our app server processing, detailed parameters please click: https://doc.open.alipay.com/doc2/detail?treeId=59&articleId=103663&docType =1

App server refer to the above detailed parameter link, get sign (Payment signature information) field and return sign field to handset client;

• The mobile client uses sign signature information to pay the client in a non-UI thread;
User action: Enter the password to pay, return the key cancellation payment, enter the payment interface, the user did not pay, the user returned, to pay, network connectionless payment failure, etc.

• The client receives the payment result;

• Alipay Server notifies us of company app Server payment results (server work, client Independent)

The benefit of this: signature logic is done on the server, and the app is more secure without exposing the public key and private key. Also is the recommended practice in demo.

More detailed payment Treasure Access Interactive process explanation 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 adjusted code (required in child thread)

New Thread () {
          @Override public
          void Run () {
            super.run ();
            Paytask paytask = new Paytask (mactivity);
            String result = Paytask.pay (Signinfo, true);
            Message message = Mhandler.obtainmessage ();
            Message.what = Pay_result;
            Message.obj = result;
            Mhandler.sendmessage (message);
          }
        . Start ();

Payment processing code (UI thread)
 

 /* Alipay Payment Result code */private static final String PAY_OK = "9000";/Pay Success private static final String pay_wait_confirm = "800 0 "//transaction to be confirmed private static final string pay_net_err =" 6002 ";/Network error private static final string pay_cancle =" 6001 ";  /transaction Cancellation private static final String pay_failed = "4000";//Transaction Failure/* Internal class, processing Alipay payment result/static class Alipayhandler extends Handler {private softreference<payactivity> activitysoftreference;//use soft references to prevent memory leaks public Alipayhandler (Pay
    Activity activity) {activitysoftreference = new softreference<payactivity> (activity);
      @Override public void Handlemessage (msg) {super.handlemessage);

      Payactivity activity = Activitysoftreference.get ();
      Alipayresult Payresult = new Alipayresult ((String) msg.obj);
      String resutstatus = Payresult.getresultstatus ();

      LOG.D (TAG, "StatusCode =" + Resutstatus);
      if (Resutstatus.equals (PAY_OK)) {activity.paysuccessed (); } ElSe if (resutstatus.equals (pay_cancle)) {activity.paycanceled ();
      else if (resutstatus.equals (Pay_net_err)) {activity.payfailed (network_err);
      else if (resutstatus.equals (pay_wait_confirm)) {activity.paywaitconfirm ();
      else {activity.payfailed (unknow_err);

 }
    }
  }

Note: Alipayresult.java from the Payresult.java in the Alipay demo

At this point, the integrated Alipay SDK is complete. I wish you the integration of Alipay pay success!

Conclusion

App Server Access Note: The official address to fill out the RSA public key is incorrect, regardless of what is filled out prompts: Public key format error. Need to move to: Alipay public key fill out the correct address

By convention, attach the official demo download link of Alipay SDK access:https://doc.open.alipay.com/doc2/detail.htm?treeid=54&articleid=104509& Doctype=1

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.