Android 微信支付的統一下單
阿新 • • 發佈:2018-11-05
準備工作
開發
①下載sdk:
②可以匯入包
在build.gradle檔案中,新增如下依賴即可:
dependencies {
compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
}
或
dependencies {
compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}
③新增Android Manifest許可權
<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"/>
呼叫統一下單介面
務必提交必須的欄位:appid,body,mch_id,nonce_str,notify_url, out_trade_no,spbill_create_ip,total_fee,trade_type,sign(都是小寫);提交到微信介面時以xml格式提交
2.sign為前面提交的引數按照引數名ASCII碼從小到大排序簽名拼接起來然後進行MD5運算,再將得到的字串所有字元轉換為大寫得到的,如簽名生成演算法
3.參與生成sign的key為商戶賬號的金鑰,key設定路徑如下:微信商戶平臺(pay.weixin.qq.com)–>賬戶設定–>API安全–>金鑰設定
下面是具體程式碼(如若檢視你的sign生成及提交的xml是否正確可以點選如下:簽名生成工具)
//拼接欄位,順序不能變
String A = "appid=你的appID" +
"&body=jinshi" +
"&mch_id=你的商戶號" +
"&nonce_str=" + nonce_str +
"¬ify_url=http://www.szgsip.com/" +
"&out_trade_no=" + trade_no +
"&spbill_create_ip=192.168.1.1" +
"&total_fee=1" +
"&trade_type=APP";
String key = "你的金鑰";
String temp = A + "&key=" + key;
// 生成sign
String sign = MD5.getMessageDigest(temp.getBytes()).toUpperCase();
接下來提交到微信下單的介面上
private void httpThreadxml() {
//組建xml資料
//拼接欄位,順序不能變
xml.append("<xml>\n");
xml.append("<appid>你的appID</appid>\n");
xml.append("<body>jinshi</body>\n");
xml.append("<mch_id>你的商戶號</mch_id>\n");
xml.append("<nonce_str>" + nonce_str + "</nonce_str>\n");
xml.append("<notify_url>http://www.szgsip.com/</notify_url>\n");
xml.append("<out_trade_no>" + trade_no + "</out_trade_no>\n");
xml.append("<spbill_create_ip>192.168.1.1</spbill_create_ip>\n");
xml.append("<total_fee>1</total_fee>\n");
xml.append("<trade_type>APP</trade_type>\n");
xml.append("<sign>" + sign + "</sign>\n");
xml.append("</xml>");
try {
final byte[] xmlbyte = xml.toString().getBytes("UTF-8");
System.out.println(xml);
URL url = new URL("https://api.mch.weixin.qq.com/pay/unifiedorder");
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoOutput(true);// 允許輸出
conn.setDoInput(true);
conn.setUseCaches(false);// 不使用快取
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");// 維持長連線
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Length",
String.valueOf(xmlbyte.length));
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setRequestProperty("X-ClientType", "2");//傳送自定義的頭資訊
conn.getOutputStream().write(xmlbyte);
conn.getOutputStream().flush();
conn.getOutputStream().close();
if (conn.getResponseCode() != 200)
throw new RuntimeException("請求url失敗");
InputStream is = conn.getInputStream();// 獲取返回資料
// 使用輸出流來輸出字元(可選)
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) != -1) {
out.write(buf, 0, len);
}
String string = out.toString("UTF-8");
System.out.println(string);
Log.e(" 微信返回資料 ", " --- " + string);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
注意在呼叫上面的方法,一定要在子執行緒中進行
new Thread(new Runnable() {
@Override
public void run() {
httpThreadxml();
}
}).start();