예제 #1
0
        public override PaymentInfo EnterprisePay(EnterprisePayPara para)
        {
            RequestHandler requestHandler = new RequestHandler();
            PaymentInfo    paymentInfo    = new PaymentInfo();
            string         empty          = string.Empty;
            Config         config         = Utility <Config> .GetConfig(base.WorkDirectory);

            if (string.IsNullOrEmpty(config.AppId))
            {
                throw new PluginException("未设置AppId");
            }
            if (string.IsNullOrEmpty(config.MCHID))
            {
                throw new PluginException("未设置MCHID");
            }
            if (string.IsNullOrWhiteSpace(config.pkcs12))
            {
                throw new PluginConfigException("未设置商户证书");
            }
            requestHandler.SetKey(config.Key);
            string noncestr = TenPayUtil.GetNoncestr();

            requestHandler.SetParameter("partner_trade_no", para.out_trade_no);
            int num = Convert.ToInt32(para.amount * new decimal(100));

            requestHandler.SetParameter("amount", num.ToString());
            requestHandler.SetParameter("openid", para.openid);
            requestHandler.SetParameter("mch_appid", config.AppId);
            requestHandler.SetParameter("mchid", config.MCHID);
            requestHandler.SetParameter("nonce_str", noncestr);
            string str = "NO_CHECK";

            if (para.check_name)
            {
                str = "OPTION_CHECK";
            }
            requestHandler.SetParameter("check_name", str);
            requestHandler.SetParameter("desc", para.desc);
            requestHandler.SetParameter("spbill_create_ip", (string.IsNullOrWhiteSpace(para.spbill_create_ip) ? "222.240.184.122" : para.spbill_create_ip));
            string str1 = requestHandler.CreateMd5Sign("key", config.Key);

            requestHandler.SetParameter("sign", str1);
            string str2 = string.Concat(base.WorkDirectory, "\\", config.pkcs12);

            if (!File.Exists(str2))
            {
                throw new PluginException("未找到商户证书文件");
            }
            string str3   = requestHandler.ParseXML();
            string empty1 = string.Empty;

            try
            {
                empty1 = TenPayV3.transfers(str3, str2, config.MCHID);
            }
            catch (Exception exception)
            {
                throw new PluginException(string.Concat("企业付款时出错:", exception.Message));
            }
            XDocument xDocument = XDocument.Parse(empty1);

            if (xDocument == null)
            {
                throw new PluginException(string.Concat("企业付款时出错:", str3));
            }
            XElement xElement  = xDocument.Element("xml").Element("return_code");
            XElement xElement1 = xDocument.Element("xml").Element("return_msg");

            if (xElement == null)
            {
                throw new PluginException("企业付款时,返回参数异常");
            }
            if (!(xElement.Value == "SUCCESS"))
            {
                throw new PluginException(string.Concat("企业付款时,接口返回异常:", xElement1.Value));
            }
            xElement1 = xDocument.Element("xml").Element("result_code");
            XElement xElement2 = xDocument.Element("xml").Element("err_code_des");

            if (!(xElement1.Value == "SUCCESS"))
            {
                throw new PluginException(string.Concat("企业付款时,接口返回异常:", xElement2.Value));
            }
            string value  = xDocument.Element("xml").Element("payment_no").Value;
            string value1 = xDocument.Element("xml").Element("partner_trade_no").Value;
            string value2 = xDocument.Element("xml").Element("payment_time").Value;

            paymentInfo.OrderIds = new List <long>()
            {
                long.Parse(value1)
            };
            paymentInfo.TradNo    = value;
            paymentInfo.TradeTime = new DateTime?(DateTime.Parse(value2));
            return(paymentInfo);
        }
예제 #2
0
        public override PaymentInfo EnterprisePay(EnterprisePayPara para)
        {
            //创建请求对象
            RequestHandler reqHandler  = new RequestHandler();
            PaymentInfo    paymentInfo = new PaymentInfo();
            string         strResult   = string.Empty;
            Config         payConfig   = Utility <Config> .GetConfig(WorkDirectory);

            if (string.IsNullOrEmpty(payConfig.AppId))
            {
                throw new PluginException("未设置AppId");
            }
            if (string.IsNullOrEmpty(payConfig.MCHID))
            {
                throw new PluginException("未设置MCHID");
            }
            if (string.IsNullOrWhiteSpace(payConfig.pkcs12))
            {
                throw new PluginConfigException("未设置商户证书");
            }

            //-----------------------------
            //设置请求参数
            //-----------------------------

            reqHandler.SetKey(payConfig.Key);
            var nonceStr = TenPayUtil.GetNoncestr();

            reqHandler.SetParameter("partner_trade_no", para.out_trade_no);
            reqHandler.SetParameter("amount", Convert.ToInt32((para.amount * 100)).ToString());
            reqHandler.SetParameter("openid", para.openid);
            reqHandler.SetParameter("mch_appid", payConfig.AppId);
            reqHandler.SetParameter("mchid", payConfig.MCHID);
            reqHandler.SetParameter("nonce_str", nonceStr);
            string checkNameOption = "NO_CHECK";

            if (para.check_name)
            {//是否检测真实姓名
                checkNameOption = "OPTION_CHECK";
            }
            reqHandler.SetParameter("check_name", checkNameOption);
            reqHandler.SetParameter("desc", para.desc);
            reqHandler.SetParameter("spbill_create_ip", string.IsNullOrWhiteSpace(para.spbill_create_ip) ? "222.240.184.122" : para.spbill_create_ip);

            string sign = reqHandler.CreateMd5Sign("key", payConfig.Key);//按约定规则生成MD5,规则参考接口文档

            reqHandler.SetParameter("sign", sign);

            var pkcs12 = WorkDirectory + "\\" + payConfig.pkcs12;

            if (!System.IO.File.Exists(pkcs12))
            {
                throw new PluginException("未找到商户证书文件");
            }

            string strXml = reqHandler.ParseXML();
            string result = string.Empty;

            try
            {
                result = TenPayV3.transfers(strXml, pkcs12, payConfig.MCHID);//调用统一接口
            }
            catch (Exception ex)
            {
                throw new PluginException("企业付款时出错:" + ex.Message);
            }
            XDocument xmlDocument = XDocument.Parse(result);

            if (xmlDocument == null)
            {
                throw new PluginException("企业付款时出错:" + strXml);
            }

            XElement e_return = xmlDocument.Element("xml").Element("return_code");
            XElement e_result = xmlDocument.Element("xml").Element("return_msg");

            if (e_return == null)
            {
                throw new PluginException("企业付款时,返回参数异常");
            }
            //处理返回时先判断协议错误码,再业务,最后交易状态
            if (e_return.Value == "SUCCESS")
            {
                e_result = xmlDocument.Element("xml").Element("result_code");
                XElement e_errdes = xmlDocument.Element("xml").Element("err_code_des");
                if (e_result.Value == "SUCCESS")
                {
                    //微信单号
                    string payment_no = xmlDocument.Element("xml").Element("payment_no").Value;
                    //商户单号
                    string partner_trade_no = xmlDocument.Element("xml").Element("partner_trade_no").Value;
                    string payment_time     = xmlDocument.Element("xml").Element("payment_time").Value;

                    //业务处理
                    paymentInfo.OrderIds = new List <long> {
                        long.Parse(partner_trade_no)
                    };
                    paymentInfo.TradNo    = payment_no;
                    paymentInfo.TradeTime = DateTime.Parse(payment_time);
                }
                else
                {
                    throw new PluginException("企业付款时,接口返回异常:" + e_errdes.Value);
                }
            }
            else
            {
                throw new PluginException("企业付款时,接口返回异常:" + e_result.Value);
            }
            return(paymentInfo);
        }
예제 #3
0
        public override PaymentInfo ProcessRefundFee(PaymentPara para)
        {
            RequestHandler requestHandler = new RequestHandler();
            PaymentInfo    paymentInfo    = new PaymentInfo();
            string         empty          = string.Empty;
            Config         config         = Utility <Config> .GetConfig(base.WorkDirectory);

            if (string.IsNullOrEmpty(config.AppId))
            {
                throw new PluginException("未设置AppId");
            }
            if (string.IsNullOrEmpty(config.MCHID))
            {
                throw new PluginException("未设置MCHID");
            }
            if (string.IsNullOrWhiteSpace(config.pkcs12))
            {
                throw new PluginConfigException("未设置商户证书");
            }
            requestHandler.SetKey(config.Key);
            string noncestr = TenPayUtil.GetNoncestr();

            requestHandler.SetParameter("out_trade_no", para.out_trade_no);
            requestHandler.SetParameter("out_refund_no", para.out_refund_no);
            int num = Convert.ToInt32(para.total_fee * new decimal(100));

            requestHandler.SetParameter("total_fee", num.ToString());
            num = Convert.ToInt32(para.refund_fee * new decimal(100));
            requestHandler.SetParameter("refund_fee", num.ToString());
            requestHandler.SetParameter("op_user_id", config.MCHID);
            requestHandler.SetParameter("appid", config.AppId);
            requestHandler.SetParameter("mch_id", config.MCHID);
            requestHandler.SetParameter("nonce_str", noncestr);
            string str = requestHandler.CreateMd5Sign("key", config.Key);

            requestHandler.SetParameter("sign", str);
            string str1 = string.Concat(base.WorkDirectory, "\\", config.pkcs12);

            if (!File.Exists(str1))
            {
                throw new PluginException("未找到商户证书文件");
            }
            string str2   = requestHandler.ParseXML();
            string empty1 = string.Empty;

            try
            {
                empty1 = TenPayV3.Refund(str2, str1, config.MCHID);
            }
            catch (Exception exception)
            {
                throw new PluginException(string.Concat("原路返回退款时出错:", exception.Message));
            }
            XDocument xDocument = XDocument.Parse(empty1);

            if (xDocument == null)
            {
                throw new PluginException(string.Concat("原路返回退款时出错:", str2));
            }
            XElement xElement  = xDocument.Element("xml").Element("return_code");
            XElement xElement1 = xDocument.Element("xml").Element("return_msg");

            if (xElement == null)
            {
                throw new PluginException("原路返回退款时,返回参数异常");
            }
            if (!(xElement.Value == "SUCCESS"))
            {
                throw new PluginException(string.Concat("原路返回退款时,接口返回异常:", xElement1.Value));
            }
            xElement1 = xDocument.Element("xml").Element("result_code");
            XElement xElement2 = xDocument.Element("xml").Element("err_code_des");

            if (!(xElement1.Value == "SUCCESS"))
            {
                throw new PluginException(string.Concat("原路返回退款时,接口返回异常:", xElement2.Value));
            }
            string value  = xDocument.Element("xml").Element("refund_id").Value;
            string value1 = xDocument.Element("xml").Element("out_refund_no").Value;

            paymentInfo.OrderIds = new List <long>()
            {
                long.Parse(value1)
            };
            paymentInfo.TradNo    = value;
            paymentInfo.TradeTime = new DateTime?(DateTime.Now);
            return(paymentInfo);
        }
예제 #4
0
        public override RefundFeeReturnModel ProcessRefundFee(PaymentPara para)
        {
            //创建请求对象
            RequestHandler reqHandler = new RequestHandler();

            RefundFeeReturnModel paymentInfo = new RefundFeeReturnModel();

            paymentInfo.RefundMode   = RefundRunMode.Sync;
            paymentInfo.RefundResult = RefundState.Failure;
            string strResult = string.Empty;
            Config payConfig = Utility <Config> .GetConfig(WorkDirectory);

            if (string.IsNullOrEmpty(payConfig.AppId))
            {
                throw new PluginException("未设置AppId");
            }
            if (string.IsNullOrEmpty(payConfig.MCHID))
            {
                throw new PluginException("未设置MCHID");
            }
            if (string.IsNullOrWhiteSpace(payConfig.pkcs12))
            {
                throw new PluginConfigException("未设置商户证书");
            }

            //-----------------------------
            //设置请求参数
            //-----------------------------
            reqHandler.SetKey(payConfig.Key);
            var nonceStr = TenPayUtil.GetNoncestr();

            reqHandler.SetParameter("out_trade_no", para.out_trade_no);
            reqHandler.SetParameter("out_refund_no", para.out_refund_no);
            reqHandler.SetParameter("total_fee", Convert.ToInt32((para.total_fee * 100)).ToString());
            reqHandler.SetParameter("refund_fee", Convert.ToInt32((para.refund_fee * 100)).ToString());
            reqHandler.SetParameter("op_user_id", payConfig.MCHID);
            reqHandler.SetParameter("appid", payConfig.AppId);
            reqHandler.SetParameter("mch_id", payConfig.MCHID);
            reqHandler.SetParameter("nonce_str", nonceStr);
            //从哪个帐号退款(余额、未结算帐号)
            reqHandler.SetParameter("refund_account", REFUND_SOURCE_RECHARGE_FUNDS); //REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款/基本账户

            string sign = reqHandler.CreateMd5Sign("key", payConfig.Key);            //按约定规则生成MD5,规则参考接口文档

            reqHandler.SetParameter("sign", sign);

            var pkcs12 = WorkDirectory + "\\" + payConfig.pkcs12;

            if (!System.IO.File.Exists(pkcs12))
            {
                throw new PluginException("未找到商户证书文件");
            }

            string strXml = reqHandler.ParseXML();
            string result = string.Empty;

            try
            {
                result = TenPayV3.Refund(strXml, pkcs12, payConfig.MCHID);//调用统一接口
            }
            catch (Exception ex)
            {
                throw new PluginException("原路返回退款时出错:" + ex.Message);
            }
            XDocument xmlDocument = XDocument.Parse(result);

            if (xmlDocument == null)
            {
                throw new PluginException("原路返回退款时出错:" + strXml);
            }

            XElement e_return = xmlDocument.Element("xml").Element("return_code");
            XElement e_result = xmlDocument.Element("xml").Element("return_msg");

            if (e_return == null)
            {
                throw new PluginException("原路返回退款时,返回参数异常");
            }

            //处理返回时先判断协议错误码,再业务,最后交易状态
            if (e_return.Value == "SUCCESS")
            {
                e_result = xmlDocument.Element("xml").Element("result_code");
                XElement e_errdes = xmlDocument.Element("xml").Element("err_code_des");
                if (e_result.Value == "SUCCESS")
                {
                    //微信退款单号
                    string refund_id = xmlDocument.Element("xml").Element("refund_id").Value;
                    //商户退款单号
                    string out_refund_no = xmlDocument.Element("xml").Element("out_refund_no").Value;

                    //业务处理
                    paymentInfo.RefundResult = RefundState.Success;
                    paymentInfo.OrderId      = long.Parse(out_refund_no);
                    paymentInfo.RefundNo     = refund_id;
                    paymentInfo.RefundTime   = DateTime.Now;
                }
                else
                {
                    throw new PluginException("原路返回退款时,接口返回异常:" + e_errdes.Value);
                }
            }
            else
            {
                throw new PluginException("原路返回退款时,接口返回异常:" + e_result.Value);
            }
            return(paymentInfo);
        }