Alipay, China's leading third-party online payment solutionAlipay, China's leading third-party online payment solution

SDK Integration Process

Import Development Resource

  • Migrate from JAR SDK to AAR SDK (from v15.5.5 to v15.6.8):

If you have already imported the .jar package, complete following steps: 
1、Remove the dependency on the Alipay SDK JAR package in the build.gradle file of the App Module (not the entire project).

copy
dependencies {
//annotationProcessor 'com.android.databinding:compiler:3.2.0'
// 支付宝SDK AAR 包所需的配置
implementation(name: 'alipaySdk-15.7.4-20200228192259', ext: 'aar')
//implementation(name: 'alipaySdk-15.6.8-20191021122514', ext: 'aar')
implementation'com.android.support:support-v4:25.3.1'
implementation'com.android.support:appcompat-v7:25.3.1'

}

2、Delete the old alipaySdk-xxx.jar file from the libs directory. 

3、Remove the registration for the following activities from AndroidManifest.xml (no need to manually register if using AAR):

    copy
    <!-- Remove the following content -->  <activity            android:name="com.alipay.sdk.app.H5PayActivity"            android:configChanges="orientation|keyboardHidden|navigation|screenSize"            android:exported="false"            android:screenOrientation="behind"            android:windowSoftInputMode="adjustResize|stateHidden" >  </activity>  <activity            android:name="com.alipay.sdk.app.H5AuthActivity"            android:configChanges="orientation|keyboardHidden|navigation"            android:exported="false"            android:screenOrientation="behind"            android:windowSoftInputMode="adjustResize|stateHidden" >  </activity>  <!-- Remove the above content -->
  • For AAR SDK:

You can view Demo for reference.
1、Place the AAR package in the libs directory of your application project. The file name in the image is only an example, please replace it with your actual SDK file name. 

SDK Integration Process
2、In the build.gradle of your main project, add the following content to make the libs directory a dependent repository: 
SDK Integration Process

copy
allprojects {
     repositories {
         // 支付宝 SDK AAR 包所需的配置
         flatDir {             dirs 'libs'         }         jcenter()
         maven { url "https://maven.google.com"}
     }
 }

3、In the build.gradle of your App Module, add the following to the Alipay SDK as a project dependency:

SDK Integration Process

    copy
    dependencies {
    //annotationProcessor 'com.android.databinding:compiler:3.2.0'
    // 支付宝SDK AAR 包所需的配置
    implementation(name: 'alipaySdk-15.7.4-20200228192259', ext: 'aar')
    //implementation(name: 'alipaySdk-15.6.8-20191021122514', ext: 'aar')
    implementation'com.android.support:support-v4:25.3.1'
    implementation'com.android.support:appcompat-v7:25.3.1'
    
    }

Now, the SDK import is completed.

Modify Manifest

Add the following declaration into file AndroidManifest.xml in the project:

copy
<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>

And privilege declaration:

copy
<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" />

Add Confusing Rules

Add relevant rules below into proguard-project.txt of seller's project:

copy
-libraryjars libs/alipaySDK-(Date).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 *;}

This step completes importing of SDK development resource.

Construct Order Data

Order information is required when calling SDK to pay. All parameters is displayed in the format of key=”value”, connected with &.

Call Payment API

Payment API needs to be called in new thread. (Reference alipay_demo)

It is required to call the payment API in a new thread. (Reference alipay_demo)

Get PayTask payment object to pay (pay or authorize activity needs to be executed in non-ui thread), code demo:

copy
final String orderInfo = info;   // order information                                   Runnable payRunnable = new Runnable() {                                               @Override                                              public void run() {                                                          PayTask alipay = new PayTask(DemoActivity.this);                                                          String result = alipay.pay(orderInfo,true);                                                           Message msg = new Message();                                                          msg.what = SDK_PAY_FLAG;                                                          msg.obj = result;                                                          mHandler.sendMessage(msg);                                              }                                  };                                 // must call asynchronously                                Thread payThread = new Thread(payRunnable);                                payThread.start();

Payment Result Handling

After payment, there are 2 ways to get the result:

Synchronize Responses

Merchant app client calls the payment Activity’s handler object and get the payment result using the callback function.(Reference alipay_demo)
Demo code:

copy
private Handler mHandler = new Handler() {                              public void handleMessage(Message msg) {                                          Result result = new Result((String) msg.obj);                                          Toast.makeText(DemoActivity.this, result.getResult(),                                                                           Toast.LENGTH_LONG).show();                              };                    };

Async Notification

Merchant needs to provide an http protocol interface, put in parameter notify_url and pass to the payment request. After payment is completed, Alipay server will call notify_url using POST method, the data is transferred in xml format.

Get the SDK Version

Call object PayTask’s getVersion() method. Demo code:

copy
PayTask payTask = new PayTask(activity);String version = payTask.getVersion();