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

SDK integration process

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

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. 

SDK integration process

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

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.pay.demo.PayDemoActivity"
    android:icon="@drawable/msp_icon"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name="com.alipay.sdk.pay.demo.H5PayDemoActivity"
    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" />

Add confusing rules

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

copy
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends java.lang.Throwable {*;}
-keep public class * extends java.lang.Exception {*;}

-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 *;}
-keep class com.alipay.sdk.app.H5PayCallback {
    <fields>;
    <methods>;
}

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 = OrderUtils1_0_Global.getSignedOrderInfo("test_subject", "test_body", "0.01");//order information
   final Runnable payRunnable = new Runnable() {

      @Override
      public void run() {
         PayTask alipay = new PayTask(PayDemoActivity.this);
         Map<String, String> result = alipay.payV2(orderInfo, true);
         Log.i("msp", result.toString());

         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() {
   @SuppressWarnings("unused")
   public void handleMessage(Message msg) {
      switch (msg.what) {
      case SDK_PAY_FLAG: {
         @SuppressWarnings("unchecked")
         PayResult payResult = new PayResult((Map<String, String>) msg.obj);
         String resultInfo = payResult.getResult();
         String resultStatus = payResult.getResultStatus();
         if (TextUtils.equals(resultStatus, "9000")) {
            showAlert(PayDemoActivity.this, getString(R.string.pay_success) + payResult);
         } else {
            showAlert(PayDemoActivity.this, getString(R.string.pay_failed) + payResult);
         }
         break;
      }
      case SDK_AUTH_FLAG: {
         @SuppressWarnings("unchecked")
         AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
         String resultStatus = authResult.getResultStatus();

         if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.getResultCode(), "200")) {
            showAlert(PayDemoActivity.this, getString(R.string.auth_success) + authResult);
         } else {
            showAlert(PayDemoActivity.this, getString(R.string.auth_failed) + authResult);
         }
         break;
      }
      default:
         break;
      }
   };
};

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();