/// <summary>
/// 重新发起支付
/// </summary>
/// <param name="tmpModel"></param>
/// <returns></returns>
[HttpPost]
public ActionResult RePayJsApi()
{
try
{
string orderId = ClassesLib.GetString( "OrderId" );
string newOrderId = ClassesLib.GetIdByTime();
var model = db.MEMBER_ORDER.Find(orderId);
model.ORDERID = newOrderId;
db.SaveChanges();
//登录判断
if (ClassesLib.WxSessionIsNull())
{
return Content( "<script>alert('登录超时,请重新登录!');window.location.href='/WeiXin/Login?returnUrl=/WeiXin/ViewProduct?objId=" + model.OBJECTID + "&salesmanId=" + model.SALEID + "'</script>" );
}
//调用支付
JsApiPay jsApiPay = new JsApiPay();
jsApiPay.bodyStr = model.OBJECTNAME + "-" + ClassesLib.getWxLoginName();
jsApiPay.attachStr = model.OBJECTNAME + "-" + ClassesLib.getWxLoginName();
jsApiPay.orderId = newOrderId;
jsApiPay.openid = model.OPENID;
jsApiPay.total_fee = Convert.ToInt32(model.MONEY * 100); //测试 订单金额(1表示分,正式金额要*100)
//JSAPI支付预处理
//调用统一下单,获得下单结果
WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();
//从统一下单成功返回的数据中获取微信浏览器调起jsapi支付所需的参数
var wxJsApiParam = jsApiPay.GetJsApiParameters(); //获取到的是json格式字符串
return Content(wxJsApiParam);
}
catch (Exception e)
{
return Content( "Error" );
}
}
[HttpPost]
public virtual ActionResult JsApiNotifyUrl()
{
ClassesLib.InsertLog( "支付测试1" , "支付" );
WxPayData notifyData = GetNotifyData();
//检查支付结果中transaction_id是否存在
if (!notifyData.IsSet( "transaction_id" ))
{
//若transaction_id不存在,则立即返回结果给微信支付后台
WxPayData res = new WxPayData();
res.SetValue( "return_code" , "FAIL" );
res.SetValue( "return_msg" , "支付结果中微信订单号不存在" );
Log.Error( this .GetType().ToString(), "The Pay result is error : " + res.ToXml());
return Content( "支付结果中微信订单号不存在" );
}
ClassesLib.InsertLog( "支付测试2" , "支付" );
string transaction_id = notifyData.GetValue( "transaction_id" ).ToString();
string trade_no = notifyData.GetValue( "out_trade_no" ).ToString();
//查询订单,判断订单真实性
if (!QueryOrder(transaction_id))
{
//若订单查询失败,则立即返回结果给微信支付后台
WxPayData res = new WxPayData();
res.SetValue( "return_code" , "FAIL" );
res.SetValue( "return_msg" , "订单查询失败" );
Log.Error( this .GetType().ToString(), "Order query failure : " + res.ToXml());
return Content( "订单查询失败" );
}
//查询订单成功
else
{
ClassesLib.InsertLog( "支付测试3" , "支付" );
WxPayData res = new WxPayData();
res.SetValue( "return_code" , "SUCCESS" );
res.SetValue( "return_msg" , "OK" );
Log.Info( this .GetType().ToString(), "order query success : " + res.ToXml());
//更新支付状态
var model = db.MEMBER_ORDER.Where(c => c.ORDERID == trade_no).FirstOrDefault();
if (model != null && model.PAYSTATE == "未支付" )
{
model.PAYSTATE = "已支付" ;
model.PAYDATE = DateTime.Now;
//发送支付成功提示,通过微信消息通知收银员已经收到付款
SendTemplateMessage.SendMessage( "oPCLNv5B1T8kYM5AYgXWHOiD24_s" , "您好,用户" + model.MEMBERNAME + "已购买服务" , model.OBJECTNAME, (Convert.ToDouble(model.MONEY) * 0.01).ToString() + "元" , DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss" ));
db.SaveChanges();
}
return Content( "支付成功" + res.ToXml());
}
}
|