/// <summary> /// 创建JSAPI支付 /// </summary> /// <param name="prepay_id"></param> /// <returns></returns> /// { result="SUCCESS", appid="", timeStamp="", nonceStr="", package="", paySign="" } public JsonResult createPay(string prepay_id) { try { List <QueryParameter> JSPayParameters = new List <QueryParameter>(); string _appid = WeChatUtilities.getConfigValue(WeChatUtilities.APP_ID); JSPayParameters.Add(new QueryParameter("appId", _appid)); string _timeStamp = CommonUtilities.generateTimeStamp().ToString(); JSPayParameters.Add(new QueryParameter("timeStamp", _timeStamp)); string _jspayNonce = CommonUtilities.generateNonce(); JSPayParameters.Add(new QueryParameter("nonceStr", CommonUtilities.generateNonce())); string _package = "prepay_id=" + prepay_id; JSPayParameters.Add(new QueryParameter("package", _package)); JSPayParameters.Add(new QueryParameter("signType", "MD5")); string JSPayQuery = QueryParameter.NormalizeRequestParameters(JSPayParameters); StringBuilder jSenValue = new StringBuilder(); jSenValue.Append(JSPayQuery); jSenValue.Append(string.Format("&key={0}", WeChatUtilities.getConfigValue(WeChatUtilities.PAYAPI_KEY))); string jssign = CommonUtilities.encrypt_MD5(jSenValue.ToString()).ToUpper(); return(Json(new { result = "SUCCESS", appid = _appid, timeStamp = _timeStamp, nonceStr = _jspayNonce, package = _package, paySign = jssign }, JsonRequestBehavior.AllowGet)); } catch { return(Json(new { result = "FAIL" }, JsonRequestBehavior.AllowGet)); } }
/// <summary> /// 提交刷卡支付 /// </summary> /// <param name="code">条码号</param> /// <param name="body">支付信息</param> /// <param name="amount">支付金额(分)</param> /// <returns></returns> public JsonResult createMicroPay(string code, string body, int amount) { //随机数字,并且生成Prepay string appid = WeChatUtilities.getConfigValue(WeChatUtilities.APP_ID); string mch_id = WeChatUtilities.getConfigValue(WeChatUtilities.MCH_ID); //先确认,之后做随机数 string nonce_str = CommonUtilities.generateNonce(); string out_trade_no = "WX" + CommonUtilities.generateTimeStamp(); Random r = new Random(); int total_fee = amount; string spbill_create_ip = Request.ServerVariables.Get("Remote_Addr").ToString(); //string notify_url = "https://api.mch.weixin.qq.com/pay/micropay"; //string trade_type = "JSAPI"; //string openid = _openId; List <QueryParameter> parameters = new List <QueryParameter>(); parameters.Add(new QueryParameter("appid", appid)); parameters.Add(new QueryParameter("mch_id", mch_id)); parameters.Add(new QueryParameter("nonce_str", nonce_str)); parameters.Add(new QueryParameter("body", body)); parameters.Add(new QueryParameter("out_trade_no", out_trade_no)); parameters.Add(new QueryParameter("total_fee", total_fee.ToString())); parameters.Add(new QueryParameter("spbill_create_ip", spbill_create_ip)); parameters.Add(new QueryParameter("auth_code", code)); string query = QueryParameter.NormalizeRequestParameters(parameters); StringBuilder enValue = new StringBuilder(); enValue.Append(query); enValue.Append("&key=" + WeChatUtilities.getConfigValue(WeChatUtilities.PAYAPI_KEY)); string sign = CommonUtilities.encrypt_MD5(enValue.ToString()).ToUpper(); string content = parseXml(parameters, sign); string post_url = "https://api.mch.weixin.qq.com/pay/micropay"; var request = WebRequest.Create(post_url) as HttpWebRequest; try { request.Method = "post"; StreamWriter streamWriter = new StreamWriter(request.GetRequestStream()); streamWriter.Write(content); streamWriter.Flush(); streamWriter.Close(); var response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string result = reader.ReadToEnd(); XmlDocument doc = new XmlDocument(); doc.LoadXml(result); string return_code = doc.GetElementsByTagName("return_code")[0].InnerText; string result_code = doc.GetElementsByTagName("result_code")[0].InnerText; string openid = doc.GetElementsByTagName("openid")[0].InnerText; //return Json(new { result = "SUCCESS", prepay_id = prepay_id, total_fee = total_fee }, JsonRequestBehavior.AllowGet); return(Json(new { result = "SUCCESS" }, JsonRequestBehavior.AllowGet)); } catch (Exception) { return(Json(new { result = "FAIL" }, JsonRequestBehavior.AllowGet)); } }
/// <summary> /// 生成扫码链接 /// </summary> /// <param name="product_id"></param> /// <returns></returns> public ContentResult create_native_pay_url(string product_id) { string appid = WeChatUtilities.getConfigValue(WeChatUtilities.APP_ID); string mch_id = WeChatUtilities.getConfigValue(WeChatUtilities.MCH_ID); string nonce = CommonUtilities.generateNonce(); string time_stamp = Convert.ToString(CommonUtilities.generateTimeStamp()); List <QueryParameter> parameters = new List <QueryParameter>(); parameters.Add(new QueryParameter("appid", appid)); parameters.Add(new QueryParameter("mch_id", mch_id)); parameters.Add(new QueryParameter("time_stamp", time_stamp)); parameters.Add(new QueryParameter("nonce_str", nonce)); parameters.Add(new QueryParameter("product_id", product_id)); string sign = WeChatUtilities.createPaySign(parameters); string url = "weixin://wxpay/bizpayurl?" + QueryParameter.NormalizeRequestParameters(parameters) + "&sign=" + sign; return(Content(url)); }