Previously told a blog about the integration with Alipay to get paid treasure public key with merchant private key blog. This piece is about how to integrate Alipay in a project. The first thing to run our demo. is to configure the public key and private key demo. As soon as the demo runs, the next thing is how to migrate the code.
First step:
Copy the Alipay jar package below the Libs folder in the demo to the Libs file in the project you want to introduce, and if you don't have it, you can create a new libs file in your project and bring the SDK jar package into it.
Step Two:
Configure the Androidmanifest.xml file. Most of this file is configuration information. Integrated Alipay, of course, is no exception. The following code is also copied from the demo, with the following information:
<!--Alipay SDK begin;
<activity
android:name= "com.alipay.sdk.app.H5PayActivity"
android:configchanges= "Orientation|keyboardhidden|navigation"
android:exported= "false"
android: screenorientation= "Behind"
android:windowsoftinputmode= " Adjustresize|statehidden "
</activity>
<!-- Alipay SDK end;
You can see that this is an activity. configuration file. Just like the usual registered activity, put it in the application node. The following are the configuration of permissions:
<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>
This is the necessary permission, and if you add it, you do not need to add it again. No, you need to add it to the inside.
Step Three:
Copy the Java file, and in the demo file, create a new package in the project where you need to introduce Alipay to store the Java files you need. These are the following files: Base64.java,payresult.java and Signutils.java files, I downloaded the Alipaysdk-20150610.jar jar package. You just need to introduce these files.
Fourth Step:
The following is the introduction of the code for the main activity. The main code is as follows:
public static final String PARTNER = "registered account pid";
Merchant Collection Account
public static final String SELLER = "Alipay account receivable";
Merchant private key, PKCS8 format
public static final String rsa_private = "Merchant private key";
Pay Treasure Public key
public static final String rsa_public = "pay Treasure key";
private static final int sdk_pay_flag = 1;
private static final int sdk_check_flag = 2;
Private Handler Mhandler = new Handler () {
public void Handlemessage (msg) {
Switch (msg.what) {
Case Sdk_pay_flag: {
Payresult Payresult = new Payresult ((String) msg.obj);
Alipay return this payment results and endorsement, the proposal to pay treasure signature information to take the signing of Alipay provided by the public key to do verification
String resultinfo = Payresult.getresult ();
String resultstatus = Payresult.getresultstatus ();
The Judge Resultstatus is "9000" to represent the successful payment, the specific status code represents the meaning can refer to the interface document
if (Textutils.equals (Resultstatus, "9000")) {
Toast.maketext (Paydemoactivity.this, "pay success",
Toast.length_short). Show ();
} else {
To judge that a resultstatus is not "9000" means it may fail to pay
"8000" on behalf of the payment results due to the payment channel or system reasons are still waiting for payment results confirm that the final transaction is successful with the service-side asynchronous notification (small probability state)
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 user active cancellation of payment, or system return error
Toast.maketext (Paydemoactivity.this, "Pay failure",
Toast.length_short). Show ();
}
}
Break
}
Case Sdk_check_flag: {
Toast.maketext (paydemoactivity.this, "Check result is:" + msg.obj,
Toast.length_short). Show ();
Break
}
Default
Break
}
};
};
This code, which is a handler, is placed at the top of the Java file.
* Call Alipay SDK pay. Call SDK Payment
*
*/
public void Pay () {
Order
String orderInfo = GetOrderInfo ("Tested commodity", "detailed description of the test product", "0.01");
Make an RSA signature on an order
String sign = sign (orderInfo);
try {
URL encoding is required only for sign
Sign = Urlencoder.encode (sign, "UTF-8");
catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}
Complete order information in accordance with the specification of Alipay parameters
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 payment interface, get payment result
String result = Alipay.pay (payinfo);
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 ();
}
/**
* Check whether the device has authentication Alipay account.
* Inquire whether the terminal equipment exists Alipay certified Account
*
*/
public void Check (View v) {
Runnable checkrunnable = new Runnable () {
@Override
public void Run () {
Constructing Paytask objects
Paytask paytask = new Paytask (paydemoactivity.this);
Call query interface, get query result
Boolean isexist = Paytask.checkaccountifexist ();
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 the SDK version number
*
*/
public void Getsdkversion () {
Paytask paytask = new Paytask (this);
String Version = Paytask.getversion ();
Toast.maketext (this, version, Toast.length_short). Show ();
}
/**
* Create the Order info. Create order Information
*
*/
public string GetOrderInfo (string subject, string body, string price) {
Contract Partner ID
String orderInfo = "partner=" + "\" "+ partner +" \ "";
Signed Sellers pay Treasure Account
OrderInfo + + "&seller_id=" + "" "+ Seller +" "";
Merchant Site Unique Order number
OrderInfo + + "&out_trade_no=" + "" "+ Getouttradeno () +" "";
Product Name
OrderInfo + + "&subject=" + "" "+ Subject +" "";
Product 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 encoding, fixed value
OrderInfo = "&_input_charset=\" utf-8\ "";
Set timeout for unpaid transactions
The default is 30 minutes, and once timed out, the deal is automatically closed.
Value range: 1m~15d.
M-Minute, H-hour, D-Day, 1c-day (regardless of when the transaction was created, closed at 0 points).
This parameter value does not accept decimal points, such as 1.5h, and can be converted to 90m.
OrderInfo = "&it_b_pay=\" 30m\ "";
Extern_token for the alipay_open_id obtained by the fast login authorization, the user will pay with the authorized account
OrderInfo + + "&extern_token=" + "" "+ Extern_token +" "";
Alipay after processing the request, the current page jumps to the merchant specified page path, can be empty
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 fast Payment" to use)
OrderInfo = "&paymethod=\" expressgateway\ "";
return orderInfo;
}
/**
* Get the Out_trade_no for a order. Generate merchant order number, which should remain unique on the merchant side (customizable format specification)
*
*/
Public String Getouttradeno () {
SimpleDateFormat format = new SimpleDateFormat ("Mmddhhmmss",
Locale.getdefault ());
Date date = new Date ();
String key = Format.format (date);
Random r = new Random ();
Key = key + R.nextint ();
Key = key.substring (0, 15);
Return key;
}
/**
* Sign the order info. Sign the order information
*
* @param content
* Pending signed Order information
*/
public string sign (string content) {
Return signutils.sign (content, rsa_private);
}
/**
* Get the sign type we use. Get Signature method
*
*/
Public String Getsigntype () {
Return "sign_type=\" Rsa\ "";
}
}
One way to be aware of this piece of code is to put it in a Java file with the previous piece of code. Pay () method and GetOrderInfo (); Note that the pay () method is invoked in the button where we choose to pay Alipay. In the Pay () method, you need to call GetOrderInfo () and carefully discover the three parameters in the GetOrderInfo () method. Where the price parameter is the amount we want to consume, when we call the Pay () method, we need to pass in a price parameter, which is the amount we need to pay. How the specific amount of incoming, calculated, according to the needs of the project to get their own, so that the project integration Alipay successfully ended.