Because the company's project has been exposed to integrated Alipay, so I will briefly explain how android integrates Alipay. This is not as difficult as everyone imagines, and it can even be said that it is not simple.
Why? You will know at the end
Step 1: Run the demo
First get Alipay's test account. I think everyone already has Alipay's test account when you read this article. It's also just public key, private key, etc. If you don't have it, move on.
https://open.alipay.com/platform/home.htm
download sdk
https://doc.open.alipay.com/doc2/detail.htm?treeId=54&articleId=104509&docType=1
Alright now the preparations are done.
Run the demo and fill in the generated public key and private key directly,
You can run this without any problems
Part 2: Integration Project
Now, let's take a look at Alipay's skd demo
Well, just these files, among which
Base64.java
PayResult.java
SignUtils.java
directly into the project
we just need to
PayDemoAvtivity.java can be modified
As for the other two individuals who think that direct Delete is not needed.
in AndroidManifest.xml
Add the corresponding 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" />
What should I do if the Alipay APP is not installed on my phone?
Alipay sdk has been processed and will jump to H5 Alipay payment by itself, we also need to add corresponding permissions
<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>
According to com.alipay.sdk.app.H5PayActivity
The path shows that this is the activity inside the SDK. It must be added otherwise, if the Alipay APP is not installed on the mobile phone, the program will hang up.
in PayDemoActivity.java
It can be seen that the main method is pay
core is
Runnable payRunnable = new Runnable() { @Override public void run() { // Construct the PayTask object PayTask alipay = new PayTask(PayDemoActivity.this); // Call the payment interface to get the payment result String result = alipay.pay(payInfo, true); Message msg = new Message(); msg.what = SDK_PAY_FLAG; msg.obj = result; mHandler.sendMessage(msg); } }; // must be called asynchronously Thread payThread = new Thread(payRunnable); payThread.start();
The root cause is sendMessage -- payInfo to Alipay,
Then payInfo is particularly important.
What's in payInfo? It's the order information
Print it out and see.
partner="2088101568353491"& seller_id="[email protected]"& out_trade_no="051111312119552"& subject="Item tested"& body="Detailed description of the test item"& total_fee="0.01"& notify_url="http://notify.msp.hk/notify.htm"& service="mobile.securitypay.pay"& payment_type="1"& _input_charset="utf-8"& it_b_pay="30m"& return_url="m.alipay.com"& sign="mX93Jy8zE92ZupO827CWCPGzK%2F1zB4x6TgTPzM%2FoCiuh04n1cdW0mxEC2yF7pPgt01lv81BTYweViJl7VM1SfJytQJXMOzBDARb9Zw2WKrq3h3neVXi8C1%2Bey2GJJvUWSMfiTv71Gd4APPPRRBD7XtwssqJahBoat%2FOC2H6eWCw%3D"& sign_type="RSA"
It is the information of some orders, the configuration plus a signature generated by the private key
What we really need is this payInfo, we generate a payInfo and send it to Alipay, that's ok.
At the beginning of the article, I said that integrating Alipay cannot be simple, why? Let me tell you below,
Because of the security of access, Alipay does not recommend exposing the public key and private key to the client code. It is best to generate the send client on the server side.
That is to say, these logics do not need to be done by android at all. Isn't it very easy.
So my personal suggestion is to use the above code to send Alipay as far as possible after payInfo is obtained from the server.
There is another important method in PayDemoActivity.java
@SuppressLint("HandlerLeak") private Handler mHandler = new Handler() { @SuppressWarnings("unused") public void handleMessage(Message msg) { switch (msg.what) { case SDK_PAY_FLAG: { PayResult payResult = new PayResult((String) msg.obj); /** * The results returned synchronously must be placed on the server for verification (see https://doc.open.alipay.com/doc2/ for verification rules * detail.htm?spm=0.0.0.0.xdvAU6&treeId=59&articleId=103665& * docType=1) It is recommended that merchants rely on asynchronous notifications */ String resultInfo = payResult.getResult();// Synchronously return the information that needs to be verified String resultStatus = payResult.getResultStatus(); // Judging that the resultStatus is "9000" means the payment is successful. For the meaning of the specific status code, please refer to the interface documentation if (TextUtils.equals(resultStatus, "9000")) { Toast.makeText(PayDemoActivity.this, "Successful payment", Toast.LENGTH_SHORT).show(); } else { // Judging that resultStatus is not "9000" means that the payment may fail // "8000" means that the payment result is still waiting for payment result confirmation due to payment channel or system reasons. Whether the final transaction is successful or not is subject to the asynchronous notification from the server (small probability status) if (TextUtils.equals(resultStatus, "8000")) { Toast.makeText(PayDemoActivity.this, "Payment result confirmation", Toast.LENGTH_SHORT).show(); } else { // Other values can be judged as payment failure, including the user's initiative to cancel the payment, or the error returned by the system Toast.makeText(PayDemoActivity.this, "Payment failed", Toast.LENGTH_SHORT).show(); } } break; } default: break; } }; };
The logic of verification, because our logic is basically on the server side, the probability of the order being tampered with is small, and the verification may not be written relatively (personally think, please point out if there is any error).
The current method provides a method after the payment is completed. Just write your own logic.
So we keep the above two methods in PayDemoActivity.java and others can delete.
If you have any questions, please leave a message. If you have any questions, please leave a message.