微信h5支付

//统一下单   

@Override

    public PrepaidResultInfo sendPayRequest(BaseRequestInfo infoTmp)
            throws PayException
    {
        PrepaidInfo info = (PrepaidInfo) infoTmp;
        if (null == info || !checkPrepaidInfo(info)) {
            log.error("申请支付失败,参数有误!");
            throw new PayException("参数有误!");
        }
        Map<String, String> tmp = info.toMapForTenPay();
        try {
            addSingToMap(tmp);
        }catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            log.error("微信支付进行签名时出现异常!", e);
            throw new PayException("系统出现异常!" + e.getMessage());
        }
        String sendMessage = XMLUtil.createSimpleXML(tmp, "xml");
        HttpClient httpclient = new HttpClient();
        httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, WAIT_TIME);
        PostMethod post = new PostMethod(ORDER_URL);
        try {
            post.setRequestEntity(new StringRequestEntity(sendMessage, "text/xml", CODE_FORMAT));
            httpclient.executeMethod(post);
            String response = new String(post.getResponseBody(), CODE_FORMAT);
            if (StringUtil.isEmpty(response)) {
                throw new PayException("系统出现异常!");
            }
            Map<String, String> tmpResult = XMLUtil.resolveSimpleXML(response);
            if (null == tmpResult || tmpResult.size() < 1) {
                log.error("解析微支付返回信息时出现异常,response" + response);
                throw new PayException("系统出现订单结果解析异常!");
            }
            if (!checkSignForMap(tmpResult)) {
                log.error("解析微支付返回信息时出现异常,数据签名有误,response" + response);
                throw new PayException("返回信息中数据签名有误!!");
            }
            PrepaidResultInfo result = createPrepaidResultInfo(tmpResult);
            result.setOrderId(info.getOrderId());
            String mweb=tmpResult.get("mweb_url");
            if(mweb!=null){
            	result.setMwebUrl(mweb);
            }
            return result;
        }
        catch (UnsupportedEncodingException e) {
            log.error("封装传输请求数据时出现异常!", e);
            throw new PayException("系统出现异常!" + e.getMessage());
        }
        catch (HttpException e) {
            log.error("执行http请求出现异常", e);
            throw new PayException("系统出现异常!" + e.getMessage());
        }
        catch (IOException e) {
            log.error("读取请求结果数据时出现异常!", e);
            throw new PayException("系统出现异常!" + e.getMessage());
        }
    }





/**   进行签名
     * @param param
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException 
     */
    private void addSingToMap(Map<String, String> param)
            throws NoSuchAlgorithmException, UnsupportedEncodingException
    {
        if (null == param || param.size() < 1) {
            return;
        }
        Set<String> keys = param.keySet();
        Set<String> orderedKeys = new TreeSet<String>();
        orderedKeys.addAll(keys);
        StringBuffer resource = new StringBuffer();
        for (String key : orderedKeys) {
            if (!StringUtil.isEmpty(param.get(key))) {
                resource.append(key).append("=").append(param.get(key)).append("&");
            }
        }
        String aim = resource.toString();
        if (!aim.contains("&")) {
            return;
        }
        aim = aim + "key=" + publicKey;
        byte[] md5 = MD5.encrypt(aim.getBytes("UTF-8"));
        String sign = Hex.encode(md5);
        param.put("sign", sign);
    }



//校验签名

private boolean checkSignForMap(Map<String, String> param)
    {
        if (null == param || param.size() < 1 || !param.containsKey(SIGN)) {
            return false;
        }
        String sign = (String) param.get(SIGN);
        param.remove(SIGN);


        Set<String> keys = param.keySet();
        Set<String> orderedKeys = new TreeSet<String>();
        orderedKeys.addAll(keys);
        StringBuffer resource = new StringBuffer();
        for (String key : orderedKeys) {
            if (!StringUtil.isEmpty((String) param.get(key))) {
                resource.append(key).append("=").append(param.get(key)).append("&");
            }
        }
        String aim = resource.toString();
        if (!aim.contains("&")) {
            return false;
        }
        aim = aim + "key=" + publicKey;
        byte[] md5 = MD5.encrypt(aim.getBytes());
        String rmpSign = Hex.encode(md5);
        boolean result = sign.endsWith(rmpSign);
        param.put(SIGN, sign);
        return result;
    }
 
 
//检验是否发送了通知
@Override
    public boolean parseNotifyInfos(int type, Map<String, String> params)
            throws ManagerException
    {
        NotifyInfo info = null;
        try {
            String tradeNumber = params.get("out_trade_no");
            log.info("接收到支付回调通知,tradeNumber:" + tradeNumber + " type:" + type);
            if (StringUtil.isEmpty(tradeNumber)) {
                return false;
            }

            SOrder order = shoppingGoodsManager.getSOrderByTradeNumber(tradeNumber, SOrder.DELSTATUS_NO);
            if (order == null) {
                return false;
            }

            if (type == SOrder.PAYMENT_TYPE_TEN) {
                info = tenPay.treadNotify(params);
                //if(order.getNotifyBy()!=null&&order.getNotifyBy()!=SOrder.NOTIFYBY_WEIXIN){
                order.setNotifyBy(SOrder.NOTIFYBY_WEIXIN);
                order.setIdentification(info.getIdentification());
                order.setNotifyAt(DateUtil.getNowTimeStamp());
                order.setPayerAccount(info.getBuyerAccount());
                order.setStatus(SOrder.STATUS_MONEY);
                String timeEnd = params.get("time_end");
                Date date = new SimpleDateFormat("yyyyMMddHHmmss").parse(timeEnd);
                int time = (int) (date.getTime() / 1000);
                order.setPayAt(time);
                shoppingFollowManager.updateSorder(order);
                return true;
                //}
            }
            if (type == SOrder.PAYMENT_TYPE_ALI) {
                info = aliPay.treadNotify(params);
                //if(order.getNotifyBy()!=null&&order.getNotifyBy()!=SOrder.NOTIFYBY_ZHIFUBAO){
                order.setNotifyBy(SOrder.NOTIFYBY_ZHIFUBAO);
                order.setIdentification(info.getIdentification());
                order.setNotifyAt(DateUtil.getNowTimeStamp());
                order.setPayerAccount(info.getBuyerAccount());
                order.setPayeeAccount(info.getSellerAccount());
                order.setStatus(SOrder.STATUS_MONEY);
                String timeEnd = params.get("gmt_payment");
                Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeEnd);
                int time = (int) (date.getTime() / 1000);
                order.setPayAt(time);
                shoppingFollowManager.updateSorder(order);
                return true;
                //}
            }
            if (null == info) {
                log.error("未知的支付结果通知类型!");
                return false;
            }
        }
        catch (PayException pe) {
            log.error("解析支付结果通知时出现异常!", pe);
        }
        catch (ParseException e) {
            log.error("转换时间出现异常!", e);
        }
        return false;
    }



猜你喜欢

转载自blog.csdn.net/qq_38468172/article/details/80067256