微信支付--工具类--产生二维码验证签名

来源:互联网 发布:爱淘宝1元天猫购物券 编辑:程序博客网 时间:2024/04/29 12:44
  1. import java.io.File;  
  2. import java.util.HashMap;  
  3. import java.util.Map;  
  4. import java.util.SortedMap;  
  5.   
  6. import javax.servlet.http.HttpSession;  
  7.   
  8. import com.google.zxing.BarcodeFormat;  
  9. import com.google.zxing.EncodeHintType;  
  10. import com.google.zxing.MultiFormatWriter;  
  11. import com.google.zxing.common.BitMatrix;  
  12.   
  13. public class WxPayHelper {  
  14.   
  15.     public static String KEY = "";// 商户支付密钥  
  16.     public static String MCH_ID = "";// 商户号  
  17.     public static String APP_ID = "";// 公众号应用ID  
  18.     public static String APP_SECRET = "";// 公众号应用密匙  
  19.   
  20.     public String getCodeUrl(SortedMap<String, String> packageParams) {  
  21.   
  22.         String strxml = XMLUtil.parseXML(packageParams);  
  23.         String urlPath = "https://api.mch.weixin.qq.com/pay/unifiedorder";  
  24.         try {  
  25.   
  26.             HttpsRequest httpsRequest = new HttpsRequest();  
  27.             String data = httpsRequest.sendPost(urlPath, strxml);  
  28.             SortedMap<String, String> map = XMLUtil.parseMap(data);  
  29.             if (map != null && map.containsKey("code_url")) {  
  30.                 return (String) map.get("code_url");  
  31.             }  
  32.   
  33.         } catch (Exception e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.         return null;  
  37.     }  
  38.   
  39.     /** 
  40.      * 支付二维码 
  41.      *  
  42.      * @param session 
  43.      * @param codeUrl 
  44.      *            code_url 
  45.      * @param orderNo 
  46.      *            订单号 
  47.      * @return 
  48.      */  
  49.     public String getQRCode(HttpSession session, String codeUrl, String orderNo) {  
  50.   
  51.         String path = Const.FILE_PATH_PAYQRCODE_DIR + "/"  
  52.                 + DateUtil.getTimestamp("yyyyMMdd");  
  53.         String filePath = session.getServletContext().getRealPath(path);  
  54.         FileUtils.createDirectory(filePath);  
  55.   
  56.         String fileName = orderNo + ".jpg";  
  57.   
  58.         path += "/" + fileName;  
  59.   
  60.         MultiFormatWriter multiFormatWriter = new MultiFormatWriter();  
  61.   
  62.         Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();  
  63.         hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
  64.         BitMatrix bitMatrix;  
  65.         try {  
  66.             bitMatrix = multiFormatWriter.encode(codeUrl,  
  67.                     BarcodeFormat.QR_CODE, 290290, hints);  
  68.             File file = new File(filePath, fileName);  
  69.             MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file);  
  70.         } catch (Exception e) {  
  71.             e.printStackTrace();  
  72.         }  
  73.         return path;  
  74.     }  
  75.   
  76.     /** 
  77.      * 判断通知参数是否合法 
  78.      *  
  79.      * @param params 
  80.      * @return 
  81.      */  
  82.     public static boolean isNotifyLegal(SortedMap<String, String> params) {  
  83.         if (params == null) {  
  84.             return false;  
  85.         }  
  86.         String sign = "";  
  87.         if (params.containsKey("sign")) {  
  88.             sign = params.get("sign");  
  89.         } else {  
  90.             return false;  
  91.         }  
  92.         String appid = "";  
  93.         if (params.containsKey("appid")) {  
  94.             appid = params.get("appid");  
  95.         } else {  
  96.             return false;  
  97.         }  
  98.         String mch_id = "";  
  99.         if (params.containsKey("mch_id")) {  
  100.             mch_id = params.get("mch_id");  
  101.         } else {  
  102.             return false;  
  103.         }  
  104.         if (!appid.equals(APP_ID) || !mch_id.equals(MCH_ID)) {  
  105.             return false;  
  106.         }  
  107.   
  108.         String mySign = MD5Util.createMD5Sign(params, KEY);  
  109.         if (sign.equals(mySign)) {  
  110.             return true;  
  111.         }  
  112.         return false;  
  113.     }  
  114.   
  115. }  
0 1
原创粉丝点击