Ejemplo n.º 1
0
        public void RefundTest()
        {
            var result = TenPayV3.Refund(data);

            Console.Write(result);
            Assert.IsNotNull(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 退款申请接口
        /// </summary>
        /// <returns></returns>
        public ActionResult Refund()
        {
            string         nonceStr          = TenPayUtil.GetNoncestr();
            RequestHandler packageReqHandler = new RequestHandler(null);

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

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

            string data = packageReqHandler.ParseXML();

            var    result = TenPayV3.Refund(data);
            var    res    = XDocument.Parse(result);
            string openid = res.Element("xml").Element("out_refund_no").Value;

            return(Content(openid));
        }
        public ActionResult Refund(int orderId = 0)
        {
            string nonceStr = TenPayV3Util.GetNoncestr();

            string outTradeNo  = (string)(Session["BillNo"]);
            string outRefundNo = "OutRefunNo-" + DateTime.Now.Ticks;
            int    totalFee    = (int)(Session["BillFee"]);
            int    refundFee   = totalFee;
            string opUserId    = Service.Config.MchId;
            var    dataInfo    = new TenPayV3RefundRequestData(Service.Config.AppId, Service.Config.MchId, Service.Config.TenPayV3_Key,
                                                               null, nonceStr, null, outTradeNo, outRefundNo, totalFee, refundFee, opUserId, null);
            var cert     = @"D:\cert\apiclient_cert_SenparcRobot.p12"; //根据自己的证书位置修改
            var password = Service.Config.MchId;                       //默认为商户号,建议修改
            var result   = TenPayV3.Refund(dataInfo, cert, password);

            return(Content("退款成功!"));
        }
Ejemplo n.º 4
0
#pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释
        public RefundResult Refund(WeChatRefundParamter paramter)
#pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释
        {
            string nonceStr = TenPayV3Util.GetNoncestr();
            TenPayV3RefundRequestData tenPayV3RefundRequestData = new TenPayV3RefundRequestData(Options.WeChatBaseOptions.AppId,
                                                                                                Options.WeChatBaseOptions.MchId, Options.WeChatBaseOptions.Key, paramter.DeviceInfo, nonceStr, paramter.TransactionId,
                                                                                                paramter.OutTradeNo, paramter.OutRefundNo, paramter.TotalFee, paramter.RefundFee,
                                                                                                Options.WeChatBaseOptions.MchId, paramter.RefundAccount, paramter.RefundDescription,
                                                                                                GetNotifyUrl(Options.WeChatBaseOptions.RefundNotifyUrl, WeChatNotifyType.Refund), paramter.RefundFeeType);

            RefundResult refundResult = TenPayV3.Refund(Options.WeChatDevOptions.ServiceProvider, tenPayV3RefundRequestData, paramter.TimeOut);

            //,
            //$"{AppDomain.CurrentDomain.BaseDirectory}/apiclient_cert.p12", Options.CertPassword);

            return(refundResult);
        }
Ejemplo n.º 5
0
        public HttpResult Refund()
        {
            try
            {
                WeixinTrace.SendCustomLog("进入退款流程", "1");

                string nonceStr = TenPayV3Util.GetNoncestr();

                string outTradeNo = HttpContext.Session.GetString("BillNo");

                WeixinTrace.SendCustomLog("进入退款流程", "2 outTradeNo:" + outTradeNo);

                string outRefundNo = "OutRefunNo-" + SystemTime.Now.Ticks;
                int    totalFee    = int.Parse(HttpContext.Session.GetString("BillFee"));
                int    refundFee   = totalFee;
                string opUserId    = TenPayV3Info.MchId;
                var    notifyUrl   = "https://yufaquan.cn/wx/PayV3/RefundNotifyUrl";
                var    dataInfo    = new TenPayV3RefundRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, TenPayV3Info.Key,
                                                                   null, nonceStr, null, outTradeNo, outRefundNo, totalFee, refundFee, opUserId, null, notifyUrl: notifyUrl);

                #region 旧方法
                //var cert = @"D:\cert\apiclient_cert_SenparcRobot.p12";//根据自己的证书位置修改
                //var password = TenPayV3Info.MchId;//默认为商户号,建议修改
                //var result = TenPayV3.Refund(dataInfo, cert, password);
                #endregion

                #region 新方法(Senparc.Weixin v6.4.4+)
                var result = TenPayV3.Refund(_serviceProvider, dataInfo);//证书地址、密码,在配置文件中设置,并在注册微信支付信息时自动记录
                #endregion

                WeixinTrace.SendCustomLog("进入退款流程", "3 Result:" + result.ToJson());

                return(HttpResult.Success($"退款结果:{result.result_code} {result.err_code_des}。", null));
            }
            catch (Exception ex)
            {
                WeixinTrace.WeixinExceptionLog(new WeixinException(ex.Message, ex));

                throw;
            }
        }
Ejemplo n.º 6
0
        public static RefundRequestData GetRefundRequestData(int refundFee, string orderCode, string cert)
        {
            var _payInfo     = TenPayV3InfoCollection.Data[System.Configuration.ConfigurationManager.AppSettings["TenPayV3_MchId"]];
            var nonceStr     = TenPayV3Util.GetNoncestr();
            var rtnOrderCode = string.Format("RTN{0}{1}{2}", _payInfo.MchId, DateTime.Now.ToString("yyyyMMddHHmmss"), TenPayV3Util.BuildRandomStr(6));

            //var _refundFee = (int)(refundFee * 100);
            var dataInfo = new TenPayV3RefundRequestData(_payInfo.AppId, _payInfo.MchId, _payInfo.Key,
                                                         null, nonceStr, null, orderCode, rtnOrderCode, refundFee, refundFee, _payInfo.MchId, null);
            //var cert = @"D:\cert\apiclient_cert_SenparcRobot.p12";//根据自己的证书位置修改
            var password = _payInfo.MchId;//默认为商户号,建议修改
            var result   = TenPayV3.Refund(dataInfo, cert, password);

            return(new RefundRequestData
            {
                RefundFee = refundFee,
                RefundResult = result.result_code,
                RtnOrderCode = rtnOrderCode,
                Remark = result.err_code_des,
                WxRtnOrderCode = result.transaction_id ?? ""
            });
        }
Ejemplo n.º 7
0
 /// <summary>
 /// 订单退款
 /// </summary>
 /// <param name="billNo">订单号</param>
 /// <param name="billFee">退款金额(单位:分)</param>
 /// <param name="tenPayV3Notif">回调地址</param>
 /// <returns></returns>
 public static string Refund(string billNo, int billFee, string tenPayV3Notif)
 {
     try
     {
         TenPayV3Info tenPayV3Info = new TenPayV3Info(WxOpenId, WxOpenSecret, MchId, MchIdkey, tenPayV3Notif);
         string       nonceStr     = TenPayV3Util.GetNoncestr();
         string       outTradeNo   = billNo;
         string       outRefundNo  = "OutRefunNo-" + DateTime.Now.Ticks;
         int          totalFee     = billFee;
         int          refundFee    = totalFee;
         string       opUserId     = tenPayV3Info.MchId;
         var          dataInfo     = new TenPayV3RefundRequestData(
             tenPayV3Info.AppId,
             tenPayV3Info.MchId,
             tenPayV3Info.Key,
             null,                    //描述
             nonceStr,
             null,                    //微信订单号
             outTradeNo,              //商户订单号 二选一
             outRefundNo,             //商户退款单号
             totalFee,                //订单金额
             refundFee,               //退款金额
             opUserId,
             null                     //退款资金来源
             );
         var cert     = CertPath;     //根据自己的证书位置修改
         var password = CertPassword; //默认为商户号,建议修改
         var result   = TenPayV3.Refund(dataInfo, cert, password);
         Log.WriteLogToTxt("订单退款 result:" + JsonHelper.ToJson(result), LogType.Info);
         return(result.result_code == "FAIL" ? result.err_code_des : "成功");
     }
     catch (Exception ex)
     {
         WxWriteLogError(ex, "异常退款");
     }
     return("");
 }
Ejemplo n.º 8
0
        /// <summary>
        /// 退款申请接口
        /// </summary>
        /// <returns></returns>
        public ActionResult Refund()
        {
            string nonceStr = TenPayV3Util.GetNoncestr();

            string outTradeNo  = HttpContext.Session.GetString("BillNo");
            string outRefundNo = "OutRefunNo-" + SystemTime.Now.Ticks;
            int    totalFee    = int.Parse(HttpContext.Session.GetString("BillFee"));
            int    refundFee   = totalFee;
            string opUserId    = TenPayV3Info.MchId;
            var    notifyUrl   = "https://sdk.weixin.senparc.com/TenPayV3/RefundNotifyUrl";
            var    dataInfo    = new TenPayV3RefundRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, TenPayV3Info.Key,
                                                               null, nonceStr, null, outTradeNo, outRefundNo, totalFee, refundFee, opUserId, null, notifyUrl: notifyUrl);
            var cert     = @"D:\cert\apiclient_cert_SenparcRobot.p12"; //根据自己的证书位置修改
            var password = TenPayV3Info.MchId;                         //默认为商户号,建议修改
            var result   = TenPayV3.Refund(dataInfo, cert, password);

            return(Content(string.Format("退款结果:{0} {1}。您可以刷新当前页面查看最新结果。", result.result_code, result.err_code_des)));

            //return Json(result, JsonRequestBehavior.AllowGet);

            #region 原始方法

            //RequestHandler packageReqHandler = new RequestHandler(null);

            //设置package订单参数
            //packageReqHandler.SetParameter("appid", TenPayV3Info.AppId);		 //公众账号ID
            //packageReqHandler.SetParameter("mch_id", TenPayV3Info.MchId);	     //商户号
            //packageReqHandler.SetParameter("out_trade_no", "124138540220170502163706139412"); //填入商家订单号
            ////packageReqHandler.SetParameter("out_refund_no", "");                //填入退款订单号
            //packageReqHandler.SetParameter("total_fee", "");                    //填入总金额
            //packageReqHandler.SetParameter("refund_fee", "100");                //填入退款金额
            //packageReqHandler.SetParameter("op_user_id", TenPayV3Info.MchId);   //操作员Id,默认就是商户号
            //packageReqHandler.SetParameter("nonce_str", nonceStr);              //随机字符串
            //string sign = packageReqHandler.CreateMd5Sign("key", TenPayV3Info.Key);
            //packageReqHandler.SetParameter("sign", sign);	                    //签名
            ////退款需要post的数据
            //string data = packageReqHandler.ParseXML();

            ////退款接口地址
            //string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
            ////本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中)
            //string cert = @"D:\cert\apiclient_cert_SenparcRobot.p12";
            ////私钥(在安装证书时设置)
            //string password = TenPayV3Info.MchId;
            //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 = XDocument.Parse(responseContent);
            ////string openid = res.Element("xml").Element("out_refund_no").Value;
            //return Content("申请成功:<br>" + HttpUtility.RequestUtility.HtmlEncode(responseContent));

            #endregion
        }
Ejemplo n.º 9
0
        protected override void DoWork()
        {
            //将已经过期的团购订单设置为失败,并退款
            //_orderRepository.GetAll().Where(t=>t.Type==OrderType.Groupon && t.GrouponStatus==GrouponStatus.Grouponing)

            var groupOrdrs = _groupOrderRepository.GetAll()
                             .Where(t => t.GrouponStatus == GrouponStatus.GrouponFail)
                             .ToList();

            Logger.Info("GrouponCancelWorker 需要退款数量:" + groupOrdrs.Count);

            foreach (var grouponOrder in groupOrdrs)
            {
                //设置对应订单的状态
                var orderInfo = _orderRepository.FirstOrDefault(t => t.JoinGrouponId == grouponOrder.Id);
                if (orderInfo != null && orderInfo.Type == OrderType.Groupon &&
                    orderInfo.GrouponStatus == GrouponStatus.GrouponFail)
                {
                    var payinfo = _orderPayRepository.FirstOrDefault(t => t.OrderId == orderInfo.Id);
                    if (payinfo == null)
                    {
                        continue;
                    }

                    switch (payinfo.Channel)
                    {
                    case "微信支付":
                        string nonceStr    = TenPayV3Util.GetNoncestr();
                        string outTradeNo  = orderInfo.OrderNo;
                        string outRefundNo = "OutRefunNo-" + DateTime.Now.Ticks;
                        int    totalFee    = (int)(payinfo.Money * 100);
                        int    refundFee   = totalFee;
                        var    paykey      = SettingManager.GetSettingValue(AppSettings.Weixin.PayKey);
                        var    AppId       = SettingManager.GetSettingValue(AppSettings.Weixin.AppId);
                        var    MchId       = SettingManager.GetSettingValue(AppSettings.Weixin.MchId);

                        string opUserId = MchId;
                        var    dataInfo = new TenPayV3RefundRequestData(AppId, MchId, paykey,
                                                                        null, nonceStr, null, outTradeNo, outRefundNo, totalFee, refundFee, opUserId, null);
                        var cert     = Path.Combine(_appFolders.Root, "apiclient_cert.p12"); //  @"D:\cert\apiclient_cert_SenparcRobot.p12";//根据自己的证书位置修改
                        var password = MchId;                                                //默认为商户号,建议修改
                        var result   = TenPayV3.Refund(dataInfo, cert, password);
                        Logger.Warn("refundFee:" + refundFee);
                        Logger.Warn(result.ToJsonString());
                        if (result.IsResultCodeSuccess() && result.IsReturnCodeSuccess())
                        {
                            payinfo.RefundsTradeNo  = result.out_refund_no;
                            payinfo.RefundsPayTime  = DateTime.Now;
                            orderInfo.GrouponStatus = GrouponStatus.GrouponFailAndRefunds;
                            _orderRepository.Update(orderInfo);

                            grouponOrder.GrouponStatus = GrouponStatus.GrouponFailAndRefunds;
                            _groupOrderRepository.Update(grouponOrder);
                        }


                        break;

                    case "支付宝":

                        string APPID             = "2016081501749843";
                        string PID               = "2088421556051016";
                        string APP_PRIVATE_KEY   = @"MIIEowIBAAKCAQEArtd47la2jHyV5wbTkn/GtVPvTzTr0w6PxWYRY/dnscHVHyPd56Yc/NMJoavcMvJaH4lUzmTBcj3Nz8cv4gsVqYUqyojtukh9JBqW4njdC3IuggBH1Lmo/6cgaZjOzSEHcwyWNU0TcgfFj+y8Kd47YVdh9+xW9/M3nQn9MAaQgfHZbmLMzV5mg/IsB9JzCG963JSuMGq1CiEtYggllC+bhLtJTi+Mmlf0EcTI7CWd80ibMVgtaCyuEl9mdPo9qf0uUHQvZjPo24H24yhinspNlWYkRz9KeqU+agaIz5VKL7Nqwf5YZMauCzsDvM6wurfAHBClRh/UfAvxHN7+Oeqz8QIDAQABAoIBAF/Vd1Gccf7bIwc4tKsuImqtkRRnO4O6DY/zfEDBETNbvUeOT0lzwZvKyRK2ssGyGTgD/FoM3AOUYMUstt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1Sf99m/iw6svyoU5OAcIPSZ38R4l00VA1w+jYSMdQukhBlgYZQgRxeXKOFiM9IpkVvH9U";
                        string ALIPAY_PUBLIC_KEY = @"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlhGDVAL2+yN9HYkxgLBTI4MswpHi9QLrN/8zkJDFWQOGJEuUic2wPEJ/Mi45wuNMpeE57gzGMEk9AODTb+dzN5uA9gd0GVBcDI+5pBxJASWCouDUQR4Lvuo52vPTJGc2oageP5Of2L7eUazIYKU6jxoJWFbjzRN36p0l3SCvyvkOFaiTyIUFqDe5pprEPRWdxJmbwDnPsF4b3W1NdivoxP9y/ztCGN7ImX/mPLRrwveZe4xqvDOJ0sZ9LgfzJL7POE8lkj7m5LWM/5cNDKB3rtElp6eDWI7blBCvBsqMuzaCjzuYswYttu/j5gVLUd1oQ8v0wdw8lmPskcYEtobF6wIDAQAB";
                        string CHARSET           = "UTF-8";


                        APPID             = SettingManager.GetSettingValue(AppSettings.Alipay.APPID);
                        PID               = SettingManager.GetSettingValue(AppSettings.Alipay.PID);
                        APP_PRIVATE_KEY   = SettingManager.GetSettingValue(AppSettings.Alipay.APP_PRIVATE_KEY);
                        ALIPAY_PUBLIC_KEY = SettingManager.GetSettingValue(AppSettings.Alipay.ALIPAY_PUBLIC_KEY);

                        var client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APPID, APP_PRIVATE_KEY, "json", "1.0", "RSA2", ALIPAY_PUBLIC_KEY, CHARSET, false);
                        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
                        request.BizContent = "{" +
                                             "    \"out_trade_no\":\"" + orderInfo.OrderNo + "\"," +
                                             "    \"trade_no\":\"" + payinfo.TradeNo + "\"," +
                                             "    \"refund_amount\":" + payinfo.Money.ToString("F2") + "," +
                                             "    \"refund_reason\":\"拼团失败退款\"" +
                                             "  }";
                        AlipayTradeRefundResponse response = client.Execute(request);
                        Logger.Warn(response.Body);
                        if (!response.IsError)
                        {
                            payinfo.RefundsTradeNo  = response.TradeNo;
                            payinfo.RefundsPayTime  = DateTime.Now;
                            orderInfo.GrouponStatus = GrouponStatus.GrouponFailAndRefunds;
                            _orderRepository.Update(orderInfo);

                            grouponOrder.GrouponStatus = GrouponStatus.GrouponFailAndRefunds;
                            _groupOrderRepository.Update(grouponOrder);
                        }
                        break;
                    }
                }



                //if (orderInfo != null && orderInfo.Type==OrderType.Groupon && orderInfo.GrouponStatus==GrouponStatus.GrouponFail)
                //{
                //    orderInfo.Status = OrderStatus.Cancel;
                //    orderInfo.GrouponStatus = GrouponStatus.GrouponFail;
                //    _orderRepository.Update(orderInfo);
                //}

                ////修改团购状态
                //grouponOrder.GrouponStatus = GrouponStatus.GrouponFail;
                //_groupOrderRepository.Update(grouponOrder);
            }
        }