public async Task <IHttpActionResult> QueryPayState(string id) { JsonResult <int> result = new JsonResult <int>(); if (id.IsNull()) { result.code = 0; result.msg = "参数错误"; result.Result = -1; return(Ok(result)); } result.code = 1; result.msg = "OK"; result.Result = 0; await Task.Run(() => { PayOrderBll bll = new PayOrderBll(); var order = bll.GetByID(id); if (order != null) { if (order.PayState == PayStateEnum.Fail) { result.Result = -1; result.ResultMsg = "支付失败"; } if (order.PayState == PayStateEnum.Paid) { result.Result = 1; result.ResultMsg = "支付成功"; } } }); return(Ok(result)); }
public JsonResult <PayOrder> Post(UnifiedOrder obj) { ClassLoger.Info("PayController.Post", obj.PayTyp.ToString(), obj.product_id, obj.productType.TryToString()); JsonResult <PayOrder> result = new JsonResult <PayOrder>(); result.code = 1; result.msg = "OK"; PayOrderBll porderbll = new PayOrderBll(); try { if (obj.PayTyp == 1) { JsApiPay jsApiPay = new JsApiPay(); jsApiPay.openid = ""; if (!obj.openid.IsNull()) { WeChatUserBll wuserbll = new WeChatUserBll(); WeChatUser wuser = wuserbll.GetWeChatUserByUnionID(obj.openid); jsApiPay.openid = wuser.ServiceOpenID; } else if (!obj.userid.IsNull()) { UserInfoBll ubll = new UserInfoBll(); var user = ubll.GetUserinfoByID(obj.userid); if (!user.Openid.IsNull()) { WeChatUserBll wuserbll = new WeChatUserBll(); WeChatUser wuser = wuserbll.GetWeChatUserByUnionID(user.Openid); jsApiPay.openid = wuser.ServiceOpenID; } } jsApiPay.total_fee = 1; jsApiPay.trade_type = obj.trade_type; if (obj.productType == 1) { //查询商品信息,获取商品价格 double Price = 0.1; jsApiPay.total_fee = (Price * 100).TryToInt(); jsApiPay.body = "商品名称"; } jsApiPay.attach = "北京xx"; string out_trade_no = porderbll.GetOnlineOrder(PayTypeEnum.WeChart); jsApiPay.out_trade_no = out_trade_no; jsApiPay.product_id = obj.product_id; ClassLoger.Info("api/Pay/UnifiedOrder", "total_fee", jsApiPay.total_fee.ToString()); WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(); PayOrder porder = new PayOrder(); if (unifiedOrderResult.GetValue("return_code").TryToString() == "SUCCESS" && unifiedOrderResult.GetValue("result_code").TryToString() == "SUCCESS") { if (unifiedOrderResult.IsSet("code_url")) { string code_url = unifiedOrderResult.GetValue("code_url").TryToString(); string filename = string.Format("WXPY{0}.png", code_url.MD5()); string filepath = Path.Combine(SystemSet.ResourcesPath, SystemSet.QrCodePic, filename); if (!File.Exists(filepath)) { QrCodeHelper.CreateImgCode(code_url, filepath); } porder.code_url = Path.Combine(SystemSet.WebResourcesSite, SystemSet.QrCodePic, filename); } porder.OnlineOrder = out_trade_no; porder.openid = obj.openid; porder.PayState = PayStateEnum.Unpaid; porder.PayType = PayTypeEnum.WeChart; porder.prepay_id = unifiedOrderResult.GetValue("prepay_id").TryToString(); porder.product_id = obj.product_id; porder.total_fee = jsApiPay.total_fee; porder.trade_type = obj.trade_type; porder.CreateTime = DateTime.Now; porder.UserID = obj.userid; porder.ID = porderbll.AddPayOrder(porder); result.Result = porder; return(result); } else { result.code = 0; result.msg = unifiedOrderResult.GetValue("return_code").TryToString(); result.ResultMsg = unifiedOrderResult.GetValue("result_code").TryToString(); } } } catch (Exception ex) { result.code = -1; result.msg = "PayController.unifiedorder:" + ex.Message; } return(result); }
public void Index() { try { WxPayData notifyData = GetNotifyData(Request, Response); ClassLoger.Info("WeChartPayCallBack.Index", notifyData.ToJson()); //检查支付结果中transaction_id是否存在 if (!notifyData.IsSet("transaction_id")) { //若transaction_id不存在,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "支付结果中微信订单号不存在"); ClassLoger.Fail("WeChartPayCallBackController.Index:支付结果中微信订单号不存在", notifyData.ToXml()); Response.Write(res.ToXml()); Response.End(); } string transaction_id = notifyData.GetValue("transaction_id").ToString(); //查询订单,判断订单真实性 if (!QueryOrder(transaction_id)) { //若订单查询失败,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "订单查询失败"); ClassLoger.Fail(this.GetType().ToString(), "Order query failure : " + res.ToXml()); Response.Write(res.ToXml()); Response.End(); }//查询订单成功 else { ThreadPool.QueueUserWorkItem(new WaitCallback(p => { string out_trade_no = notifyData.GetValue("out_trade_no").ToString(); PayOrderBll porderBll = new PayOrderBll(); PayOrder porder = porderBll.GetByOnlineOrder(out_trade_no); string mqttmsg = string.Empty; if (notifyData.GetValue("result_code").ToString() == "SUCCESS") { porder.PayState = Model.EnumModel.PayStateEnum.Paid; mqttmsg = MqttAgreement.GetPsychtestWeChartPayState(out_trade_no, true); } else { porder.PayState = Model.EnumModel.PayStateEnum.Fail; if (notifyData.IsSet("err_code_des")) { porder.PayStateMsg = notifyData.GetValue("err_code_des").TryToString(); } mqttmsg = MqttAgreement.GetPsychtestWeChartPayState(out_trade_no, false); } porderBll.UpdatePayOrder(porder); MqttPublishClient.Ins.PublishOneUser(porder.ID, mqttmsg); }), null); WxPayData res = new WxPayData(); res.SetValue("return_code", "SUCCESS"); res.SetValue("return_msg", "OK"); ClassLoger.Info(this.GetType().ToString(), "order query success : " + res.ToXml()); Response.Write(res.ToXml()); Response.End(); } } catch (Exception ex) { ClassLoger.Error("WeChartPayCallBackController.Index", ex); } }