/// <summary> /// 微信退单 /// </summary> /// <param name="dingdan"></param> /// <param name="dto"></param> /// <param name="returnMsg"></param> /// <returns></returns> private bool WeChatRefund(wx_hotel_dingdan dingdan, TuidanDto dto, int wid, out string returnMsg) { bool result = false; returnMsg = null; var refundResult = dingdanbll.GetWeChatRefundParams(wid, dingdan.hotelid.Value, dingdan.id, dto.refundCode); //使用系统订单号退单 if (refundResult != null && refundResult.Tables.Count > 0 && refundResult.Tables[0].Rows.Count > 0) { var orderNumber = refundResult.Tables[0].Rows[0]["orderNumber"].ToString(); var transaction_id = refundResult.Tables[0].Rows[0]["transaction_id"].ToString(); var refundAmount = Convert.ToInt32(refundResult.Tables[0].Rows[0]["refundAmount"]); var payAmount = Convert.ToInt32(refundResult.Tables[0].Rows[0]["payAmount"]); var wxModel = new BLL.wx_userweixin().GetModel(wid); var payInfo = new BLL.wx_payment_wxpay().GetModelByWid(wid); var requestHandler = new RequestHandler(null); requestHandler.SetParameter("out_trade_no", orderNumber); //requestHandler.SetParameter("transaction_id", transaction_id); requestHandler.SetParameter("out_refund_no", dto.refundCode); requestHandler.SetParameter("appid", wxModel.AppId); requestHandler.SetParameter("mch_id", payInfo.mch_id);//商户号 requestHandler.SetParameter("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); //退款金额 if (PayHelper.IsDebug) { requestHandler.SetParameter("total_fee", (payAmount).ToString()); requestHandler.SetParameter("refund_fee", (refundAmount).ToString()); } else { requestHandler.SetParameter("total_fee", (payAmount * 100).ToString()); requestHandler.SetParameter("refund_fee", (refundAmount * 100).ToString()); } requestHandler.SetParameter("op_user_id", wxModel.AppId); requestHandler.SetParameter("sign", requestHandler.CreateMd5Sign("key", payInfo.paykey)); var refundInfo = TenPayV3Helper.Refund(requestHandler.ParseXML(), string.Format(@"{0}{1}", AppDomain.CurrentDomain.BaseDirectory, payInfo.certInfoPath), payInfo.cerInfoPwd); var refundOrderResponse = new RefundOrderResponse(refundInfo); result = refundOrderResponse.IsSuccess; returnMsg = refundOrderResponse.return_msg; } return(result); }
/// <summary> /// 统一下单 /// </summary> /// <param name="unifiedOrderInfo"></param> /// <param name="errMessage"></param> /// <returns></returns> public JsAPIParameter UnifiedOrder(UnifiedOrderInfo unifiedOrderInfo, out string errMessage) { if (unifiedOrderInfo == null) { errMessage = "下单数据为空"; return null; } if (!unifiedOrderInfo.CheckRequired(out errMessage)) { return null; } var paymentInfoRepository = DependencyManager.Resolve<IPaymentInfoRepository>(); //判断当前订单号是否存在 var orderInfo = paymentInfoRepository.GetPaymentInfoBySysOrderNo(unifiedOrderInfo.PayModuleName, unifiedOrderInfo.OutTradeNo); if (orderInfo != null && orderInfo.Status != 0 && orderInfo.OpenId.Equals(unifiedOrderInfo.Openid)) //0表示尚未支付 { errMessage = "系统异常,请重新下单操作"; return null; } //调用微信统一下单接口 var appInfo = DependencyManager.Resolve<IAppInfoRepository>().GetAppInfo(unifiedOrderInfo.AppId); var payment = DependencyManager.Resolve<Weixin.Repository.IPaymentInfoRepository>() .GetModelByAppId(unifiedOrderInfo.AppId); if (string.IsNullOrEmpty(appInfo.AppId) || string.IsNullOrEmpty(payment.MchId) || string.IsNullOrEmpty(payment.Paykey)) { errMessage = "当前公众号支付配置不完整"; return null; } var packageReqHandler = new RequestHandler(default(HttpContext)); //初始化 packageReqHandler.Init(); var nonceStr = TenPayV3Util.GetNoncestr(); //设置package订单参数 packageReqHandler.SetParameter("appid", appInfo.AppId); //公众账号ID packageReqHandler.SetParameter("mch_id", payment.MchId); //商户号 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("body", unifiedOrderInfo.Body); //商品描述 packageReqHandler.SetParameter("attach", unifiedOrderInfo.PayModuleName); //向微信传递系统支付模块ID packageReqHandler.SetParameter("out_trade_no", unifiedOrderInfo.OutTradeNo); //商家订单号 //debug模式下,单位为 packageReqHandler.SetParameter("total_fee", PayHelper.IsDebug ? (unifiedOrderInfo.TotalFee / 100).ToString() : (unifiedOrderInfo.TotalFee * 100).ToString()); packageReqHandler.SetParameter("spbill_create_ip", "1.1.1.1"); //用户的公网ip,不是商户服务器IP packageReqHandler.SetParameter("notify_url", PayHelper.GetPayNotifyUrl()); //接收财付通通知的URL packageReqHandler.SetParameter("trade_type", TenPayV3Type.JSAPI.ToString()); //交易类型 packageReqHandler.SetParameter("openid", unifiedOrderInfo.Openid); //用户的openId var sign = packageReqHandler.CreateMd5Sign("key", payment.Paykey); packageReqHandler.SetParameter("sign", sign); //签名 var data = packageReqHandler.ParseXML(); //同意下单,获取到预付订单号 var unifiedOrderResult = TenPayV3Helper.Unifiedorder(data); var rtnUnifiedOrderResult = new UnifiedOrderResponse(unifiedOrderResult); //下单成功,保存下单对象 if (rtnUnifiedOrderResult.IsSuccess) { var paymentInfo = new PaymentInfo { PaymentId = Guid.NewGuid(), AppId = unifiedOrderInfo.AppId, CreateTime = DateTime.Now, Description = "无", Body = unifiedOrderInfo.Body, ModuleName = unifiedOrderInfo.PayModuleName, OrderCode = unifiedOrderInfo.OutTradeNo, OrderId = unifiedOrderInfo.OrderId, OpenId = unifiedOrderInfo.Openid, PayAmount = unifiedOrderInfo.TotalFee, WxOrderCode = rtnUnifiedOrderResult.prepay_id, ModifyTime = DateTime.Now, Status = 0 }; paymentInfoRepository.Add(paymentInfo); var jsApiParameters = rtnUnifiedOrderResult.GetJsApiParameters(payment.Paykey); return jsApiParameters; } errMessage = "下单失败"; return null; }
public void UnifiedOrder(string request) { try { var requestModel = JSONHelper.Deserialize <UnifiedOrderEntity>(request); //通过wid获取公众号的信息 var wxModel = new BLL.wx_userweixin().GetModel(requestModel.wid); var wxPayInfo = new BLL.wx_payment_wxpay().GetModelByWid(requestModel.wid); var packageReqHandler = new RequestHandler(null); //初始化 packageReqHandler.Init(); var timeStamp = TenPayV3Util.GetTimestamp(); var nonceStr = TenPayV3Util.GetNoncestr(); //设置package订单参数 packageReqHandler.SetParameter("appid", wxModel.AppId); //公众账号ID packageReqHandler.SetParameter("mch_id", wxPayInfo.mch_id); //商户号 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("body", requestModel.body); //商品描述 packageReqHandler.SetParameter("attach", requestModel.PayModuleID.ToString()); //向微信传递系统支付模块ID packageReqHandler.SetParameter("out_trade_no", requestModel.out_trade_no); //商家订单号 //debug模式下,穿的单位为分 packageReqHandler.SetParameter("total_fee", PayHelper.IsDebug ? requestModel.total_fee.ToString() : (requestModel.total_fee * 100).ToString()); //packageReqHandler.SetParameter("spbill_create_ip", wxPayInfo); //用户的公网ip,不是商户服务器IP packageReqHandler.SetParameter("notify_url", HttpContext.Current.Request.Url.ToString().ToLower().Replace("wechatservice.asmx", "PayNotify.aspx")); //接收财付通通知的URL packageReqHandler.SetParameter("trade_type", TenPayV3Type.JSAPI.ToString()); //交易类型 packageReqHandler.SetParameter("openid", requestModel.openid); //用户的openId var sign = packageReqHandler.CreateMd5Sign("key", wxPayInfo.paykey); packageReqHandler.SetParameter("sign", sign); //签名 var data = packageReqHandler.ParseXML(); //同意下单,获取到预付订单号 var unifiedOrderResult = TenPayV3Helper.Unifiedorder(data); var rtnUnifiedOrderResult = new UnifiedOrderResponse(unifiedOrderResult); //下单成功,保存下单对象 if (rtnUnifiedOrderResult.IsSuccess) { var paymentInfo = new PaymentInfo { PaymentId = Guid.NewGuid(), Wid = requestModel.wid, CreateTime = DateTime.Now, Description = "无", ShopName = requestModel.body, ModuleName = ((PayModuleEnum)requestModel.PayModuleID).ToString(), OrderCode = requestModel.out_trade_no, OrderId = requestModel.OrderId, Pid = requestModel.openid, PayAmount = requestModel.total_fee, WXOrderCode = rtnUnifiedOrderResult.prepay_id, ModifyTime = DateTime.Now, Status = 0 }; paymentInfo.Add(); } //var jsApiParameters = rtnUnifiedOrderResult.GetJsApiParameters("4A5E7B87F3324A6DA22E55FDC12150B6"); var jsApiParameters = rtnUnifiedOrderResult.GetJsApiParameters(wxPayInfo.paykey); HttpContext.Current.Response.Write(AjaxResult.Success(jsApiParameters)); } catch (Exception exception) { HttpContext.Current.Response.Write(AjaxResult.Error(exception.Message)); } }
protected void btnAgreeRefund_Click(object sender, EventArgs e) { try { var refundBll = new BLL.wx_diancai_tuidan_manage(); var refundResult = refundBll.GetWeChatRefundParams(shopid, orderID, refundCode); //使用系统订单号退单 if (refundResult != null && refundResult.Tables.Count > 0 && refundResult.Tables[0].Rows.Count > 0) { var orderNumber = refundResult.Tables[0].Rows[0]["orderNumber"].ToString(); var transaction_id = refundResult.Tables[0].Rows[0]["transaction_id"].ToString(); var refundAmount = Convert.ToInt32(refundResult.Tables[0].Rows[0]["refundAmount"]); var payAmount = Convert.ToInt32(refundResult.Tables[0].Rows[0]["payAmount"]); var shopInfo = new BLL.wx_diancai_shopinfo().GetModel(shopid); var wxModel = new BLL.wx_userweixin().GetModel((int)shopInfo.wid); var payInfo = new BLL.wx_payment_wxpay().GetModelByWid((int)shopInfo.wid); var requestHandler = new RequestHandler(null); requestHandler.SetParameter("out_trade_no", orderNumber); //requestHandler.SetParameter("transaction_id", transaction_id); requestHandler.SetParameter("out_refund_no", refundCode); requestHandler.SetParameter("appid", wxModel.AppId); requestHandler.SetParameter("mch_id", payInfo.mch_id);//商户号 requestHandler.SetParameter("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); //退款金额 if (PayHelper.IsDebug) { requestHandler.SetParameter("total_fee", (payAmount / 100).ToString()); requestHandler.SetParameter("refund_fee", (refundAmount / 100).ToString()); } else { requestHandler.SetParameter("total_fee", payAmount.ToString()); requestHandler.SetParameter("refund_fee", refundAmount.ToString()); } requestHandler.SetParameter("op_user_id", wxModel.AppId); requestHandler.SetParameter("sign", requestHandler.CreateMd5Sign("key", payInfo.paykey)); var refundInfo = TenPayV3Helper.Refund(requestHandler.ParseXML(), string.Format(@"{0}{1}", AppDomain.CurrentDomain.BaseDirectory, payInfo.certInfoPath), payInfo.cerInfoPwd); var refundOrderResponse = new RefundOrderResponse(refundInfo); if (refundOrderResponse.IsSuccess) { new BLL.wx_diancai_tuidan_manage().RefundComplete(refundCode, this.txtRefundReason.Value.Trim()); Response.Redirect("diancai_dingdanRefund_manage.aspx"); } else { Response.Write(refundOrderResponse.return_msg); List(); } } } catch (Exception exception) { Response.Write(exception.Message); Response.Write(exception.StackTrace); List(); } }
/// <summary> /// 最新接口调用 /// 2014-11-24 /// </summary> /// <param name="ttFee">支付金额(单位元)</param> /// <param name="busiBody">订单内容</param> /// <param name="out_trade_no">订单号</param> /// <param name="code"></param> protected void WxPayDataV3(decimal ttFee, string busiBody, string out_trade_no, string code) { BLL.wx_payment_wxpay wxPayBll = new BLL.wx_payment_wxpay(); Model.wx_payment_wxpay paymentInfo = wxPayBll.GetModelByWid(wid); BLL.wx_userweixin uwBll = new BLL.wx_userweixin(); Model.wx_userweixin uwEntity = uwBll.GetModel(wid); // logBll.AddLog(wid,"【微支付】微信预定", "paypage.aspx WxPayDataV3", "211 wid:" + wid, 1); //先设置基本信息 string MchId = paymentInfo.mch_id; // "1218574001";// string partnerKey = paymentInfo.paykey; // 商户支付密钥Key。登录微信商户后台,进入栏目【账户设置】【密码安全】【API 安全】【API 密钥】 string notify_url = "http://" + HttpContext.Current.Request.Url.Authority + "/api/payment/wxpay/notify_url.aspx"; WXLogs.AddLog(wid, "【微支付】微信预定", "paypage.aspx WxPayDataV3", "uwEntity.AppId: " + uwEntity.AppId + "| uwEntity.AppSecret;" + uwEntity.AppSecret + "|code:" + code, 1); string timeStamp = ""; string nonceStr = ""; string paySign = ""; string sp_billno = out_trade_no; //当前时间 yyyyMMdd string date = DateTime.Now.ToString("yyyyMMdd"); if (null == sp_billno) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 sp_billno = DateTime.Now.ToString("HHmmss") + TenPayV3Util.BuildRandomStr(28); } //创建支付应答对象 OneGulp.WeChat.MP.TenPayLibV3.RequestHandler packageReqHandler = new OneGulp.WeChat.MP.TenPayLibV3.RequestHandler(null); //初始化 packageReqHandler.Init(); //packageReqHandler.SetKey(""/*TenPayV3Info.Key*/); timeStamp = TenPayV3Util.GetTimestamp(); nonceStr = TenPayV3Util.GetNoncestr(); //设置package订单参数 packageReqHandler.SetParameter("appid", uwEntity.AppId); //公众账号ID packageReqHandler.SetParameter("mch_id", MchId); //商户号 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("body", busiBody); //商品描述 packageReqHandler.SetParameter("attach", wid + "|" + busiBody); packageReqHandler.SetParameter("out_trade_no", sp_billno); //商家订单号 packageReqHandler.SetParameter("total_fee", ((int)(ttFee * 100)).ToString()); //商品金额,以分为单位(money * 100).ToString() packageReqHandler.SetParameter("spbill_create_ip", Request.UserHostAddress); //用户的公网ip,不是商户服务器IP packageReqHandler.SetParameter("notify_url", notify_url); //接收财付通通知的URL packageReqHandler.SetParameter("trade_type", TenPayV3Type.JSAPI.ToString()); //交易类型 packageReqHandler.SetParameter("openid", openid); //用户的openId string sign = packageReqHandler.CreateMd5Sign("key", partnerKey); packageReqHandler.SetParameter("sign", sign); //签名 string data = packageReqHandler.ParseXML(); var result = TenPayV3Helper.Unifiedorder(data); var res = XDocument.Parse(result); prepayId = res.Element("xml").Element("prepay_id").Value; //设置支付参数 OneGulp.WeChat.MP.TenPayLibV3.RequestHandler paySignReqHandler = new OneGulp.WeChat.MP.TenPayLibV3.RequestHandler(null); paySignReqHandler.SetParameter("appId", uwEntity.AppId); paySignReqHandler.SetParameter("timeStamp", timeStamp); paySignReqHandler.SetParameter("nonceStr", nonceStr); paySignReqHandler.SetParameter("package", string.Format("prepay_id={0}", prepayId)); paySignReqHandler.SetParameter("signType", "MD5"); paySign = paySignReqHandler.CreateMd5Sign("key", partnerKey); packageValue = ""; packageValue += " \"appId\": \"" + uwEntity.AppId + "\", "; packageValue += " \"timeStamp\": \"" + timeStamp + "\", "; packageValue += " \"nonceStr\": \"" + nonceStr + "\", "; packageValue += " \"package\": \"" + string.Format("prepay_id={0}", prepayId) + "\", "; packageValue += " \"signType\": \"MD5\", "; packageValue += " \"paySign\": \"" + paySign + "\""; }