|
@@ -54,48 +54,87 @@ public class WxPayServiceMy {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public Map<String, String> pay(PayOrder payOrder) throws UnknownHostException {
|
|
|
+ public Object pay(PayOrder payOrder) throws UnknownHostException, WxPayException {
|
|
|
WxPayService wxPayService = getPayService();
|
|
|
-
|
|
|
- WxPayUnifiedOrderRequest orderRequest = WxPayUnifiedOrderRequest.newBuilder()
|
|
|
+ //H5
|
|
|
+ WxPayUnifiedOrderRequest build = WxPayUnifiedOrderRequest.newBuilder()
|
|
|
.body(payOrder.getSubject())
|
|
|
- .openid(payOrder.getOpenId())
|
|
|
.outTradeNo(payOrder.getTraceNo())
|
|
|
- .totalFee((int) Math.round(payOrder.getTotalAmount() * 100)) // 单位是分
|
|
|
+ .feeType("CNY")
|
|
|
+ .totalFee((int) Math.round(payOrder.getTotalAmount() * 100))
|
|
|
.spbillCreateIp(InetAddress.getLocalHost().getHostAddress())
|
|
|
.notifyUrl(wxConfig.getNotifyUrl())
|
|
|
- .tradeType("JSAPI")
|
|
|
- .productId(IdUtil.fastUUID())
|
|
|
+ .tradeType("MWEB")
|
|
|
.build();
|
|
|
+ // // 设置商户子账户id
|
|
|
+ build.setSubMchId(wxConfig.getSubMchId());
|
|
|
+ WxPayUnifiedOrderResult wxPayUnifiedOrderResult = wxPayService.unifiedOrder(build);
|
|
|
+ return wxPayUnifiedOrderResult.getMwebUrl(); // 包含 mweb_url
|
|
|
|
|
|
- // 设置商户子账户id
|
|
|
- orderRequest.setSubMchId(wxConfig.getSubMchId());
|
|
|
- try {
|
|
|
- WxPayUnifiedOrderResult result = wxPayService.unifiedOrder(orderRequest);
|
|
|
- Map<String, String> payData = new HashMap<>();
|
|
|
- payData.put("appId", wxConfig.getAppId());
|
|
|
- payData.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
|
|
|
- payData.put("nonceStr", result.getNonceStr());
|
|
|
- payData.put("package", "prepay_id=" + result.getPrepayId());
|
|
|
- payData.put("signType", "MD5");
|
|
|
- payData.put("openid", payOrder.getOpenId());
|
|
|
- payData.put("paySign", SignUtils.createSign(payData, wxPayService.getConfig().getMchKey(), null, null));
|
|
|
- System.out.println(payData.toString());
|
|
|
- return payData;
|
|
|
- } catch (WxPayException e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
+
|
|
|
+// WxPayUnifiedOrderRequest orderRequest = WxPayUnifiedOrderRequest.newBuilder()
|
|
|
+// .body(payOrder.getSubject())
|
|
|
+// .outTradeNo(payOrder.getTraceNo())
|
|
|
+// .totalFee((int) Math.round (payOrder.getTotalAmount()*100)) // 单位是分
|
|
|
+// .spbillCreateIp(InetAddress.getLocalHost().getHostAddress())
|
|
|
+// .notifyUrl(wxConfig.getNotifyUrl())
|
|
|
+// .tradeType("NATIVE")
|
|
|
+// .productId(IdUtil.fastUUID())
|
|
|
+// .build();
|
|
|
+// // 设置商户子账户id
|
|
|
+// orderRequest.setSubMchId(wxConfig.getSubMchId()); //
|
|
|
+// try {
|
|
|
+// WxPayUnifiedOrderResult result = wxPayService.unifiedOrder(orderRequest);
|
|
|
+// return result.getCodeURL(); // 获取生成的二维码URL
|
|
|
+// } catch (WxPayException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+// WxPayService wxPayService = getPayService();
|
|
|
+//
|
|
|
+// WxPayUnifiedOrderRequest orderRequest = WxPayUnifiedOrderRequest.newBuilder()
|
|
|
+// .body(payOrder.getSubject())
|
|
|
+// .openid(payOrder.getOpenId())
|
|
|
+// .outTradeNo(payOrder.getTraceNo())
|
|
|
+// .totalFee((int) Math.round(payOrder.getTotalAmount() * 100)) // 单位是分
|
|
|
+// .spbillCreateIp(InetAddress.getLocalHost().getHostAddress())
|
|
|
+// .notifyUrl(wxConfig.getNotifyUrl())
|
|
|
+// .tradeType("JSAPI")
|
|
|
+// .productId(IdUtil.fastUUID())
|
|
|
+// .build();
|
|
|
+//
|
|
|
+//
|
|
|
+// wxPayService.createOrder(orderRequest)
|
|
|
+//
|
|
|
+// // 设置商户子账户id
|
|
|
+// orderRequest.setSubMchId(wxConfig.getSubMchId());
|
|
|
+// try {
|
|
|
+// WxPayUnifiedOrderResult result = wxPayService.unifiedOrder(orderRequest);
|
|
|
+// Map<String, String> payData = new HashMap<>();
|
|
|
+// payData.put("appId", wxConfig.getAppId());
|
|
|
+// payData.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
|
|
|
+// payData.put("nonceStr", result.getNonceStr());
|
|
|
+// payData.put("package", "prepay_id=" + result.getPrepayId());
|
|
|
+// payData.put("signType", "MD5");
|
|
|
+// payData.put("openid", payOrder.getOpenId());
|
|
|
+// payData.put("paySign", SignUtils.createSign(payData, wxPayService.getConfig().getMchKey(), null, null));
|
|
|
+// System.out.println(payData.toString());
|
|
|
+// return payData;
|
|
|
+// } catch (WxPayException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// return null;
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
- public String getOpenIdCode(Long orderId,String ip) throws UnsupportedEncodingException {
|
|
|
+ public String getOpenIdCode(Long orderId, String ip) throws UnsupportedEncodingException {
|
|
|
String oAuth2Url = wxConfig.getOAuth2Url();
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
- map.put("orderId",orderId);
|
|
|
- map.put("ip",ip);
|
|
|
+ map.put("orderId", orderId);
|
|
|
+ map.put("ip", ip);
|
|
|
return String.format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect",
|
|
|
- wxConfig.getAppId(), URLEncoder.encode(wxConfig.getOAuth2Url(), "UTF-8"), "snsapi_base", JSONUtil.toJsonStr(map));
|
|
|
-
|
|
|
+ wxConfig.getAppId(), URLEncoder.encode(wxConfig.getOAuth2Url(), "UTF-8"), "snsapi_base", orderId+"--"+ip);
|
|
|
}
|
|
|
|
|
|
public String getOpenId(String code) {
|