package com.fdkankan.pay.alipay;

import com.fdkankan.pay.alipay.sdk.AlipayConfig;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class AlipayDefaultConfig extends AlipayConfig {
    private static Logger log = LoggerFactory.getLogger("programLog");

    public AlipayDefaultConfig() {}

    /** 支付宝gatewayUrl */
    private String gatewayUrl = "https://openapi.alipay.com/gateway.do";
    /** 商户应用id */
    private String appid = "2021003139665735";
    /** RSA私钥，用于对商户请求报文加签 */
    private String appPrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCazIXAbKGQQ8rev0a7EP2SKEekAJ8fBTsP6nQ6ZOjxF/FtCSF3TLFWdy49hqBil6YlszHeC7mcv9zZubo5prMdQM6R0xlETILC8+NisiXgow1L9JQPb8KoAVrg1kW6nU0O5cKjEHc+s05JmH/05eYVuq27z1Cg3KJZo2kWRaX8H6+AkYeA0qTnL9FIiEsM1H0jMFxnrcJGRhrIJOfrZrgBGjmKtkKdeqY4VY2GKgk+iEibFrBAAX0YbXLHKwvDZfvxQOluUe5/vheue7hIq+sFySfDZ7571G0hMciXANgmtSBPigJ6BR0ZpvK/Xac1GnSMXsrqhFWS/7iYbZgax3BbAgMBAAECggEADaoKEG9GX66sRLdLrg2U5Ibrs/1eDmXuzbvbYKAxs75yUDajHp3FdaV1jnbIx4stXlyQBVKxorWI8Csl1hrx6p4i3dpte4rpa/4NT1P8ZfTeoprHSwmJfD0KYZhodU2XVL5RTQWSERXQzkxI1zxH8BkXZuGo81omdpUnHJEvUq7x0VIuFfPTF53QHzyagoJu/VxTZkpSKoIhJT5NKQnidMxTbSHwKdxSJ4Xiw9rjlDW3PiIip1+wXmcjM2mqVUsMaO+9jxTxe8DCICCGbpo2fg0fnmpmyd+QbErfV8IFaEJsmpz8glk9sGT1COfeDUIIvRRdRz/f0meVUNQX+iZ26QKBgQDmS7V1z1C2LEw4uPvzfJsOXaWn80fvkotpEUhEr8JrGIQrdov8jOyg3JPjdgi5VVnmp606GfZkHWis0AzYtfPmCfge2fDt06juOLuPphx2f5xplUXDyJRldje1GZaV/H8YPnt7gFhSuKfJuIvZmNH9HZmHUCHb24tCQeLMRrst/QKBgQCsE6BPdr6Oatc3g1jBRv06NXyCsA/0CBsAsDRIoiDZVdrbhbNavUtrofBhz3LOSpFPl7+hXFNq1+dzllgMbApTrjEu0bDDwAegSFz248KEqh/snxBbmcoVkcjAOSlr4ej+gacl9ZIPeRXuZfdraK5V7bngGLfsc2B4+ZgUnNh7NwKBgQCiLHhpsQeOch1u6F2MtpfegvXwErL6s4D3CVOSAw9vKYGRtdaYj4jnMs32JR25nbBEbJnhS30mPUuEPrJ9PYy21xhiaWlvoVjaARub6gcrL2wvMg5B9BMEbD6Iiq9vibLamM2OR/BquFx4AjyBjEt76oc+MaDWWYF0tD8JSVnw5QKBgCsKybm2wRmMngLTxvqpkCPkRx3FfmgTMvXsHVfhvge/EvyAKAnVea0Vl7RPmK8DNe/LzHJHTtJV6R4fiCyrRbYCx2inqOh9+ypbj21uMTTP/SZJ72SKRmM40RdL9RF4M7ido7q5/2wBZLv6I3Zpe9ogmbL1W+Q4Q/ResmzNgtTnAoGBAOFhm5n73jFVR9TASxh19Tookj5NvpS4uVdbdosQqlaTX8i9dRs1OHptR11bF/tVDQx3fWqzsBC2SIhjtq74O1QIpg7NulR4S1qKld676VNz/wKzxO6uW43CQ+zc6JitSqoTKvFFZ8H0J1Zb5SU3nYBZEhANzmaAIQjqOSgbD9zS";
    /** 支付宝RSA公钥，用于验签支付宝应答 */
    private String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApg62K3VOPoZm+LA2anUgV6sM40z8W4ST4/iIw16DPAGBQRSYg8USVM/9LOUb0UMN6Nod4SDUTZz4dO5ZltUERA9XJ5md2F5Ny4BK4w+cLKyU2C6Qb8FcacW4IfZjlTRr5dWeWT1AZS28ZvPEh8S8TalqGXWfMDxPuZUhGO5gls/6DGkfv934ODnZ64JOg4bHvhTUcwVSJvlRGXrrrvU4YKC/7hramiJifZ+OC7qQ9ZTAGGHrc80Swl5njpzrnUQvM5k/y6tokQxS+7P0ToqtE0x2na4pWiE0voEW+ZVFUeB8U3x63aFs13pwW5jxRooaEAZ63I48HSfkh3z5VPuwZwIDAQAB";
    /** 同步地址 */
    private String returnUrl = "mobile.html#/payresult/success";
    /** 异步地址 */
    private String notifyUrl = "api/order/pay/alipay/async_notify";

    /**
     * PostContruct是spring框架的注解，在方法上加该注解会在项目启动的时候执行该方法，也可以理解为在spring容器初始化的时候执行该方法。
     */
    @PostConstruct
    public void init() {
        log.info(description());
    }

    public String description() {
        StringBuilder sb = new StringBuilder("\nConfigs{");
        sb.append("支付宝网关: ").append(getGatewayUrl()).append("\n");
        sb.append(", appid: ").append(getAppid()).append("\n");
        sb.append(", 商户RSA私钥: ").append(getKeyDescription(getAppPrivateKey())).append("\n");
        sb.append(", 支付宝RSA公钥: ").append(getKeyDescription(getAlipayPublicKey())).append("\n");
        sb.append(", 签名类型: ").append(getSignType()).append("\n");

        sb.append(", 查询重试次数: ").append(getMaxQueryRetry()).append("\n");
        sb.append(", 查询间隔(毫秒): ").append(getQueryDuration()).append("\n");
        sb.append(", 撤销尝试次数: ").append(getMaxCancelRetry()).append("\n");
        sb.append(", 撤销重试间隔(毫秒): ").append(getCancelDuration()).append("\n");
        sb.append("}");
        return sb.toString();
    }

    private String getKeyDescription(String key) {
        int showLength = 6;
        if (StringUtils.isNotEmpty(key) && key.length() > showLength) {
            return new StringBuilder(key.substring(0, showLength)).append("******")
                    .append(key.substring(key.length() - showLength)).toString();
        }
        return null;
    }

    @Override
    public String getGatewayUrl() {
        return gatewayUrl;
    }

    @Override
    public String getAppid() {
        return appid;
    }

    @Override
    public String getAppPrivateKey() {
        return appPrivateKey;
    }

    @Override
    public String getAlipayPublicKey() {
        return alipayPublicKey;
    }

    @Override
    public String getReturnUrl() {
        return returnUrl;
    }

    @Override
    public String getNotifyUrl() {
        return notifyUrl;
    }
}
