|
@@ -1,34 +1,137 @@
|
|
|
package com.hys.app.pay.wx;
|
|
|
|
|
|
-import com.github.binarywang.wxpay.bean.applyment.WxPayApplymentCreateResult;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
|
|
|
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
|
|
|
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
|
|
|
+import com.github.binarywang.wxpay.config.WxPayConfig;
|
|
|
import com.github.binarywang.wxpay.exception.WxPayException;
|
|
|
import com.github.binarywang.wxpay.service.WxPayService;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
|
|
|
+import com.github.binarywang.wxpay.util.SignUtils;
|
|
|
+import com.hys.app.framework.database.annotation.Id;
|
|
|
+import com.hys.app.framework.util.JsonUtil;
|
|
|
+import com.hys.app.pay.ali.PayOrder;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.UnknownHostException;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
public class WxPayServiceMy {
|
|
|
+ @Resource
|
|
|
+ private WxConfig wxConfig;
|
|
|
|
|
|
- @Autowired
|
|
|
- private WxPayService wxPayService;
|
|
|
-
|
|
|
- public WxPayUnifiedOrderResult createOrder(String orderId, String description, int totalFee, String ip, String notifyUrl) throws Exception {
|
|
|
- WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
|
|
|
- request.setOutTradeNo(orderId);
|
|
|
- request.setBody(description);
|
|
|
- request.setTotalFee(totalFee);
|
|
|
- request.setSpbillCreateIp(ip);
|
|
|
- request.setNotifyUrl(notifyUrl);
|
|
|
- request.setTradeType("JSAPI"); // 或者 "NATIVE", "APP" 等
|
|
|
- return wxPayService.unifiedOrder(request);
|
|
|
+ private WxPayService getPayService() {
|
|
|
+ WxPayConfig payConfig = new WxPayConfig();
|
|
|
+ payConfig.setAppId(wxConfig.getAppId());
|
|
|
+ payConfig.setMchId(wxConfig.getMchId());
|
|
|
+ payConfig.setMchKey(wxConfig.getMchKey());
|
|
|
+ payConfig.setKeyPath(wxConfig.getKeyPath());
|
|
|
+ payConfig.setApiV3Key(wxConfig.getApiV3Key());
|
|
|
+ WxPayService wxPayService = new WxPayServiceImpl();
|
|
|
+ wxPayService.setConfig(payConfig);
|
|
|
+ return wxPayService;
|
|
|
}
|
|
|
|
|
|
-// public WxPayApplymentForSubCreateResult registerSubMerchant(WxPayApplymentForSubCreateRequest request) throws WxPayException {
|
|
|
-// // 调用wxPayService的方法执行API调用
|
|
|
-// return wxPayService.createApplymentForSub(request);
|
|
|
-// }
|
|
|
-}
|
|
|
+
|
|
|
+ public Map<String, String> pay(PayOrder payOrder) throws UnknownHostException {
|
|
|
+ WxPayService wxPayService = getPayService();
|
|
|
+
|
|
|
+ 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("JSAPI")
|
|
|
+ .productId(IdUtil.fastUUID())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 设置商户子账户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("paySign", SignUtils.createSign(payData, wxPayService.getConfig().getMchKey(), null, null));
|
|
|
+ return payData;
|
|
|
+ } catch (WxPayException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getAccessToken(String appId, String secret) {
|
|
|
+ String accessToken = "";
|
|
|
+ try {
|
|
|
+ String authString = appId + ":" + secret;
|
|
|
+ byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
|
|
|
+ String authStringEnc = new String(authEncBytes);
|
|
|
+
|
|
|
+ URL url = new URL("https://api.weixin.qq.com/sns/oauth2/access_token");
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
|
|
|
+
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
+ String inputLine;
|
|
|
+ StringBuffer response = new StringBuffer();
|
|
|
+
|
|
|
+ while ((inputLine = in.readLine()) != null) {
|
|
|
+ response.append(inputLine);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+
|
|
|
+ accessToken = response.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getOpenId(String accessToken) {
|
|
|
+ String openId = "";
|
|
|
+ try {
|
|
|
+ URL url = new URL("https://api.weixin.qq.com/sns/oauth2/access_token"+accessToken);
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
+ String inputLine;
|
|
|
+ StringBuffer response = new StringBuffer();
|
|
|
+
|
|
|
+ while ((inputLine = in.readLine()) != null) {
|
|
|
+ response.append(inputLine);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ LinkedHashMap<String, Object> map = JsonUtil.toMap(response.toString());
|
|
|
+ openId = map.get("openid").toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return openId;
|
|
|
+ }
|
|
|
+}
|