微信支付-JSAPI支付V3-退款申请

接口地址

接口链接:https://api.mch.weixin.qq.com/secapi/pay/refund

是否需要证书

请求需要双向证书。

请求参数

字段名

变量名

必填

类型

示例值

描述

公众账号ID

appid

String(32)

wx8888888888888888

微信分配的公众账号ID

商户号

mch_id

String(32)

1900000109

微信支付分配的商户号

设备号

device_info

String(32)

013467007045764

终端设备号

随机字符串

nonce_str

String(32)

5K8264ILTKCH16CQ2502SI8ZNMTM67VS

随机字符串,不长于32位。推荐随机数生成算法

签名

sign

String(32)

C380BEC2BFD727A4B6845133519F3AD6

签名,详见签名生成算法

微信订单号

transaction_id

String(28)

1217752501201407033233368018

微信订单号

商户订单号

out_trade_no

String(32)

1217752501201407033233368018

商户系统内部的订单号,

transaction_id、out_trade_no二选一,如果同时存在优先级:transaction_id> out_trade_no

商户退款单号

out_refund_no

String(32)

1217752501201407033233368018

商户系统内部的退款单号,商户系统内部唯一,同一退款单号多次请求只退一笔

总金额

total_fee

Int

100

订单总金额,单位为分,只能为整数,详见支付金额

退款金额

refund_fee

Int

100

退款总金额,订单总金额,单位为分,只能为整数,详见支付金额

货币种类

refund_fee_type

String(8)

CNY

货币类型,符合ISO 4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型

操作员

op_user_id

String(32)

1900000109

操作员帐号, 默认为商户号

返回结果

字段名

变量名

必填

类型

示例值

描述

返回状态码

return_code

String(16)

SUCCESS

SUCCESS/FAIL

返回信息

return_msg

String(128)

签名失败

 

返回信息,如非空,为错误原因

签名失败

参数格式校验错误

以下字段在return_code为SUCCESS的时候有返回

字段名

变量名

必填

类型

示例值

描述

业务结果

result_code

String(16)

SUCCESS

SUCCESS/FAIL

SUCCESS退款申请接收成功,结果通过退款查询接口查询

FAIL 提交业务失败

错误代码

err_code

String(32)

SYSTEMERROR

列表详见第6节

错误代码描述

err_code_des

String(128)

系统超时

结果信息描述

公众账号ID

appid

String(32)

wx8888888888888888

微信分配的公众账号ID

商户号

mch_id

String(32)

1900000109

微信支付分配的商户号

设备号

device_info

String(32)

013467007045764

微信支付分配的终端设备号,与下单一致

随机字符串

nonce_str

String(32)

5K8264ILTKCH16CQ2502SI8ZNMTM67VS

随机字符串,不长于32位

签名

sign

String(32)

5K8264ILTKCH16CQ2502SI8ZNMTM67VS

签名,详见签名算法

微信订单号

transaction_id

String(28)

1217752501201407033233368018

微信订单号

商户订单号

out_trade_no

String(32)

1217752501201407033233368018

商户系统内部的订单号

商户退款单号

out_refund_no

String(32)

1217752501201407033233368018

商户退款单号

微信退款单号

refund_id

String(28)

1217752501201407033233368018

微信退款单号

退款渠道

refund_channel

String(16)

ORIGINAL

ORIGINAL—原路退款

BALANCE—退回到余额

退款金额

refund_fee

Int

100

退款总金额,单位为分,可以做部分退款

订单总金额

total_fee

Int

100

订单总金额,单位为分,只能为整数,详见支付金额

订单金额货币种类

fee_type

String(8)

CNY

订单金额货币类型,符合ISO 4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型

现金支付金额

cash_fee

Int

100

现金支付金额,单位为分,只能为整数,详见支付金额

现金退款金额

cash_refund_fee

Int

100

现金退款金额,单位为分,只能为整数,详见支付金额

代金券或立减优惠退款金额

coupon_refund_fee

Int

100

代金券或立减优惠退款金额=订单金额-现金退款金额,注意:立减优惠金额不会退回

代金券或立减优惠使用数量

coupon_refund_count

Int

1

代金券或立减优惠使用数量

代金券或立减优惠ID

coupon_refund_id

String(20)

10000 

代金券或立减优惠ID

 

后台代码:

/// <summary>
        /// 退款申请接口
        /// </summary>
        /// <returns></returns>
        public JsonResult Refund(TRADE_Model trade)
        {
            string nonceStr = Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetNoncestr();

            Senparc.Weixin.MP.TenPayLibV3.RequestHandler packageReqHandler = new Senparc.Weixin.MP.TenPayLibV3.RequestHandler(null);

            //设置package订单参数
            packageReqHandler.SetParameter("appid", AppId);          //公众账号ID
            packageReqHandler.SetParameter("mch_id", MchId);          //商户号
            packageReqHandler.SetParameter("out_trade_no", trade.TRADE_NO);                 //填入商家订单号
            packageReqHandler.SetParameter("out_refund_no", "");                //填入退款订单号
            packageReqHandler.SetParameter("total_fee", "");               //填入总金额
            packageReqHandler.SetParameter("refund_fee", "");               //填入退款金额
            packageReqHandler.SetParameter("op_user_id", MchId);   //操作员Id,默认就是商户号
            packageReqHandler.SetParameter("nonce_str", nonceStr);              //随机字符串

            string sign = packageReqHandler.CreateMd5Sign("key", Key);

            packageReqHandler.SetParameter("sign", sign);                        //签名

            //退款需要post的数据
            string data = packageReqHandler.ParseXML();

            //退款接口地址
            string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";

            //本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中)
            string cert = @"D:\apiclient_cert.p12";

            //私钥(在安装证书时设置)
            string password = "";

            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            //调用证书
            X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);

            #region 发起post请求
            HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
            webrequest.ClientCertificates.Add(cer);
            webrequest.Method = "post";

            byte[] postdatabyte = Encoding.UTF8.GetBytes(data);
            webrequest.ContentLength = postdatabyte.Length;
            Stream stream;
            stream = webrequest.GetRequestStream();
            stream.Write(postdatabyte, 0, postdatabyte.Length);
            stream.Close();

            HttpWebResponse httpWebResponse = (HttpWebResponse)webrequest.GetResponse();
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
            string responseContent = streamReader.ReadToEnd();
            #endregion

            var res = System.Xml.Linq.XDocument.Parse(responseContent);
            string return_code = res.Element("xml").Element("return_code").Value;

            Hashtable hashtable = new Hashtable();

            hashtable.Add("return_code", return_code);

            return Json(hashtable);
        }

          private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
          {
              if (errors == SslPolicyErrors.None)
                   return true;
              return false;
          }

 

 

 

 

 

 

转载于:https://www.cnblogs.com/rockchen/p/4479029.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值