杨杰 1 year ago
parent
commit
f3234cdeb3

+ 8 - 8
src/main/java/com/llisoft/pay/common/Constant.java

@@ -7,22 +7,22 @@ package com.llisoft.pay.common;
 public class Constant {
 	
 	// 支付状态 未支付
-	public static final byte STATUS_WAIT = 1;
+	public static final int STATUS_WAIT = 1;
 	// 支付状态 已支付
-	public static final byte STATUS_PAYED = 2;
+	public static final int STATUS_PAYED = 2;
 	
 	// 支付类型 支付宝
-	public static final byte TYPE_ALI = 1;
+	public static final int TYPE_ALI = 1;
 	// 支付类型 微信
-	public static final byte TYPE_WX = 2;
+	public static final int TYPE_WX = 2;
 	
 	// 支付标记 - 异步通知 
-	public static final byte FLAG_NOTIFY = 1;
+	public static final int FLAG_NOTIFY = 1;
 	// 支付标记 - 同步回调 
-	public static final byte FLAG_RETURN = 2;
+	public static final int FLAG_RETURN = 2;
 	// 支付标记 - 主动查询 
-	public static final byte FLAG_QUERY = 3;
+	public static final int FLAG_QUERY = 3;
 	// 支付标记 - 对账 
-	public static final byte FLAG_CHECK = 4;
+	public static final int FLAG_CHECK = 4;
 	
 }

+ 5 - 5
src/main/java/com/llisoft/pay/controller/ApiController.java

@@ -31,13 +31,12 @@ public class ApiController{
 	private ApiService apiService;
 	
 	
-	@ApiOperation(value="查询 公用")
-	@PostMapping("/info")
-	public ResponseVo<ApiInfoResponseVo> info(@Valid @RequestBody ApiInfoRequestVo requestVo) throws Exception{
-		return ResponseVo.success(apiService.info(requestVo.getPid()));
+	@ApiOperation(value="青题库 微信支付 APP")
+	@PostMapping("/qingtiku/wx/info")
+	public ResponseVo<ApiInfoResponseVo> qingtikuWxInfo(@Valid @RequestBody ApiInfoRequestVo requestVo) throws Exception{
+		return ResponseVo.success(apiService.wxInfoQinggu(requestVo.getPid()));
 	}
 	
-	
 	@ApiOperation(value="青题库 微信支付 APP")
 	@PostMapping("/qingtiku/wx/app")
 	public ResponseVo<ApiPayWxAppResponseVo> qingtikuWxApp(@Valid @RequestBody ApiPayRequestVo requestVo) throws Exception{
@@ -45,6 +44,7 @@ public class ApiController{
 		return ResponseVo.success(apiService.wxAppQinggu(appid, requestVo.getMoney(), requestVo.getTitle()));
 	}
 	
+	
 	@ApiOperation(value="青题库 支付宝 APP")
 	@PostMapping("/qingtiku/ali/app")
 	public ResponseVo<ApiPayAliAppResponseVo> qingtikuAliApp(@Valid @RequestBody ApiPayRequestVo requestVo) throws Exception{

+ 6 - 15
src/main/java/com/llisoft/pay/controller/CallbackWxPayController.java

@@ -1,21 +1,16 @@
 package com.llisoft.pay.controller;
 
-import java.nio.charset.StandardCharsets;
-import java.util.Map;
-
 import javax.servlet.http.HttpServletRequest;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Controller;
-import org.springframework.util.StreamUtils;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.ResponseBody;
 
-import com.llisoft.pay.service.PayWxService;
-import com.llisoft.pay.util.XmlUtil;
+import com.llisoft.pay.service.PayWxV3Service;
 
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -28,18 +23,14 @@ public class CallbackWxPayController {
 	private Logger logger = LoggerFactory.getLogger(CallbackWxPayController.class);
 
 	@Autowired
-	private PayWxService wxPayService;
+	private PayWxV3Service payWxV3Service;
 	
 	
 	@ApiOperation(value="异步通知")
 	@PostMapping(value="/notify")
-	public @ResponseBody String notify(HttpServletRequest request) throws Exception {
-		// https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7&index=8
-		String xml = StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8);
-		logger.debug("收到微信异步通知: {}", xml);
-		Map<String, String> resultMap = XmlUtil.toObject(xml, Map.class, String.class, String.class);
-		logger.debug("解析异步通知内容: {}", resultMap);
-		return wxPayService.dispose(resultMap) ? "success" : "";
+	public ResponseEntity.BodyBuilder notify(HttpServletRequest request) throws Exception {
+		logger.debug("收到微信支付异步通知: {}", "");
+		return ResponseEntity.badRequest();
 	}
 	
 }

+ 2 - 2
src/main/java/com/llisoft/pay/dao/PayDao.java

@@ -17,7 +17,7 @@ public interface PayDao {
     @Insert("insert into mta_pay (pid,type,status,money,title,ctime) values (#{pid},#{type},#{status},#{money},#{title},now())")
     boolean insert(Pay item);
 
-    @Update("update mta_pay set status=#{status},flag=#{flag},pnum=#{pnum},ptime=now() where paynum=#{paynum}")
-	boolean update(@Param("paynum")String paynum, @Param("status")byte status, @Param("flag")byte flag, @Param("pnum")String pnum);
+    @Update("update mta_pay set status=#{status},flag=#{flag},ptime=now() where pid=#{pid}")
+	boolean update(@Param("pid")String pid, @Param("status")int status, @Param("flag")int flag);
 	
 }

+ 0 - 13
src/main/java/com/llisoft/pay/entity/Pay.java

@@ -48,11 +48,6 @@ public class Pay {
     private Date pime;
 
     /**
-     * 第三方交易号(由第三方通知返回)
-     */
-    private String pnum;
-
-    /**
      * 创建时间
      */
     private Date ctime;
@@ -126,14 +121,6 @@ public class Pay {
 		this.pime = pime;
 	}
 
-	public String getPnum() {
-		return pnum;
-	}
-
-	public void setPnum(String pnum) {
-		this.pnum = pnum;
-	}
-
 	public Date getCtime() {
 		return ctime;
 	}

+ 52 - 20
src/main/java/com/llisoft/pay/service/ApiService.java

@@ -26,51 +26,70 @@ public class ApiService {
 	private PayDao payDao;
 	@Autowired
 	private PayWxV3Service payWxV3Service;
+
 	
+	/**
+	 * 微信支付 APP 栋科
+	 * @return
+	 * @throws Exception 
+	 */
+	public ApiPayWxAppResponseVo wxAppDongke(String appid, int money, String title) throws Exception {
+		Pay pay = this.add(appid, money, title);
+		logger.debug("添加支付记录:{}", JsonUtil.toJson(pay));
+		return payWxV3Service.appDongke(appid, pay.getPid(), money, title);
+	}
 	
 	/**
-	 * 查询
+	 * 微信支付 APP 青谷
 	 * @return
+	 * @throws Exception 
 	 */
-	public ApiInfoResponseVo info(String pid) {
-		Pay pay = payDao.selectByPid(pid);
-		if(Objects.isNull(pay)) {
-			throw MtaException.build("支付记录不存在:" + pid);
-		}
-		ApiInfoResponseVo responseVo = new ApiInfoResponseVo();
-		responseVo.setPid(pay.getPid());
-		responseVo.setPayed(pay.getStatus() == Constant.STATUS_PAYED);
-		return responseVo;
+	public ApiPayWxAppResponseVo wxAppQinggu(String appid, int money, String title) throws Exception {
+		Pay pay = this.add(appid, money, title);
+		return payWxV3Service.appQinggu(appid, pay.getPid(), money, title);
 	}
 	
+	
 	/**
-	 * 支付 栋科
+	 * 微信查询 栋科
 	 * @return
 	 * @throws Exception 
 	 */
-	public ApiPayWxAppResponseVo wxAppDongke(String appid, int money, String title) throws Exception {
-		Pay pay = this.pay(appid, money, title);
-		logger.debug("添加支付记录:{}", JsonUtil.toJson(pay));
-		return payWxV3Service.appDongke(appid, pay.getPid(), money, title);
+	public ApiInfoResponseVo wxInfoDongke(String pid) throws Exception {
+		boolean payed = this.payed(pid);
+		if(!payed) { // 待支付订单通过接口查询并更新支付状态
+			if(payWxV3Service.queryDongke(pid)) { // 接口查询已支付
+				payDao.update(pid, Constant.STATUS_PAYED, Constant.FLAG_QUERY);
+				payed = true;
+			}
+		}
+		return ApiInfoResponseVo.Build(pid, payed);
 	}
 	
 	/**
-	 * 支付 青谷
+	 * 微信查询 青谷
 	 * @return
 	 * @throws Exception 
 	 */
-	public ApiPayWxAppResponseVo wxAppQinggu(String appid, int money, String title) throws Exception {
-		Pay pay = this.pay(appid, money, title);
-		return payWxV3Service.appQinggu(appid, pay.getPid(), money, title);
+	public ApiInfoResponseVo wxInfoQinggu(String pid) throws Exception {
+		boolean payed = this.payed(pid);
+		if(!payed) { // 待支付订单通过接口查询并更新支付状态
+			if(payWxV3Service.queryQinggu(pid)) { // 接口查询已支付
+				payDao.update(pid, Constant.STATUS_PAYED, Constant.FLAG_QUERY);
+				payed = true;
+			}
+		}
+		return ApiInfoResponseVo.Build(pid, payed);
 	}
 	
 	
+
 	/**
 	 * 添加支付记录
 	 * @return
 	 * @throws Exception 
 	 */
-	private Pay pay(String appid, int money, String title) throws Exception {
+	private Pay add(String appid, int money, String title) throws Exception {
 		Pay pay = new Pay();
 		pay.setMoney(money);
 		pay.setTitle(title);
@@ -82,4 +101,17 @@ public class ApiService {
 		return pay;
 	}
 	
+	/**
+	 * 查询支付记录
+	 * @return
+	 * @throws Exception 
+	 */
+	private boolean payed(String pid) throws Exception {
+		Pay pay = payDao.selectByPid(pid);
+		if(Objects.isNull(pay)) {
+			throw MtaException.build("支付记录不存在:" + pid);
+		}
+		return pay.getStatus() == Constant.STATUS_PAYED;
+	}
+	
 }

+ 29 - 27
src/main/java/com/llisoft/pay/service/PayWxV3Service.java

@@ -29,6 +29,8 @@ public class PayWxV3Service {
 	@Value("${mta.wxpay.keyPathQinggu}")
 	private String keyPathQinggu;
 	
+	private static String callbackWx = "/callback/wxpay/notify";
+	
 	
 	// 商户号
 	private static String mchidDongke = "1570392471";
@@ -54,8 +56,8 @@ public class PayWxV3Service {
 	 * @return
 	 * @throws Exception 
 	 */
-	public String pcDongke(String appid, String paynum, int money, String body) throws Exception {
-		return this.pc(this.getConfigDongke(), appid, mchidDongke, paynum, money, body);
+	public String pcDongke(String appid, String pid, int money, String body) throws Exception {
+		return this.pc(this.getConfigDongke(), appid, mchidDongke, pid, money, body);
 	}
 	
 	/**
@@ -63,8 +65,8 @@ public class PayWxV3Service {
 	 * @return
 	 * @throws Exception 
 	 */
-	public String pcQinggu(String appid, String paynum, int money, String body) throws Exception {
-		return this.pc(this.getConfigQinggu(), appid, mchidQinggu, paynum, money, body);
+	public String pcQinggu(String appid, String pid, int money, String body) throws Exception {
+		return this.pc(this.getConfigQinggu(), appid, mchidQinggu, pid, money, body);
 	}
 	
 	/**
@@ -73,7 +75,7 @@ public class PayWxV3Service {
 	 * @return 返回用于生成二维码的url
 	 * @throws Exception 
 	 */
-	private String pc(Config config, String appid, String mchid, String paynum, int money, String body) throws Exception {
+	private String pc(Config config, String appid, String mchid, String pid, int money, String body) throws Exception {
 		com.wechat.pay.java.service.payments.nativepay.NativePayService service = new com.wechat.pay.java.service.payments.nativepay.NativePayService.Builder().config(config).build();
         com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest request = new com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest();
         com.wechat.pay.java.service.payments.nativepay.model.Amount amount = new com.wechat.pay.java.service.payments.nativepay.model.Amount();
@@ -82,8 +84,8 @@ public class PayWxV3Service {
         request.setAppid(appid);
         request.setMchid(mchid);
         request.setDescription(body);
-        request.setOutTradeNo(paynum);
-        request.setNotifyUrl(domain+"/wxpay/notify");
+        request.setOutTradeNo(pid);
+        request.setNotifyUrl(domain + callbackWx);
         try {
         	logger.debug("微信Native支付下单请求参数: {}", JsonUtil.toJson(request));
         	com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse response = service.prepay(request);
@@ -101,8 +103,8 @@ public class PayWxV3Service {
 	 * @return
 	 * @throws Exception 
 	 */
-	public ApiPayWxAppResponseVo appDongke(String appid, String paynum, int money, String body) throws Exception {
-		return this.app(this.getConfigDongke(), appid, mchidDongke, paynum, money, body);
+	public ApiPayWxAppResponseVo appDongke(String appid, String pid, int money, String body) throws Exception {
+		return this.app(this.getConfigDongke(), appid, mchidDongke, pid, money, body);
 	}
 	
 	/**
@@ -110,15 +112,15 @@ public class PayWxV3Service {
 	 * @return
 	 * @throws Exception 
 	 */
-	public ApiPayWxAppResponseVo appQinggu(String appid, String paynum, int money, String body) throws Exception {
-		return this.app(this.getConfigQinggu(), appid, mchidQinggu, paynum, money, body);
+	public ApiPayWxAppResponseVo appQinggu(String appid, String pid, int money, String body) throws Exception {
+		return this.app(this.getConfigQinggu(), appid, mchidQinggu, pid, money, body);
 	}
 	
 	/**
 	 * App支付是指商户通过在移动端应用App中集成开放SDK调起微信支付模块来完成支付。目前微信支付支持手机系统有:IOS(苹果)、Android(安卓)和WP(Windows Phone)。
 	 * @return
 	 */
-	private ApiPayWxAppResponseVo app(Config config, String appid, String mchid, String paynum, int money, String body) {
+	private ApiPayWxAppResponseVo app(Config config, String appid, String mchid, String pid, int money, String body) {
 		com.wechat.pay.java.service.payments.app.AppServiceExtension service = new com.wechat.pay.java.service.payments.app.AppServiceExtension.Builder().config(config).build();
 		com.wechat.pay.java.service.payments.app.model.PrepayRequest request = new com.wechat.pay.java.service.payments.app.model.PrepayRequest();
 		com.wechat.pay.java.service.payments.app.model.Amount amount = new com.wechat.pay.java.service.payments.app.model.Amount();
@@ -127,8 +129,8 @@ public class PayWxV3Service {
         request.setAppid(appid);
         request.setMchid(mchid);
         request.setDescription(body);
-        request.setOutTradeNo(paynum);
-        request.setNotifyUrl(domain+"/wxpay/notify");
+        request.setOutTradeNo(pid);
+        request.setNotifyUrl(domain + callbackWx);
         try {
         	logger.debug("微信APP支付下单请求参数: {}", JsonUtil.toJson(request));
         	com.wechat.pay.java.service.payments.app.model.PrepayWithRequestPaymentResponse response = service.prepayWithRequestPayment(request);
@@ -148,8 +150,8 @@ public class PayWxV3Service {
 	 * @return
 	 * @throws Exception 
 	 */
-	public ApiPayWxJsApiResponseVo jsapiDongke(String appid, String paynum, int money, String body) throws Exception {
-		return this.jsapi(this.getConfigDongke(), appid, mchidDongke, paynum, money, body);
+	public ApiPayWxJsApiResponseVo jsapiDongke(String appid, String pid, int money, String body) throws Exception {
+		return this.jsapi(this.getConfigDongke(), appid, mchidDongke, pid, money, body);
 	}
 	
 	/**
@@ -157,15 +159,15 @@ public class PayWxV3Service {
 	 * @return
 	 * @throws Exception 
 	 */
-	public ApiPayWxJsApiResponseVo jsapiQinggu(String appid, String paynum, int money, String body) throws Exception {
-		return this.jsapi(this.getConfigQinggu(), appid, mchidQinggu, paynum, money, body);
+	public ApiPayWxJsApiResponseVo jsapiQinggu(String appid, String pid, int money, String body) throws Exception {
+		return this.jsapi(this.getConfigQinggu(), appid, mchidQinggu, pid, money, body);
 	}
 	
 	/**
 	 * JSAPI支付是指商户通过调用微信支付提供的JSAPI接口,在支付场景中调起微信支付模块完成收款。
 	 * @return
 	 */
-	private ApiPayWxJsApiResponseVo jsapi(Config config, String appid, String mchid, String paynum, int money, String body) {
+	private ApiPayWxJsApiResponseVo jsapi(Config config, String appid, String mchid, String pid, int money, String body) {
 		com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension service = new com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension.Builder().config(config).build();
 		com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest request = new com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest();
 		com.wechat.pay.java.service.payments.jsapi.model.Amount amount = new com.wechat.pay.java.service.payments.jsapi.model.Amount();
@@ -174,8 +176,8 @@ public class PayWxV3Service {
         request.setAppid(appid);
         request.setMchid(mchid);
         request.setDescription(body);
-        request.setOutTradeNo(paynum);
-        request.setNotifyUrl(domain+"/wxpay/notify");
+        request.setOutTradeNo(pid);
+        request.setNotifyUrl(domain + callbackWx);
         try {
         	logger.debug("微信JSAPI支付下单请求参数: {}", JsonUtil.toJson(request));
         	com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse response = service.prepayWithRequestPayment(request);
@@ -195,8 +197,8 @@ public class PayWxV3Service {
 	 * @return
 	 * @throws Exception 
 	 */
-	public boolean queryDongke(String appid, String paynum, int money, String body) throws Exception {
-		return this.query(this.getConfigDongke(), mchidDongke, paynum);
+	public boolean queryDongke(String pid) throws Exception {
+		return this.query(this.getConfigDongke(), mchidDongke, pid);
 	}
 	
 	/**
@@ -204,8 +206,8 @@ public class PayWxV3Service {
 	 * @return
 	 * @throws Exception 
 	 */
-	public boolean queryQinggu(String appid, String paynum, int money, String body) throws Exception {
-		return this.query(this.getConfigQinggu(), mchidQinggu, paynum);
+	public boolean queryQinggu(String pid) throws Exception {
+		return this.query(this.getConfigQinggu(), mchidQinggu, pid);
 	}
 	
 	/**
@@ -213,11 +215,11 @@ public class PayWxV3Service {
 	 * @return
 	 * @throws Exception
 	 */
-	public boolean query(Config config, String mchid, String paynum) throws Exception {
+	public boolean query(Config config, String mchid, String pid) throws Exception {
 		com.wechat.pay.java.service.payments.nativepay.NativePayService service = new com.wechat.pay.java.service.payments.nativepay.NativePayService.Builder().config(config).build();
 		com.wechat.pay.java.service.payments.nativepay.model.QueryOrderByOutTradeNoRequest request = new com.wechat.pay.java.service.payments.nativepay.model.QueryOrderByOutTradeNoRequest();
 		request.setMchid(mchid);
-		request.setOutTradeNo(paynum);
+		request.setOutTradeNo(pid);
 		try {
 			logger.debug("微信订单查询api请求参数: {}", JsonUtil.toJson(request));
 			com.wechat.pay.java.service.payments.model.Transaction transaction = service.queryOrderByOutTradeNo(request);

+ 7 - 11
src/main/java/com/llisoft/pay/vo/ApiInfoResponseVo.java

@@ -8,12 +8,16 @@ public class ApiInfoResponseVo {
 
 	@ApiModelProperty("支付号")
     private String pid;
-
-	@ApiModelProperty("支付金额(分)")
-    private int money;
 	
 	@ApiModelProperty("是否已支付")
 	private boolean payed;
+	
+	public static ApiInfoResponseVo Build(String pid, boolean payed) {
+		ApiInfoResponseVo responseVo = new ApiInfoResponseVo();
+		responseVo.setPid(pid);
+		responseVo.setPayed(payed);
+		return responseVo;
+	}
 
 	public String getPid() {
 		return pid;
@@ -23,14 +27,6 @@ public class ApiInfoResponseVo {
 		this.pid = pid;
 	}
 
-	public int getMoney() {
-		return money;
-	}
-
-	public void setMoney(int money) {
-		this.money = money;
-	}
-
 	public boolean getPayed() {
 		return payed;
 	}