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

eleme.openapi.h5.sdk.pay.service.base.BasePayService Maven / Gradle / Ivy

There is a newer version: 1.0.11-RELEASE
Show newest version
package eleme.openapi.h5.sdk.pay.service.base;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import eleme.openapi.h5.sdk.pay.model.request.CloseRequest;
import eleme.openapi.h5.sdk.pay.model.request.DownloadBillRequest;
import eleme.openapi.h5.sdk.pay.model.request.PayRequest;
import eleme.openapi.h5.sdk.pay.model.request.QueryPayRequest;
import eleme.openapi.h5.sdk.pay.model.request.QueryRefundRequest;
import eleme.openapi.h5.sdk.pay.model.request.RefundRequest;
import eleme.openapi.h5.sdk.pay.model.response.CloseResponse;
import eleme.openapi.h5.sdk.pay.model.response.DownloadBillResponse;
import eleme.openapi.h5.sdk.pay.model.response.QueryPayResponse;
import eleme.openapi.h5.sdk.pay.model.response.QueryRefundResponse;
import eleme.openapi.h5.sdk.pay.model.response.RefundResponse;
import eleme.openapi.h5.sdk.pay.service.PayService;
import eleme.openapi.h5.sdk.pay.utils.PaySignUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 饿了么支付入口base实现,机构侧继承该类
 */
public abstract class BasePayService implements PayService {

    /**
     * 发起支付
     * @param body post请求体
     * @param request http请求
     * @param response http响应
     * @return 饿了么不感知响应结果,机构侧处理跳机构收银台
     */
    @Override
    @RequestMapping("/pay")
    public String pay(@RequestBody(required = false) String body, HttpServletRequest request, HttpServletResponse response) {
        // 1.获取请求内容
        String content = body;
        if (StringUtils.equals(request.getMethod(), HttpMethod.GET.name())) {
            Map params = this.params(request);
            content = JSON.toJSONString(params);
        }

        // 2.签名验证
        boolean verifyResult = PaySignUtil.verify(content, getElemePublicKey());
        if (!verifyResult) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("returnCode", "FAIL");
            jsonObject.put("returnMsg", "签名验证失败");
            return PaySignUtil.sign(jsonObject, getPrivateKey());
        }

        // 3.业务逻辑处理
        PayRequest payRequest = JSON.parseObject(content, PayRequest.class);
        return pay(payRequest, request, response);
    }

    private Map params(HttpServletRequest request) {
        Map ret = new HashMap<>();
        Enumeration names = request.getParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            ret.put(name, request.getParameter(name));
        }

        return ret;
    }

    /**
     * 查询支付结果
     * @param content 请求内容
     * @return 响应结果
     */
    @Override
    @RequestMapping("/queryPay")
    public String queryPay(@RequestBody String content) {
        return execute(content, QueryPayRequest.class, this::queryPay);
    }

    /**
     * 关单
     * @param content 请求内容
     * @return 响应结果
     */
    @Override
    @RequestMapping("/close")
    public String close(@RequestBody String content) {
        return execute(content, CloseRequest.class, this::close);
    }

    /**
     * 发起退款
     * @param content 请求内容
     * @return 响应结果
     */
    @Override
    @RequestMapping("/refund")
    public String refund(@RequestBody String content) {
        return execute(content, RefundRequest.class, this::refund);
    }

    /**
     * 查询退款结果
     * @param content 请求内容
     * @return 响应结果
     */
    @Override
    @RequestMapping("/queryRefund")
    public String queryRefund(@RequestBody String content) {
        return execute(content, QueryRefundRequest.class, this::queryRefund);
    }

    /**
     * 查询账单地址
     * @param content 请求内容
     * @return 响应结果
     */
    @Override
    @RequestMapping("/downloadBill")
    public String downloadBill(@RequestBody String content) {
        return execute(content, DownloadBillRequest.class, this::downloadBill);
    }

    /**
     * 支付通用服务逻辑执行
     *
     * @param content  请求体
     * @param reqClass 业务参数类型
     * @param function 业务逻辑
     * @param  请求类型
     * @param  响应类型
     * @return 响应结果
     */
    private  String execute(String content, Class reqClass, Function function) {
        // 1.签名验证
        boolean verifyResult = PaySignUtil.verify(content, getElemePublicKey());
        if (!verifyResult) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("returnCode", "FAIL");
            jsonObject.put("returnMsg", "签名验证失败");
            return PaySignUtil.sign(jsonObject, getPrivateKey());
        }

        // 2.业务逻辑处理
        Req request = JSON.parseObject(content, reqClass);
        Resp response = function.apply(request);

        // 3.业务结果做加签
        return PaySignUtil.sign(response, getPrivateKey());
    }

    /**
     * 获取饿了么公钥
     * @return 饿了么公钥
     */
    public abstract String getElemePublicKey();

    /**
     * 获取机构私钥
     * @return 机构私钥
     */
    public abstract String getPrivateKey();

    /**
     * 发起支付
     * @param payRequest 支付请求
     * @param httpServletRequest http请求
     * @param httpServletResponse http响应
     * @return 饿了么不感知响应结果,机构侧处理跳机构收银台
     */
    public abstract String pay(PayRequest payRequest, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse);

    /**
     * 查询支付结果
     *
     * @param request 请求
     * @return 响应
     */
    public abstract QueryPayResponse queryPay(QueryPayRequest request);

    /**
     * 关单
     *
     * @param request 请求
     * @return 响应
     */
    public abstract CloseResponse close(CloseRequest request);

    /**
     * 退款
     *
     * @param request 请求
     * @return 响应
     */
    public abstract RefundResponse refund(RefundRequest request);

    /**
     * 查询退款结果
     *
     * @param request 请求
     * @return 响应
     */
    public abstract QueryRefundResponse queryRefund(QueryRefundRequest request);

    /**
     * 下载账单
     *
     * @param request 请求
     * @return 响应
     */
    public abstract DownloadBillResponse downloadBill(DownloadBillRequest request);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy