public class UnifiedorderDto implements WeiXinConstants {
private String appid;
private String body;
private String device_info;
private String mch_id;
private String nonce_str;
private String notify_url;
private String openId;
private String out_trade_no;
private String spbill_create_ip;
private int total_fee;
private String trade_type;
private String product_id;
private String sign;
public UnifiedorderDto() {
this.appid = APPID;
this.mch_id = WXPAYMENTACCOUNT;
this.device_info = DEVICE_INFO_WEB;
this.notify_url = CALLBACK_URL;
this.trade_type = TRADE_TYPE_NATIVE;
}
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getDevice_info() {
return device_info;
}
public void setDevice_info(String device_info) {
this.device_info = device_info;
}
public String getMch_id() {
return mch_id;
}
public void setMch_id(String mch_id) {
this.mch_id = mch_id;
}
public String getNonce_str() {
return nonce_str;
}
public void setNonce_str(String nonce_str) {
this.nonce_str = nonce_str;
}
public String getNotify_url() {
return notify_url;
}
public void setNotify_url(String notify_url) {
this.notify_url = notify_url;
}
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public String getOut_trade_no() {
return out_trade_no;
}
public void setOut_trade_no(String out_trade_no) {
this.out_trade_no = out_trade_no;
}
public String getSpbill_create_ip() {
return spbill_create_ip;
}
public void setSpbill_create_ip(String spbill_create_ip) {
this.spbill_create_ip = spbill_create_ip;
}
public int getTotal_fee() {
return total_fee;
}
public void setTotal_fee(int total_fee) {
this.total_fee = total_fee;
}
public String getTrade_type() {
return trade_type;
}
public void setTrade_type(String trade_type) {
this.trade_type = trade_type;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getProduct_id() {
return product_id;
}
public void setProduct_id(String product_id) {
this.product_id = product_id;
}
public String generateXMLContent() {
String xml = "<xml>" +
"" + this.appid + "</appid>" +
"" + this.body + "" +
"<device_info>WEB</device_info>" +
"<mch_id>" + this.mch_id + "</mch_id>" +
"<nonce_str>" + this.nonce_str + "</nonce_str>" +
"<notify_url>" + this.notify_url + "</notify_url>" +
"<out_trade_no>" + this.out_trade_no + "</out_trade_no>" +
"<product_id>" + this.product_id + "</product_id>" +
"<spbill_create_ip>" + this.spbill_create_ip+ "</spbill_create_ip>" +
"<total_fee>" + String.valueOf(this.total_fee) + "</total_fee>" +
"<trade_type>" + this.trade_type + "</trade_type>" +
"<sign>" + this.sign + "</sign>" +
"</xml>";
return xml;
}
public String makeSign() {
String content ="appid=" + this.appid +
"&body=" + this.body +
"&device_info=WEB" +
"&mch_id=" + this.mch_id +
"&nonce_str=" + this.nonce_str +
"?ify_url=" + this.notify_url +
"&out_trade_no=" + this.out_trade_no +
"&product_id=" + this.product_id +
"&spbill_create_ip=" + this.spbill_create_ip+
"&total_fee=" + String.valueOf(this.total_fee) +
"&trade_type=" + this.trade_type;
content = content + "&key=" + WeiXinConstants.MD5_API_KEY;
String esignature = WeiXinPaymentUtil.MD5Encode(content, "utf-8");
return esignature.toUpperCase();
}
}
Map<string,object> result=new HashMap<string,object>();
UnifiedorderDto dto = new UnifiedorderDto();
if(cash == null || "".equals(cash)) {
result.put("error", "cash could not be zero");
return result;
}
int totalfee = 100*Integer.parseInt(cash);
logger.info("total recharge cash : " + totalfee);
dto.setProduct_id(String.valueOf(System.currentTimeMillis()));
dto.setBody("repair");
dto.setNonce_str(String.valueOf(System.nanoTime()));
LoginInfo loginInfo = LoginInfoUtil.getLoginInfo();
// 通过我们后台订单号+UUID为身份识别标志
dto.setOut_trade_no("你的订单号+关键信息,微信回调之后传回,你可以验证");
dto.setTotal_fee(totalfee);
dto.setSpbill_create_ip("127.0.0.1");
// generate signature
dto.setSign(dto.makeSign());
logger.info("sign : " + dto.makeSign());
logger.info("xml content : " + dto.generateXMLContent());
try {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(WeiXinConstants.UNIFIEDORDER_URL);
post.addHeader("Content-Type", "text/xml; charset=UTF-8");
StringEntity xmlEntity = new StringEntity(dto.generateXMLContent(), ContentType.TEXT_XML);
post.setEntity(xmlEntity);
HttpResponse httpResponse = httpClient.execute(post);
String responseXML = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
logger.info("response xml content : " + responseXML);
// parse CODE_URL CONTENT
Map<string, string=""> resultMap = (Map<string, string="">)XMLUtil.doXMLParse(responseXML);
logger.info("response code_url : " + resultMap.get("code_url"));
String codeurl = resultMap.get("code_url");
if(codeurl != null && !"".equals(codeurl)) {
String imageurl = generateQrcode(codeurl);
result.put("QRIMAGE", imageurl);
}
post.releaseConnection();
} catch(Exception e) {
e.printStackTrace();
}
result.put("success", "1");
return result;</string,></string,></string,object></string,object>
private String generateQrcode(String codeurl) {
File foldler = new File(basePath + "qrcode");
if(!foldler.exists()) {
foldler.mkdirs();
}
String f_name = UUIDUtil.uuid() + ".png";
try {
File f = new File(basePath + "qrcode", f_name);
FileOutputStream fio = new FileOutputStream(f);
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型
BitMatrix bitMatrix = null;
bitMatrix = multiFormatWriter.encode(codeurl, BarcodeFormat.QR_CODE, 300, 300,hints);
BufferedImage image = toBufferedImage(bitMatrix);
//输出二维码图片流
ImageIO.write(image, "png", fio);
return ("qrcode/" + f_name);
} catch (Exception e1) {
e1.printStackTrace();
return null;
}
}
@RequestMapping(value = "/your_callback_url", method = RequestMethod.POST)
@ResponseBody
public void finishPayment(HttpServletRequest request, HttpServletResponse response) {
try { logger.info("start to callback from weixin server: " + request.getRemoteHost());
Map<string, string=""> resultMap = new HashMap<string, string="">();
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXBuilder saxBuilder= new SAXBuilder();
Document document = saxBuilder.build(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List list = root.getChildren();
Iterator it = list.iterator();
while(it.hasNext()) {
Element e = (Element) it.next();
String k = e.getName();
String v = "";
List children = e.getChildren();
if(children.isEmpty()) {
v = e.getTextNormalize();
} else {
v = XMLUtil.getChildrenText(children);
}
resultMap.put(k, v);
}
// 验证签名!!!
/*
String[] keys = resultMap.keySet().toArray(new String[0]);
Arrays.sort(keys);
String kvparams = "";
for(int i=0; i<keys.length; i++)="" {="" if(keys[i].equals("esign"))="" continue;="" }="" 签名算法="" if(i="=" 0)="" kvparams="" +="(keys[i]" "=" + resultMap.get(keys[i]));
} else {
kvparams += (" &"="" keys[i]="" &key=" + WeiXinConstants.MD5_API_KEY;
String md5esign = WeiXinPaymentUtil.MD5Encode(esign, " utf-8");="" if(!md5esign.equals(resultmap.get("sign")))="" return;="" }*="" 关闭流="" 释放资源="" inputstream.close();="" inputstream="null;" string="" returncode="resultMap.get("return_code");" outtradeno="resultMap.get("out_trade_no");" 以分为单位="" int="" nfee="Integer.parseInt(resultMap.get("total_fee"));" logger.info("out="" trade="" no="" :="" outtradeno);="" logger.info("total_fee="" nfee);="" 业务处理流程="" if("success".equals(returncode))="" todo:="" your="" business="" process="" add="" here="" response.getwriter().print(xmlutil.getretresultxml(resultmap.get("return_code"),="" resultmap.get("return_code")));="" else="" resultmap.get("return_msg")));="" catch(ioexception="" ioe)="" ioe.printstacktrace();="" catch="" (jdomexception="" e1)="" e1.printstacktrace();="" }
<dependency> <groupid>jdom</groupid> jdom</artifactid> <version>1.1</version> </dependency> <dependency> <groupid>com.google.zxing</groupid> core</artifactid> <version>3.3.0</version> </dependency>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有