Example #1
0
        private bool VerifySignature(TradeJournal tradeSource, IDictionary <string, string> paymentDetail)
        {
            string signParams = null;

            if (paymentDetail.ContainsKey(API_TradePayResult))
            {
                signParams = paymentDetail[API_TradePayResult];
                var alipayResponse = paymentDetail[API_TradePayResult].ConvertEntity <AlipayProvider.AlipayResult.AlipayResponse>();

                if (alipayResponse.Code != API_Code_Success || alipayResponse.Msg != API_MsgStatus_Success ||
                    alipayResponse.OutTradeNO != tradeSource.TradeCode)
                {
                    return(false);
                }

                var apiMethod = paymentDetail[API_MethodName];
                if (apiMethod == API_Query)
                {
                    if (alipayResponse.TradeStatus != API_TradeStatus_Success)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (false == string.IsNullOrEmpty(alipayResponse.AppId) && alipayResponse.AppId != this.AppId)
                    {
                        return(false);
                    }
                    if (false == string.IsNullOrEmpty(alipayResponse.TradeStatus) && alipayResponse.TradeStatus != API_TradeStatus_Success)
                    {
                        return(false);
                    }
                }
            }
            else
            {
                var ignoreParams  = new string[] { "sign", "sign_type" };
                var signParamsDic = new Dictionary <string, string>();
                foreach (var item in paymentDetail)
                {
                    if (false == ignoreParams.Contains(item.Key) && null != item.Value)
                    {
                        signParamsDic[item.Key] = item.Value.ToString();
                    }
                }

                signParams = BuildSignParams(signParamsDic).ToString();
            }

            if (string.IsNullOrEmpty(signParams))
            {
                return(false);
            }

            var isVerified = RSAProvider.VerifySignature(signParams, this.PublicKey, paymentDetail["sign"]);

            return(isVerified);
        }