liujie
2023-08-16 db7fa6a91b9534ac90e219b6f554c54c43c83a5a
driver/guns-admin/src/main/java/com/supersavedriving/driver/modular/system/util/PayMoneyUtil.java
@@ -16,13 +16,19 @@
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.supersavedriving.driver.modular.system.util.httpClinet.HttpClientUtil;
import com.wechat.pay.java.core.RSAConfig;
import com.wechat.pay.java.core.cipher.PrivacyEncryptor;
import com.wechat.pay.java.core.cipher.RSAPrivacyEncryptor;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;
import com.wechat.pay.java.service.transferbatch.model.InitiateBatchTransferRequest;
import com.wechat.pay.java.service.transferbatch.model.InitiateBatchTransferResponse;
import com.wechat.pay.java.service.transferbatch.model.TransferDetailInput;
import org.apache.commons.collections.map.HashedMap;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -36,12 +42,10 @@
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.*;
import java.util.*;
/**
@@ -74,7 +78,11 @@
    @Value("${callbackPath}")
    private String callbackPath;//支付回调网关地址
    private Map<String, JSONObject> order = new HashMap<>();//存储支付订单用于主动查询支付结果
    private String wechatPayCertificatesFromPath = "/usr/local/server/app/cert/weixin/apiclient_cert.p12";//微信支付证书地址
    private String privateKeyFromPath = "/usr/local/server/app/cert/weixin/apiclient_cert.pem";//微信私钥证书地址
    /**
@@ -629,13 +637,14 @@
     * @return
     * @throws Exception
     */
    public ResultUtil queryWXOrder() throws Exception{
    public ResultUtil<Map<String, String>> queryWXOrder(String out_trade_no, String transaction_id) throws Exception{
        String url = "https://api.mch.weixin.qq.com/pay/orderquery";
        String nonce_str = UUIDUtil.getRandomCode(16);
        Map<String, Object> map = new HashMap<>();
        map.put("appid", appid);
        map.put("mch_id", mchId);
        map.put("transaction_id", nonce_str);//微信订单号
        map.put("out_trade_no", out_trade_no);//商户订单号
        map.put("transaction_id", transaction_id);//微信订单号
        map.put("nonce_str", nonce_str);//随机字符串
        String s = this.weixinSignature(map);
        map.put("sign", s);
@@ -670,31 +679,71 @@
        if("SUCCESS".equals(return_code)){
            String result_code = map1.get("result_code");
            if("SUCCESS".equals(result_code)){
                String type = map1.get("trade_type");
                switch (type){
                    case "JSAPI":
                        break;
                    case "NATIVE":
                        String code_url = map1.get("code_url");
                        return ResultUtil.success(code_url);
                    case "APP":
                        String trade_state = map1.get("trade_state");
                        String time_end = map1.get("time_end");
                        Map<String, Object> map2 = new HashMap<>();
                        map2.put("trade_state", trade_state);//订单状态SUCCESS—支付成功,REFUND—转入退款,NOTPAY—未支付,CLOSED—已关闭,REVOKED—已撤销(刷卡支付),USERPAYING--用户支付中,PAYERROR--支付失败(其他原因,如银行返回失败)
                        map2.put("time_end", time_end);//订单支付时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。
                        return ResultUtil.success(map2);
                }
                return null;
                Map<String, String> map2 = new HashMap<>();
                map2.put("trade_type", map1.get("trade_type"));
                map2.put("trade_state", map1.get("trade_state"));//订单状态SUCCESS—支付成功,REFUND—转入退款,NOTPAY—未支付,CLOSED—已关闭,REVOKED—已撤销(刷卡支付),USERPAYING--用户支付中,PAYERROR--支付失败(其他原因,如银行返回失败)
                map2.put("transaction_id", map1.get("transaction_id"));
                return ResultUtil.success(map2);
            }else{
                System.err.println(map1.get("err_code_des"));
                return ResultUtil.error(map1.get("err_code_des"));
            }
        }else{
            System.err.println(map1.get("return_msg") + appid + "----" + mchId);
            return ResultUtil.error(map1.get("return_msg"), new JSONObject());
            return ResultUtil.error(map1.get("return_msg"));
        }
    }
    /**
     * 微信商户转账零钱
     * @param outDetailNo   商家流水号
     * @param amount        转账金额
     * @param openid        收款openid
     * @param userName      收款用户姓名
     * @param remark        备注
     * @return
     */
    public String weixinTransferMoney(String outDetailNo, Double amount, String openid, String userName, String remark){
        RSAConfig config =
                new RSAConfig.Builder()
                        .merchantId(mchId)
                        .privateKeyFromPath(privateKeyFromPath)
                        .merchantSerialNumber("7630124B15BA87F2CF0F35883988DE34D62089DA")
                        .wechatPayCertificatesFromPath(wechatPayCertificatesFromPath)
                        .build();
        TransferBatchService service = new TransferBatchService.Builder().config(config).build();
        InitiateBatchTransferRequest initiateBatchTransferRequest =
                new InitiateBatchTransferRequest();
        initiateBatchTransferRequest.setAppid(appid);
        initiateBatchTransferRequest.setOutBatchNo(outDetailNo);
        initiateBatchTransferRequest.setBatchName("付款");
        initiateBatchTransferRequest.setBatchRemark(remark);
        initiateBatchTransferRequest.setTotalAmount(new BigDecimal(amount).multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_EVEN).longValue());
        initiateBatchTransferRequest.setTotalNum(1);
        {
            List<TransferDetailInput> transferDetailListList = new ArrayList<>();
            {
                TransferDetailInput transferDetailInput = new TransferDetailInput();
                transferDetailInput.setOutDetailNo(outDetailNo);
                transferDetailInput.setTransferAmount(new BigDecimal(amount).multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_EVEN).longValue());
                transferDetailInput.setTransferRemark(remark);
                transferDetailInput.setOpenid(openid);
                if(amount > 0.3){
                    transferDetailInput.setUserName(userName);
                }
                transferDetailListList.add(transferDetailInput);
            }
            initiateBatchTransferRequest.setTransferDetailList(
                    transferDetailListList);
        }
        initiateBatchTransferRequest.setTransferSceneId("1000");
        InitiateBatchTransferResponse response =
                service.initiateBatchTransfer(initiateBatchTransferRequest);
        return response.getBatchId();
    }
@@ -755,6 +804,16 @@
        return null;
    }
    public String RSAPrivacyEncryptor(){
        // 微信支付平台证书中的公钥
        PublicKey wechatPayPublicKey = null;
        String plaintext = "";
        PrivacyEncryptor encryptor = new RSAPrivacyEncryptor(wechatPayPublicKey, "");
        String ciphertext = encryptor.encrypt(plaintext);
        return ciphertext;
    }
    /**
     * 微信退款成功后的解密