コード例 #1
0
        //static string API_URL = "http://58.246.136.11:8006/wapDetect/gateWay!gate.ac";

        #region 工具方法
        /// <summary>
        /// 调用API
        /// </summary>
        /// <typeparam name="TRequest">向支付接口发送的请求的类型</typeparam>
        /// <typeparam name="TResponse"></typeparam>
        /// <param name="pChannel"></param>
        /// <param name="pRequest"></param>
        /// <returns></returns>
        private static TResponse CallAPI <TRequest, TResponse>(UnionPayChannel pChannel, TRequest pRequest)
            where TRequest : BaseAPIRequest
            where TResponse : BaseAPIResponse, new()
        {
            //参数校验
            if (pChannel == null)
            {
                throw new ArgumentNullException("pChannel");
            }
            if (string.IsNullOrWhiteSpace(pChannel.MerchantID))
            {
                throw new ArgumentNullException("pChannel.MerchantID");
            }
            if (pRequest == null)
            {
                throw new ArgumentNullException("pRequest");
            }
            //
            var reqContent = pRequest.GetContent();
            var merchantID = EncDecUtil.Base64Encrypt(pChannel.MerchantID);       //Base64加密商户ID
            var desKey     = EncDecUtil.GetEncryptKey(pChannel.PacketEncryptKey); //获取实际加密密钥
            //采用Base64(RSA(加密密钥))计算出报文中的加密密钥
            var encryptedDesKey = EncDecUtil.Base64Encrypt(EncDecUtil.RSAEncrypt(pChannel.CertificateFilePath, pChannel.CertificateFilePassword, desKey + "11111111"));
            //采用Base64(3DES(报文))加密报文
            var encryptedContent = EncDecUtil.Base64Encrypt(EncDecUtil.TripleDESEncrypt(desKey, reqContent));
            //
            var requestContent = merchantID + "|" + encryptedDesKey + "|" + encryptedContent;

            using (WebClient wc = new WebClient())
            {
                string   strResponse      = wc.UploadString(IVRGateway.API_URL, requestContent);
                string[] responseSections = strResponse.Split('|');
                string   rspCode          = responseSections[0];
                string   rspContent       = responseSections[1];
                //
                if (rspCode == "1")
                {
                    var    decRspBytes   = EncDecUtil.TripleDESDecrypt(desKey, EncDecUtil.Base64Decrypt(rspContent));
                    string decRspContent = Encoding.UTF8.GetString(decRspBytes);
                    decRspContent = decRspContent.Replace("\0", string.Empty);
                    TResponse rsp = new TResponse();
                    rsp.Load(decRspContent);
                    //
                    return(rsp);
                }
                else
                {
                    string            errorCode = rspContent;
                    string            errorMsg  = Encoding.UTF8.GetString(EncDecUtil.Base64Decrypt(responseSections[2]));
                    UnionPayException ex        = new UnionPayException(errorMsg);
                    ex.Code = errorCode;
                    throw ex;
                }
            }
        }
コード例 #2
0
        public static ReqResponse CallAPI <TRequest>(NegotiationPayChannel pChannel, TRequest pRequest)
            where TRequest : NeBaseAPIRequest
        {
            if (pRequest == null)
            {
                throw new ArgumentNullException("pRequest");
            }
            var    reqContent          = pRequest.GetContent();
            var    merchantID          = JIT.CPOS.BS.Web.ApplicationInterface.Negotiation.Util.EncDecUtil.Base64Encrypt(pChannel.MerchantID);
            string CertificateFilePath = HttpContext.Current.Server.MapPath("~/" + pChannel.CertificateFilePath);

            RSAGenerator rsa    = new RSAGenerator();
            string       key    = rsa.getPublicKey();
            var          desKey = EncDecUtil.GetEncryptKey(key); //获取实际加密密钥
            //采用Base64(RSA(加密密钥))计算出报文中的加密密钥
            var encryptedDesKey = EncDecUtil.Base64Encrypt(EncDecUtil.RSAEncrypt(CertificateFilePath, pChannel.CertificateFilePassword, desKey + "11111111"));
            //采用Base64(3DES(报文))加密报文
            var encryptedContent = EncDecUtil.Base64Encrypt(EncDecUtil.TripleDESEncrypt(desKey, reqContent));
            //使用md5生成摘要
            var md5            = EncDecUtil.CreateMD5Encod(reqContent);
            var requestContent = merchantID + "|" + encryptedDesKey + "|" + encryptedContent + "|" + md5;

            using (WebClient wc = new WebClient())
            {
                string   strResponse      = wc.UploadString(url, requestContent);
                string[] responseSections = strResponse.Split('|');
                string   rspCode          = responseSections[0];
                string   rspContent       = responseSections[1];
                if (rspCode == "1")
                {
                    var    decRspBytes   = EncDecUtil.TripleDESDecrypt(desKey, EncDecUtil.Base64Decrypt(rspContent));
                    string decRspContent = Encoding.UTF8.GetString(decRspBytes);
                    decRspContent = decRspContent.Replace("\0", string.Empty);
                    ReqResponse req = new ReqResponse();
                    req.ResultCode = rspCode;
                    req.Data       = decRspContent;
                    return(req);
                }
                else
                {
                    string            errorCode = rspContent;
                    string            errorMsg  = Encoding.UTF8.GetString(EncDecUtil.Base64Decrypt(responseSections[2]));
                    UnionPayException ex        = new UnionPayException(errorMsg);
                    ex.Code = errorCode;
                    Log.Loggers.Exception(new Log.ExceptionLogInfo(ex)
                    {
                        ErrorMessage = ex.ToString()
                    });
                    ReqResponse req = new ReqResponse();
                    req.ResultCode = errorCode;
                    req.Message    = errorMsg;
                    return(req);
                }
            }
        }