123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- package com.szwl.controller;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.google.gson.Gson;
- import com.szwl.feign.bean.SzwlFeign;
- import com.szwl.model.bo.JsonMessage;
- import com.szwl.model.bo.R;
- import com.szwl.model.bo.ResponseModel;
- import com.szwl.model.entity.*;
- import com.szwl.constant.JoinpayConstant;
- import com.szwl.model.utils.PushUtils;
- import com.szwl.service.TEquipmentService;
- import com.szwl.service.TOrderService;
- import com.szwl.service.TWeixinPayConfigService;
- import com.szwl.utils.IDGenerator;
- import com.szwl.weixin.refund.model.CreateRequest;
- import com.szwl.weixin.util.HttpUtils;
- import com.wechat.pay.java.service.refund.model.AmountReq;
- import com.wechat.pay.java.service.refund.model.Refund;
- import io.swagger.annotations.ApiOperation;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.ResponseBody;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import static com.wechat.pay.java.service.refund.model.Status.PROCESSING;
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @author wuhs
- * @since 2023-04-08
- */
- @RestController
- @RequestMapping("/tWeixinPay")
- public class TWeixinPayController {
- @Autowired
- SzwlFeign szwlFeign;
- @Autowired
- TOrderService orderService;
- @Autowired
- TWeixinPayConfigService weixinPayConfigService;
- @Autowired
- TEquipmentService equipmentService;
- @ApiOperation(value = "购物车 请求在线支付 ")
- @PostMapping("/carsPay")
- public Object carsPay(String clientId, @RequestBody Map<String,String> productNameMap) {
- if(productNameMap==null){
- return JsonMessage.error("数据出错");
- }
- StringBuffer productName1 = new StringBuffer();
- StringBuffer note = new StringBuffer();
- StringBuffer productName = new StringBuffer();
- TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId));
- if(equipment==null||equipment.getId()==null){
- return JsonMessage.error("找不到设备");
- }
- BigDecimal price = new BigDecimal("0.00");
- // JSONObject mapList = JSON.parseObject(productNameMap);
- TEquipmentDesc equipmentDesc = R.getDataIfSuccess(szwlFeign.findEquipmentById(equipment.getId()));
- if(equipmentDesc==null||equipmentDesc.getEquipmentId()==null||equipmentDesc.getPayType()==null||equipmentDesc.getPayType().equals("0")){
- //1,type=0,原始设定,没有第二件半价
- for (String key : productNameMap.keySet()) {
- String entryValue = String.valueOf(productNameMap.get(key));
- List<String> value = JSON.parseArray(entryValue, String.class);
- List<TPromoCode> promoCodeList = new ArrayList<>();
- //1,校验优惠码
- if(value.size()>0){
- for(String code:value){
- if(!code.equals("0")){
- TPromoCode promoCode = new TPromoCode();
- ResponseModel<TPromoCode> tPromoCodeResponseModel = szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString());
- TPromoCode tPromoCode = tPromoCodeResponseModel.getData();
- // TPromoCode tPromoCode = R.getDataIfSuccess(szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString()));
- promoCode = tPromoCode;
- if (tPromoCode == null||tPromoCode.getId()==null) {
- //不存在
- return JsonMessage.success("1");
- }
- Date lastUseDate = null;
- if (promoCode != null) {
- lastUseDate = promoCode.getLastUseDate();
- }
- if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) {
- promoCode.setIsUse("2");
- szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()),"2");
- //过期
- return JsonMessage.success("4");
- }
- if (promoCode.getId() == null) {
- //不存在
- return JsonMessage.success("1");
- }
- if (promoCode.getIsUse().equals("1")) {
- //被使用
- return JsonMessage.success("2");
- }
- if(!promoCode.getAdminId().equals("1")){
- if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) {
- } else {
- //不是本机
- return JsonMessage.success("3");
- }
- }
- if (promoCode.getDiscount() == null) {
- //旧优惠码
- return JsonMessage.success("0");
- }
- if (promoCode.getDiscount() != null && promoCode.getDiscount() == 0) {
- //0折
- return JsonMessage.success("0");
- }
- promoCodeList.add(promoCode);
- }
- }
- }
- String[] productNum = key.split("-");
- String productNamea = productNum[0];
- String num = productNum[1];
- productName.append(productNamea).append(num);
- note.append(productNamea).append(num).append(":");
- productName1.append(productNamea).append(num);
- //确定价格,然后叠加R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName));
- TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), productNamea));
- if (product == null) {
- return JsonMessage.error("找不到商品");
- }
- BigDecimal productPrice = product.getRmbPrice();
- if(promoCodeList.size()>0){
- //有优惠码
- BigDecimal onePrice = new BigDecimal("0.00");
- int i;
- for( i = 0;i<promoCodeList.size();i++){
- BigDecimal discount = BigDecimal.valueOf(promoCodeList.get(i).getDiscount()).setScale(2, RoundingMode.HALF_DOWN);
- BigDecimal price1 = productPrice.multiply(discount).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN);
- price = price.add(price1);
- onePrice = onePrice.add(price1);
- }
- if(i<Integer.valueOf(num)){
- int a = Integer.valueOf(num)-i;
- BigDecimal price2 = productPrice.multiply(new BigDecimal(a)).setScale(2, RoundingMode.HALF_DOWN);
- price =price.add(price2);
- onePrice = onePrice.add(price2);
- }
- note.append(onePrice).append(",");
- }else {
- //1,type=0,原始设定,没有第二件半价
- int a = Integer.valueOf(num);
- BigDecimal price2 = productPrice.multiply(new BigDecimal(a)).setScale(2, RoundingMode.HALF_DOWN);
- price =price.add(price2);
- note.append(price2).append(",");
- }
- }
- }else {
- //2,type=1,第二件半价
- //2.1 判定是否有第二件,有多少个第二件半价
- for (String key : productNameMap.keySet()) {
- // System.out.println("key= "+ key + " and value= " + productNameMap.get(key));
- String entryValue = String.valueOf(productNameMap.get(key));
- List<String> value = JSON.parseArray(entryValue, String.class);
- String[] productNum = key.split("-");
- String productNamea = productNum[0];
- String num = productNum[1];
- TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipment.getId()), productNamea));
- BigDecimal productPrice = product.getRmbPrice();
- productName.append(productNamea).append(num);
- productName1.append(productNamea).append(num);
- note.append(productNamea).append(num).append(":");
- switch (num) {
- case "1":
- //1个 0个半价
- price =price.add(productPrice);
- note.append(price).append(",");
- break;
- case "2":
- //2个 1个半价
- price =price.add(productPrice);
- price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
- note.append(price).append(",");
- break;
- case "3":
- //3个 1个半价
- price =price.add(productPrice);
- price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
- price =price.add(productPrice);
- note.append(price).append(",");
- break;
- case "4":
- //4个 2个半价
- price =price.add(productPrice);
- price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
- price =price.add(productPrice);
- price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
- note.append(price).append(",");
- break;
- case "5":
- //5个 2个半价
- price =price.add(productPrice);
- price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
- price =price.add(productPrice);
- price =price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
- price =price.add(productPrice);
- note.append(price).append(",");
- break;
- }
- }
- }
- if (equipment == null) {
- return JsonMessage.error("找不到设备");
- }
- Long equipmentId = equipment.getId();
- //查找商品
- // TProduct product = R.getDataIfSuccess(szwlFeign.getProduct(String.valueOf(equipmentId), productName.toString()));
- // if (product == null) {
- // return JsonMessage.error("找不到商品");
- // }
- if (BigDecimal.ZERO.compareTo(price) >= 0) {
- return JsonMessage.error("商品价格异常");
- }
- String sn1 = orderService.initSn(equipmentId);
- //获取分销 关联设备
- TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId())));
- if (admin == null) {
- return JsonMessage.error("找不到设备商家");
- }
- JSONArray altInfo = new JSONArray();
- String orderNo1 = sn1;
- BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN);
- amount = amount.multiply(new BigDecimal(100));
- String notifyUrl = JoinpayConstant.Notify_Url;
- String frpCode1 = "WEIXIN_NATIVE";
- TOrder order = new TOrder();
- order.setId(IDGenerator.orderID());
- order.setCreateDate(new Date());
- order.setModifyDate(new Date());
- order.setAdminId(admin.getId());
- order.setSn(sn1);
- order.setProductName(productName.toString());
- order.setPrice(price);
- order.setClientId(equipment.getClientId());
- order.setEquipmentId(equipmentId);
- order.setFrpCode(frpCode1);
- order.setAltInfo(altInfo.toString());
- order.setStatus(0);
- String payPlatform = "3";
- order.setPayPlatform(payPlatform);
- if(StringUtils.isNotEmpty(payPlatform)&&payPlatform.equals("1")){
- order.setIsSettlement("0");
- }
- order.setNote(note.toString());
- orderService.save(order);
- Iterator<Object> iterator = altInfo.iterator();
- while (iterator.hasNext()) {
- JSONObject jsonObject = (JSONObject) iterator.next();
- String altAmount = jsonObject.getString("altAmount");
- if (altAmount.equals("0.00")) {
- iterator.remove();
- }
- }
- String result = null;
- try {
- result = orderService.weixinPay(
- orderNo1, amount, productName.toString(), admin.getId(), notifyUrl
- );
- } catch (Exception e) {
- e.printStackTrace();
- return JsonMessage.error("申请支付失败");
- }
- // 微信支付支付申请返回支付二维码图片
- JSONObject kindData = new JSONObject();
- kindData.put("sn", sn1);
- kindData.put("code_url", result);
- return JsonMessage.success(kindData.toString());
- }
- /**
- * 成功回调 微信
- *
- * @return
- */
- @RequestMapping(value = "/notify", method = RequestMethod.POST)
- @ResponseBody
- public Object notify(HttpServletRequest request, HttpServletResponse response) {
- Gson gson = new Gson();
- //应答对象
- Map<String, String> map = new HashMap<>();
- String body = HttpUtils.readData(request);
- Map<String, Object> bodyMap = gson.fromJson(body, HashMap.class);
- //处理支付回调成功的订单
- String plainText = weixinPayConfigService.decryptFromResource(bodyMap);
- if(StringUtils.isNotEmpty(plainText)){
- JSONObject resultJson = JSONObject.parseObject(plainText);
- String sn = resultJson.getString("out_trade_no");
- if(StringUtils.isNotEmpty(sn)){
- LambdaQueryWrapper<TOrder> query = Wrappers.lambdaQuery();
- query.eq(TOrder::getSn,sn);
- query.eq(TOrder::getStatus,0);
- TOrder order = orderService.getOne(query);
- order.setStatus(1);
- String success_time = resultJson.getString("success_time");
- Date date = new Date();
- try {
- date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(success_time);
- } catch (ParseException e) {
- //LOGGER.error("时间转换错误, string = {}", s, e);
- }
- order.setPayDate(date);
- order.setTrxNo(resultJson.getString("transaction_id"));
- orderService.updateById(order);
- TEquipment byClientId = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(order.getClientId()));
- JSONObject kindData = new JSONObject();
- kindData.put("sn", order.getSn());
- kindData.put("productName", order.getProductName());
- equipmentService.sentMessage(byClientId.getClientId(), PushUtils.buildJson("pay_success", kindData.toString()).toString());
- }
- }
- return "success";
- }
- @ApiOperation(value = "退款")
- @GetMapping("/refund")
- ResponseModel<String> refund(@RequestParam String sn,@RequestParam BigDecimal refusePrice){
- if(StringUtils.isEmpty(sn)){
- return R.fail("订单为空/error");
- }
- LambdaQueryWrapper<TOrder> query = Wrappers.lambdaQuery();
- query.eq(TOrder::getSn,sn);
- query.eq(TOrder::getStatus,1);
- TOrder order = orderService.getOne(query);
- if(order==null){
- return R.fail("订单为空/error");
- }
- CreateRequest createRequest = new CreateRequest();
- createRequest.setOutTradeNo(order.getSn());
- AmountReq amount = new AmountReq();
- amount.setRefund(refusePrice.multiply(new BigDecimal(100)).longValue());
- if(order.getRefundAmount()!=null){
- BigDecimal refusep = order.getPrice().add(order.getRefundAmount()).multiply(new BigDecimal(100));
- amount.setTotal(refusep.longValue());
- }else {
- BigDecimal multiply = order.getPrice().multiply(new BigDecimal(100));
- amount.setTotal(multiply.longValue());
- }
- amount.setCurrency("CNY");
- createRequest.setAmount(amount);
- String refundNo = orderService.initSn(order.getEquipmentId());
- createRequest.setOutRefundNo(refundNo);
- createRequest.setNotifyUrl("https://app.sunzee.com.cn/PAY-SERVER/tWeixinPay/refundNotify");
- // createRequest.setNotifyUrl("http://mianhuatang.gz2vip.91tunnel.com/tWeixinPay/refundNotify");
- // createRequest.setNotifyUrl("http://soobin.5gzvip.91tunnel.com/tWeixinPay/refundNotify");
- Refund refund = orderService.weixinRefundApi(createRequest,order.getAdminId());
- System.out.println("退款参数"+refund.toString());
- if(refund.getStatus()==PROCESSING){
- return R.ok("退款申请中,请30秒后再查询");
- }else {
- return R.ok("退款失败,请重试");
- }
- };
- @ApiOperation(value = "退款成功回调")
- @PostMapping("/refundNotify")
- ResponseModel<String> refundNotify(HttpServletRequest request, HttpServletResponse response){
- String result = orderService.weixinRefund(request,response);
- return R.ok(result);
- };
- }
|