protected void Page_Load(object sender, EventArgs e) { string sp_billno = Request["order_no"]; //当前时间 yyyyMMdd string date = DateTime.Now.ToString("yyyyMMdd"); if (null == sp_billno) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 sp_billno = DateTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(4); } else { sp_billno = Request["order_no"].ToString(); } sp_billno = TenPayInfo.PartnerId + sp_billno; //创建支付应答对象 RequestHandler packageReqHandler = new RequestHandler(Context); //初始化 packageReqHandler.Init(); //设置package订单参数 packageReqHandler.SetParameter("partner", TenPayInfo.PartnerId); //商户号 packageReqHandler.SetParameter("fee_type", "1"); //币种,1人民币 packageReqHandler.SetParameter("input_charset", "GBK"); packageReqHandler.SetParameter("out_trade_no", sp_billno); //商家订单号 packageReqHandler.SetParameter("total_fee", "1"); //商品金额,以分为单位(money * 100).ToString() packageReqHandler.SetParameter("notify_url", TenPayInfo.TenPayNotify); //接收财付通通知的URL packageReqHandler.SetParameter("body", "JSAPIdemo"); //商品描述 packageReqHandler.SetParameter("spbill_create_ip", Page.Request.UserHostAddress); //用户的公网ip,不是商户服务器IP //获取package包 packageValue = packageReqHandler.GetRequestURL(); //调起微信支付签名 timeStamp = TenPayUtil.GetTimestamp(); nonceStr = TenPayUtil.GetNoncestr(); //设置支付参数 RequestHandler paySignReqHandler = new RequestHandler(Context); paySignReqHandler.SetParameter("appid", appId); paySignReqHandler.SetParameter("appkey", TenPayInfo.AppKey); paySignReqHandler.SetParameter("noncestr", nonceStr); paySignReqHandler.SetParameter("timestamp", timeStamp); paySignReqHandler.SetParameter("package", packageValue); paySign = paySignReqHandler.CreateSHA1Sign(); //获取debug信息,建议把请求和debug信息写入日志,方便定位问题 //string pakcageDebuginfo = packageReqHandler.getDebugInfo(); //Response.Write("<br/>pakcageDebuginfo:" + pakcageDebuginfo + "<br/>"); //string paySignDebuginfo = paySignReqHandler.getDebugInfo(); //Response.Write("<br/>paySignDebuginfo:" + paySignDebuginfo + "<br/>"); }
/// <summary> /// 目前支持向指定微信用户的openid发放指定金额红包 /// 注意total_amount、min_value、max_value值相同 /// total_num=1固定 /// 单个红包金额介于[1.00元,200.00元]之间 /// </summary> /// <returns></returns> public ActionResult SendRedPack() { string mchbillno = DateTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(28); string nonceStr = TenPayUtil.GetNoncestr(); RequestHandler packageReqHandler = new RequestHandler(null); //设置package订单参数 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("wxappid", TenPayV3Info.AppId); //公众账号ID packageReqHandler.SetParameter("mch_id", TenPayV3Info.MchId); //商户号 packageReqHandler.SetParameter("mch_billno", mchbillno); //填入商家订单号 packageReqHandler.SetParameter("nick_name", "提供方名称"); //提供方名称 packageReqHandler.SetParameter("send_name", "红包发送者名称"); //红包发送者名称 packageReqHandler.SetParameter("re_openid", "接受收红包的用户的openId"); //接受收红包的用户的openId packageReqHandler.SetParameter("total_amount", "100"); //付款金额,单位分 packageReqHandler.SetParameter("min_value", "100"); //最小红包金额,单位分 packageReqHandler.SetParameter("max_value", "100"); //最大红包金额,单位分 packageReqHandler.SetParameter("total_num", "1"); //红包发放总人数 packageReqHandler.SetParameter("wishing", "红包祝福语"); //红包祝福语 packageReqHandler.SetParameter("client_ip", Request.UserHostAddress); //调用接口的机器Ip地址 packageReqHandler.SetParameter("act_name", "活动名称"); //活动名称 packageReqHandler.SetParameter("remark", "备注信息"); //备注信息 string sign = packageReqHandler.CreateMd5Sign("key", TenPayV3Info.Key); packageReqHandler.SetParameter("sign", sign); //签名 //退款需要post的数据 string data = packageReqHandler.ParseXML(); //退款接口地址 string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; //本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中) string cert = @"F:\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 return(Content(responseContent)); }
public ActionResult Native() { string sp_billno = Request["order_no"]; //当前时间 yyyyMMdd string date = DateTime.Now.ToString("yyyyMMdd"); if (null == sp_billno) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 sp_billno = DateTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(4); } else { sp_billno = Request["order_no"].ToString(); } sp_billno = TenPayInfo.PartnerId + sp_billno; RequestHandler outParams = new RequestHandler(null); outParams.Init(); string productid = sp_billno; string timeStamp = TenPayUtil.GetTimestamp(); string nonceStr = TenPayUtil.GetNoncestr(); RequestHandler Params = new RequestHandler(null); Params.SetParameter("appid", TenPayInfo.AppId); Params.SetParameter("appkey", TenPayInfo.AppKey); Params.SetParameter("noncestr", nonceStr); Params.SetParameter("timestamp", timeStamp); Params.SetParameter("productid", productid); string sign = Params.CreateSHA1Sign(); Params.SetParameter("sign", sign); var parm = TenPay.NativePay(TenPayInfo.AppId, timeStamp, nonceStr, productid, sign); parm = QRCode.QRfromGoogle(parm); ViewData["parm"] = parm; return(View()); }
public ActionResult Delivernotify() { string timeStamp = ""; string appSignature = ""; //string appId, string openId, string transId, string out_Trade_No, string deliver_TimesTamp, string deliver_Status, string deliver_Msg, string app_Signature, string sp_billno = Request["order_no"]; //当前时间 yyyyMMdd string date = DateTime.Now.ToString("yyyyMMdd"); if (null == sp_billno) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 sp_billno = DateTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(4); } else { sp_billno = Request["order_no"].ToString(); } sp_billno = TenPayInfo.PartnerId + sp_billno; //调起微信支付签名 timeStamp = TenPayUtil.GetTimestamp(); //设置支付参数 RequestHandler paySignReqHandler = new RequestHandler(null); paySignReqHandler.SetParameter("appid", TenPayInfo.AppId); paySignReqHandler.SetParameter("openId", TenPayInfo.AppKey); paySignReqHandler.SetParameter("transId", "111112222233333"); paySignReqHandler.SetParameter("deliver_TimesTamp", timeStamp); paySignReqHandler.SetParameter("out_Trade_No", sp_billno); paySignReqHandler.SetParameter("deliver_Status", "1"); paySignReqHandler.SetParameter("deliver_Msg", "ok"); appSignature = paySignReqHandler.CreateSHA1Sign(); var result = TenPay.Delivernotify(TenPayInfo.AppId, "oX99MDgNcgwnz3zFN3DNmo8uwa-w", "111112222233333", sp_billno, timeStamp, "1", "ok", appSignature, "sha1"); ViewData["message"] = result.errcode; return(View()); }
protected void Page_Load(object sender, EventArgs e) { string sp_billno = Request["order_no"]; //当前时间 yyyyMMdd string date = DateTime.Now.ToString("yyyyMMdd"); if (null == sp_billno) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 sp_billno = DateTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(4); } else { sp_billno = Request["order_no"].ToString(); } sp_billno = TenPayInfo.PartnerId + sp_billno; RequestHandler outParams = new RequestHandler(Context); outParams.Init(); string productid = sp_billno; string timeStamp = TenPayUtil.GetTimestamp(); string nonceStr = TenPayUtil.GetNoncestr(); RequestHandler Params = new RequestHandler(Context); Params.SetParameter("appid", TenPayInfo.AppId); Params.SetParameter("appkey", TenPayInfo.AppKey); Params.SetParameter("noncestr", nonceStr); Params.SetParameter("timestamp", timeStamp); Params.SetParameter("productid", productid); string sign = Params.CreateSHA1Sign(); Params.SetParameter("sign", sign); parm = "weixin://wxpay/bizpayurl?sign=" + sign + "&appid=" + TenPayInfo.AppId + "&productid=" + productid + "&timeStamp=" + timeStamp + "&nonceStr=" + nonceStr; parm = QRfromGoogle(parm); }
public ActionResult JsApi() { //string appId = TenPayInfo.AppId; string timeStamp = ""; string nonceStr = ""; string packageValue = ""; string paySign = ""; string sp_billno = Request["order_no"]; //当前时间 yyyyMMdd string date = DateTime.Now.ToString("yyyyMMdd"); if (null == sp_billno) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 sp_billno = DateTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(4); } else { sp_billno = Request["order_no"].ToString(); } sp_billno = TenPayInfo.PartnerId + sp_billno; //创建支付应答对象 RequestHandler packageReqHandler = new RequestHandler(null); //初始化 packageReqHandler.Init(); packageReqHandler.SetKey(TenPayInfo.Key); //设置package订单参数 packageReqHandler.SetParameter("partner", TenPayInfo.PartnerId); //商户号 packageReqHandler.SetParameter("fee_type", "1"); //币种,1人民币 packageReqHandler.SetParameter("input_charset", "GBK"); packageReqHandler.SetParameter("bank_type", "WX"); packageReqHandler.SetParameter("out_trade_no", sp_billno); //商家订单号 packageReqHandler.SetParameter("total_fee", "1"); //商品金额,以分为单位(money * 100).ToString() packageReqHandler.SetParameter("notify_url", TenPayInfo.TenPayNotify); //接收财付通通知的URL packageReqHandler.SetParameter("body", "JSAPIdemo"); //商品描述 packageReqHandler.SetParameter("spbill_create_ip", Request.UserHostAddress); //用户的公网ip,不是商户服务器IP //获取package包 packageValue = packageReqHandler.GetRequestURL(); //调起微信支付签名 timeStamp = TenPayUtil.GetTimestamp(); nonceStr = TenPayUtil.GetNoncestr(); //设置支付参数 RequestHandler paySignReqHandler = new RequestHandler(null); paySignReqHandler.SetParameter("appid", TenPayInfo.AppId); paySignReqHandler.SetParameter("appkey", TenPayInfo.AppKey); paySignReqHandler.SetParameter("noncestr", nonceStr); paySignReqHandler.SetParameter("timestamp", timeStamp); paySignReqHandler.SetParameter("package", packageValue); paySign = paySignReqHandler.CreateSHA1Sign(); //TenPay.Delivernotify(TenPayInfo.AppId, "oX99MDgNcgwnz3zFN3DNmo8uwa-w", "111112222233333", sp_billno, // timeStamp, "1", "ok", "53cca9d47b883bd4a5c85a9300df3da0cb48565c", "sha1"); //获取debug信息,建议把请求和debug信息写入日志,方便定位问题 //string pakcageDebuginfo = packageReqHandler.getDebugInfo(); //Response.Write("<br/>pakcageDebuginfo:" + pakcageDebuginfo + "<br/>"); //string paySignDebuginfo = paySignReqHandler.getDebugInfo(); //Response.Write("<br/>paySignDebuginfo:" + paySignDebuginfo + "<br/>"); //TODO:和JSSDK一样整合信息包 ViewData["appId"] = TenPayInfo.AppId; ViewData["timeStamp"] = timeStamp; ViewData["nonceStr"] = nonceStr; ViewData["packageValue"] = packageValue; ViewData["paySign"] = paySign; return(View()); }
public ActionResult NativeCall() { string sp_billno = Request["order_no"]; //当前时间 yyyyMMdd string date = DateTime.Now.ToString("yyyyMMdd"); //订单号,此处用时间和随机数生成,商户根据自己调整,保证唯一 string out_trade_no = "" + DateTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(4); if (null == sp_billno) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 sp_billno = DateTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(4); } else { sp_billno = Request["order_no"].ToString(); } sp_billno = TenPayInfo.PartnerId + sp_billno; //创建RequestHandler实例 RequestHandler packageReqHandler = new RequestHandler(null); //初始化 packageReqHandler.Init(); packageReqHandler.SetKey(TenPayInfo.Key); //设置package订单参数 packageReqHandler.SetParameter("partner", TenPayInfo.PartnerId); //商户号 packageReqHandler.SetParameter("bank_type", "WX"); //银行类型 packageReqHandler.SetParameter("fee_type", "1"); //币种,1人民币 packageReqHandler.SetParameter("input_charset", "GBK"); packageReqHandler.SetParameter("out_trade_no", sp_billno); //商家订单号 packageReqHandler.SetParameter("total_fee", "1"); //商品金额,以分为单位(money * 100).ToString() packageReqHandler.SetParameter("notify_url", TenPayInfo.TenPayNotify); //接收财付通通知的URL packageReqHandler.SetParameter("body", "nativecall"); //商品描述 packageReqHandler.SetParameter("spbill_create_ip", "8.8.8.8" /*Page.Request.UserHostAddress*/); //用户的公网ip,不是商户服务器IP //获取package包 string packageValue = packageReqHandler.GetRequestURL(); //调起微信支付签名 string timeStamp = TenPayUtil.GetTimestamp(); string nonceStr = TenPayUtil.GetNoncestr(); //设置支付参数 RequestHandler payHandler = new RequestHandler(null); payHandler.SetParameter("appid", TenPayInfo.AppId); payHandler.SetParameter("noncestr", nonceStr); payHandler.SetParameter("timestamp", timeStamp); payHandler.SetParameter("package", packageValue); payHandler.SetParameter("RetCode", "0"); payHandler.SetParameter("RetErrMsg", "成功"); string paySign = payHandler.CreateSHA1Sign(); payHandler.SetParameter("app_signature", paySign); payHandler.SetParameter("sign_method", "SHA1"); Response.ContentType = "text/xml"; Response.Clear(); ViewData["payHandler"] = payHandler.ParseXML(); return(View()); }
public ActionResult JsApi(string code, string state) { if (string.IsNullOrEmpty(code)) { return(Content("您拒绝了授权!")); } if (state != "JeffreySu") { //这里的state其实是会暴露给客户端的,验证能力很弱,这里只是演示一下 //实际上可以存任何想传递的数据,比如用户ID,并且需要结合例如下面的Session["OAuthAccessToken"]进行验证 return(Content("验证失败!请从正规途径进入!")); } //通过,用code换取access_token var openIdResult = OAuth.GetAccessToken(TenPayV3Info.AppId, TenPayV3Info.AppSecret, code); if (openIdResult.errcode != ReturnCode.请求成功) { return(Content("错误:" + openIdResult.errmsg)); } string timeStamp = ""; string nonceStr = ""; string paySign = ""; string sp_billno = Request["order_no"]; //当前时间 yyyyMMdd string date = DateTime.Now.ToString("yyyyMMdd"); if (null == sp_billno) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 sp_billno = DateTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(28); } else { sp_billno = Request["order_no"].ToString(); } //创建支付应答对象 RequestHandler packageReqHandler = new RequestHandler(null); //初始化 packageReqHandler.Init(); //packageReqHandler.SetKey(""/*TenPayV3Info.Key*/); timeStamp = TenPayUtil.GetTimestamp(); nonceStr = TenPayUtil.GetNoncestr(); //设置package订单参数 packageReqHandler.SetParameter("appid", TenPayV3Info.AppId); //公众账号ID packageReqHandler.SetParameter("mch_id", TenPayV3Info.MchId); //商户号 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("body", "test"); packageReqHandler.SetParameter("out_trade_no", sp_billno); //商家订单号 packageReqHandler.SetParameter("total_fee", "1"); //商品金额,以分为单位(money * 100).ToString() packageReqHandler.SetParameter("spbill_create_ip", Request.UserHostAddress); //用户的公网ip,不是商户服务器IP packageReqHandler.SetParameter("notify_url", TenPayV3Info.TenPayV3Notify); //接收财付通通知的URL packageReqHandler.SetParameter("trade_type", TenPayV3Type.JSAPI.ToString()); //交易类型 packageReqHandler.SetParameter("openid", openIdResult.openid); //用户的openId string sign = packageReqHandler.CreateMd5Sign("key", TenPayV3Info.Key); packageReqHandler.SetParameter("sign", sign); //签名 string data = packageReqHandler.ParseXML(); var result = TenPayV3.Unifiedorder(data); var res = XDocument.Parse(result); string prepayId = res.Element("xml").Element("prepay_id").Value; //设置支付参数 RequestHandler paySignReqHandler = new RequestHandler(null); paySignReqHandler.SetParameter("appId", TenPayV3Info.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", TenPayV3Info.Key); ViewData["appId"] = TenPayV3Info.AppId; ViewData["timeStamp"] = timeStamp; ViewData["nonceStr"] = nonceStr; ViewData["package"] = string.Format("prepay_id={0}", prepayId); ViewData["paySign"] = paySign; return(View()); }
/// <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.partnerId; // "1218574001";// string partnerKey = paymentInfo.partnerKey; // 商户支付密钥Key。登录微信商户后台,进入栏目【账户设置】【密码安全】【API 安全】【API 密钥】 string notify_url = "http://" + HttpContext.Current.Request.Url.Authority + "/api/payment/wxpay/notify_url.aspx"; logBll.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") + TenPayUtil.BuildRandomStr(28); } //创建支付应答对象 Senparc.Weixin.MP.TenPayLibV3.RequestHandler packageReqHandler = new Senparc.Weixin.MP.TenPayLibV3.RequestHandler(null); //初始化 //packageReqHandler.SetKey(""/*TenPayV3Info.Key*/); timeStamp = TenPayUtil.GetTimestamp(); nonceStr = TenPayUtil.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 = TenPayV3.Unifiedorder(data); var res = XDocument.Parse(result); string prepayId = res.Element("xml").Element("prepay_id").Value; //设置支付参数 Senparc.Weixin.MP.TenPayLibV3.RequestHandler paySignReqHandler = new Senparc.Weixin.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 + "\""; }
public ActionResult Index() { string _code = Request.QueryString["code"]; //微信授权(如需授权,可开通) //weChatOauth(_code); //this.oAuthAccessTokenResult = OAuth.GetAccessToken("AppId", "AppSecret", _code); //this.oAuthAccessTokenResult = OAuth.RefreshToken("AppId", this.oAuthAccessTokenResult.refresh_token); //this.openid = this.oAuthAccessTokenResult.openid; //this.accessToken = this.oAuthAccessTokenResult.access_token; string timeStamp = TenPayUtil.GetTimestamp(); string nonceStr = TenPayUtil.GetNoncestr(); string paySign = ""; string sp_billno = Request["order_no"]; string openid = this.openid; //附加数据 string attach = sp_billno; //当前时间 yyyyMMdd string date = DateTime.Now.ToString("yyyyMMdd"); if (null == sp_billno) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 sp_billno = DateTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(28); } else { sp_billno = Request["order_no"].ToString(); } //创建支付应答对象 RequestHandler packageReqHandler = new RequestHandler(null); //初始化 //packageReqHandler.Init(); //packageReqHandler.SetKey(TenPayInfo.Key); //设置package订单参数 packageReqHandler.SetParameter("appid", TenPayInfo.AppId); //公众账号ID packageReqHandler.SetParameter("body", "test"); packageReqHandler.SetParameter("mch_id", TenPayInfo.Mchid); //商户号 packageReqHandler.SetParameter("nonce_str", nonceStr.ToLower()); //随机字符串 packageReqHandler.SetParameter("notify_url", TenPayInfo.TenPayNotify); //接收财付通通知的URL packageReqHandler.SetParameter("openid", openid); //openid packageReqHandler.SetParameter("out_trade_no", sp_billno); //商家订单号 packageReqHandler.SetParameter("spbill_create_ip", Request.UserHostAddress); //用户的公网ip,不是商户服务器IP packageReqHandler.SetParameter("total_fee", "1"); //商品金额,以分为单位(money * 100).ToString() packageReqHandler.SetParameter("trade_type", "JSAPI"); //交易类型 //获取package包 string sign = packageReqHandler.CreateMd5Sign("key", TenPayInfo.Key); packageReqHandler.SetParameter("sign", sign); //交易类型 string data = packageReqHandler.ParseXML(); var result = TenPayV3.Unifiedorder(data); var res = XDocument.Parse(result); string prepayId = ""; try { if (res.Element("xml").Element("return_code").Value == "SUCCESS") { prepayId = res.Element("xml").Element("prepay_id").Value; } } catch (Exception ex) { return(View()); } string package = string.Format("prepay_id={0}", prepayId); timeStamp = TenPayUtil.GetTimestamp(); //设置支付参数 RequestHandler paySignReqHandler = new RequestHandler(null); paySignReqHandler.SetParameter("appId", TenPayInfo.AppId); paySignReqHandler.SetParameter("timeStamp", timeStamp); paySignReqHandler.SetParameter("nonceStr", nonceStr); paySignReqHandler.SetParameter("package", package); paySignReqHandler.SetParameter("signType", "MD5"); paySign = paySignReqHandler.CreateMd5Sign("key", TenPayInfo.Key); ViewData["appId"] = TenPayInfo.AppId; ViewData["timeStamp"] = timeStamp; ViewData["nonceStr"] = nonceStr; ViewData["package"] = package; ViewData["paySign"] = paySign; return(View()); }
//public ActionResult FeedBack() //{ // //创建支付应答对象 // ResponseHandler resHandler = new ResponseHandler(null); // resHandler.Init(); // resHandler.SetKey(TenPayInfo.Key, TenPayInfo.AppKey); // //判断签名 // if (resHandler.IsWXsignfeedback()) // { // //回复服务器处理成功 // Response.Write("OK"); // Response.Write("OK:" + resHandler.GetDebugInfo()); // } // else // { // //sha1签名失败 // Response.Write("fail"); // Response.Write("fail:" + resHandler.GetDebugInfo()); // } // return null; //} public ActionResult JsApi() { string appId = "";//TenPayV3Info.AppId string timeStamp = ""; string nonceStr = ""; string package = ""; string paySign = ""; string sp_billno = Request["order_no"]; //当前时间 yyyyMMdd string date = DateTime.Now.ToString("yyyyMMdd"); if (null == sp_billno) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 sp_billno = DateTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(28); } else { sp_billno = Request["order_no"].ToString(); } //创建支付应答对象 RequestHandler packageReqHandler = new RequestHandler(null); //初始化 packageReqHandler.Init(); packageReqHandler.SetKey("" /*TenPayV3Info.Key*/); timeStamp = TenPayUtil.GetTimestamp(); nonceStr = TenPayUtil.GetNoncestr(); //设置package订单参数 packageReqHandler.SetParameter("appid", appId); //公众账号ID packageReqHandler.SetParameter("mch_id", "" /*TenPayV3Info.Mchid*/); //商户号 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("body", "test"); packageReqHandler.SetParameter("out_trade_no", sp_billno); //商家订单号 packageReqHandler.SetParameter("total_fee", "1"); //商品金额,以分为单位(money * 100).ToString() packageReqHandler.SetParameter("spbill_create_ip", /*Request.UserHostAddress*/ "8.8.8.8"); //用户的公网ip,不是商户服务器IP packageReqHandler.SetParameter("notify_url", "www.weiweihi.com" /*_tenPayInfo.TenPayNotify*/); //接收财付通通知的URL packageReqHandler.SetParameter("trade_type", TenPayV3Type.JSAPI.ToString()); //交易类型 //获取package包 string sign = packageReqHandler.CreateMd5Sign("key", "" /*TenPayV3Info.Key*/); packageReqHandler.SetParameter("sign", sign); //交易类型 string data = packageReqHandler.ParseXML(); var result = TenPayV3.Unifiedorder(data); var res = XDocument.Parse(result); string prepayId = res.Element("xml").Element("prepay_id").Value; //设置支付参数 RequestHandler paySignReqHandler = new RequestHandler(null); paySignReqHandler.SetParameter("appid", appId); paySignReqHandler.SetParameter("timestamp", timeStamp); paySignReqHandler.SetParameter("noncestr", nonceStr); paySignReqHandler.SetParameter("package", string.Format("prepay_id={0}", prepayId)); paySign = paySignReqHandler.CreateMd5Sign("signType", "MD5"); //获取debug信息,建议把请求和debug信息写入日志,方便定位问题 //string pakcageDebuginfo = packageReqHandler.getDebugInfo(); //Response.Write("<br/>pakcageDebuginfo:" + pakcageDebuginfo + "<br/>"); //string paySignDebuginfo = paySignReqHandler.getDebugInfo(); //Response.Write("<br/>paySignDebuginfo:" + paySignDebuginfo + "<br/>"); ViewData["appId"] = appId; ViewData["timeStamp"] = timeStamp; ViewData["nonceStr"] = nonceStr; ViewData["package"] = package; ViewData["paySign"] = paySign; return(View()); }
/// <summary> /// JSAPI 支付 /// </summary> /// <returns></returns> public ActionResult JsApi() { string timeStamp = null; string nonceStr = null; string packageValue = null; string paySign = null; string sp_billno = Request.Form["order_no"]; //当前时间 yyyyMMdd string date = SystemTime.Now.ToString("yyyyMMdd"); if (null == sp_billno) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一 sp_billno = SystemTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(4); } else { sp_billno = Request.Form["order_no"].ToString(); } sp_billno = TenPayInfo.PartnerId + sp_billno; //创建支付应答对象 RequestHandler packageReqHandler = new RequestHandler(null); //初始化 packageReqHandler.Init(); packageReqHandler.SetKey(TenPayInfo.Key); //设置package订单参数 packageReqHandler.SetParameter("partner", TenPayInfo.PartnerId); //商户号 packageReqHandler.SetParameter("fee_type", "1"); //币种,1人民币 packageReqHandler.SetParameter("input_charset", "GBK"); packageReqHandler.SetParameter("bank_type", "WX"); packageReqHandler.SetParameter("out_trade_no", sp_billno); //商家订单号 packageReqHandler.SetParameter("total_fee", "1"); //商品金额,以分为单位(money * 100).ToString() packageReqHandler.SetParameter("notify_url", TenPayInfo.TenPayNotify); //接收财付通通知的URL packageReqHandler.SetParameter("body", "JSAPIdemo"); //商品描述 packageReqHandler.SetParameter("spbill_create_ip", HttpContext.UserHostAddress()?.ToString()); //用户的公网ip,不是商户服务器IP //获取package包 packageValue = packageReqHandler.GetRequestURL(); //调起微信支付签名 timeStamp = TenPayUtil.GetTimestamp(); nonceStr = TenPayUtil.GetNoncestr(); //设置支付参数 RequestHandler paySignReqHandler = new RequestHandler(null); paySignReqHandler.SetParameter("appid", TenPayInfo.AppId); paySignReqHandler.SetParameter("appkey", TenPayInfo.AppKey); paySignReqHandler.SetParameter("noncestr", nonceStr); paySignReqHandler.SetParameter("timestamp", timeStamp); paySignReqHandler.SetParameter("package", packageValue); paySign = paySignReqHandler.CreateSHA1Sign(); //TODO:和JSSDK一样整合信息包 ViewData["appId"] = TenPayInfo.AppId; ViewData["timeStamp"] = timeStamp; ViewData["nonceStr"] = nonceStr; ViewData["packageValue"] = packageValue; ViewData["paySign"] = paySign; return(View()); }