Commit 15d19821 authored by cy's avatar cy

微信小程序支付

parent ead6e6b6
...@@ -31,4 +31,7 @@ public class PayPageDto { ...@@ -31,4 +31,7 @@ public class PayPageDto {
*/ */
@ApiModelProperty(value = "支付类型 1:支付宝 2:微信 3:充值卡", name = "payType") @ApiModelProperty(value = "支付类型 1:支付宝 2:微信 3:充值卡", name = "payType")
private Integer payType; private Integer payType;
@ApiModelProperty(value = "微信用户的openid",name="openid")
private String openid;
} }
...@@ -8,6 +8,7 @@ import javax.servlet.http.HttpServletRequest; ...@@ -8,6 +8,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.SortedMap;
/** /**
...@@ -15,15 +16,32 @@ import java.util.Map; ...@@ -15,15 +16,32 @@ import java.util.Map;
*/ */
public interface TestWxPayService { public interface TestWxPayService {
/** /**
* 测试-微信支付 * pc端微信支付
* @Param * @Param
* @return * @return
*/ */
R<Map<String, String>> wxPay(PayPageDto payPageDto, HttpServletResponse response); R<Map<String, String>> wxPay(PayPageDto payPageDto, HttpServletResponse response);
/***
* h5微信支付
* @param payPageDto
* @param request
* @param response
* @return
* @throws JsonProcessingException
*/
R<String> h5WxPay(PayPageDto payPageDto,HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException; R<String> h5WxPay(PayPageDto payPageDto,HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException;
R<String> WxPayApplets(PayPageDto payPageDto,HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException; /***
* 小程序微信支付
* @param payPageDto
* @param request
* @param response
* @return
* @throws JsonProcessingException
*/
R<SortedMap<String, String>> WxPayApplets(PayPageDto payPageDto, HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException;
/*** /***
*支付成功回调 *支付成功回调
* @param request * @param request
...@@ -32,6 +50,13 @@ public interface TestWxPayService { ...@@ -32,6 +50,13 @@ public interface TestWxPayService {
*/ */
void wxPayCallBack(HttpServletRequest request, HttpServletResponse response) throws Exception; void wxPayCallBack(HttpServletRequest request, HttpServletResponse response) throws Exception;
/***
* 订单状态查询
* @param out_trade_no
* @return
* @throws Exception
*/
R<Map<String,String>> wxQuery(String out_trade_no) throws Exception; R<Map<String,String>> wxQuery(String out_trade_no) throws Exception;
} }
...@@ -10,11 +10,9 @@ import cn.wisenergy.service.app.AliPayService; ...@@ -10,11 +10,9 @@ import cn.wisenergy.service.app.AliPayService;
import cn.wisenergy.service.app.UserLimitService; import cn.wisenergy.service.app.UserLimitService;
import cn.wisenergy.service.common.Common; import cn.wisenergy.service.common.Common;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient; import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayResponse; import com.alipay.api.AlipayResponse;
import com.alipay.api.DefaultAlipayClient; import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.internal.util.AlipaySignature; import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest; import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest; import com.alipay.api.request.AlipayTradeWapPayRequest;
...@@ -24,15 +22,11 @@ import lombok.extern.slf4j.Slf4j; ...@@ -24,15 +22,11 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import springfox.documentation.schema.Entry;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
...@@ -61,10 +55,6 @@ public class AliPayServiceImpl implements AliPayService { ...@@ -61,10 +55,6 @@ public class AliPayServiceImpl implements AliPayService {
/*** /***
* pc端支付宝支付 * pc端支付宝支付
* @param payPageDto
* @param request
* @param httpResponse
* @return
*/ */
@Override @Override
public R<String> doPost(PayPageDto payPageDto, HttpServletRequest request, HttpServletResponse httpResponse) { public R<String> doPost(PayPageDto payPageDto, HttpServletRequest request, HttpServletResponse httpResponse) {
...@@ -75,12 +65,12 @@ public class AliPayServiceImpl implements AliPayService { ...@@ -75,12 +65,12 @@ public class AliPayServiceImpl implements AliPayService {
AlipayClient alipayClient = new DefaultAlipayClient(Common.PAY_URL, APP_ID, PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2"); AlipayClient alipayClient = new DefaultAlipayClient(Common.PAY_URL, APP_ID, PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
String tradeNo = "21" + System.currentTimeMillis(); String tradeNo = "21" + System.currentTimeMillis();
Double amount = 0.01; double amount = 0.01;
//支付成功跳转地址 //支付成功跳转地址
alipayRequest.setReturnUrl(Common.RETURN_URL_PC); alipayRequest.setReturnUrl(Common.RETURN_URL_PC);
//支付回调地址 //支付回调地址
alipayRequest.setNotifyUrl(Common.NOTIFY_URL); alipayRequest.setNotifyUrl(Common.NOTIFY_URL);
//alipayRequest.setNotifyUrl(" http://iub3mv.natappfree.cc/pay/aliPayCallBack.do"); //alipayRequest.setNotifyUrl("http://bga7hc.natappfree.cc/pay/aliPayCallBack.do");
alipayRequest.setBizContent("{" + alipayRequest.setBizContent("{" +
"\"out_trade_no\":\"" + tradeNo + "\"," + "\"out_trade_no\":\"" + tradeNo + "\"," +
"\"total_amount\":" + amount + "," + "\"total_amount\":" + amount + "," +
...@@ -88,7 +78,7 @@ public class AliPayServiceImpl implements AliPayService { ...@@ -88,7 +78,7 @@ public class AliPayServiceImpl implements AliPayService {
"\"body\":\"充值\"," + "\"body\":\"充值\"," +
"\"product_code\":\"FAST_INSTANT_TRADE_PAY\"" + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"" +
"}"); "}");
String form = ""; String form;
//调用SDK生成表单 //调用SDK生成表单
try { try {
AlipayTradePagePayResponse response = alipayClient.pageExecute(alipayRequest); AlipayTradePagePayResponse response = alipayClient.pageExecute(alipayRequest);
...@@ -100,7 +90,6 @@ public class AliPayServiceImpl implements AliPayService { ...@@ -100,7 +90,6 @@ public class AliPayServiceImpl implements AliPayService {
httpResponse.getWriter().flush(); httpResponse.getWriter().flush();
httpResponse.getWriter().close(); httpResponse.getWriter().close();
if (response.isSuccess()) { if (response.isSuccess()) {
System.out.println("调用成功");
//生成预支付订单,充值结果设为失败(result=1) //生成预支付订单,充值结果设为失败(result=1)
PayRecord payRecord = new PayRecord(); PayRecord payRecord = new PayRecord();
payRecord.setMoney(payPageDto.getTotal()); payRecord.setMoney(payPageDto.getTotal());
...@@ -120,10 +109,6 @@ public class AliPayServiceImpl implements AliPayService { ...@@ -120,10 +109,6 @@ public class AliPayServiceImpl implements AliPayService {
/*** /***
* 手机网页支付宝支付 * 手机网页支付宝支付
* @param payPageDto
* @param request
* @param httpResponse
* @return
*/ */
@Override @Override
public R<String> wapAliPay(PayPageDto payPageDto, HttpServletRequest request, HttpServletResponse httpResponse) { public R<String> wapAliPay(PayPageDto payPageDto, HttpServletRequest request, HttpServletResponse httpResponse) {
...@@ -133,7 +118,7 @@ public class AliPayServiceImpl implements AliPayService { ...@@ -133,7 +118,7 @@ public class AliPayServiceImpl implements AliPayService {
AlipayClient alipayClient = new DefaultAlipayClient(Common.PAY_URL, APP_ID, PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2"); AlipayClient alipayClient = new DefaultAlipayClient(Common.PAY_URL, APP_ID, PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest(); AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
String tradeNo = "21" + System.currentTimeMillis(); String tradeNo = "21" + System.currentTimeMillis();
Double amount = 0.01; double amount = 0.01;
//支付成功跳转地址 //支付成功跳转地址
alipayRequest.setReturnUrl(Common.RETURN_URL_WAP); alipayRequest.setReturnUrl(Common.RETURN_URL_WAP);
//支付后回调地址 //支付后回调地址
...@@ -146,7 +131,7 @@ public class AliPayServiceImpl implements AliPayService { ...@@ -146,7 +131,7 @@ public class AliPayServiceImpl implements AliPayService {
"\"body\":\"充值\"," + "\"body\":\"充值\"," +
"\"product_code\":\"QUICK_WAP_WAY\"" + "\"product_code\":\"QUICK_WAP_WAY\"" +
"}"); "}");
String form = ""; String form;
//调用SDK生成表单 //调用SDK生成表单
try { try {
AlipayResponse response = alipayClient.pageExecute(alipayRequest); AlipayResponse response = alipayClient.pageExecute(alipayRequest);
...@@ -185,9 +170,6 @@ public class AliPayServiceImpl implements AliPayService { ...@@ -185,9 +170,6 @@ public class AliPayServiceImpl implements AliPayService {
/*** /***
* 支付后回调方法 * 支付后回调方法
* @param response
* @param request
* @throws Exception
*/ */
@Override @Override
public void aliPayCallBack(HttpServletResponse response, HttpServletRequest request) throws Exception { public void aliPayCallBack(HttpServletResponse response, HttpServletRequest request) throws Exception {
...@@ -233,11 +215,9 @@ public class AliPayServiceImpl implements AliPayService { ...@@ -233,11 +215,9 @@ public class AliPayServiceImpl implements AliPayService {
/*** /***
* 将request中的参数转换成Map * 将request中的参数转换成Map
* @param request
* @return
*/ */
private static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) { private static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) {
Map<String, String> retMap = new HashMap<String, String>(); Map<String, String> retMap = new HashMap<>();
Set<Map.Entry<String, String[]>> entries = request.getParameterMap().entrySet(); Set<Map.Entry<String, String[]>> entries = request.getParameterMap().entrySet();
for (Map.Entry<String, String[]> entry : entries) { for (Map.Entry<String, String[]> entry : entries) {
......
package cn.wisenergy.service.util; package cn.wisenergy.service.util;
import com.itextpdf.text.DocumentException;
import org.apache.poi.util.DocumentHelper;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import javax.management.Attribute;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
...@@ -18,78 +13,19 @@ import javax.xml.transform.stream.StreamResult; ...@@ -18,78 +13,19 @@ import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
public class MapToXmlUtils { public class MapToXmlUtils {
/**
* map 对象转换成 xml 字符串
**//*
public static String mapToXml(Map map) throws Exception{
if (null == map) return null;
System.out.println("转换前的Map对象数据:" + map.toString());
StringBuffer sb = new StringBuffer();
*//** 添加xml 的公共头信息 *//*
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
mapToXmlHelp(map, sb);
System.out.println("将map转成xml后的数据:" + sb.toString());
return sb.toString();
}
*//**
* 转换帮助方法
**//*
private static void mapToXmlHelp(Map map, StringBuffer str) throws Exception{
Set set = map.keySet();
*//** 遍历key *//*
for (Iterator it = set.iterator(); it.hasNext();) {
String key = (String) it.next();
Object value = map.get(key);
if (null == value)
value = "";
*//** 如果对象是集合 *//*
if (value.getClass().getName().equals("java.util.ArrayList")) {
ArrayList list = (ArrayList) map.get(key);
str.append("<" + key + ">");
for (int i = 0; i < list.size(); i++) {
HashMap hm = (HashMap) list.get(i);
mapToXmlHelp(hm, str);
}
str.append("</" + key + ">");
*//** 如果该集合对象的长度只有1,那么默认重复添加集合下节点 *//*
if (null !=list && !list.isEmpty() && list.size() <= 1) {
str.append("<" + key + ">");
for (int i = 0; i < list.size(); i++) {
HashMap hm = (HashMap) list.get(i);
mapToXmlHelp(hm, str);
}
str.append("</" + key + ">");
}
*//** 对象非集合时 *//*
} else {
if (value instanceof HashMap) {
str.append("<" + key + ">");
mapToXmlHelp((HashMap) value, str);
str.append("</" + key + ">");
} else {
str.append("<" + key + ">" + value + "</" + key + ">");
}
}
}
}*/
/** /**
* XML格式字符串转换为Map * XML格式字符串转换为Map
*
* @param strXML XML字符串
* @return XML数据转换后的Map
* @throws Exception
*/ */
public static Map<String, String> xmlToMap(String strXML) throws Exception { public static Map<String, String> xmlToMap(String strXML) throws Exception {
try { Map<String, String> data = new HashMap<>();
Map<String, String> data = new HashMap<String, String>();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8")); InputStream stream = new ByteArrayInputStream(strXML.getBytes(StandardCharsets.UTF_8));
org.w3c.dom.Document doc = documentBuilder.parse(stream); org.w3c.dom.Document doc = documentBuilder.parse(stream);
doc.getDocumentElement().normalize(); doc.getDocumentElement().normalize();
NodeList nodeList = doc.getDocumentElement().getChildNodes(); NodeList nodeList = doc.getDocumentElement().getChildNodes();
...@@ -102,22 +38,14 @@ public class MapToXmlUtils { ...@@ -102,22 +38,14 @@ public class MapToXmlUtils {
} }
try { try {
stream.close(); stream.close();
} catch (Exception ex) { } catch (Exception ignored) {
// do nothing
} }
return data; return data;
} catch (Exception ex) {
throw ex;
}
} }
/** /**
* 将Map转换为XML格式的字符串 * 将Map转换为XML格式的字符串
*
* @param data Map类型数据
* @return XML格式的字符串
* @throws Exception
*/ */
public static String mapToXml(Map<String, String> data) throws Exception { public static String mapToXml(Map<String, String> data) throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
...@@ -147,7 +75,7 @@ public class MapToXmlUtils { ...@@ -147,7 +75,7 @@ public class MapToXmlUtils {
try { try {
writer.close(); writer.close();
} }
catch (Exception ex) { catch (Exception ignored) {
} }
return output; return output;
} }
......
package cn.wisenergy.service.util; package cn.wisenergy.service.util;
import cn.wisenergy.common.utils.Md5Util;
import cn.wisenergy.model.dto.PayPageDto; import cn.wisenergy.model.dto.PayPageDto;
import cn.wisenergy.service.httpClient.WechatPayHttpClientBuilder; import cn.wisenergy.service.httpClient.WechatPayHttpClientBuilder;
import cn.wisenergy.service.httpClient.auth.AutoUpdateCertificatesVerifier; import cn.wisenergy.service.httpClient.auth.AutoUpdateCertificatesVerifier;
...@@ -10,7 +9,7 @@ import cn.wisenergy.service.httpClient.auth.WechatPay2Credentials; ...@@ -10,7 +9,7 @@ import cn.wisenergy.service.httpClient.auth.WechatPay2Credentials;
import cn.wisenergy.service.httpClient.auth.WechatPay2Validator; import cn.wisenergy.service.httpClient.auth.WechatPay2Validator;
import cn.wisenergy.service.httpClient.util.PemUtil; import cn.wisenergy.service.httpClient.util.PemUtil;
import cn.wisenergy.service.wxpay.WxCommon; import cn.wisenergy.service.wxpay.WxCommon;
import com.fasterxml.jackson.core.JsonProcessingException;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.RandomStringUtils;
...@@ -27,6 +26,7 @@ import javax.servlet.http.HttpServletRequest; ...@@ -27,6 +26,7 @@ import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey; import java.security.PrivateKey;
...@@ -39,12 +39,6 @@ import java.util.*; ...@@ -39,12 +39,6 @@ import java.util.*;
public class WxPayUtil { public class WxPayUtil {
/* @Value("${wxPay.APP_ID}")
private static String appid;
@Value("${wxPay.MCH_ID}")
private static String mchid;
@Value("${wxPay.PRIVATE_KEY}")
private static String key;*/
/** /**
* 商户号 * 商户号
*/ */
...@@ -166,10 +160,9 @@ public class WxPayUtil { ...@@ -166,10 +160,9 @@ public class WxPayUtil {
// httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//完成签名并执行请求 //完成签名并执行请求
CloseableHttpResponse response = httpClient.execute(httpPost);
try { try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
if (response.getStatusLine().getStatusCode() == 200) { if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity(); HttpEntity httpEntity = response.getEntity();
String content = EntityUtils.toString(httpEntity, "utf8"); String content = EntityUtils.toString(httpEntity, "utf8");
...@@ -177,27 +170,23 @@ public class WxPayUtil { ...@@ -177,27 +170,23 @@ public class WxPayUtil {
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}finally {
response.close();
} }
} }
/*** /***
* pc端生成统一下单格式的订单,生成一个XML格式的字符串 * pc端生成统一下单格式的订单,生成一个XML格式的字符串
* @return
* @throws UnsupportedEncodingException
*/ */
public static String createOrderInfo(PayPageDto payPageDto,String tradeNo,String product_id,String time_expire) throws UnsupportedEncodingException { public static String createOrderInfo(String tradeNo,String product_id,String time_expire,String appid,String mchid,String key) throws UnsupportedEncodingException {
String nonce_str = RandomStringUtils.randomAlphanumeric(16); String nonce_str = RandomStringUtils.randomAlphanumeric(16);
SortedMap<String,String> parameters = new TreeMap<String,String>(); SortedMap<String,String> parameters = new TreeMap<>();
parameters.put("appid", WxCommon.APP_ID); parameters.put("appid", appid);
parameters.put("mch_id",mchId); parameters.put("mch_id",mchid);
parameters.put("body", "充值"); parameters.put("body", "充值");
parameters.put("out_trade_no",tradeNo); parameters.put("out_trade_no",tradeNo);
parameters.put("notify_url", "http://raux9u.natappfree.cc/pay/wxPayCallBack.do"); //parameters.put("notify_url", "http://raux9u.natappfree.cc/pay/wxPayCallBack.do");
//parameters.put("notify_url",WxCommon.NOTIFY_URL); parameters.put("notify_url",WxCommon.NOTIFY_URL);
DecimalFormat df = new DecimalFormat("#"); DecimalFormat df = new DecimalFormat("#");
//parameters.put("total_fee", df.format(payPageDto.getTotal()*100)); //parameters.put("total_fee", df.format(payPageDto.getTotal()*100));
parameters.put("total_fee", df.format(1)); parameters.put("total_fee", df.format(1));
...@@ -206,8 +195,7 @@ public class WxPayUtil { ...@@ -206,8 +195,7 @@ public class WxPayUtil {
parameters.put("product_id", product_id); parameters.put("product_id", product_id);
parameters.put("spbill_create_ip","0.0.0.0"); parameters.put("spbill_create_ip","0.0.0.0");
parameters.put("time_expire",time_expire); parameters.put("time_expire",time_expire);
String characterEncoding = "UTF-8"; String sign = createSign(parameters,key);
String sign = createSign(characterEncoding,parameters);
parameters.put("sign", sign);//签名 parameters.put("sign", sign);//签名
//将订单对象转为xml格式 //将订单对象转为xml格式
String s = null; String s = null;
...@@ -216,24 +204,23 @@ public class WxPayUtil { ...@@ -216,24 +204,23 @@ public class WxPayUtil {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return new String(s.getBytes("UTF-8")); assert false;
return new String(s.getBytes(StandardCharsets.UTF_8));
} }
/*** /***
* h5生成统一下单格式的订单,生成一个XML格式的字符串 * h5生成统一下单格式的订单,生成一个XML格式的字符串
* @return
* @throws UnsupportedEncodingException
*/ */
public static String createOrderInfoH5(PayPageDto payPageDto,String tradeNo,HttpServletRequest request) throws UnsupportedEncodingException, JsonProcessingException { public static String createOrderInfoH5(String tradeNo, HttpServletRequest request, String appid, String mchid, String key) {
String nonce_str = RandomStringUtils.randomAlphanumeric(16); String nonce_str = RandomStringUtils.randomAlphanumeric(16);
String sceneInfo="{'h5_info':{'type':'WAP','wap_url': 'http://111.203.232.171:8999/','wap_name': '充值'}}"; String sceneInfo="{'h5_info':{'type':'WAP','wap_url': 'http://111.203.232.171:8999/','wap_name': '充值'}}";
String spbill_create_ip = getRealIp(request); String spbill_create_ip = getRealIp(request);
SortedMap<String,String> parameters = new TreeMap<String,String>(); SortedMap<String,String> parameters = new TreeMap<>();
parameters.put("appid", WxCommon.APP_ID); parameters.put("appid", appid);
parameters.put("mch_id", mchId); parameters.put("mch_id", mchid);
parameters.put("body", "充值"); parameters.put("body", "充值");
parameters.put("out_trade_no", tradeNo); parameters.put("out_trade_no", tradeNo);
parameters.put("nonce_str", nonce_str); parameters.put("nonce_str", nonce_str);
...@@ -244,8 +231,7 @@ public class WxPayUtil { ...@@ -244,8 +231,7 @@ public class WxPayUtil {
parameters.put("trade_type","MWEB"); parameters.put("trade_type","MWEB");
parameters.put("scene_info",sceneInfo); parameters.put("scene_info",sceneInfo);
parameters.put("spbill_create_ip",spbill_create_ip); parameters.put("spbill_create_ip",spbill_create_ip);
String characterEncoding = "UTF-8"; String sign = createSign(parameters,key);
String sign = createSign(characterEncoding,parameters);
parameters.put("sign", sign);//签名 parameters.put("sign", sign);//签名
//将订单对象转为xml格式 //将订单对象转为xml格式
String s = null; String s = null;
...@@ -254,22 +240,19 @@ public class WxPayUtil { ...@@ -254,22 +240,19 @@ public class WxPayUtil {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return new String(s.getBytes("UTF-8")); assert false;
return new String(s.getBytes(StandardCharsets.UTF_8));
} }
/*** /***
* 小程序生成统一下单格式的订单,生成一个XML格式的字符串 * 小程序生成统一下单格式的订单,生成一个XML格式的字符串
* @return
* @throws UnsupportedEncodingException
*/ */
public static String createOrderInfoWx(PayPageDto payPageDto,String tradeNo,HttpServletRequest request,String nonce_str) throws UnsupportedEncodingException, JsonProcessingException { public static String createOrderInfoWx(PayPageDto payPageDto,String tradeNo,String nonce_str,String Applets_ID,String mchid,String key) {
SortedMap<String,String> parameters = new TreeMap<>();
SortedMap<String,String> parameters = new TreeMap<String,String>(); parameters.put("appid", Applets_ID);
parameters.put("appid", WxCommon.APP_ID); parameters.put("mch_id", mchid);
parameters.put("mch_id", mchId);
parameters.put("body", "充值"); parameters.put("body", "充值");
parameters.put("description","充值");
parameters.put("out_trade_no", tradeNo); parameters.put("out_trade_no", tradeNo);
parameters.put("notify_url",WxCommon.NOTIFY_URL); parameters.put("notify_url",WxCommon.NOTIFY_URL);
parameters.put("nonce_str", nonce_str); parameters.put("nonce_str", nonce_str);
...@@ -277,8 +260,9 @@ public class WxPayUtil { ...@@ -277,8 +260,9 @@ public class WxPayUtil {
//parameters.put("total_fee", df.format(payPageDto.getTotal()*100)); //parameters.put("total_fee", df.format(payPageDto.getTotal()*100));
parameters.put("total_fee", df.format(1)); parameters.put("total_fee", df.format(1));
parameters.put("trade_type","JSAPI"); parameters.put("trade_type","JSAPI");
String characterEncoding = "UTF-8"; parameters.put("openid", payPageDto.getOpenid());
String sign = createSign(characterEncoding,parameters); parameters.put("spbill_create_ip","0.0.0.0");
String sign = createSign(parameters,key);
parameters.put("sign", sign);//签名 parameters.put("sign", sign);//签名
//将订单对象转为xml格式 //将订单对象转为xml格式
String s = null; String s = null;
...@@ -288,18 +272,18 @@ public class WxPayUtil { ...@@ -288,18 +272,18 @@ public class WxPayUtil {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return new String(s.getBytes("UTF-8")); assert false;
return new String(s.getBytes(StandardCharsets.UTF_8));
} }
/*** /***
* 生成签名 * 生成签名
* @param characterEncoding
* @param parameters
* @return
*/ */
public static String createSign(String characterEncoding, SortedMap<String,String> parameters) { public static String createSign(SortedMap<String,String> parameters,String key) {
StringBuilder sb = new StringBuilder(); // 多线程访问的情况下需要用StringBuffer // 多线程访问的情况下需要用StringBuffer
Set es = parameters.keySet(); // 所有参与传参的key按照accsii排序(升序) StringBuilder sb = new StringBuilder();
// 所有参与传参的key按照accsii排序(升序)
Set es = parameters.keySet();
for (Object set : es) { for (Object set : es) {
String k = set.toString(); String k = set.toString();
Object v = parameters.get(k); Object v = parameters.get(k);
...@@ -309,15 +293,13 @@ public class WxPayUtil { ...@@ -309,15 +293,13 @@ public class WxPayUtil {
.append("&"); .append("&");
} }
sb.append("key=") sb.append("key=")
.append(WxCommon.PRIVATE_KEY); .append(key);
return DigestUtils.md5Hex(sb.toString()).toUpperCase(); return DigestUtils.md5Hex(sb.toString()).toUpperCase();
} }
/** /**
* 获取真实ip地址 通过阿帕奇代理的也能获取到真实ip * 获取真实ip地址 通过阿帕奇代理的也能获取到真实ip
* @param request
* @return
*/ */
public static String getRealIp(HttpServletRequest request) { public static String getRealIp(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for"); String ip = request.getHeader("x-forwarded-for");
...@@ -335,8 +317,6 @@ public class WxPayUtil { ...@@ -335,8 +317,6 @@ public class WxPayUtil {
/** /**
* 设置微信二维码失效时间,并返回具体失效的时间点 * 设置微信二维码失效时间,并返回具体失效的时间点
* @param expire 二维码的有效时间,单位是毫秒
* @return
*/ */
public static String getOrderExpireTime(Long expire){ public static String getOrderExpireTime(Long expire){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
......
...@@ -32,6 +32,7 @@ import java.security.NoSuchAlgorithmException; ...@@ -32,6 +32,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.SignatureException; import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException; import java.security.spec.InvalidKeySpecException;
import java.util.Map; import java.util.Map;
import java.util.SortedMap;
/** /**
* @author 86187 * @author 86187
...@@ -45,8 +46,6 @@ import java.util.Map; ...@@ -45,8 +46,6 @@ import java.util.Map;
@Slf4j @Slf4j
public class PayController { public class PayController {
@Autowired
private WxPayService1 wxPayService;
@Autowired @Autowired
private TestWxPayService testWxPayService; private TestWxPayService testWxPayService;
...@@ -115,7 +114,7 @@ public class PayController { ...@@ -115,7 +114,7 @@ public class PayController {
@ApiOperation(value = "小程序-统一下单", notes = "小程序-统一下单", httpMethod = "POST") @ApiOperation(value = "小程序-统一下单", notes = "小程序-统一下单", httpMethod = "POST")
@ApiImplicitParam(name = "payPageDto", value = "支付入参", dataType = "PayPageDto") @ApiImplicitParam(name = "payPageDto", value = "支付入参", dataType = "PayPageDto")
@PostMapping("/WxPayApplets") @PostMapping("/WxPayApplets")
public R<String> WxPayApplets(@RequestBody PayPageDto payPageDto,HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException { public R<SortedMap<String, String>> WxPayApplets(@RequestBody PayPageDto payPageDto, HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException {
return testWxPayService.WxPayApplets(payPageDto,request, response); return testWxPayService.WxPayApplets(payPageDto,request, response);
} }
/** /**
......
...@@ -3,8 +3,8 @@ spring: ...@@ -3,8 +3,8 @@ spring:
type: com.alibaba.druid.pool.DruidDataSource type: com.alibaba.druid.pool.DruidDataSource
druid: druid:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.110.165:3306/volunteer?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8 #url: jdbc:mysql://192.168.110.165:3306/volunteer?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8
#url: jdbc:mysql://localhost:3306/volunteer?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8 url: jdbc:mysql://localhost:3306/volunteer?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8
username: root username: root
password: adm4HYservice$ password: adm4HYservice$
initial-size: 10 initial-size: 10
...@@ -74,8 +74,10 @@ aliPay: ...@@ -74,8 +74,10 @@ aliPay:
APP_ID: 2021002126609937 APP_ID: 2021002126609937
ALIPAY_PUBLIC_KEY: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2cUDFyw7sLpTpICDURNefRfzr7I7qO5Spq6HwdyFbVv5m6wVzt3fXYTcDrVUuzfbV7+rrXqnvk7LNOp10qPtUyHyZKO2jTBBcUK5H0RPNzs1XInKaSTAznGH8dEAk6yQiwhSDpQzwoOq1Ool2MQocdkcBWwAMEcEUByIbJ4ABsrwXdmkgu2hDHR/GgLvarUp1zOXfZkaTK//L1GUI5uCI+/omcqMPDIOwd8DfcNr0TKlXfAEixp9eVuU/b09b7vW7O9Vh+w0gmy5FN+BWX/JaRRL1DhRqbHl7Sl4/oGu1yjfQA5U8jXBgd+QjwdUiAXVwPdESS/AUbPWE517eH5FtQIDAQAB ALIPAY_PUBLIC_KEY: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2cUDFyw7sLpTpICDURNefRfzr7I7qO5Spq6HwdyFbVv5m6wVzt3fXYTcDrVUuzfbV7+rrXqnvk7LNOp10qPtUyHyZKO2jTBBcUK5H0RPNzs1XInKaSTAznGH8dEAk6yQiwhSDpQzwoOq1Ool2MQocdkcBWwAMEcEUByIbJ4ABsrwXdmkgu2hDHR/GgLvarUp1zOXfZkaTK//L1GUI5uCI+/omcqMPDIOwd8DfcNr0TKlXfAEixp9eVuU/b09b7vW7O9Vh+w0gmy5FN+BWX/JaRRL1DhRqbHl7Sl4/oGu1yjfQA5U8jXBgd+QjwdUiAXVwPdESS/AUbPWE517eH5FtQIDAQAB
PRIVATE_KEY: MIIEvQIBADANBgkqhkiG9w0BAQEFA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
#微信支付appid、mchid、密钥 #微信支付appid、mchid、密钥、小程序appid
wxPay: wxPay:
APP_ID: wx7d8f4502a2bfa865 APP_ID: wx7d8f4502a2bfa865
MCH_ID: 1606042985 MCH_ID: 1606042985
PRIVATE_KEY: a72e174a8c7f903bc4e5ae3c021eb552 PRIVATE_KEY: a72e174a8c7f903bc4e5ae3c021eb552
Applets_ID: wx9390a17892607f6c
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment