public ActionResult Pay() { var appId = WeixinConfig.AppID; var nonceStr = Util.CreateNonce_str(); var timestamp = Util.CreateTimestamp(); var domain = System.Configuration.ConfigurationManager.AppSettings["Domain"]; var url = domain + Request.Url.PathAndQuery; var jsTickect = WeixinConfig.TokenHelper.GetJSTickect(); var string1 = ""; var signature = JSAPI.GetSignature(jsTickect, nonceStr, timestamp, url, out string1); var userAgent = Request.UserAgent; var userVersion = userAgent.Substring(userAgent.LastIndexOf("/") + 1);//微信版本号高于或者等于5.0才支持微信支付 var model = new JSPayModel { appId = appId, nonceStr = nonceStr, signature = signature, timestamp = timestamp, jsapiTicket = jsTickect, string1 = string1, userAgent = userAgent, userVersion = userVersion, }; return(View(model)); }
public ActionResult Index() { try { var out_trade_no = Guid.NewGuid().ToString("N"); var domain = System.Configuration.ConfigurationManager.AppSettings["Domain"]; var body = "商品描述"; var detail = "商品详情"; var attach = ""; var product_id = "1"; var openid = User.Identity.Name; var goods_tag = ""; var fee_type = "CNY"; var total_fee = 10; var trade_type = "JSAPI"; var appId = System.Configuration.ConfigurationManager.AppSettings["AppId"]; var nonceStr = Util.CreateNonce_str(); var timestamp = Util.CreateTimestamp(); var success_redict_url = string.Format("{0}/WxPay/Success", domain); var url = domain + Request.Url.PathAndQuery; var userAgent = Request.UserAgent; var userVersion = userAgent.Substring(userAgent.LastIndexOf("MicroMessenger/") + 15, 3);//微信版本号高于或者等于5.0才支持微信支付 var spbill_create_ip = (trade_type == "APP" || trade_type == "NATIVE") ? Request.UserHostName : WeixinConfig.spbill_create_ip; var time_start = DateTime.Now.ToString("yyyyMMddHHmmss"); var time_expire = DateTime.Now.AddHours(1).ToString("yyyyMMddHHmmss"); //默认1个小时订单过期,开发者可自定义其他超时机制,原则上微信订单超时时间不超过2小时 var notify_url = string.Format("{0}/WxPay/Notify", domain); //与下面的Notify对应,开发者可自定义其他url地址 var partnerKey = WeixinConfig.PartnerKey; var mch_id = WeixinConfig.mch_id; var device_info = WeixinConfig.device_info; var returnXML = ""; var paramaterXml = ""; var content = WxPayAPI.UnifiedOrder( appId, mch_id, device_info, nonceStr, body, detail, attach, out_trade_no, fee_type, total_fee, spbill_create_ip, time_start, time_expire, goods_tag, notify_url, trade_type, product_id, openid, partnerKey, out returnXML, out paramaterXml); LogWriter.Default.WriteError(paramaterXml); LogWriter.Default.WriteError(returnXML); var prepay_id = ""; var sign = ""; var return_code = ""; var return_msg = ""; var err_code = ""; var err_code_des = ""; var isUnifiedOrderSuccess = false; if (content.return_code.Value == "SUCCESS" && content.result_code.Value == "SUCCESS") { prepay_id = content.prepay_id.Value; sign = WxPayAPI.SignPay(appId, timestamp.ToString(), nonceStr, prepay_id, partnerKey); trade_type = content.trade_type.Value; isUnifiedOrderSuccess = true; } else { return_code = content.return_code.Value; return_msg = content.return_msg.Value; isUnifiedOrderSuccess = false; } if (!isUnifiedOrderSuccess) { return(RedirectToAction("Failed", new { msg = "(代码:101)服务器下单失败,请重试" })); } var model = new JSPayModel { appId = appId, nonceStr = nonceStr, timestamp = timestamp, userAgent = userAgent, userVersion = userVersion, attach = "", body = body, detail = detail, openid = openid, product_id = out_trade_no, goods_tag = goods_tag, price = total_fee, total_fee = total_fee, prepay_id = prepay_id, trade_type = trade_type, sign = sign, return_code = return_code, return_msg = return_msg, err_code = err_code, err_code_des = err_code_des, success_redict_url = success_redict_url }; return(View(model)); } catch (Exception ex) { LogWriter.Default.WriteError(ex.Message); return(RedirectToAction("Failed", new { msg = "(代码:200)" + ex.Message })); } }