Example #1
0
        private bool Verify(HttpRequestBase request, NameValueCollection requestVal, out AliPayReturnModel model)
        {
            bool result = false;
            SortedDictionary <string, string> sortedDic = new SortedDictionary <string, string>();

            foreach (var item in requestVal.AllKeys)
            {
                if (item.ToLower() != "sign" && item.ToLower() != "sign_type" && !string.IsNullOrEmpty(item))
                {
                    sortedDic.Add(item, requestVal[item]);
                }
            }

            string requestSign     = requestVal["sign"];
            string requestSigntype = requestVal["sign_type"];
            string param           = CreateURLParamString(sortedDic);

            EnumSignType signType = requestSigntype == "MD5" ? EnumSignType.MD5
                                    : requestSigntype == "RSA" ? EnumSignType.RSA
                                    : EnumSignType.MD5;

            if (signType == EnumSignType.MD5)
            {
                string sign = BuildRequestsign(param, signType);
                if (requestSign.Equals(sign))
                {
                    result = true;
                }
            }
            else
            {
                result = RSAFromPkcs8.verify(param, requestSign, AlipayConfig.ALIPay_RSA_ALI_PUBLICKEY, "utf-8");
            }

            string responseText = GetResponseTxt(requestVal["notify_id"]);

            bool resultVal = result && responseText == "true";

            if (resultVal)
            {
                model = new AliPayReturnModel()
                {
                    OutTradeNo  = request.Form["out_trade_no"],
                    TradeNo     = request.Form["trade_no"],
                    TradeStatus = request.Form["trade_status"]
                };

                decimal total_fee;
                decimal.TryParse(request.Form["total_fee"], out total_fee);
                model.TotalFee = total_fee;
            }
            else
            {
                model = null;
            }

            return(resultVal);
        }
Example #2
0
 private string RSASign(string prestr, string privateKey, string input_charset)
 {
     try
     {
         return(RSAFromPkcs8.sign(prestr, privateKey, input_charset));
     }
     catch (Exception e)
     {
         return(e.Message);
     }
 }