微信小程序退款

 			var AppId = "小程序appid";
            var MchId = "商户号";
            var MchKey = "商户密钥";
            var zhengshu = "证书";
            if (radioButton1.Checked)
            {
                 AppId = "wx83e3fd2cffd8a402";
                 MchId = "1505386021";
                 MchKey = "0iCjINWd2CXYUnW3yl524K32Yw2Rt85m";
                zhengshu = "D:\\zhengshu\\shuziyu\\apiclient_cert.p12";
            }
            else if (radioButton2.Checked)
            {
                 AppId = "";
                 MchId = "";
                 MchKey = "";
                zhengshu = "D:\\zhengshu\\ruien\\apiclient_cert.p12";
            }
            var dh = this.textBox1.Text;
            var je = this.textBox2.Text;
            string RefundNotifyUrl = "https://wxxcx001.shuziyu.net/api/wxapp/paynotify";
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            var orderAmount = Math.Round(Convert.ToDecimal(je) * 100, 0, MidpointRounding.AwayFromZero); //退款金额
            var orderAmount1 = Math.Round(Convert.ToDecimal(je) * 100, 0, MidpointRounding.AwayFromZero);//当前单号的总金额
            var refundOrderNo = UniqueKeyHelper.GetUniqueKey(12);
            var list = new SortedList<string, object>
            {
                {"appid", AppId},
                {"mch_id", MchId},
                {"nonce_str", Guid.NewGuid().ToString("N")},
                {"out_trade_no", dh},
                {"out_refund_no", refundOrderNo},
                {"total_fee", orderAmount1},
                {"refund_fee", orderAmount},
                {"notify_url", "https://wxxcx001.shuziyu.net/api/wxapp/paynotify"}
            };
            StringBuilder builder = new StringBuilder();

            foreach (KeyValuePair<string, object> item in list)
            {
                var name = item.Key;
                var value = item.Value.ToString();
                if (value != "")
                {
                    builder.Append(name + "=" + value + "&");
                }
            }
            var stringSignTemp = builder.ToString().TrimEnd('&') + "&key=" + MchKey;

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bt = md5.ComputeHash(Encoding.UTF8.GetBytes(stringSignTemp));
            var singString = BitConverter.ToString(bt).Replace("-", "").ToUpper();

            string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";

            string xml = @"<xml>
                            <appid>{0}</appid>
                            <mch_id>{1}</mch_id>
                            <nonce_str>{2}</nonce_str>
                            <out_trade_no>{3}</out_trade_no>
                            <out_refund_no>{4}</out_refund_no>
                            <total_fee>{5}</total_fee>
                            <refund_fee>{6}</refund_fee>
                            <sign>{7}</sign>
                            <notify_url>{8}</notify_url>
                            </xml>";
            xml = string.Format(xml, AppId, MchId, list["nonce_str"], dh, refundOrderNo, orderAmount1, orderAmount, singString, RefundNotifyUrl);

            var content = new StringContent(xml, Encoding.UTF8, "application/xml");
            WebRequestHandler handler = new WebRequestHandler();
            X509Certificate2 cert = new X509Certificate2(zhengshu, MchId);
            handler.ClientCertificates.Add(cert);
            HttpClient client = new HttpClient(handler);

            var response = client.PostAsync(url, content).Result;
            if (response.IsSuccessStatusCode)
            {
                string msg = response.Content.ReadAsStringAsync().Result;
                this.richTextBox1.Text = msg;
            }
            else
            {
                MessageBox.Show("退款失败");
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值