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

org.yes.tools.pay.service.impl.BaseServiceImpl 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 lombok.extern.slf4j.Slf4j;
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.redis.RedisOps;
import org.yes.tools.pay.service.BasePayService;
import org.yes.tools.pay.service.PayService;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public abstract class BaseServiceImpl implements BasePayService {

    private final RedisOps redisOps;

    private final String keyPrefix;

    private final PayService payService = new PayServiceImpl(this);

    public BaseServiceImpl(RedisOps redisOps, String keyPrefix) {
        this.redisOps = redisOps;
        this.keyPrefix = keyPrefix;
    }

    @Override
    public String getPayBaseUrl() {
        return this.getConfig().getPayBaseUrl();
    }

    public PayConfig getConfig() {
        String appId = redisOps.getValue(keyPrefix + ":" + PayConstants.RedisKey.CHOICE_CONFIG_KEY);
        String json = redisOps.getValue(keyPrefix + ":" + PayConstants.RedisKey.ALL_CONFIG + appId);
        return JSONObject.parseObject(json, PayConfig.class);
    }

    public void setConfig(PayConfig payConfig) {
        String defaultAppId = payConfig.getAppid();
        this.setMultiConfig(payConfig, defaultAppId);
    }

    public Boolean switchConfig(String appId) {
        Boolean exists = redisOps.exists(keyPrefix + ":" + PayConstants.RedisKey.ALL_CONFIG + appId);
        if (exists) {
            redisOps.setValue(keyPrefix + ":" + PayConstants.RedisKey.CHOICE_CONFIG_KEY, appId, 0, null);
            return true;
        } else {
            log.error("无法找到对应【{}】的APPID配置信息,请核实!", appId);
            return false;
        }
    }

    public void setMultiConfig(PayConfig payConfig, String defaultAppId) {
        redisOps.setValue(keyPrefix + ":" + PayConstants.RedisKey.ALL_CONFIG + defaultAppId, JSONObject.toJSONString(payConfig), 0, null);
        redisOps.setValue(keyPrefix + ":" + PayConstants.RedisKey.CHOICE_CONFIG_KEY, defaultAppId, 0, null);
    }

    public void addConfig(String appId, PayConfig payConfig) {
        this.setMultiConfig(payConfig, appId);
    }

    @Override
    public PayService getPayService() {
        return this.payService;
    }

    @Override
    public String getAuthorization(String reqBody) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        PayConfig config = this.getConfig();
        String authorization = PayUtils.getAuthorization(config.getAppid(),
                config.getAppKey(),
                dateFormat.format(new Date()),
                RandomUtil.randomString(32),
                reqBody);
        return authorization;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy