SDK integration process

Last updated: 2019-10-30

Import development resource

For JAR SDK v15.5.5:

1. Import AlipaySDK-(Date).jar into libs folder in the merchant project, as shown in the figure below.
2. Go to the project's Java Build Path and import alipaySDK-(Date).jar under libs.
3. Select Order and Export, and check alipaySDK-(Date).jar

  • 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).

1dependencies {     
2                ... ...     // Delete the following dependency     
3                compile files('libs/alipaySdk-20170725.jar')     
4                ... ...  
5            }


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):

 1<!-- Remove the following content -->  
 2<activity     
 3       android:name="com.alipay.sdk.app.H5PayActivity"     
 4       android:configChanges="orientation|keyboardHidden|navigation|screenSize"     
 5       android:exported="false"     
 6       android:screenOrientation="behind"     
 7       android:windowSoftInputMode="adjustResize|stateHidden" >  
 8</activity>  
 9<activity     
10       android:name="com.alipay.sdk.app.H5AuthActivity"     
11       android:configChanges="orientation|keyboardHidden|navigation"     
12       android:exported="false"     
13       android:screenOrientation="behind"     
14       android:windowSoftInputMode="adjustResize|stateHidden" >  
15</activity>  
16<!-- Remove the above content -->

For AAR SDK v15.6.8: You can view Demo for reference.
1、Place the alipaySdk-15.6.8-20191021122514.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. 


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


1allprojects {     
2               repositories {          
3                   // add the following content         
4                   flatDir {             
5                       dirs 'libs'         
6                   }          
7                   // ... jcenter() and other repos     
8                } 
9           }


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


1dependencies { 
2     
3                // Add the following content     
4                compile (name: 'alipaySdk-15.6.8-20191021122514', ext: 'aar') 
5                // ... other dependencies 
6}


Now, the SDK import is completed.

Modify manifest

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

 1<activity
 2    android:name="com.alipay.sdk.pay.demo.PayDemoActivity"
 3    android:icon="@drawable/msp_icon"
 4    android:label="@string/app_name" >
 5    <intent-filter>
 6        <action android:name="android.intent.action.MAIN" />
 7
 8        <category android:name="android.intent.category.LAUNCHER" />
 9    </intent-filter>
10</activity>
11<activity
12    android:name="com.alipay.sdk.pay.demo.H5PayDemoActivity"
13    android:configChanges="orientation|keyboardHidden|navigation"
14    android:exported="false"
15    android:screenOrientation="behind" >
16</activity>

And privilege declaration:

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

Add confusing rules

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

 1-keep public class * extends android.app.Activity
 2-keep public class * extends android.app.Application
 3-keep public class * extends android.app.Service
 4-keep public class * extends android.content.BroadcastReceiver
 5-keep public class * extends android.content.ContentProvider
 6-keep public class * extends java.lang.Throwable {*;}
 7-keep public class * extends java.lang.Exception {*;}
 8
 9-keep class com.alipay.android.app.IAlixPay{*;}
10-keep class com.alipay.android.app.IAlixPay$Stub{*;}
11-keep class com.alipay.android.app.IRemoteServiceCallback{*;}
12-keep class com.alipay.android.app.IRemoteServiceCallback$Stub{*;}
13-keep class com.alipay.sdk.app.PayTask{ public *;}
14-keep class com.alipay.sdk.app.AuthTask{ public *;}
15-keep class com.alipay.sdk.app.H5PayCallback {
16    <fields>;
17    <methods>;
18}

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:

 1    final String orderInfo = OrderUtils1_0_Global.getSignedOrderInfo("test_subject", "test_body", "0.01");//order information
 2   final Runnable payRunnable = new Runnable() {
 3
 4      @Override
 5      public void run() {
 6         PayTask alipay = new PayTask(PayDemoActivity.this);
 7         Map<String, String> result = alipay.payV2(orderInfo, true);
 8         Log.i("msp", result.toString());
 9
10         Message msg = new Message();
11         msg.what = SDK_PAY_FLAG;
12         msg.obj = result;
13         mHandler.sendMessage(msg);
14      }
15   };
16
17   // must call asynchronously 
18   Thread payThread = new Thread(payRunnable);
19   payThread.start();
20}

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:


 1private Handler mHandler = new Handler() {
 2   @SuppressWarnings("unused")
 3   public void handleMessage(Message msg) {
 4      switch (msg.what) {
 5      case SDK_PAY_FLAG: {
 6         @SuppressWarnings("unchecked")
 7         PayResult payResult = new PayResult((Map<String, String>) msg.obj);
 8         String resultInfo = payResult.getResult();
 9         String resultStatus = payResult.getResultStatus();
10         if (TextUtils.equals(resultStatus, "9000")) {
11            showAlert(PayDemoActivity.this, getString(R.string.pay_success) + payResult);
12         } else {
13            showAlert(PayDemoActivity.this, getString(R.string.pay_failed) + payResult);
14         }
15         break;
16      }
17      case SDK_AUTH_FLAG: {
18         @SuppressWarnings("unchecked")
19         AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
20         String resultStatus = authResult.getResultStatus();
21
22         if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.getResultCode(), "200")) {
23            showAlert(PayDemoActivity.this, getString(R.string.auth_success) + authResult);
24         } else {
25            showAlert(PayDemoActivity.this, getString(R.string.auth_failed) + authResult);
26         }
27         break;
28      }
29      default:
30         break;
31      }
32   };
33};


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:

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


If you have any question, please contact us