无关风月
2024-07-11 eb6b6dbb35a9f029e0b7d269773685c19fd40976
cloud-server-course/src/main/java/com/dsh/course/util/PayMoneyUtil.java
@@ -6,7 +6,7 @@
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.*;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.dsh.course.util.httpClinet.HttpClientUtil;
@@ -54,6 +54,7 @@
    private String appid = "wx82f853a410b0c7c0";//微信appid
    private String appletsAppid = "";//微信小程序appid
    private String smid = "2088330203191220";//平台支付宝商户号
    private String mchId = "1501481761";//微信商户号
@@ -69,11 +70,91 @@
    private String certPath = "C:\\cert\\1523106371_20211206_cert\\apiclient_cert.p12";//微信证书
    public ResultUtil confirm(String smid,String code, String outTradeNo, String amount) {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                aliAppid,
                appPrivateKey,
                "json",
                "GBK",
                alipay_public_key,
                "RSA2");
        AlipayTradeSettleConfirmRequest request = new AlipayTradeSettleConfirmRequest();
        request.setBizContent("{" +
                "  \"out_request_no\":\""+code+"\"," +
                "  \"trade_no\":\""+outTradeNo+"\"," +
                "  \"settle_info\":{" +
                "    \"settle_detail_infos\":[" +
                "      {" +
                "        \"trans_in_type\":\"defaultSettle\"," +
                "        \"settle_entity_id\":\""+smid+"\"," +
                "        \"settle_entity_type\":\"SecondMerchant\"," +
                "        \"amount\":"+amount+"," +
                "      }" +
                "    ]" +
                "  }," +
                "  \"extend_params\":{" +
                "    \"royalty_freeze\":\"true\"" +
                "  }" +
                "}");
        AlipayTradeSettleConfirmResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
            return ResultUtil.success();
        } else {
            System.out.println("调用失败");
            return ResultUtil.error("出现问题啦");
        }
    }
    // 属于平台的运营商 因为无需分账不冻结资金
    public ResultUtil confirm1(String smid,String code, String outTradeNo, String amount) {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                aliAppid,
                appPrivateKey,
                "json",
                "GBK",
                alipay_public_key,
                "RSA2");
        AlipayTradeSettleConfirmRequest request = new AlipayTradeSettleConfirmRequest();
        request.setBizContent("{" +
                "  \"out_request_no\":\""+code+"\"," +
                "  \"trade_no\":\""+outTradeNo+"\"," +
                "  \"settle_info\":{" +
                "    \"settle_detail_infos\":[" +
                "      {" +
                "        \"trans_in_type\":\"defaultSettle\"," +
                "        \"settle_entity_id\":\""+smid+"\"," +
                "        \"settle_entity_type\":\"SecondMerchant\"," +
                "        \"amount\":"+amount+"," +
                "      }" +
                "    ]" +
                "  }," +
                "  \"extend_params\":{" +
                "    \"royalty_freeze\":\"false\"" +
                "  }" +
                "}");
        AlipayTradeSettleConfirmResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
            return ResultUtil.success();
        } else {
            System.out.println("调用失败");
            return ResultUtil.error("出现问题啦");
        }
    }
    /**
     * 支付宝支付
     */
    public ResultUtil alipay(String body, String subject, String passbackParams, String outTradeNo, String amount, String notifyUrl) {
    public ResultUtil alipay(String smid,String body, String subject, String passbackParams, String outTradeNo, String amount, String notifyUrl) {
//        //构造client
//        CertAlipayRequest certAlipayRequest = new CertAlipayRequest ();
//        //设置网关地址
@@ -124,8 +205,6 @@
//        }  catch (AlipayApiException e ) {
//            e.printStackTrace();
//        }
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, "json", "UTF-8", alipay_public_key, "RSA2");
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
@@ -139,9 +218,23 @@
        model.setTotalAmount(amount);//付款金额
        model.setProductCode("QUICK_MSECURITY_PAY");
        model.setPassbackParams(passbackParams);//自定义参数
        SettleInfo settleInfo = new SettleInfo();
        settleInfo.setSettlePeriodTime("3d");
        SettleDetailInfo settleDetailInfo = new SettleDetailInfo();
        settleDetailInfo.setTransInType("defaultSettle");
        settleDetailInfo.setAmount(amount);
        List<SettleDetailInfo> settleDetailInfos = new ArrayList<>();
        settleDetailInfos.add(settleDetailInfo);
        settleInfo.setSettleDetailInfos(settleDetailInfos);
        model.setSettleInfo(settleInfo);
        SubMerchant subMerchant = new SubMerchant();
        subMerchant.setMerchantId(smid);
        model.setSubMerchant(subMerchant);
        ExtendParams extendParams = new ExtendParams();
        extendParams.setRoyaltyFreeze("true");// 冻结资金 用于后续分账处理
        model.setExtendParams(extendParams);
        request.setBizModel(model);
        request.setNotifyUrl(callbackPath + notifyUrl);
        try {
            //这里和普通的接口调用不同,使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);