// unified order @Override public PrepaidResultInfo sendPayRequest (BaseRequestInfo infoTmp) throws PayException { PrepaidInfo info = (PrepaidInfo) infoTmp; if (null == info || !checkPrepaidInfo(info)) { log.error("Failed to apply for payment, the parameters are wrong!"); throw new PayException("The parameter is wrong!"); } Map<String, String> tmp = info.toMapForTenPay(); try { addSingToMap(tmp); }catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { log.error("There was an exception when signing WeChat Pay!", e); throw new PayException("The system is abnormal!" + 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("The system is abnormal!"); } Map<String, String> tmpResult = XMLUtil.resolveSimpleXML(response); if (null == tmpResult || tmpResult.size() < 1) { log.error("An exception occurred when parsing the return information of the micropayment, response" + response); throw new PayException("The system has an order result parsing exception!"); } if (!checkSignForMap(tmpResult)) { log.error("There was an exception when parsing the return information of the micropayment, the data signature was wrong, response" + response); throw new PayException("The data signature in the returned information is wrong!!"); } 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("An exception occurred when encapsulating the transmission request data!", e); throw new PayException("The system is abnormal!" + e.getMessage()); } catch (HttpException e) { log.error("An exception occurred while executing the http request", e); throw new PayException("The system is abnormal!" + e.getMessage()); } catch (IOException e) { log.error("An exception occurred while reading the request result data!", e); throw new PayException("The system is abnormal!" + e.getMessage()); } } /** sign * @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); } //check signature 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; }
//Check if notification was sent
@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("Received payment callback notification, 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("Unknown payment result notification type!"); return false; } } catch (PayException pe) { log.error("An exception occurred while parsing the payment result notification!", pe); } catch (ParseException e) { log.error("The conversion time is abnormal!", e); } return false; }