puzhibing
2023-03-22 12b75ea8869fa6e65b1bd53d4460328e73a66125
driver/guns-admin/src/main/java/com/supersavedriving/driver/modular/system/util/PayMoneyUtil.java
@@ -16,6 +16,13 @@
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;
@@ -37,10 +44,7 @@
import java.math.BigDecimal;
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.*;
/**
@@ -73,7 +77,10 @@
    @Value("${callbackPath}")
    private String callbackPath;//支付回调网关地址
    private Map<String, JSONObject> order = new HashMap<>();//存储支付订单用于主动查询支付结果
    private String wechatPayCertificatesFromPath = "E:\\项目文档\\超省新代驾\\1636941942_20230320_cert\\apiclient_cert.p12";//微信支付证书地址
    private String privateKeyFromPath = "E:\\项目文档\\超省新代驾\\1636941942_20230320_cert\\apiclient_cert.pem";//微信私钥证书地址
    /**
@@ -628,13 +635,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);
@@ -669,31 +677,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。
                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);
                }
                return null;
            }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(amount.longValue() * 100);
        initiateBatchTransferRequest.setTotalNum(1);
        {
            List<TransferDetailInput> transferDetailListList = new ArrayList<>();
            {
                TransferDetailInput transferDetailInput = new TransferDetailInput();
                transferDetailInput.setOutDetailNo(outDetailNo);
                transferDetailInput.setTransferAmount(amount.longValue() * 100);
                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();
    }
@@ -754,6 +802,16 @@
        return null;
    }
    public String RSAPrivacyEncryptor(){
        // 微信支付平台证书中的公钥
        PublicKey wechatPayPublicKey = null;
        String plaintext = "";
        PrivacyEncryptor encryptor = new RSAPrivacyEncryptor(wechatPayPublicKey, "");
        String ciphertext = encryptor.encrypt(plaintext);
        return ciphertext;
    }
    /**
     * 微信退款成功后的解密