Ejemplo n.º 1
0
        protected override void SetSign()
        {
            var sortTempdic = new SortedDictionary <string, string>(Paras);
            var tempdic     = AliPayFunction.FilterPara(sortTempdic);
            var prestr      = AliPayFunction.CreateLinkString(tempdic);

            Log.Loggers.Debug(new Log.DebugLogInfo()
            {
                Message = "Sign字符串:" + prestr
            });
            switch (SignType)
            {
            case "MD5":
                Sign = AlipayMD5.Sign(prestr, Channel.MD5Key, InputCharset);
                break;

            case "RSA":
                Sign = RSAFromPkcs8.Sign(prestr, Channel.RSA_PrivateKey, InputCharset);
                break;

            case "0001":
                Sign = RSAFromPkcs8.Sign(prestr, Channel.RSA_PrivateKey, InputCharset);
                break;

            default:
                throw new Exception("未知的加密方式:" + SignType);
            }
            this.SignType = "MD5";
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取AliPay的支付跳转URL
        /// </summary>
        /// <param name="pRequest"></param>
        /// <returns></returns>
        public static AliPayWapQueryOrderResponse GetQueryTradeResponse(AliPayWapTokenRequest pRequest, AliPayChannel pChannel)
        {
            AliPayWapQueryOrderResponse orderresponse = new AliPayWapQueryOrderResponse();
            var str = BaseGeteway.GetResponseStr(pRequest, Geteway);

            if (!string.IsNullOrEmpty(pChannel.RSA_PublicKey))
            {
                var token = Res_dataDecrypt(str, pChannel, pRequest.InputCharset);
            }
            TokenResponse response = new TokenResponse();
            var           tempdic  = AliPayFunction.ParseResponse(str, pRequest.SecID, pChannel.RSA_PrivateKey, pRequest.InputCharset);

            response.Load(tempdic);
            orderresponse.TokenResponse = response;
            AliPayWapQueryTradeRequest tradeRequeset = new AliPayWapQueryTradeRequest(pChannel)
            {
                RequestToken = response.Token
            };

            orderresponse.RedirectURL = CreateUrl(tradeRequeset);
            orderresponse.IsSucess    = !string.IsNullOrEmpty(response.ResData);
            if (!orderresponse.IsSucess)
            {
                orderresponse.Message = response.ResError;
            }
            return(orderresponse);
        }
Ejemplo n.º 3
0
 public string GetContent()
 {
     SetService();
     SetReqData();
     SetSign();
     if (!IsValid())
     {
         throw new Exception("缺少参数或者参数未赋值。");
     }
     return(AliPayFunction.CreateLinkString(Paras, Encoding.GetEncoding(this.InputCharset)));
 }
Ejemplo n.º 4
0
        public static RoyaltyResponse GetRoyaltyResponse(RoyaltyRequest pRequest)
        {
            var str = BaseGeteway.GetResponseStr(pRequest, AliPayConfig.RoyaltyUrl);

            Log.Loggers.Debug(new Log.DebugLogInfo()
            {
                Message = "分润调用接口:" + str
            });
            RoyaltyResponse response = new RoyaltyResponse();

            if (!string.IsNullOrEmpty(str))
            {
                var tempdic = AliPayFunction.ParseResponse(str);
                response.Load(tempdic);
            }
            return(response);
        }
Ejemplo n.º 5
0
        public static string GetResponseStr(RequestScanEntity pRequest, string scanDetailJson, string key)
        {
            Encoding code = Encoding.GetEncoding("utf-8");

            pRequest.biz_content = scanDetailJson;
            string requestPar = AliPayFunction.GetSignContent(pRequest.Paras);

            string sign = RSAFromPkcs8.Sign(requestPar, key, "utf-8");                     //生成签名
            //构造请求地址
            string strUrl = Geteway + requestPar + "&sign=" + HttpUtility.UrlEncode(sign); //请求参数

            //请求远程HTTP
            string strResult = "";

            try
            {
                //设置HttpWebRequest基本信息
                HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
                myReq.Method      = "post";
                myReq.ContentType = "application/x-www-form-urlencoded";

                //发送POST数据请求服务器
                HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
                Stream          myStream  = HttpWResp.GetResponseStream();

                //获取服务器返回信息
                StreamReader  reader       = new StreamReader(myStream, code);
                StringBuilder responseData = new StringBuilder();
                String        line;
                while ((line = reader.ReadLine()) != null)
                {
                    responseData.Append(line);
                }

                //释放
                myStream.Close();

                strResult = responseData.ToString();
            }
            catch (Exception exp)
            {
                throw exp;
            }

            return(HttpUtility.UrlDecode(strResult));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 获取Sign
        /// </summary>
        protected override void SetSign()
        {
            var sortTempdic = new SortedDictionary <string, string>(Paras);
            var tempdic     = AliPayFunction.FilterPara(sortTempdic);
            var prestr      = AliPayFunction.CreateLinkString(tempdic);

            switch (SecID)
            {
            case "MD5":
                Sign = AlipayMD5.Sign(prestr, Channel.MD5Key, InputCharset);
                break;

            case "RSA":
                Sign = RSAFromPkcs8.Sign(prestr, Channel.RSA_PrivateKey, InputCharset);
                break;

            case "0001":
                Sign = RSAFromPkcs8.Sign(prestr, Channel.RSA_PrivateKey, InputCharset);
                break;

            default:
                throw new Exception("未知的加密方式:" + SecID);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 验签,返回token字符串
        /// </summary>
        /// <param name="strResult">创建订单返回信息</param>
        /// <param name="sellprivatekey">商户私钥</param>
        /// <param name="input_charset">编码格式</param>
        /// <returns>token字符串</returns>
        private static string Res_dataDecrypt(string strResult, AliPayChannel pChannel, string input_charset, bool needCheck = false)
        {
            //分解返回数据 用&拆分赋值给result
            string[] result = strResult.Split('&');

            //提取res_data参数
            string res_data = string.Empty;

            for (int i = 0; i < result.Length; i++)
            {
                string data = result[i];
                if (data.IndexOf("res_data=") >= 0)
                {
                    res_data = data.Replace("res_data=", string.Empty);

                    //解密(用"商户私钥"对"res_data"进行解密)
                    res_data = AliPayFunction.Decrypt(res_data, pChannel.RSA_PrivateKey, input_charset);

                    //res_data 赋值 给 result[0]
                    result[i] = "res_data=" + res_data;
                }
            }

            //创建待签名数组
            SortedDictionary <string, string> sd = new SortedDictionary <string, string>();
            int    count  = 0;
            string sparam = "";
            string key    = "";
            string value  = "";

            for (int i = 0; i < result.Length; i++)
            {
                sparam = result[i];
                count  = sparam.IndexOf('=');
                key    = sparam.Substring(0, count);
                value  = sparam.Substring(count + 1, sparam.Length - (count + 1));
                sd.Add(key, value);
            }

            string sign = sd["sign"];

            //配置待签名数据
            Dictionary <string, string> dicData = AliPayFunction.FilterPara(sd);
            string req_Data = AliPayFunction.CreateLinkString(dicData);

            //验签,使用支付宝公钥
            bool vailSign = RSAFromPkcs8.Verify(req_Data, sign, pChannel.RSA_PublicKey, input_charset);

            if (!needCheck)
            {
                vailSign = true;
            }
            if (vailSign)//验签通过
            {
                //得到 request_token 的值
                string token = string.Empty;
                try
                {
                    token = AliPayFunction.GetStrForXmlDoc(res_data, "direct_trade_create_res/request_token");
                }
                catch (Exception ex)
                {
                    throw new Exception("方法:AliPayFunction.GetStrForXmlDoc()解析数据失败", ex);
                }
                return(token);
            }
            else
            {
                throw new Exception("返回的数据未通过验证,验签失败");
            }
        }