首页 > 代码库 > 微信支付退款

微信支付退款

    public string Wxzhifurefund(string orderid,string openid,string weiId,string refund_fee)        {            Wxrefund refund = new Wxrefund();            try {                 //查询微商城appid                string sql = "select * from pay_info where Enable=1";//必须保证enable=1唯一                DataTable paydata = http://www.mamicode.com/SQLiteDBHelper.GetDataTable(sql, weiId);"Appid"].ToString();//公众号ID                    refund.mch_id = paydata.Rows[0]["Mchid"].ToString();//商户号                    Setkey = paydata.Rows[0]["KeyValue"].ToString();//商户key                }                else {                    writeLogs.writelog("微支付退款时,查询商户配置参数错误 请检查参数是否正确");                    return "{\"errcode\":\"3\"}";//商户未设置支付参数                }                refund.nonce_str = openid;//随机字符串                refund.out_trade_no = orderid;//商户订单号                refund.out_refund_no =SystemSettingController.ConvertDateTimeInt(DateTime.Now);//创建退款订单号                //使用订单编号,查询订单总金额                sql = string.Format( "select * from order_info where orderid={0} and OpenId=‘{1}‘",orderid,openid);//必须保证订单号唯一性                DataTable orderdata = http://www.mamicode.com/SQLiteDBHelper.GetDataTable(sql, weiId);"Receivables"].ToString()) * 100);//订单总金额                if (string.IsNullOrEmpty(refund_fee))//退款金额等于空,就退全款 默认写入订单总金额                {                    refund.refund_fee = Convert.ToString(float.Parse(orderdata.Rows[0]["Receivables"].ToString()) * 100);//退款金额                }                else {                    refund.refund_fee = refund_fee;//退款金额单位:分                }                refund.op_user_id = refund.appid;//默认商户号                //字典排序                Dictionary<string, string> dicrefund = new Dictionary<string, string>();                dicrefund.Add("appid", refund.appid);                dicrefund.Add("mch_id", refund.mch_id);                dicrefund.Add("nonce_str", refund.nonce_str);                dicrefund.Add("out_trade_no",refund.out_trade_no );                dicrefund.Add("out_refund_no",refund.out_refund_no);                dicrefund.Add("total_fee",refund.total_fee);                dicrefund.Add("refund_fee",refund.refund_fee);                dicrefund.Add("op_user_id", refund.op_user_id);                dicrefund.Add("sign", WxSignCreate(dicrefund));                //转换xml                string xml = WxXml(dicrefund);                string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";                //设置证书路径,密码                string cert = @"xxxxx";                string password = "xxxxxx";                X509Certificate x509 = new X509Certificate(cert,password);                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);                request.ClientCertificates.Add(x509);                 //传输post数据                var encoding = System.Text.Encoding.UTF8;                byte[] databyte = encoding.GetBytes(xml);                request.Method = "post";                request.ContentType = "application/x-www-form-urlencoded";                request.ContentLength = databyte.Length;                using (Stream responstream = request.GetRequestStream())                {                    responstream.Write(databyte, 0, databyte.Length);                    responstream.Close();                }                HttpWebResponse response =(HttpWebResponse)request.GetResponse();                Stream stream = response.GetResponseStream();                string result = string.Empty;                using (StreamReader reader = new StreamReader(stream))                {                    result = reader.ReadToEnd();                    writeLogs.writelog("退款接口返回结果:"+result);                }                //反序列化                refund_return refunreturn = null;                XmlSerializer refundxml = new XmlSerializer(typeof(refund_return));                using (StringReader stringreader = new StringReader(result))                {                    refunreturn = refundxml.Deserialize(stringreader) as refund_return;                }                if (refunreturn.result_code == "" || refunreturn.result_code == null)                {                    writeLogs.writelog("退款失败,失败原因:"+ refunreturn.return_msg);                    return "{\"errcode\":\"18\"}";//退款失败                }                if (refunreturn.result_code == "SUCCESS")//退款申请成功                {                        //处理退款操作                }                else if (refunreturn.result_code == "FAIL")//退款申请失败                {                    writeLogs.writelog("退款申请失败,错误码:"+refunreturn.err_code+",失败原因:" + refunreturn.err_code_des);                    return "{\"errcode\":\"18\"}";//退款申请失败                }            }            catch (Exception e)            {                writeLogs.writelog("微信支付退款异常,参数:订单号:"+orderid+",openid编号:"+openid+",公众号编号:"+weiId);                writeLogs.writelog("退款异常打印:"+e.Message);                return "{\"errcode\":\"18\"}";//退款失败            }            return "";//返回成功信息        }

  

微信支付退款