All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yes.tools.pay.service.impl.PayServiceImpl Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package org.yes.tools.pay.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.yes.tools.core.exception.YesBaseException;
import org.yes.tools.pay.PayUtils;
import org.yes.tools.pay.config.PayConfig;
import org.yes.tools.pay.constant.PayConstants;
import org.yes.tools.pay.module.request.*;
import org.yes.tools.pay.module.result.*;
import org.yes.tools.pay.service.BasePayService;
import org.yes.tools.pay.service.PayService;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

@Slf4j
public class PayServiceImpl implements PayService {

    private final BasePayService baseService;

    public PayServiceImpl(BasePayService baseService) {
        this.baseService = baseService;
    }

    @Override
    public WxAppletPayResult wxAppLetPay(WxAppletPayRequest request) throws Exception {
        PayConfig config = baseService.getConfig();
        if (Objects.isNull(config)) {
            throw new YesBaseException("请配置支付配置!");
        }
        request.setInstMid(PayConstants.InstMid.MINIDEFAULT);
        request.setTradeType("MINI");
        request.checkAndSign(config);
        String reqBody = JSONObject.toJSONString(request);
        try {
            String authorization = baseService.getAuthorization(reqBody);
            String url = String.format("%s/v1/netpay/wx/unified-order", this.baseService.getPayBaseUrl());
            String response = PayUtils.request(url, authorization, reqBody);
            log.info("wxAppLetPay response ----------------------------->{}", response);
            Result judgeResult = JSONObject.parseObject(response, Result.class);
            if (!judgeResult.getErrCode().equals("SUCCESS")) {
                throw new YesBaseException("调用微信小程序支付失败");
            }
            Result result = JSONObject.parseObject(response, new TypeReference>() {
            });
            WxAppletPayResult miniPayRequest = result.getMiniPayRequest();
            miniPayRequest.setPackages(((JSONObject) judgeResult.getMiniPayRequest()).get("package").toString());
            return miniPayRequest;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YesBaseException("调用微信小程序支付失败");
        }
    }

    @Override
    public AliPayResult aliAppPay(AppPayRequest request) throws Exception {
        PayConfig config = baseService.getConfig();
        if (Objects.isNull(config)) {
            throw new YesBaseException("请配置支付配置!");
        }
        request.setInstMid(PayConstants.InstMid.APPDEFAULT);
        request.setTradeType("APP");
        request.checkAndSign(config);
        String reqBody = JSONObject.toJSONString(request);
        try {
            String authorization = baseService.getAuthorization(reqBody);
            String url = String.format("%s/v1/netpay/trade/app-pre-order", this.baseService.getPayBaseUrl());
            String response = PayUtils.request(url, authorization, reqBody);
            log.info("aliAppPay response ----------------------------->{}", response);
            Result judgeResult = JSONObject.parseObject(response, Result.class);
            if (!judgeResult.getErrCode().equals("SUCCESS")) {
                throw new YesBaseException("调用支付宝APP支付失败");
            }
            Result result = JSONObject.parseObject(response, new TypeReference>() {
            });
            return result.getAppPayRequest();
        } catch (Exception e) {
            e.printStackTrace();
            throw new YesBaseException("调用支付宝APP支付失败");
        }
    }

    @Override
    public UacResult uacAppPay(PayRequest request) {
        PayConfig config = baseService.getConfig();
        if (Objects.isNull(config)) {
            throw new YesBaseException("请配置支付配置!");
        }
        request.setInstMid(PayConstants.InstMid.APPDEFAULT);
        request.setTradeType("APP");
        request.checkAndSign(config);
        String reqBody = JSONObject.toJSONString(request);

        try {
            String authorization = baseService.getAuthorization(reqBody);
            String url = String.format("%s/v1/netpay/uac/app-order", this.baseService.getPayBaseUrl());
            String response = PayUtils.request(url, authorization, reqBody);
            log.info("uacAppPay response ----------------------------->{}", response);
            Result judgeResult = JSONObject.parseObject(response, Result.class);
            if (!judgeResult.getErrCode().equals("SUCCESS")) {
                throw new YesBaseException("调用云闪付支付失败");
            }
            Result result = JSONObject.parseObject(response, new TypeReference>() {
            });
            return result.getAppPayRequest();
        } catch (Exception e) {
            e.printStackTrace();
            throw new YesBaseException("调用云闪付支付失败");
        }

    }

    @Override
    public CToBPayResult cTOBPay(CToBPayRequest request) throws Exception {
        PayConfig config = baseService.getConfig();
        if (Objects.isNull(config)) {
            throw new YesBaseException("请配置支付配置!");
        }
        request.setInstMid(PayConstants.InstMid.QRPAYDEFAULT);

        String reqBody = JSONObject.toJSONString(request);

        try {
            String authorization = baseService.getAuthorization(reqBody);
            String url = String.format("%s/v1/netpay/bills/get-qrcode", this.baseService.getPayBaseUrl());
            String response = PayUtils.request(url, authorization, reqBody);
            log.info("cTOBPay response ----------------------------->{}", response);
            CToBPayResult cbPayRes = JSONObject.parseObject(response, CToBPayResult.class);
            if (!cbPayRes.getErrCode().equals("SUCCESS")) {
                throw new YesBaseException("调用C TO B支付失败");
            }
            return cbPayRes;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YesBaseException("调用C TO B支付失败");
        }
    }

    @Override
    public Result closeQrcode(CloseQrcodeRequest request) {
        PayConfig config = baseService.getConfig();
        if (Objects.isNull(config)) {
            throw new YesBaseException("请配置支付配置!");
        }
        request.setInstMid(PayConstants.InstMid.QRPAYDEFAULT);

        String reqBody = JSONObject.toJSONString(request);

        try {
            String authorization = baseService.getAuthorization(reqBody);
            String url = String.format("%s/v1/netpay/bills/close-qrcode", this.baseService.getPayBaseUrl());
            String response = PayUtils.request(url, authorization, reqBody);
            log.info("closeQrcode response ----------------------------->{}", response);
            Result cbPayRes = JSONObject.parseObject(response, Result.class);
            if (!cbPayRes.getErrCode().equals("SUCCESS")) {
                throw new YesBaseException("调用关闭二维码失败");
            }
            return cbPayRes;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YesBaseException("调用关闭二维码失败");
        }
    }

    @Override
    public H5PayResult aliH5Pay(PayRequest request) throws Exception {
        PayConfig config = baseService.getConfig();
        if (Objects.isNull(config)) {
            throw new YesBaseException("请配置支付配置!");
        }
        request.setInstMid(PayConstants.InstMid.H5DEFAULT);
        request.checkAndSign(config);
        String reqBody = JSONObject.toJSONString(request);

        String authorization = "OPEN-FORM-PARAM";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String timestamp = dateFormat.format(new Date());
        String nonce = RandomUtil.randomString(32);

        try {
            String signature = PayUtils.getSignature(config.getAppid(),
                    config.getAppKey(), timestamp, nonce, reqBody);

            String aliH5PayUrl = String.format("%s/v1/netpay/trade/h5-pay", this.baseService.getPayBaseUrl());

            String URL = aliH5PayUrl + "?" + "authorization=" + authorization + "&appId=" + config.getAppid()
                    + "×tamp=" + timestamp + "&nonce=" + nonce + "&content=" + URLEncoder.encode(reqBody, "UTF-8")
                    + "&signature=" + URLEncoder.encode(signature, "UTF-8");

            H5PayResult response = new H5PayResult();
            response.setPayUrl(URL);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YesBaseException("支付失败");
        }

    }

    @Override
    public PayNotifyResult parseOrderNotifyResult(String body) {
        log.info("parseOrderNotifyResult------------------------------>{}", body);
        PayNotifyResult payNotifyResult = JSONObject.parseObject(body, PayNotifyResult.class);
        PayConfig config = this.baseService.getConfig();
        String[] split = payNotifyResult.getMerOrderId().split(config.getSourceSn());
        payNotifyResult.setMerOrderId(split[1]);
        return payNotifyResult;
    }

    @Override
    public Result appletRefund(RefundRequest request) throws Exception {
        request.setInstMid(PayConstants.InstMid.MINIDEFAULT);
        return this.refund(request);
    }

    @Override
    public Result cToBRefund(CToBRefundRequest request) throws Exception {
        PayConfig config = baseService.getConfig();
        if (Objects.isNull(config)) {
            throw new YesBaseException("请配置支付配置!");
        }
        request.setInstMid(PayConstants.InstMid.QRPAYDEFAULT);
        request.checkAndSign(config);

        String reqBody = JSONObject.toJSONString(request);
        log.info("cToB退款请求的信息--------------------------------->reqBody{}", reqBody);
        String url = String.format("%s/v1/netpay/bills/refund", this.baseService.getPayBaseUrl());
        try {
            String authorization = baseService.getAuthorization(reqBody);
            String response = PayUtils.request(url, authorization, reqBody);
            log.info("cToB退款返回的信息--------------------------------->{}", response);
            Result judgeResult = JSONObject.parseObject(response, Result.class);
            return judgeResult;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YesBaseException("退款失败");
        }

    }

    @Override
    public Result appletRefundQuery(RefundQueryRequest request) throws Exception {
        request.setInstMid(PayConstants.InstMid.MINIDEFAULT);
        return this.refundQuery(request);
    }

    @Override
    public Result refund(RefundRequest request) throws Exception {
        PayConfig config = baseService.getConfig();
        if (Objects.isNull(config)) {
            throw new YesBaseException("请配置支付配置!");
        }
        request.checkAndSign(config);
        String reqBody = JSONObject.toJSONString(request);
        log.info("退款请求的信息--------------------------------->reqBody{}", reqBody);
        String url = String.format("%s/v1/netpay/refund", this.baseService.getPayBaseUrl());
        try {
            String authorization = baseService.getAuthorization(reqBody);
            String response = PayUtils.request(url, authorization, reqBody);
            log.info("退款返回的信息--------------------------------->{}", response);
            Result judgeResult = JSONObject.parseObject(response, Result.class);
            return judgeResult;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YesBaseException("退款失败");
        }
    }

    @Override
    public Result refundQuery(RefundQueryRequest request) throws Exception {
        PayConfig config = baseService.getConfig();
        if (Objects.isNull(config)) {
            throw new YesBaseException("请配置支付配置!");
        }
        request.checkAndSign(config);
        String reqBody = JSONObject.toJSONString(request);
        String authorization = baseService.getAuthorization(reqBody);
        String url = String.format("%s/v1/netpay/refund", this.baseService.getPayBaseUrl());
        String response = PayUtils.request(url, authorization, reqBody);
        Result judgeResult = JSONObject.parseObject(response, Result.class);
        log.info("refundQuery 返回的信息--------------------------------->{}", response);
        return judgeResult;
    }

    @Override
    public BTOCResult bTOCPay(BToCPayRequest request) throws Exception {
        PayConfig config = baseService.getConfig();
        if (Objects.isNull(config)) {
            throw new YesBaseException("请配置支付配置!");
        }
        request.checkAndSign(config);
        String reqBody = JSONObject.toJSONString(request);
        String authorization = baseService.getAuthorization(reqBody);
        String url = "https://api-mop-wh.open.chinaums.com/v6/poslink/transaction/pay";
        String response = PayUtils.request(url, authorization, reqBody);
        BTOCResult judgeResult = JSONObject.parseObject(response, BTOCResult.class);
        log.info("bTOCPay 返回的信息--------------------------------->{}", response);
        return judgeResult;
    }

    @Override
    public BTOCQueryOrderResult bTOCPayOrderQuery(BTOCPayOrderQueryRequest request) throws Exception {
        PayConfig config = baseService.getConfig();
        if (Objects.isNull(config)) {
            throw new YesBaseException("请配置支付配置!");
        }
        String reqBody = JSONObject.toJSONString(request);
        String authorization = baseService.getAuthorization(reqBody);
        String url = "https://api-mop-wh.open.chinaums.com/v6/poslink/transaction/query";
        String response = PayUtils.request(url, authorization, reqBody);
        BTOCQueryOrderResult judgeResult = JSONObject.parseObject(response, BTOCQueryOrderResult.class);
        log.info("bTOCPayOrderQuery 返回的信息--------------------------------->{}", response);
        return judgeResult;
    }

    @Override
    public Result bTOCRefund(BTOCRefundRequest request) throws Exception {
        PayConfig config = baseService.getConfig();
        if (Objects.isNull(config)) {
            throw new YesBaseException("请配置支付配置!");
        }
        request.checkAndSign(config);
        String reqBody = JSONObject.toJSONString(request);
        String authorization = baseService.getAuthorization(reqBody);
        String url = "https://api-mop-wh.open.chinaums.com/v6/poslink/transaction/refund";
        String response = PayUtils.request(url, authorization, reqBody);
        Result judgeResult = JSONObject.parseObject(response, Result.class);
        log.info("bTOCRefund 返回的信息--------------------------------->{}", response);
        return judgeResult;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy