package com.fanmei.pay4j.alipay.sign;

import com.fanmei.pay4j.alipay.config.AlipayConfig;
import com.fanmei.pay4j.common.Constants;
import com.fanmei.pay4j.common.SignType;
import com.fanmei.pay4j.sign.AbstractSignature;
import com.fanmei.pay4j.util.DigestUtil;
import com.fanmei.pay4j.util.StringUtil;

import javax.annotation.Nonnull;
import java.nio.charset.Charset;

/**
 * 支付宝签名
 * Created by rongtian on 16/7/7.
 */
public class AlipaySignature extends AbstractSignature {

    private AlipayConfig alipayConfig;
    private final SignType signType;

    public AlipaySignature(AlipayConfig alipayConfig) {
        this.alipayConfig = alipayConfig;
        this.signType = alipayConfig.getSignType();
    }

    @Override
    public String sign(Object obj, Charset charset, boolean encode) {
        StringBuilder sb = join(obj, charset, encode);
        return sign(sb.toString(), charset);
    }

    /**
     * alipay专用
     * @param obj
     * @param charset
     * @param encode
     * @return
     */
    public String signWithQuota(Object obj, Charset charset, boolean encode) {
        StringBuilder sb = join(obj, charset, encode, true);
        return sign(sb.toString(), charset);
    }

    @Override
    public boolean isValidSign(Object obj, String sign) {
        StringBuilder str = join(obj, Constants.UTF_8, false);
        if (signType.isMd5()) {
            return sign.equalsIgnoreCase(DigestUtil.MD5(str.toString()).toUpperCase());
        } else if (signType.isRsa()) {
            return RSA.verify(str.toString(), sign, alipayConfig.getPublicKey(), Constants.UTF_8.name());
        }
        return false;
    }


    private StringBuilder join(Object obj, Charset charset, boolean encode, boolean withQuota) {
        return new StringBuilder(StringUtil.toJoinForSign(obj, charset, encode, withQuota));
    }


    private String sign(@Nonnull String str, Charset charset) {
        if (signType.isMd5()) {
            return DigestUtil.MD5(str).toUpperCase();
        } else if (signType.isRsa()) {
            return RSA.sign(str, alipayConfig.getPrivateKey(), charset.name());
        }
        return null;
    }
}

@@ -940,16 +940,22 @@
 aram obj
+ %E7%AD%BE%E5%90%8D%E7%9A%84%E5%AF%B9%E8%B1%A1
 %0A     * 
@@ -968,16 +968,20 @@
  charset
+ %E5%AD%97%E7%AC%A6%E9%9B%86
 %0A     * 
@@ -993,16 +993,21 @@
 m encode
+ %E6%98%AF%E5%90%A6%E7%BC%96%E7%A0%81
 %0A     * 
@@ -1013,16 +1013,19 @@
  @return
+ %E7%AD%BE%E5%90%8D
 %0A     */

fix javadoc