/** * * 统一下单 * @param WxPaydata inputObj 提交给统一下单API的参数 * @param int timeOut 超时时间 * @throws Exception * @return 成功时返回,其他抛异常 */ public SortedDictionary <string, object> UnifiedOrder(SortedDictionary <string, object> inputObj, int timeOut = 6) { //检测必填参数 if (!inputObj.ContainsKey("out_trade_no")) { throw new Exception("缺少统一支付接口必填参数out_trade_no!"); } else if (!inputObj.ContainsKey("body")) { throw new Exception("缺少统一支付接口必填参数body!"); } else if (!inputObj.ContainsKey("total_fee")) { throw new Exception("缺少统一支付接口必填参数total_fee!"); } else if (!inputObj.ContainsKey("trade_type")) { throw new Exception("缺少统一支付接口必填参数trade_type!"); } //关联参数 if (inputObj["trade_type"].ToString() == "JSAPI" && !inputObj.ContainsKey("openid")) { throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } if (inputObj["trade_type"].ToString() == "NATIVE" && !inputObj.ContainsKey("product_id")) { throw new Exception("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!"); } //异步通知url未设置,则使用配置文件中的url if (!inputObj.ContainsKey("notify_url")) { inputObj.Add("notify_url", "http://url");//异步通知url } inputObj.Add("appid", appid); //公众账号ID inputObj.Add("mch_id", mch_id); //商户号 inputObj.Add("spbill_create_ip", ip); //终端ip inputObj.Add("nonce_str", PayHelper.Nonce_str); //随机字符串 inputObj.Add("sign", PayHelper.MakeSign(inputObj, key)); string response = HttpService.Post(PayHelper.ToXml(inputObj), "https://api.mch.weixin.qq.com/pay/unifiedorder", false, timeOut); return(PayHelper.FromXml(response, key)); }
/** * * 关闭订单 * @param WxPayData inputObj 提交给关闭订单API的参数 * @param int timeOut 接口超时时间 * @throws Exception * @return 成功时返回,其他抛异常 */ public SortedDictionary <string, object> CloseOrder(SortedDictionary <string, object> inputObj, int timeOut = 6) { //检测必填参数 if (!inputObj.ContainsKey("out_trade_no")) { throw new Exception("关闭订单接口中,out_trade_no必填!"); } inputObj.Add("appid", appid); //公众账号ID inputObj.Add("mch_id", mch_id); //商户号 inputObj.Add("nonce_str", PayHelper.Nonce_str); //随机字符串 inputObj.Add("sign", PayHelper.MakeSign(inputObj)); //签名 string xml = PayHelper.ToXml(inputObj); string response = HttpService.Post(xml, "https://api.mch.weixin.qq.com/pay/closeorder", false, timeOut); return(PayHelper.FromXml(response)); }
/** * 刷卡支付完整业务流程逻辑 * @param body 商品描述 * @param total_fee 总金额 * @param auth_code 支付授权码 * @throws WxPayException * @return 刷卡支付结果 */ public ActionResult MicroPay(string body, string total_fee, string auth_code) { SortedDictionary <string, object> data = new SortedDictionary <string, object> { { "auth_code", auth_code }, //用户出示授权码 { "body", body }, //商品描述 { "total_fee", int.Parse(total_fee) }, //总金额 { "out_trade_no", PayHelper.Out_trade_no }, //产生随机的商户订单号 { "spbill_create_ip", ip }, //终端ip { "appid", appid }, { "mch_id", mch_id }, { "nonce_str", PayHelper.Nonce_str }//随机字符串 }; data.Add("sign", PayHelper.MakeSign(data));//签名 string xml = PayHelper.ToXml(data); string url = "https://api.mch.weixin.qq.com/pay/micropay"; string response = HttpService.Post(xml, url, false, 10);//调用HTTP通信接口以提交数据到API //将xml格式的结果转换为对象以返回 SortedDictionary <string, object> result = PayHelper.FromXml(response); //如果提交被扫支付接口调用失败,则抛异常 if (!result.ContainsKey("return_code") || result["return_code"].ToString() == "FAIL") { string returnMsg = result.ContainsKey("return_msg") ? result["return_msg"].ToString() : ""; throw new Exception("Micropay API interface call failure, return_msg : " + returnMsg); } //签名验证 result.CheckSign(); //刷卡支付直接成功 if (result["return_code"].ToString() == "SUCCESS" && result["result_code"].ToString() == "SUCCESS") { return(Json(result)); } /****************************************************************** * 剩下的都是接口调用成功,业务失败的情况 * ****************************************************************/ //1)业务结果明确失败 if (result["err_code"].ToString() != "USERPAYING" && result["err_code"].ToString() != "SYSTEMERROR") { return(Json(result)); } //2)不能确定是否失败,需查单 //用商户订单号去查单 string out_trade_no = data["out_trade_no"].ToString(); //确认支付是否成功,每隔一段时间查询一次订单,共查询10次 int queryTimes = 10;//查询次数计数器 while (queryTimes-- > 0) { SortedDictionary <string, object> queryResult = MicroPayQuery(out_trade_no, out int succResult); //如果需要继续查询,则等待2s后继续 if (succResult == 2) { Thread.Sleep(2000); continue; } else if (succResult == 1) { //查询成功,返回订单查询接口返回的数据 return(Json(queryResult)); } else { //订单交易失败,直接返回刷卡支付接口返回的结果,失败原因会在err_code中描述 return(Json(result)); } } //确认失败,则撤销订单 if (!MicroPayCancel(out_trade_no)) { throw new Exception("Reverse order failure!"); } return(Json(result)); }
protected void Page_Load(object sender, System.EventArgs e) { string formString = GameRequest.GetFormString("account"); if (formString == "") { base.Response.Write("充值账号错误"); base.Response.End(); } int formInt = GameRequest.GetFormInt("amount", 0); if (formInt < 6) { base.Response.Write("充值金额不能低于6元"); base.Response.End(); } string text = GameRequest.GetFormString("type"); OnLineOrder onLineOrder = new OnLineOrder(); onLineOrder.OrderID = PayHelper.GetOrderIDByPrefix("dd"); if (Fetch.GetUserCookie() == null) { onLineOrder.OperUserID = 0; } else { onLineOrder.OperUserID = Fetch.GetUserCookie().UserID; } onLineOrder.Accounts = formString; onLineOrder.OrderAmount = formInt; onLineOrder.IPAddress = GameRequest.GetUserIP(); string text4 = text; switch (text4) { case "alipay": text = "010008"; onLineOrder.ShareID = 2; goto IL_289; case "weixin": text = "010007"; onLineOrder.ShareID = 3; goto IL_289; case "alipay-scan": text = "010002"; this.paytype = "支付宝"; onLineOrder.ShareID = 4; goto IL_289; case "weixin-scan": text = "010001"; this.paytype = "微信"; onLineOrder.ShareID = 5; goto IL_289; case "qq": text = "7"; onLineOrder.ShareID = 6; goto IL_289; case "kuaijie": text = "010010"; onLineOrder.ShareID = 7; goto IL_289; case "qq-scan": text = "010000"; this.paytype = "QQ"; onLineOrder.ShareID = 8; goto IL_289; case "jd": text = "6"; onLineOrder.ShareID = 9; goto IL_289; case "baidu": text = "5"; onLineOrder.ShareID = 10; goto IL_289; } text = "4"; onLineOrder.ShareID = 1; IL_289: Message message = FacadeManage.aideTreasureFacade.RequestOrder(onLineOrder); if (!message.Success) { base.Response.Write(message.Content); base.Response.End(); } string url = ApplicationSettings.Get("url_dd"); string value = ApplicationSettings.Get("parter_dd"); string str = ApplicationSettings.Get("key_dd"); string text2 = ApplicationSettings.Get("pay_url"); if (text2 == "") { text2 = "http://" + base.Request.Url.Host; } string orderID = onLineOrder.OrderID; string value2 = text2 + "/pay/diandian/notify_url.aspx"; string str2 = "{\"mch_app_id\":\"http://www.qp137.com\",\"device_info\":\"AND_WAP\",\"ua\":\"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36\",\"mch_app_name\":\"支付测试\",\"cashier_desk\":\"1\"}"; string s = System.Web.HttpUtility.UrlEncode(str2); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s); string value3 = System.Convert.ToBase64String(bytes); System.Collections.Generic.Dictionary <string, string> dictionary = new System.Collections.Generic.Dictionary <string, string>(); dictionary["version"] = "1.0"; dictionary["charset"] = "utf-8"; dictionary["merchant_id"] = value; dictionary["out_trade_no"] = orderID; dictionary["user_ip"] = GameRequest.GetUserIP(); dictionary["subject"] = "支付测试"; dictionary["body"] = "支付测试"; dictionary["user_id"] = formString; dictionary["total_fee"] = formInt.ToString("#0.00"); dictionary["notify_url"] = value2; dictionary["return_url"] = value2; dictionary["nonce_str"] = TextUtility.CreateAuthStr(20, false); dictionary["biz_content"] = value3; dictionary["trade_type"] = text; dictionary = ( from p in dictionary orderby p.Key select p).ToDictionary((System.Collections.Generic.KeyValuePair <string, string> p) => p.Key, (System.Collections.Generic.KeyValuePair <string, string> o) => o.Value); string str3 = PayHelper.PrepareSign(dictionary) + "&key=" + str; dictionary["sign"] = Jiami.MD5(str3).ToUpper(); string param = PayHelper.ToXml(dictionary); string text3 = HttpHelper.HttpRequest(url, param, "post", "utf-8", "text/xml"); System.Collections.Generic.Dictionary <string, string> dictionary2 = PayHelper.XmlToDic(text3); if (!dictionary2.ContainsKey("status")) { base.Response.Write(text3); base.Response.End(); } else { string a = dictionary2["status"]; if (a == "0") { this.order_no = orderID; this.order_amount = formInt.ToString(); this.qrcode = dictionary2["pay_info"]; base.Response.Redirect(this.qrcode); } else { base.Response.Write(dictionary2["message"]); base.Response.End(); } } }