From eed76fde47443e42bfbd3c7ceb1500016b1abc88 Mon Sep 17 00:00:00 2001
From: KKSU <15274802129@163.com>
Date: Fri, 19 Jan 2024 10:24:38 +0800
Subject: [PATCH] fapiao

---
 src/main/java/cc/mrbird/febs/pay/service/impl/WxFaPiaoServiceImpl.java |  105 +++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 98 insertions(+), 7 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/pay/service/impl/WxFaPiaoServiceImpl.java b/src/main/java/cc/mrbird/febs/pay/service/impl/WxFaPiaoServiceImpl.java
index 7c0a9ba..24070ac 100644
--- a/src/main/java/cc/mrbird/febs/pay/service/impl/WxFaPiaoServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/pay/service/impl/WxFaPiaoServiceImpl.java
@@ -3,14 +3,19 @@
 import cc.mrbird.febs.common.properties.XcxProperties;
 import cc.mrbird.febs.common.utils.AppContants;
 import cc.mrbird.febs.common.utils.SpringContextHolder;
+import cc.mrbird.febs.mall.entity.MallInvoice;
 import cc.mrbird.febs.mall.entity.MallOrderInfo;
+import cc.mrbird.febs.mall.entity.MallOrderItem;
+import cc.mrbird.febs.mall.mapper.MallInvoiceMapper;
 import cc.mrbird.febs.mall.mapper.MallOrderInfoMapper;
+import cc.mrbird.febs.mall.mapper.MallOrderItemMapper;
 import cc.mrbird.febs.pay.model.FPCertificateVo;
 import cc.mrbird.febs.pay.model.FPCertificates;
 import cc.mrbird.febs.pay.model.FPEncryptCertificate;
 import cc.mrbird.febs.pay.service.WxFaPiaoService;
 import cc.mrbird.febs.pay.util.JCEUtil;
 import cc.mrbird.febs.pay.util.RandomStringGenerator;
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
@@ -18,10 +23,7 @@
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import okhttp3.HttpUrl;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPatch;
@@ -56,6 +58,8 @@
 public class WxFaPiaoServiceImpl implements WxFaPiaoService {
 
     private final MallOrderInfoMapper mallOrderInfoMapper;
+    private final MallOrderItemMapper mallOrderItemMapper;
+    private final MallInvoiceMapper mallInvoiceMapper;
 
     private final XcxProperties xcxProperties = SpringContextHolder.getBean(XcxProperties.class);
 
@@ -291,7 +295,7 @@
         log.info("获取到的body信息:" + body);
         //应对签名探测流量
         if(signature.contains("WECHATPAY/SIGNTEST")){
-            map.put("code","500");
+            map.put("code",500);
             map.put("message", "失败");
             return map;
         }
@@ -348,8 +352,57 @@
                     log.info("微信电子发票回调接口....resource解密-JSONObject:"+parseObj);
                     String fapiao_apply_id = parseObj.getStr("fapiao_apply_id");
                     log.info("微信电子发票回调接口....resource解密-fapiao_apply_id:"+fapiao_apply_id);
-                    MallOrderInfo mallOrderInfo = mallOrderInfoMapper.selectByOrderNo(fapiao_apply_id);
+                    MallOrderInfo mallOrderInfo = mallOrderInfoMapper.selectBypayOrderNo(fapiao_apply_id);
+                    if(ObjectUtil.isEmpty(mallOrderInfo)){
+                        map.put("code",404);
+                        map.put("message", "订单不存在");
+                        return map;
+                    }
+
                     if(ObjectUtil.isNotEmpty(mallOrderInfo)){
+                        //更新订单状态
+                        mallOrderInfo.setIsInvoice(1);
+                        mallOrderInfoMapper.updateById(mallOrderInfo);
+                        //获取用户的抬头信息
+                        String userInvoiceInfo = getUserInvoiceInfo(fapiao_apply_id);
+                        JSONObject userInvoiceInfoJson = JSONUtil.parseObj(userInvoiceInfo);
+                        //查看当前订单是否已经开具发票
+                        Long memberId = mallOrderInfo.getMemberId();
+                        String payOrderNo = mallOrderInfo.getPayOrderNo();
+                        List<MallInvoice> mallInvoices = mallInvoiceMapper.selectByFapiaoApplyIdAndMemberId(fapiao_apply_id, mallOrderInfo.getMemberId());
+                        if(CollUtil.isEmpty(mallInvoices)){
+                            MallInvoice mallInvoice = new MallInvoice();
+                            mallInvoice.setMemberId(memberId);
+                            mallInvoice.setFapiaoApplyId(payOrderNo);
+                            mallInvoice.setOrderNo(mallOrderInfo.getOrderNo());
+                            mallInvoice.setOrderId(mallOrderInfo.getId());
+                            mallInvoice.setStatus(mallOrderInfo.getStatus());
+                            mallInvoice.setAmount(mallOrderInfo.getAmount());
+                            List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectListByOrderId(mallOrderInfo.getId());
+                            mallInvoice.setGoodsImg(mallOrderItemList.get(0).getSkuImage());
+                            mallInvoice.setState(1);
+                            mallInvoice.setType(userInvoiceInfoJson.getStr("type"));
+                            mallInvoice.setName(userInvoiceInfoJson.getStr("name"));
+                            mallInvoice.setTaxpayerId(userInvoiceInfoJson.getStr("taxpayer_id"));
+                            mallInvoice.setAddress(userInvoiceInfoJson.getStr("address"));
+                            mallInvoice.setTelephone(userInvoiceInfoJson.getStr("telephone"));
+                            mallInvoice.setBankName(userInvoiceInfoJson.getStr("bank_name"));
+                            mallInvoice.setBankAccount(userInvoiceInfoJson.getStr("bank_account"));
+                            mallInvoiceMapper.insert(mallInvoice);
+                        }else{
+                            MallInvoice mallInvoice = mallInvoices.get(0);
+                            if(2 != mallInvoice.getState()){
+                                mallInvoice.setState(1);
+                                mallInvoice.setType(userInvoiceInfoJson.getStr("type"));
+                                mallInvoice.setName(userInvoiceInfoJson.getStr("name"));
+                                mallInvoice.setTaxpayerId(userInvoiceInfoJson.getStr("taxpayer_id"));
+                                mallInvoice.setAddress(userInvoiceInfoJson.getStr("address"));
+                                mallInvoice.setTelephone(userInvoiceInfoJson.getStr("telephone"));
+                                mallInvoice.setBankName(userInvoiceInfoJson.getStr("bank_name"));
+                                mallInvoice.setBankAccount(userInvoiceInfoJson.getStr("bank_account"));
+                                mallInvoiceMapper.updateById(mallInvoice);
+                            }
+                        }
                         //省略查询订单
                         //此处处理业务
                         map.put("code","SUCCESS");
@@ -358,17 +411,55 @@
                         return map;
                     }
                 }
-                map.put("code","RESOURCE_NOT_EXISTS");
+                map.put("code",404);
                 map.put("message", "订单不存在");
                 return map;
             }catch (Exception e) {
                 e.printStackTrace();
             }
         }
-        map.put("code","500");
+        map.put("code",500);
         map.put("message", "失败");
         return map;
     }
+    /**
+     * 获取用户的抬头信息
+     */
+    public String getUserInvoiceInfo(String fapiaoApplyId) throws IOException {
+
+        //初始化请求参数
+        Map<String, Object> params = new HashMap<>();
+        params.put("scene","WITH_WECHATPAY");
+        params.put("fapiao_apply_id","4200002070202401175245187105");
+
+        String baseUrl = "https://api.mch.weixin.qq.com";
+        String canonicalUrl = "/v3/new-tax-control-fapiao/user-title";
+
+        String urlparm = baseUrl+canonicalUrl;//有参数的get请求,加密是需要把参数加载进去URL
+        List<BasicNameValuePair> parameters = new ArrayList<>();
+        for (Map.Entry<String, Object> entry : params.entrySet()) {
+            parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
+        }
+        StringBuilder dataparm = new StringBuilder();
+        params.forEach((k, v) -> dataparm.append("&" + k + "=" + v));
+        String string = dataparm.toString();
+        if (!"".equals(string)) {
+            urlparm = baseUrl+canonicalUrl + "?" + dataparm.substring(1);
+        }
+        PrivateKey privateKey = this.getPrivateKeyV3();
+        String postStr = null;
+        try {
+            postStr = this.createAuthorization(
+                    "GET",
+                    urlparm,
+                    "",
+                    privateKey
+            );
+        } catch (NoSuchAlgorithmException e) {
+            e.printStackTrace();
+        }
+        return this.sendGet(baseUrl+canonicalUrl, params, "WECHATPAY2-SHA256-RSA2048 "+postStr);
+    }
 
     /**
      * 验证签名

--
Gitblit v1.9.1