浏览代码

微信支付

傅行帆 6 年前
父节点
当前提交
cbe4b690b2

+ 6
- 6
CODE/smart-community/app-api/src/main/java/com/community/huiju/common/wxpay/WXPay.java 查看文件

@@ -40,12 +40,12 @@ public class WXPay {
40 40
         this.notifyUrl = notifyUrl;
41 41
         this.autoReport = autoReport;
42 42
         this.useSandbox = useSandbox;
43
-        if (useSandbox) {
44
-            this.signType = SignType.MD5; // 沙箱环境unifiedOrder
45
-        }
46
-        else {
47
-            this.signType = SignType.HMACSHA256;
48
-        }
43
+        //if (useSandbox) {
44
+            this.signType = SignType.MD5;
45
+        //}
46
+        //else {
47
+        //    this.signType = SignType.HMACSHA256;
48
+        //}
49 49
         this.wxPayRequest = new WXPayRequest(config);
50 50
     }
51 51
 

+ 46
- 14
CODE/smart-community/app-api/src/main/java/com/community/huiju/controller/WxPayController.java 查看文件

@@ -1,11 +1,17 @@
1 1
 package com.community.huiju.controller;
2 2
 
3 3
 import com.community.commom.mode.ResponseBean;
4
+import com.community.commom.redis.RedisTemplate;
4 5
 import com.community.huiju.common.wxpay.HfWxConfig;
5 6
 import com.community.huiju.common.wxpay.WXPay;
7
+import com.community.huiju.service.TaVistorServiceI;
8
+import com.community.huiju.service.WxPayServiceI;
6 9
 import com.google.common.collect.Maps;
7 10
 import io.swagger.annotations.Api;
8 11
 import io.swagger.annotations.ApiOperation;
12
+import org.slf4j.Logger;
13
+import org.slf4j.LoggerFactory;
14
+import org.springframework.beans.factory.annotation.Autowired;
9 15
 import org.springframework.cloud.context.config.annotation.RefreshScope;
10 16
 import org.springframework.web.bind.annotation.PathVariable;
11 17
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -24,29 +30,55 @@ import java.util.Map;
24 30
 @RequestMapping("/")
25 31
 @Api(value = "微信支付 API", description = "微信支付 API")
26 32
 public class WxPayController {
33
+	private Logger log = LoggerFactory.getLogger(WxPayController.class);
34
+	
35
+	@Autowired
36
+	private WxPayServiceI wxPayService;
27 37
 	
28 38
 	@ApiOperation(value = "统一下单", notes = "统一下单")
29 39
 	@RequestMapping(value = "/wxUnifiedOrder",method = RequestMethod.GET)
30 40
 	public ResponseBean wxUnifiedOrder(){
31 41
 		ResponseBean responseBean = new ResponseBean();
32
-		HfWxConfig config = new HfWxConfig();
42
+		
43
+		Map<String, String> resp = Maps.newHashMap();
44
+		try {
45
+			resp = wxPayService.wxUnifiedOrder();
46
+		} catch (Exception e) {
47
+			e.printStackTrace();
48
+			responseBean.addError("下单失败");
49
+			return responseBean;
50
+		}
51
+		log.info("下单成功: {}",resp);
52
+		responseBean.addSuccess(resp);
53
+		return responseBean;
54
+	}
55
+	
56
+	@ApiOperation(value = "下载对账单", notes = "下载对账单")
57
+	@RequestMapping(value = "/wxDownloadBill",method = RequestMethod.GET)
58
+	public ResponseBean wxDownloadBill(){
59
+		ResponseBean responseBean = new ResponseBean();
60
+		
61
+		Map<String, String> resp = Maps.newHashMap();
62
+		try {
63
+			resp = wxPayService.wxDownloadBill();
64
+		} catch (Exception e) {
65
+			e.printStackTrace();
66
+		}
67
+		responseBean.addSuccess(resp);
68
+		return responseBean;
69
+	}
70
+	
71
+	@ApiOperation(value = "订单查询", notes = "订单查询")
72
+	@RequestMapping(value = "/wxOrderQuery",method = RequestMethod.GET)
73
+	public ResponseBean wxOrderQuery(){
74
+		ResponseBean responseBean = new ResponseBean();
33 75
 		Map<String, String> resp = Maps.newHashMap();
34 76
 		try {
35
-			WXPay wxpay = new WXPay(config);
36
-			
37
-			Map<String, String> data = new HashMap<String, String>();
38
-			data.put("body", "支付测试");
39
-			data.put("out_trade_no", "2019021910595900000012");
40
-			data.put("device_info", "");
41
-			data.put("fee_type", "CNY");
42
-			data.put("total_fee", "1");
43
-			data.put("spbill_create_ip", "123.12.12.123");
44
-			data.put("notify_url", "http://www.example.com/wxpay/notify");
45
-			// 此处指定为APP支付
46
-			data.put("trade_type", "APP");
47
-			resp = wxpay.unifiedOrder(data);
77
+			resp = wxPayService.wxOrderQuery();
48 78
 		} catch (Exception e) {
49 79
 			e.printStackTrace();
80
+			responseBean.addError("查询失败");
81
+			return responseBean;
50 82
 		}
51 83
 		responseBean.addSuccess(resp);
52 84
 		return responseBean;

+ 23
- 0
CODE/smart-community/app-api/src/main/java/com/community/huiju/service/WxPayServiceI.java 查看文件

@@ -0,0 +1,23 @@
1
+package com.community.huiju.service;
2
+
3
+import java.util.Map;
4
+
5
+public interface WxPayServiceI {
6
+	/**
7
+	 * 微信支付统一下单
8
+	 * @return
9
+	 */
10
+	Map<String, String> wxUnifiedOrder() throws Exception;
11
+	
12
+	/**
13
+	 * 下载对账单
14
+	 * @return
15
+	 */
16
+	Map<String, String> wxDownloadBill() throws Exception;
17
+	
18
+	/**
19
+	 * 订单查询
20
+	 * @return
21
+	 */
22
+	Map<String, String> wxOrderQuery() throws Exception;
23
+}

+ 96
- 0
CODE/smart-community/app-api/src/main/java/com/community/huiju/service/impl/WxPayServiceImpl.java 查看文件

@@ -0,0 +1,96 @@
1
+package com.community.huiju.service.impl;
2
+
3
+import com.community.huiju.common.wxpay.HfWxConfig;
4
+import com.community.huiju.common.wxpay.WXPay;
5
+import com.community.huiju.common.wxpay.WXPayConstants;
6
+import com.community.huiju.common.wxpay.WXPayUtil;
7
+import com.community.huiju.dao.TpBillInvoiceMapper;
8
+import com.community.huiju.service.BillServiceI;
9
+import com.community.huiju.service.WxPayServiceI;
10
+import com.github.pagehelper.Page;
11
+import com.github.pagehelper.PageHelper;
12
+import com.google.common.collect.Maps;
13
+import org.springframework.beans.factory.annotation.Autowired;
14
+import org.springframework.stereotype.Service;
15
+
16
+import java.time.Instant;
17
+import java.util.HashMap;
18
+import java.util.List;
19
+import java.util.Map;
20
+
21
+/**
22
+ * @author FXF
23
+ * @date 2018-10-25
24
+ */
25
+@Service("wxPayService")
26
+public class WxPayServiceImpl implements WxPayServiceI {
27
+	/**
28
+	 * 微信支付统一下单
29
+	 *
30
+	 * @return
31
+	 */
32
+	@Override
33
+	public Map<String, String> wxUnifiedOrder() throws Exception {
34
+		HfWxConfig config = new HfWxConfig();
35
+		WXPay wxpay = new WXPay(config);
36
+		
37
+		Map<String, String> data = new HashMap<String, String>();
38
+		data.put("body", "支付测试");
39
+		//商品号唯一
40
+		data.put("out_trade_no", "2019021910595900001116");
41
+		data.put("device_info", "");
42
+		data.put("fee_type", "CNY");
43
+		//金额 后台计算
44
+		data.put("total_fee", "1");
45
+		//终端IP
46
+		data.put("spbill_create_ip", "123.12.12.123");
47
+		//回调地址
48
+		data.put("notify_url", "http://106.14.20.193:8086/app-api/wxDownloadBill");
49
+		// 此处指定为APP支付
50
+		data.put("trade_type", "APP");
51
+		Map<String, String> resp = wxpay.unifiedOrder(data);
52
+		
53
+		//第二次重新生成签名
54
+		Map<String, String> secondSignData = new HashMap<String, String>();
55
+		long timestamp = Instant.now().getEpochSecond();
56
+		secondSignData.put("appid", config.getAppID());
57
+		secondSignData.put("partnerid", config.getMchID());
58
+		secondSignData.put("prepayid", resp.get("prepay_id"));
59
+		secondSignData.put("noncestr", resp.get("nonce_str"));
60
+		secondSignData.put("timestamp", String.valueOf(timestamp));
61
+		secondSignData.put("package", "Sign=WXPay");
62
+		secondSignData.put("sign", WXPayUtil.generateSignature(secondSignData, config.getKey(), WXPayConstants.SignType.MD5));
63
+		return secondSignData;
64
+	}
65
+	
66
+	/**
67
+	 * 下载对账单
68
+	 *
69
+	 * @return
70
+	 */
71
+	@Override
72
+	public Map<String, String> wxDownloadBill() throws Exception {
73
+		HfWxConfig config = new HfWxConfig();
74
+		WXPay wxpay = new WXPay(config);
75
+		
76
+		Map<String, String> data = new HashMap<String, String>();
77
+		data.put("bill_date", "20190219");
78
+		data.put("bill_type", "ALL");
79
+		return wxpay.downloadBill(data);
80
+	}
81
+	
82
+	/**
83
+	 * 订单查询
84
+	 *
85
+	 * @return
86
+	 */
87
+	@Override
88
+	public Map<String, String> wxOrderQuery() throws Exception {
89
+		HfWxConfig config = new HfWxConfig();
90
+		WXPay wxpay = new WXPay(config);
91
+		
92
+		Map<String, String> data = new HashMap<String, String>();
93
+		data.put("out_trade_no", "2019021910595900000012");
94
+		return wxpay.orderQuery(data);
95
+	}
96
+}