1.前期註冊操做
1-1:微信開發平臺:https://open.weixin.qq.com/
1-2:建立移動應用
簽名獲取:
1.將本身的apk簽名打包,運行到手機上。
2.將微信支付的簽名工具,運行到手機上。
3.經過簽名工具,獲取簽名。java
2.android端初始化
2-1:簽名配置node
signingConfigs { debug{ storeFile file("f:/style.keystore") storePassword "13873129725" keyAlias "style" keyPassword "13873129725" } release { storeFile file("f:/style.keystore") storePassword "13873129725" keyAlias "style" keyPassword "13873129725" } }
2-2:權限android
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2-3:支付按鈕json
<Button android:id="@+id/appay_btn" android:background="@drawable/btn_style_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#fff" android:textSize="20sp" android:text="開始支付" />
按鈕點擊樣式:api
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/btn_style_one_pressed" /> <item android:drawable="@drawable/btn_style_one_normal" /> </selector>
2-4:導入jar包
2-5:調用微信支付的代碼服務器
Button appayBtn; private IWXAPI api; api = WXAPIFactory.createWXAPI(this, Constant.OPEN_ID); appayBtn = (Button) findViewById(R.id.appay_btn); appayBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // String url = "http://139.196.196.64:8080/PayServer/mypay.jsp"; String name = null; try { name = URLEncoder.encode("你好","utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String url = "http://172.16.46.114:8080/Pay/pay.jsp?goodsName="+name+"&price=11"; Toast.makeText(MainActivity.this, "獲取訂單中...", Toast.LENGTH_SHORT).show(); HttpUtil.get(url, new HttpUtil.CallBack() { @Override public void onSuccess(String content) { try { Log.e("get server pay params:", content); if (content != null) { JSONObject json = new JSONObject(content); if (null != json && !json.has("retcode")) { PayReq req = new PayReq(); req.appId = json.getString("appid"); req.partnerId = json.getString("partnerid"); req.prepayId = json.getString("prepayid"); req.nonceStr = json.getString("noncestr"); req.timeStamp = json.getString("timestamp"); req.packageValue = json.getString("package"); req.sign = json.getString("sign"); req.extData = "app data"; // optional Toast.makeText(MainActivity.this, "正常調起支付", Toast.LENGTH_SHORT).show(); api.sendReq(req); } else { Log.d("PAY_GET", "返回錯誤" + json.getString("retmsg")); Toast.makeText(MainActivity.this, "返回錯誤" + json.getString("retmsg"), Toast.LENGTH_SHORT).show(); } } else { Log.d("PAY_GET", "服務器請求錯誤"); Toast.makeText(MainActivity.this, "服務器請求錯誤", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { Log.e("PAY_GET", "異常:" + e.getMessage()); Toast.makeText(MainActivity.this, "異常:" + e.getMessage(), Toast.LENGTH_SHORT).show(); } appayBtn.setEnabled(true); } @Override public void onError(Throwable e) { Toast.makeText(MainActivity.this, "網絡鏈接失敗", Toast.LENGTH_SHORT).show(); appayBtn.setEnabled(true); } }); } });
3.微信支付服務器搭建
3-1:jsp頁面微信
<% //獲得輸入的商品名稱和價格 String goodsName = request.getParameter("goodsName"); String price = request.getParameter("price"); if (goodsName == null || goodsName.trim().equals("")) { out.print("商品名不能爲空"); } if (price == null || price.trim().equals("")) { out.print("商品價格不能爲空"); } int goodsPrice = Integer.parseInt(price); //把相關參數傳給微信支付 TreeMap<String, Object> map = new TreeMap<String, Object>(); map.put("appid", Constant.OPEN_ID); map.put("mch_id", Constant.SW_ID); map.put("nonce_str", Utils.getRandomStr()); map.put("body", goodsName); map.put("out_trade_no", Utils.getRandomStr()); map.put("total_fee", goodsPrice); map.put("spbill_create_ip", request.getLocalAddr()); map.put("notify_url", "http://139.196.196.64:8080/PayServer/mypay.jsp"); map.put("trade_type", "APP"); map.put("sign", Utils.getSignStr(map)); //把map轉換成xml,併發送到微信支付接口 String info = Utils.map2xml(map); String i = getHttpData.sendMsg(Constant.ORDER, info); //接收它的返回信息 Map<String, Object> returnMap = Utils.xml2Map(i); //返回的鍵要相對應,因此要改過來 TreeMap<String, Object> resultmap = new TreeMap<String, Object>(); resultmap.put("appid", Constant.OPEN_ID); resultmap.put("partnerid", Constant.SW_ID); resultmap.put("prepayid",returnMap.get("prepay_id")); resultmap.put("noncestr",returnMap.get("nonce_str")); resultmap.put("timestamp",Utils.getDataStr("yyyyMMddHH")); resultmap.put("package","WXPay"); resultmap.put("sign",Utils.getSignStr(resultmap)); String inf = JSON.toJSONString(resultmap); out.print(inf); %>
3-2:經常使用方法 Utils.java網絡
package com.firefly.common; import java.io.ByteArrayInputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Random; import java.util.Set; import java.util.TreeMap; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Utils { // 獲得32位隨機數 public static String getRandomStr() { StringBuffer sb = new StringBuffer(); Random r = new Random(); String str = "QWERTYUIOPASDFGHJKLZXCVBNM0123456789"; for (int i = 0; i < 32; i++) { sb.append(str.charAt(r.nextInt(str.length()))); } return sb.toString(); } // 獲得sign public static String getSignStr(TreeMap<String, Object> map) { String info = ""; Set<String> keySet = map.keySet(); for (String string : keySet) { info += string + "=" + map.get(string) + "&"; } info += "key=" + Constant.API_KEY; return MD5.MD5Encode(info).toUpperCase(); } // 獲得日期 public static String getDataStr(String type) { Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat(type); return format.format(date); } // 轉換爲xml public static String map2xml(TreeMap<String, Object> map) { String info = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; info += "<xml>"; Set<String> keySet = map.keySet(); for (String string : keySet) { info += "<" + string + ">" + map.get(string) + "</" + string + ">"; } info += "</xml>"; return info; } public static Map<String, Object> xml2Map(String xmlStr) { Map<String, Object> map = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // 將字符串轉換成流 ByteArrayInputStream bis = new ByteArrayInputStream(xmlStr .getBytes()); Document doc = builder.parse(bis); Node root = doc.getFirstChild();// 根節點 NodeList nodeList = root.getChildNodes(); map = new HashMap<String, Object>(); for (int i = 0; i < nodeList.getLength(); i++) { Node child = nodeList.item(i); if (child instanceof Element) { Element e = (Element) child; map.put(e.getNodeName(), e.getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } return map; } // 測試 public static void main(String[] args) { // System.out.println("隨機:"+Utils.getRandomStr()); TreeMap<String, Object> ma = new TreeMap<String, Object>(); ma.put("appid", "wefoi"); ma.put("eei", "20934jf"); System.out.println(Utils.map2xml(ma)); // System.out.println(getDataStr("yyyyMMddHH")); } }