|
@@ -0,0 +1,151 @@
|
|
|
+package com.ruoyi.app.pay;
|
|
|
+
|
|
|
+import com.ijpay.core.enums.SignType;
|
|
|
+import com.ijpay.core.enums.TradeType;
|
|
|
+import com.ijpay.core.kit.HttpKit;
|
|
|
+import com.ijpay.core.kit.WxPayKit;
|
|
|
+import com.ijpay.wxpay.WxPayApi;
|
|
|
+import com.ijpay.wxpay.WxPayApiConfig;
|
|
|
+import com.ijpay.wxpay.WxPayApiConfigKit;
|
|
|
+import com.ijpay.wxpay.model.UnifiedOrderModel;
|
|
|
+import com.ruoyi.app.annotation.PassToken;
|
|
|
+import com.ruoyi.app.shop.orders.domain.TbOrders;
|
|
|
+import com.ruoyi.app.shop.orders.service.ITbOrdersService;
|
|
|
+import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 微信支付
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/app/wxPay")
|
|
|
+public class WxPayController extends AbstractWxPayApiController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ WxPayBean wxPayBean;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ITbOrdersService service;
|
|
|
+
|
|
|
+ private String notifyUrl; //支付通知
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public WxPayApiConfig getApiConfig() {
|
|
|
+ WxPayApiConfig apiConfig;
|
|
|
+ try {
|
|
|
+ apiConfig = WxPayApiConfigKit.getApiConfig(wxPayBean.getAppId());
|
|
|
+ } catch (Exception e) {
|
|
|
+ apiConfig = WxPayApiConfig.builder()
|
|
|
+ .appId(wxPayBean.getAppId())
|
|
|
+ .mchId(wxPayBean.getMchId())
|
|
|
+ .partnerKey(wxPayBean.getPartnerKey())
|
|
|
+ .certPath(wxPayBean.getCertPath())
|
|
|
+ .domain(wxPayBean.getDomain())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+ notifyUrl = apiConfig.getDomain().concat("app/wxPay/notifyUrl");
|
|
|
+ return apiConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信APP支付
|
|
|
+ */
|
|
|
+ @PostMapping(value = "/appPay")
|
|
|
+ public AjaxResult appPay(@RequestBody TbOrders order) {
|
|
|
+ if (order == null) {
|
|
|
+ return AjaxResult.error("订单无效");
|
|
|
+ }
|
|
|
+ //用户第一次未支付成功,去我的订单支付,验证订单的有效性
|
|
|
+ TbOrders check = service.selectTbOrdersByOrderNum(order.getOrderNum());
|
|
|
+ if (check == null) {
|
|
|
+ order.setPayType(0);
|
|
|
+ order.setOrderNum(WxPayKit.generateStr());
|
|
|
+ } else {
|
|
|
+ order = check;
|
|
|
+ }
|
|
|
+ WxPayApiConfig wxPayApiConfig = WxPayApiConfigKit.getWxPayApiConfig();
|
|
|
+ Map<String, String> params = UnifiedOrderModel
|
|
|
+ .builder()
|
|
|
+ .appid(wxPayApiConfig.getAppId())
|
|
|
+ .mch_id(wxPayApiConfig.getMchId())
|
|
|
+ .nonce_str(WxPayKit.generateStr())
|
|
|
+ .body(order.getTitle())
|
|
|
+ .attach(order.getTitle())
|
|
|
+ .out_trade_no(order.getOrderNum())
|
|
|
+ .total_fee("1")
|
|
|
+ .spbill_create_ip("127.0.0.1")
|
|
|
+ .notify_url(notifyUrl)
|
|
|
+ .trade_type(TradeType.APP.getTradeType())
|
|
|
+ .build()
|
|
|
+ .createSign(wxPayApiConfig.getPartnerKey(), SignType.HMACSHA256);
|
|
|
+
|
|
|
+ String xmlResult = WxPayApi.pushOrder(false, params);
|
|
|
+ Map<String, String> result = WxPayKit.xmlToMap(xmlResult);
|
|
|
+
|
|
|
+ String returnCode = result.get("return_code");
|
|
|
+ String returnMsg = result.get("return_msg");
|
|
|
+ if (!WxPayKit.codeIsOk(returnCode)) {
|
|
|
+ return AjaxResult.error(returnMsg);
|
|
|
+ }
|
|
|
+ String resultCode = result.get("result_code");
|
|
|
+ if (!WxPayKit.codeIsOk(resultCode)) {
|
|
|
+ return AjaxResult.error(returnMsg);
|
|
|
+ }
|
|
|
+ // 以下字段在 return_code 和 result_code 都为 SUCCESS 的时候有返回
|
|
|
+ String prepayId = result.get("prepay_id");
|
|
|
+ Map<String, String> packageParams = WxPayKit.appPrepayIdCreateSign(wxPayApiConfig.getAppId(), wxPayApiConfig.getMchId(), prepayId, wxPayApiConfig.getPartnerKey(), SignType.HMACSHA256);
|
|
|
+ // 保存订单信息*********************************************
|
|
|
+ if (check == null) {
|
|
|
+ order.setPrepayId(prepayId);
|
|
|
+ order.setCreateTime(new Date());
|
|
|
+ boolean flag = service.save(order);
|
|
|
+ if (!flag) {
|
|
|
+ return AjaxResult.error("订单异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return AjaxResult.success(packageParams);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 祭品订单异步通知
|
|
|
+ */
|
|
|
+ @PassToken
|
|
|
+ @RequestMapping(value = "/notifyUrl", method = {RequestMethod.POST, RequestMethod.GET})
|
|
|
+ @ResponseBody
|
|
|
+ public String notifyUrl(HttpServletRequest request) {
|
|
|
+ String xmlMsg = HttpKit.readData(request);
|
|
|
+ Map<String, String> params = WxPayKit.xmlToMap(xmlMsg);
|
|
|
+ String returnCode = params.get("return_code");
|
|
|
+ String out_trade_no = params.get("out_trade_no");
|
|
|
+ // 注意重复通知的情况,同一订单号可能收到多次通知,请注意一定先判断订单状态
|
|
|
+ // 注意此处签名方式需与统一下单的签名类型一致
|
|
|
+ if (WxPayKit.verifyNotify(params, WxPayApiConfigKit.getWxPayApiConfig().getPartnerKey(), SignType.HMACSHA256)) {
|
|
|
+ if (WxPayKit.codeIsOk(returnCode)) {
|
|
|
+ System.out.println("订单编号out_trade_no:" + params);
|
|
|
+ // 更新订单信息*********************************************
|
|
|
+ TbOrders order = service.selectTbOrdersByOrderNum(out_trade_no);
|
|
|
+ if (order != null && order.getState() == 0) {
|
|
|
+ order.setTransactionId(params.get("transaction_id"));
|
|
|
+ order.setOpenId(params.get("openid"));
|
|
|
+ order.setState(1);
|
|
|
+ order.setPaySuccessTime(new Date());
|
|
|
+ service.updateById(order);
|
|
|
+ }
|
|
|
+ // 更新订单信息*********************************************
|
|
|
+ Map<String, String> xml = new HashMap<String, String>(2);
|
|
|
+ xml.put("return_code", "SUCCESS");
|
|
|
+ xml.put("return_msg", "OK");
|
|
|
+ return WxPayKit.toXml(xml);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|