SoFunction
Updated on 2025-04-10

Android implements unified WeChat payment order

This article shares the specific code for Android's WeChat payment unified order for reference. The specific content is as follows

Preparation

Apply for a WeChat developer account, add applications and apply for activation of WeChat payment functions, such as
Check the activation process

Unify the single interface document:
View the interface

Development

①Download sdk:

sdk and demo download

② You can import packages

In the file, add the following dependencies:

dependencies {
 compile ':wechat-sdk-android-with-mta:+'
}

or

dependencies {
 compile ':wechat-sdk-android-without-mta:+'
}

③Add Android Manifest permissions

<uses-permission android:name=""/> 
<uses-permission android:name=".ACCESS_NETWORK_STATE"/> 
<uses-permission android:name=".ACCESS_WIFI_STATE"/> 
<uses-permission android:name=".READ_PHONE_STATE"/> 
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE"/>

Call unified single interface

1. Be sure to submit the required fields: appid, body, mch_id, nonce_str, notify_url, out_trade_no, spbill_create_ip, total_fee, trade_type, sign (all lowercase); submit in XML format when submitting to the WeChat interface

The previously submitted parameters are sorted from small to large according to the parameter name ASCII code and then spliced ​​together the signatures from small to large, and then performed MD5 operations, and then converted all characters of the obtained string into capitalized ones, such as the signature generation algorithm

3. The key participating in the generation of the sign is the key of the merchant account. The key setting path is as follows: WeChat Merchant Platform ()->Account Settings->API Security->Key Settings

The following is the specific code (if you check whether the XML generated and submitted by your sign is correct, you can click as follows:Signature generation tool

//Seaming fields, the order cannot be changed    String A = "appid=yourappID" +
      "&amp;body=jinshi" +
      "&amp;mch_id=your商户号" +
      "&amp;nonce_str=" + nonce_str +
      "&amp;notify_url=/" +
      "&amp;out_trade_no=" + trade_no +
      "&amp;spbill_create_ip=192.168.1.1" +
      "&amp;total_fee=1" +
      "&amp;trade_type=APP";
    String key = "Your key";
    String temp = A + "&amp;key=" + key;
 // Generate sign     String sign = (()).toUpperCase();

Next submit it to the WeChat order interface

 private void httpThreadxml() {


  //Create xml data  //Seaming fields, the order cannot be changed
  ("&lt;xml&gt;\n");
    ("<appid>your appID</appid>\n");
    ("&lt;body&gt;jinshi&lt;/body&gt;\n");
    ("<mch_id>your merchant number</mch_id>\n");
    ("&lt;nonce_str&gt;" + nonce_str + "&lt;/nonce_str&gt;\n");
    ("&lt;notify_url&gt;/&lt;/notify_url&gt;\n");
    ("&lt;out_trade_no&gt;" + trade_no + "&lt;/out_trade_no&gt;\n");
    ("&lt;spbill_create_ip&gt;192.168.1.1&lt;/spbill_create_ip&gt;\n");
    ("&lt;total_fee&gt;1&lt;/total_fee&gt;\n");
    ("&lt;trade_type&gt;APP&lt;/trade_type&gt;\n");
    ("&lt;sign&gt;" + sign + "&lt;/sign&gt;\n");
    ("&lt;/xml&gt;");

  try {
   final byte[] xmlbyte = ().getBytes("UTF-8");

   (xml);

   URL url = new URL("/pay/unifiedorder");


   final HttpURLConnection conn = (HttpURLConnection) ();
   (5000);
   (true);// Allow output   (true);
   (false);// No cache   ("POST");
   ("Connection", "Keep-Alive");// Maintain long connection   ("Charset", "UTF-8");
   ("Content-Length",
     ());
   ("Content-Type", "text/xml; charset=UTF-8");
   ("X-ClientType", "2");//Send custom header information

   ().write(xmlbyte);
   ().flush();
   ().close();


   if (() != 200)
    throw new RuntimeException("Request url failed");

   InputStream is = ();// Get the return data

   // Use the output stream to output characters (optional)   ByteArrayOutputStream out = new ByteArrayOutputStream();
   byte[] buf = new byte[1024];
   int len;
   while ((len = (buf)) != -1) {
    (buf, 0, len);
   }
   String string = ("UTF-8");
   (string);

   ("WeChat Returns Data", " --- " + string);
   ();


  } catch (Exception e) {

   (e);
  }
}

Note that when calling the above method, it must be performed in the child thread.

new Thread(new Runnable() {
    @Override
    public void run() {
     httpThreadxml();
   }
 }).start();

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.