I. Preparations
Download Development Kit
https://b.alipay.com/order/productDetail.htm?productId=2014110308141993&tabId=4#ps-tabinfo-hash
In the "Alipay wallet payment interface development kit" under the compression pack, there are Andoid using Alipay's JAR and Demo.
2. Create Alipay applications
Application creation in Alipay open platform
https://open.alipay.com/index.htm
The application creation of Alipay platform is limited to the company's real name authenticated users. The personal account is unable to create the application.
3. Android Manifest. XML modification (declaration of permissions, interfaces, services, etc.)
<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>
<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" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
4. Obtaining the parameter data needed for development
//Merchant PID public static final String PARTNER = ""; //Business Account public static final String SELLER = ""; //Business private key, PKCs 8 format public static final String RSA_PRIVATE = ""; //Alipay public key public static final String RSA_PUBLIC = "";
Merchant PID and merchant account receivable are all available on Alipay application.
Both the private key and the Alipay public key need to be generated by the OpenSSL program under the bin directory under the OpenSSL folder in the Alipay development kit.
The order is as follows:
RSA Key Generation Command Generating RSA Private Key openssl>genrsa -out rsa_private_key.pem 1024 Generating RSA Public Key openssl>rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem Converting RSA Private Key into PKCS8 Format openssl>pkcs8 -topk8 -nocrypt -inform PEM -in rsa_private_key.pem -outform PEM outform
Convert RSA private key into PKCS8 format:
Generating RSA Public Key
Developer's private key
1. It must be guaranteed that there is only one line of text, that is, no return, line change, space, etc.
2. Remove the words "BEGIN RSA PRIVATE KEY---" and "END RSA PRIVATE KEY---", and save only part of the two words.
_Developer's Public Key
1. It must be guaranteed that there is only one line of text, that is, no return, line change, space, etc.
2. Remove the words "BEGIN PUBLIC KEY---" and "END PUBLIC KEY---", and save only part of the two words.
3. Keep it in a temporary notebook.
Parameter settings completed
5. Loading Jar files
Copy the jar file under the alipay-sdk-common folder in the development package to the libs directory of the project and load it
6. Adding confusion rules
-libraryjars libs/alipaysdk.jar -libraryjars libs/alipaysecsdk.jar -libraryjars libs/alipayutdid.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 *;} -keep class com.alipay.mobilesecuritysdk.* -keep class com.ut.*
II. Development
1. Create order information
/** * create the order info. Create order information * */ public String getOrderInfo(String subject, String body, String price) { // Signing Partner ID String orderInfo = "partner=" + "\"" + PARTNER + "\""; // Signed seller Alipay account orderInfo += "&seller_id=" + "\"" + SELLER + "\""; // The sole order number of the merchant website orderInfo += "&out_trade_no=" + "\"" + getOutTradeNo() + "\""; // Name of commodity orderInfo += "&subject=" + "\"" + subject + "\""; // Commodity details orderInfo += "&body=" + "\"" + body + "\""; // Amount of Goods orderInfo += "&total_fee=" + "\"" + price + "\""; // Server asynchronous notification page path orderInfo += "¬ify_url=" + "\"" + "http://notify.msp.hk/notify.htm" + "\""; // Service interface name, fixed value orderInfo += "&service=\"mobile.securitypay.pay\""; // Payment type, fixed value orderInfo += "&payment_type=\"1\""; // Parameter coding, fixed value orderInfo += "&_input_charset=\"utf-8\""; // Set timeout for unpaid transactions // By default, 30 minutes, the transaction will be automatically closed once the timeout has expired. // Value range: 1m-15d. // m-minute, h-hour, d-day, 1c-day (closing at 0 o'clock whenever a transaction is created). // This parameter value does not accept decimal points, such as 1.5h, can be converted to 90m. orderInfo += "&it_b_pay=\"30m\""; // extern_token is alipay_open_id acquired by fast-login authorization. Users with this parameter will use authorized accounts to make payments. // orderInfo += "&extern_token=" + "\"" + extern_token + "\""; // After processing Alipay's request, the current page skips to the path specified by the merchant. orderInfo += "&return_url=\"m.alipay.com\""; // Call bank card payment, need to configure this parameter, participate in signature, fixed value (need to sign "Wireless Bank Card Express Payment" to use) // orderInfo += "&paymethod=\"expressGateway\""; return orderInfo; }
2. Call SDK Payment
/** * call alipay sdk pay. Call SDK Payment * */ public void pay(View v) { // Order String orderInfo = getOrderInfo("Tested goods", "Detailed description of the test product", "0.01"); // RSA Signature for Order String sign = sign(orderInfo); try { // Only the sign needs to be URL coded sign = URLEncoder.encode(sign, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // Complete order information conforming to Alipay parameter specification final String payInfo = orderInfo + "&sign=\"" + sign + "\"&" + getSignType(); Runnable payRunnable = new Runnable() { @Override public void run() { // Constructing PayTask objects PayTask alipay = new PayTask(PayDemoActivity.this); // Call the payment interface to get the payment result String result = alipay.pay(payInfo); Message msg = new Message(); msg.what = SDK_PAY_FLAG; msg.obj = result; mHandler.sendMessage(msg); } }; // Must be invoked asynchronously Thread payThread = new Thread(payRunnable); payThread.start(); }
3. Acquisition and Processing of Payment Result
private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case SDK_PAY_FLAG: { PayResult payResult = new PayResult((String) msg.obj); // Alipay returned to the results of the payment and signed a contract. It is suggested that Alipay sign information be signed by Alipay when signing the contract. String resultInfo = payResult.getResult(); String resultStatus = payResult.getResultStatus(); // Judging resultStatus as "9000" means successful payment, and the specific status code means referable interface documents. if (TextUtils.equals(resultStatus, "9000")) { Toast.makeText(PayDemoActivity.this, "Successful payment", Toast.LENGTH_SHORT).show(); } else { // Judging that resultStatus is not a "9000" means that payment may fail. // "8000" represents the result of payment, which is still waiting for confirmation because of payment channel or system reasons. The final success of the transaction depends on the asynchronous notification of the server (small probability state). if (TextUtils.equals(resultStatus, "8000")) { Toast.makeText(PayDemoActivity.this, "Confirmation of Payment Result", Toast.LENGTH_SHORT).show(); } else { // Other values can be judged as payment failure, including the user's initiative to cancel the payment, or the system's return error. Toast.makeText(PayDemoActivity.this, "Failure to pay", Toast.LENGTH_SHORT).show(); } } break; } case SDK_CHECK_FLAG: { Toast.makeText(PayDemoActivity.this, "The results were as follows:" + msg.obj, Toast.LENGTH_SHORT).show(); break; } default: break; } }; };
4. Other Functional Codes
/** * check whether the device has authentication alipay account. * Is there a Alipay certified account for terminal devices? * */ public void check(View v) { Runnable checkRunnable = new Runnable() { @Override public void run() { // Constructing PayTask objects PayTask payTask = new PayTask(PayDemoActivity.this); // Invoke the query interface to get the query results boolean isExist = payTask.checkAccountIfExist(); Message msg = new Message(); msg.what = SDK_CHECK_FLAG; msg.obj = isExist; mHandler.sendMessage(msg); } }; Thread checkThread = new Thread(checkRunnable); checkThread.start(); }
/** * get the sdk version. Get SDK version number * */ public void getSDKVersion() { PayTask payTask = new PayTask(this); String version = payTask.getVersion(); Toast.makeText(this, version, Toast.LENGTH_SHORT).show(); }
Specifically refer to Alipay Demo and access and use rules.