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 = "2017092508923628";
    /** RSA私钥，用于对商户请求报文加签 */
    private String appPrivateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDKMiR/Pq/DghjqklxyvJlAKF6jCWJFmYLrmRavtmmqsE+3ANNYkDEGfuOZaYWwMrL5crXIKJNSQhAOthiD+VNGFpi2WgE0hveZw76x+ljBQzUcf+AB4pnvQ+hOqBgu3gLa43koHbVGwmCw+CBRwJHb2R69XyTHZjmRfbtAFUsyeUd3ehgSBGGo09djgYUvb9jbLygKDrzFVNi3MRaSa9yasKP5rXyT8r1GQVORIqQdtcEMx7uulv344s8Rz8B4fyiShM386lldp9Q5w4uB3B+pzsPBhCvdshOjBbvCrZ4kL1WOmc9+bCrqaGDrCAvEGHthNNle7zy7VTEFFCXAT97RAgMBAAECggEAHioG2djPPdp9vxjgo9lQ/4pGPApdV8bECkPHZ6VN9EHjUojT/sVcGvz5FVzxrcySeLZNOjQbbk0euiMeWynp623ZkHKy9VQhQhY6r7CKZyKQnNTS5VUZ4+D8midnnC5QvLjszThJX+oFpMEPiUKyP9sPQ+8avIialALu2WuT0U6UoXvJ/mVWgxz6F3ejLVYY8seC8Wp/5rnpkXBQgQZsc3FK4HDNPvMMKAw2V5tFls01xXC2ugnxeJw/9KicskFsux9o0P20Psa7eDYJt9H8NIWucZJm7bjP0GW/RhrQ/yRZekj3UyHcbvzBmATcBy47dZlYGLeGGQX5HbmioHxe8QKBgQDkyWn4OcVgfGhiseAnKcNIUwEIbgWRek4jzXaTvdhK3fp+SrjQgupSk0U7StQJJw/XMs3O75vSF04yyYhPH3Tb3mVnDAF9KdTXsvZhMbdJs8MX+DO16e8uQZiwBlE1kPFhVN7ENEYBXTT+1L20iQaDFjeRCCu+ArehuszvZNe5DQKBgQDiPwhBuMpiRx1RVX8Mq2KCUTv7Zin4H7JcUtwoq5m8eQhondBPl21aW6AnJt7LgannlSb7n/zwRamlw2g3ORe7eS3Uvu/lvzpT7g60mA16pQJnICTnYKq9jKQRG3wWnuKA69JpDTbaUrNu8wJBVwcc7bo7y5R5PAEiKd53rtbD1QKBgFEOdwfl1+MWUJle8c6OQky2c7A74qcnENYobjFr2ly0BpKGALKHH2PAH5AhM7xGZZtVuYruY2NstFzUAAIgX/Oh+IUowMJKdskw84994LUxBhbQ7ekuh3n47ZK3uj/jKVHVktMqvf6vuh3q24dB4NfCoE82+OBp8AtRtle+f5tlAoGBAKcUQXa3js4REneUGeUzU+ZFM3Sut+PDFKbVkjx+rWPgRfNsR//fJiSKcbU4IWRMdzrMjSNZvSN7bpM5wWa2xO3vyYbJEsSp+dstfLKlb57Y6PNba6VrX+vnRupTT8osv7zRamumFJIabORMlafHKzuT2+p4p0b4pyWhoebkuTmZAoGAW1cUS7F9YTZU0IhYRqo6Gtw5qQz7FP3f+vFdwhiCjY2TwxVCCFyIzWd0l6sgFmkg8qqdCwz3Us3779YhPnTDtOoIVUbqwDCVpD1/em/XJWWXB/LUZ0BKh8AxzPo/bD++jnLasyzR+Ht/tuw2itkduucCEMkbu75QOB7w2ETLDtw=";
    /** 支付宝RSA公钥，用于验签支付宝应答 */
    private String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhENfMzTcppdAFrfiF1e1lSO4+cWaA+5vVsZbLvlWRsxiJRWqIPskTsjbsfugK9imt980LyIVqZ1R1IT7xmHq1p8ccsINNBFNTj651T3f1O4cjKkLCry3xCXYnxrb2ONv8fDt02ubbNOgFjk0PMCfeTVI9sRKNsMt37Zj9t3SKrDHjHkByKDFkLrho10n+jwG29jhqkZFykqg+5MJ7PWDblMIds0VzYjEMDThDqlIJ8oX964bowJuRLNFXQ4so1CGHulkuUkUD17LMvjOeatkdb1wi4ZyYV/s9YbCjtlzuAbCx+Di8YiZ+QOoSfnvYmsV5eiydwPjJ9E6RGi+ZFNymwIDAQAB";
    /** 同步地址 */
    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;
    }
}
