您的位置:首页 > 移动开发 > 微信开发

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

2015-11-24 10:13 246 查看
import java.io.File;  

import java.util.HashMap;  

import java.util.Map;  

import java.util.SortedMap;  

  

import javax.servlet.http.HttpSession;  

  

import com.google.zxing.BarcodeFormat;  

import com.google.zxing.EncodeHintType;  

import com.google.zxing.MultiFormatWriter;  

import com.google.zxing.common.BitMatrix;  

  

public class WxPayHelper {  

  

    public static String KEY = "";// 商户支付密钥  

    public static String MCH_ID = "";// 商户号  

    public static String APP_ID = "";// 公众号应用ID  

    public static String APP_SECRET = "";// 公众号应用密匙  

  

    public String getCodeUrl(SortedMap<String, String> packageParams) {  

  

        String strxml = XMLUtil.parseXML(packageParams);  

        String urlPath = "https://api.mch.weixin.qq.com/pay/unifiedorder";  

        try {  

  

            HttpsRequest httpsRequest = new HttpsRequest();  

            String data = httpsRequest.sendPost(urlPath, strxml);  

            SortedMap<String, String> map = XMLUtil.parseMap(data);  

            if (map != null && map.containsKey("code_url")) {  

                return (String) map.get("code_url");  

            }  

  

        } catch (Exception e) {  

            e.printStackTrace();  

        }  

        return null;  

    }  

  

    /** 

     * 支付二维码 

     *  

     * @param session 

     * @param codeUrl 

     *            code_url 

     * @param orderNo 

     *            订单号 

     * @return 

     */  

    public String getQRCode(HttpSession session, String codeUrl, String orderNo) {  

  

        String path = Const.FILE_PATH_PAYQRCODE_DIR + "/"  

                + DateUtil.getTimestamp("yyyyMMdd");  

        String filePath = session.getServletContext().getRealPath(path);  

        FileUtils.createDirectory(filePath);  

  

        String fileName = orderNo + ".jpg";  

  

        path += "/" + fileName;  

  

        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();  

  

        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();  

        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  

        BitMatrix bitMatrix;  

        try {  

            bitMatrix = multiFormatWriter.encode(codeUrl,  

                    BarcodeFormat.QR_CODE, 290, 290, hints);  

            File file = new File(filePath, fileName);  

            MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file);  

        } catch (Exception e) {  

            e.printStackTrace();  

        }  

        return path;  

    }  

  

    /** 

     * 判断通知参数是否合法 

     *  

     * @param params 

     * @return 

     */  

    public static boolean isNotifyLegal(SortedMap<String, String> params) {  

        if (params == null) {  

            return false;  

        }  

        String sign = "";  

        if (params.containsKey("sign")) {  

            sign = params.get("sign");  

        } else {  

            return false;  

        }  

        String appid = "";  

        if (params.containsKey("appid")) {  

            appid = params.get("appid");  

        } else {  

            return false;  

        }  

        String mch_id = "";  

        if (params.containsKey("mch_id")) {  

            mch_id = params.get("mch_id");  

        } else {  

            return false;  

        }  

        if (!appid.equals(APP_ID) || !mch_id.equals(MCH_ID)) {  

            return false;  

        }  

  

        String mySign = MD5Util.createMD5Sign(params, KEY);  

        if (sign.equals(mySign)) {  

            return true;  

        }  

        return false;  

    }  

  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: