java 微信支付

//微信支付必备的ASCII字典序排序并拼接商户平台设置的密钥key

public static String signature(Map<String, String> map, String key) {
        Set<String> keySet = map.keySet();
        String[] str = new String[map.size()];
        StringBuilder tmp = new StringBuilder();
        // 进行字典排序
        str = keySet.toArray(str);
        Arrays.sort(str);
        for (int i = 0; i < str.length; i++) {
            String t = str[i] + "=" + map.get(str[i]) + "&";
            tmp.append(t);
        }
        if (null != key) {
            tmp.append("key=" + key);
        }
        return tmp.toString();
    }

//md5加密

    public static String transformMD5(String inputStr) {

        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            return null;
        }
        char[] charArray = inputStr.toCharArray(); // 将字符串转换为字符数组
        byte[] byteArray = new byte[charArray.length]; // 创建字节数组

        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }

        // 将得到的字节数组进行MD5运算
        byte[] md5Bytes = md5.digest(byteArray);

        StringBuffer md5Str = new StringBuffer();

        for (int i = 0; i < md5Bytes.length; i++) {
            if (Integer.toHexString(0xFF & md5Bytes[i]).length() == 1)
                md5Str.append("0").append(Integer.toHexString(0xFF & md5Bytes[i]));
            else
                md5Str.append(Integer.toHexString(0xFF & md5Bytes[i]));
        }
        return md5Str.toString();
    }

//解析微信返回来的xml数据

public static Map<String,String> getDomxml(String result) throws DocumentException{
        Map map = new HashMap(); 
        InputStream in=new ByteArrayInputStream(result.getBytes());  
       // 读取输入流 
       SAXReader reader = new SAXReader(); 
       Document document = reader.read(in); 
       // 得到xml根元素 
       Element root = document.getRootElement(); 
       // 得到根元素的所有子节点 
       @SuppressWarnings("unchecked") 
       List <Element>elementList = root.elements(); 
       for (Element element : elementList) { 
           map.put(element.getName(), element.getText()); 
       } 
       // 返回信息 
    return map;
    } 

//发送https请求,调用微信支付接口

    public static String httpRequest(String requestUrl,String requestMethod,String outputStr){ 
        // 创建SSLContext 
        StringBuffer buffer=null; 
        try{ 
        URL url = new URL(requestUrl); 
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
        conn.setRequestMethod(requestMethod); 
        conn.setDoOutput(true); 
        conn.setDoInput(true); 
        conn.connect(); 
        //往服务器端写内容 
        if(null !=outputStr){ 
            OutputStream os=conn.getOutputStream(); 
            os.write(outputStr.getBytes("utf-8")); 
            os.close(); 
        } 
        // 读取服务器端返回的内容 
        InputStream is = conn.getInputStream(); 
        InputStreamReader isr = new InputStreamReader(is, "utf-8"); 
        BufferedReader br = new BufferedReader(isr); 
        buffer = new StringBuffer(); 
        String line = null; 
        while ((line = br.readLine()) != null) { 
                      buffer.append(line); 
        } 
        }catch(Exception e){ 
            e.printStackTrace(); 
        } 
        return buffer.toString(); 
        }

//wxappidutil中的属性

    //小程序appid
    public static String appid = "appid";
    //小程序密钥
    public static String secret = "密钥";
    //小程序中支付接口的商户id
    public static String shanghuid = "商户id";
    //小程序中支付接口APIkey
    public static String key = "支付api key";(商户平台里面的)
    //统一下单接口
    public static String zhifuUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";

//微信支付详细请看

<a href="https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1">微信支付</a>

 

//微信支付

public static Object getData(WxPay wxpay) throws DocumentException {
        Map<String ,String>map = new HashMap<String,String>();
        map.put("appid", WxAppidUtil.appid);
        map.put("mch_id", WxAppidUtil.shanghuid);
        map.put("body", "ligeflag");
        map.put("nonce_str", "1add1a30ac87aa2db72f57a2375d8fec");
        map.put("notify_url", "https://www.ligeflag.com/ligeflag/wxpay/getresult.do");
        map.put("openid", wxpay.getOpenid());
        map.put("out_trade_no", wxpay.getOrderid());
        map.put("spbill_create_ip", "服务器ip地址");
        map.put("total_fee", wxpay.getJine()+"");
        map.put("trade_type", "JSAPI");
        String StringA = signature(map,WxAppidUtil.key);
        System.out.println("加上密钥后的数据"+StringA);
        String sign = Md5Util.transformMD5(StringA).toUpperCase();
        System.out.println(sign);
        String xml = "<xml>" +
                "<appid>"+ WxAppidUtil.appid +"</appid>"+
                "<body>ligeflag</body>"+
                "<mch_id>"+ WxAppidUtil.shanghuid +"</mch_id>"+
                "<nonce_str>随机字符串</nonce_str>"+
                "<notify_url>https://www.ligeflag.com/ligeflag/wxpay/getresult.do</notify_url>"+
                "<openid>"+ wxpay.getOpenid() +"</openid>"+
                "<out_trade_no>"+wxpay.getOrderid()+"</out_trade_no>"+
                "<spbill_create_ip>119.3.233.118</spbill_create_ip>"+
                "<total_fee>"+wxpay.getJine()+"</total_fee>"+
                "<trade_type>JSAPI</trade_type>"+
                "<sign>"+ sign +"</sign>"+
             "</xml>";
        String result = HttpClientUtil.httpRequest(WxAppidUtil.zhifuUrl, "POST", xml);
        System.out.println(result);
        Map<String,String> resmap = getDomxml(result);
        String return_code = resmap.get("return_code");//返回状态码 
        Map<String,String> twoSignMap = new HashMap<String,String>();
        if(return_code=="SUCCESS"||return_code.equals(return_code)){
            // 业务结果 
            Long timeStamp= System.currentTimeMillis()/1000; 
            String prepay_id = resmap.get("prepay_id");//返回的预付单信息 
            twoSignMap.put("nonceStr", "随机字符串"); 
            twoSignMap.put("package", "prepay_id="+prepay_id); 
            twoSignMap.put("timeStamp", timeStamp+""); 
            twoSignMap.put("appId", WxAppidUtil.appid);
            twoSignMap.put("signType", "MD5");
            StringA = signature(twoSignMap,WxAppidUtil.key);
            sign = Md5Util.transformMD5(StringA).toUpperCase();
            wxpay.setTime(timeStamp+"");
            wxpay.setNoncestr("随机字符串");
            wxpay.setPackageStr("prepay_id="+prepay_id);
            wxpay.setSigntype("MD5");
            wxpay.setPaySign(sign);
        }
        return wxpay;
    }

 

 

//微信获取openid (按照链接中的一步步操作即可)

<a href="https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html">网页授权获取openid</a>

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值