lyhzzz 2 лет назад
Родитель
Сommit
f60f843528

+ 76 - 1
src/main/java/com/fdkankan/sale/controller/pay/OrderWechatPayController.java

@@ -3,14 +3,25 @@ import com.alibaba.fastjson.JSONObject;
 import com.fdkankan.common.constant.Constant;
 import com.fdkankan.common.constant.ConstantUrl;
 import com.fdkankan.common.constant.ErrorCode;
-import com.fdkankan.common.exception.BusinessException;
 import com.fdkankan.redis.util.RedisUtil;
+import com.fdkankan.sale.common.CacheUtil;
+import com.fdkankan.sale.common.ResultCode;
 import com.fdkankan.sale.common.ResultData;
 import com.fdkankan.sale.controller.BaseController;
+import com.fdkankan.sale.entity.Repair;
+import com.fdkankan.sale.entity.RepairPay;
+import com.fdkankan.sale.exception.BusinessException;
+import com.fdkankan.sale.pay.wx.WXPayDefaultConfig;
 import com.fdkankan.sale.pay.wx.WechatPayService;
+import com.fdkankan.sale.service.IPriceListService;
+import com.fdkankan.sale.service.IRepairLogService;
+import com.fdkankan.sale.service.IRepairPayService;
+import com.fdkankan.sale.service.IRepairService;
+import com.fdkankan.sale.util.DateUtil;
 import com.fdkankan.sale.util.UrlUtils;
 import com.fdkankan.sale.vo.request.WechatMobileParam;
 import lombok.extern.log4j.Log4j2;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -33,6 +44,14 @@ import java.util.Map;
 public class OrderWechatPayController  extends BaseController {
     @Autowired
     WechatPayService wechatPayService;
+    @Autowired
+    IPriceListService priceListService;
+    @Autowired
+    IRepairPayService repairPayService;
+    @Autowired
+    IRepairService repairService;
+    @Autowired
+    IRepairLogService repairLogService;
 
     @ResponseBody
     @RequestMapping(value = "/wechatMobilePay", method = RequestMethod.POST)
@@ -41,6 +60,62 @@ public class OrderWechatPayController  extends BaseController {
     }
 
 
+    @RequestMapping(value = "/wechatPreJsPay")
+    public String wechatPreJsPay(String repairId,String openId) throws Exception {
+        // 这个url链接地址和参数皆不能变
+        try {
+            if(StringUtils.isBlank(repairId)){
+                throw new com.fdkankan.sale.exception.BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
+            }
+            Repair repair = repairService.getById(repairId);
+            if(repair == null){
+                throw new com.fdkankan.sale.exception.BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
+            }
+            if(repair.getStatus() != 9 && repair.getStatus() != 4){
+                throw new com.fdkankan.sale.exception.BusinessException(ResultCode.REPAIR_STATUS_NOT_EXITS);
+            }
+            RepairPay repairPay = repairPayService.getByRepairId(repairId,1);
+            if(repairPay != null  ){
+                throw new BusinessException(ResultCode.ORDER_PAY_ERROR);
+            }
+            String orderSn = null;
+
+            BigDecimal amount = BigDecimal.ZERO;
+            String subject = null;
+            Integer orderType = null;
+            if(repair.getStatus() == 9){
+                amount = priceListService.getAmountByRepairId(repairId);
+                orderSn = "se_s"+ DateUtil.getDate(DateUtil.repairIdFmt);
+                subject = "维修订单";
+                orderType = 0;
+            }
+            if(repair.getStatus() == 3){
+                amount = priceListService.getRobAmountByRepairId(repairId);
+                orderSn = "se_c"+DateUtil.getDate(DateUtil.repairIdFmt);
+                subject = "取消维修订单";
+                orderType = 1;
+            }
+
+            repairPay = new RepairPay();
+            repairPay.setRepairId(repairId);
+            repairPay.setPayType(1);
+            repairPay.setOrderSn(orderSn);
+            repairPay.setPayAmount(amount);
+            repairPay.setOrderType(orderType);
+            repairPayService.save(repairPay);
+
+            amount =amount .multiply(new BigDecimal(100));
+            Map<String, String> resp = wechatPayService.wechatPay(orderSn,subject, subject+subject, amount, UrlUtils.getIpAddr(request), openId);
+            String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
+            return "redirect:" + CacheUtil.mainUrl + ConstantUrl.WEIXIN_MOBILE_PAY_URL + timeStamp + "&signType=MD5&appId=" + resp.get("appid")
+                    + "&nonceStr=" + resp.get("nonce_str") + "&prepay_id=" + resp.get("prepay_id") + "&paySign=" + resp.get("paySign");
+        }catch (Exception e){
+            log.debug("获取access_token发生异常",e);
+        }
+        return "redirect:" + CacheUtil.mainUrl + "mobile.html#/payresult/fail";
+    }
+
+
     @RequestMapping(value = "/wechatPay/notify", method = RequestMethod.POST)
     public void callback() {
         wechatPayService.callBack(request,response);

+ 32 - 0
src/main/java/com/fdkankan/sale/pay/wx/WechatPayService.java

@@ -122,6 +122,38 @@ public class WechatPayService {
     }
 
 
+    public Map<String, String> wechatPay(String orderSn, String subject, String body, BigDecimal totalFee, String ipAddress, String openid) throws Exception {
+        Map<String, String> data = new HashMap<String, String>();
+        data.put("body", subject);
+        data.put("detail", body);
+        data.put("out_trade_no", orderSn + "_" + new Random().nextInt(100));
+        data.put("device_info", "wechat");
+        data.put("fee_type", "CNY");
+        data.put("total_fee", totalFee.stripTrailingZeros().toPlainString());
+        data.put("spbill_create_ip", ipAddress);
+        data.put("notify_url", CacheUtil.mainUrl + WXPayDefaultConfig.notifyURL);
+        data.put("trade_type", "JSAPI");
+        data.put("product_id", orderSn);
+        data.put("openid", openid);
+
+        Map<String, String> resp = WXPay.unifiedOrder(data);
+        log.warn("wechatPay" + resp);
+
+        Map<String, String> sign = new HashMap<>();
+        sign.put("package", "prepay_id="+resp.get("prepay_id"));
+        sign.put("appId", WXPayDefaultConfig.appId);
+        sign.put("timeStamp",String.valueOf(System.currentTimeMillis() / 1000));
+        sign.put("nonceStr", resp.get("nonce_str"));
+        sign.put("signType", "MD5");
+        String paySign = WXPayUtil.generateSignature(sign, WXPayDefaultConfig.key);
+
+        resp.put("signType", "MD5");
+        resp.put("paySign", paySign);
+        log.warn("wechatPay" + resp);
+        return resp;
+    }
+
+
     public void callBack(HttpServletRequest request, HttpServletResponse response) {
         log.warn("微信回调接口方法 start");
         String inputLine = "";