Ejemplo n.º 1
0
        public void UnifiedOrder(string request)
        {
            try
            {
                var requestModel = JSONHelper.Deserialize <UnifiedOrderEntity>(request);

                //通过wid获取公众号的信息
                var wxModel   = new BLL.wx_userweixin().GetModel(requestModel.wid);
                var wxPayInfo = new BLL.wx_payment_wxpay().GetModelByWid(requestModel.wid);

                var packageReqHandler = new RequestHandler(null);
                //初始化
                packageReqHandler.Init();

                var timeStamp = TenPayV3Util.GetTimestamp();
                var nonceStr  = TenPayV3Util.GetNoncestr();

                //设置package订单参数
                packageReqHandler.SetParameter("appid", wxModel.AppId);                        //公众账号ID
                packageReqHandler.SetParameter("mch_id", wxPayInfo.mch_id);                    //商户号
                packageReqHandler.SetParameter("nonce_str", nonceStr);                         //随机字符串
                packageReqHandler.SetParameter("body", requestModel.body);                     //商品描述
                packageReqHandler.SetParameter("attach", requestModel.PayModuleID.ToString()); //向微信传递系统支付模块ID
                packageReqHandler.SetParameter("out_trade_no", requestModel.out_trade_no);     //商家订单号

                //debug模式下,穿的单位为分
                packageReqHandler.SetParameter("total_fee", PayHelper.IsDebug ? requestModel.total_fee.ToString() : (requestModel.total_fee * 100).ToString());

                //packageReqHandler.SetParameter("spbill_create_ip", wxPayInfo);   //用户的公网ip,不是商户服务器IP

                packageReqHandler.SetParameter("notify_url", HttpContext.Current.Request.Url.ToString().ToLower().Replace("wechatservice.asmx", "PayNotify.aspx")); //接收财付通通知的URL
                packageReqHandler.SetParameter("trade_type", TenPayV3Type.JSAPI.ToString());                                                                        //交易类型
                packageReqHandler.SetParameter("openid", requestModel.openid);                                                                                      //用户的openId

                var sign = packageReqHandler.CreateMd5Sign("key", wxPayInfo.paykey);
                packageReqHandler.SetParameter("sign", sign);                       //签名

                var data = packageReqHandler.ParseXML();

                //同意下单,获取到预付订单号
                var unifiedOrderResult    = TenPayV3Helper.Unifiedorder(data);
                var rtnUnifiedOrderResult = new UnifiedOrderResponse(unifiedOrderResult);

                //下单成功,保存下单对象
                if (rtnUnifiedOrderResult.IsSuccess)
                {
                    var paymentInfo = new PaymentInfo
                    {
                        PaymentId   = Guid.NewGuid(),
                        Wid         = requestModel.wid,
                        CreateTime  = DateTime.Now,
                        Description = "无",
                        ShopName    = requestModel.body,
                        ModuleName  = ((PayModuleEnum)requestModel.PayModuleID).ToString(),
                        OrderCode   = requestModel.out_trade_no,
                        OrderId     = requestModel.OrderId,
                        Pid         = requestModel.openid,
                        PayAmount   = requestModel.total_fee,
                        WXOrderCode = rtnUnifiedOrderResult.prepay_id,
                        ModifyTime  = DateTime.Now,
                        Status      = 0
                    };

                    paymentInfo.Add();
                }

                //var jsApiParameters = rtnUnifiedOrderResult.GetJsApiParameters("4A5E7B87F3324A6DA22E55FDC12150B6");
                var jsApiParameters = rtnUnifiedOrderResult.GetJsApiParameters(wxPayInfo.paykey);

                HttpContext.Current.Response.Write(AjaxResult.Success(jsApiParameters));
            }
            catch (Exception exception)
            {
                HttpContext.Current.Response.Write(AjaxResult.Error(exception.Message));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 最新接口调用
        /// 2014-11-24
        /// </summary>
        /// <param name="ttFee">支付金额(单位元)</param>
        /// <param name="busiBody">订单内容</param>
        /// <param name="out_trade_no">订单号</param>
        /// <param name="code"></param>
        protected void WxPayDataV3(decimal ttFee, string busiBody, string out_trade_no, string code)
        {
            BLL.wx_payment_wxpay   wxPayBll    = new BLL.wx_payment_wxpay();
            Model.wx_payment_wxpay paymentInfo = wxPayBll.GetModelByWid(wid);

            BLL.wx_userweixin   uwBll    = new BLL.wx_userweixin();
            Model.wx_userweixin uwEntity = uwBll.GetModel(wid);

            // logBll.AddLog(wid,"【微支付】微信预定", "paypage.aspx WxPayDataV3", "211 wid:" + wid, 1);

            //先设置基本信息
            string MchId = paymentInfo.mch_id;      // "1218574001";//

            string partnerKey = paymentInfo.paykey; // 商户支付密钥Key。登录微信商户后台,进入栏目【账户设置】【密码安全】【API 安全】【API 密钥】

            string notify_url = "http://" + HttpContext.Current.Request.Url.Authority + "/api/payment/wxpay/notify_url.aspx";

            WXLogs.AddLog(wid, "【微支付】微信预定", "paypage.aspx WxPayDataV3", "uwEntity.AppId: " + uwEntity.AppId + "| uwEntity.AppSecret;" + uwEntity.AppSecret + "|code:" + code, 1);


            string timeStamp = "";
            string nonceStr  = "";
            string paySign   = "";

            string sp_billno = out_trade_no;
            //当前时间 yyyyMMdd
            string date = DateTime.Now.ToString("yyyyMMdd");

            if (null == sp_billno)
            {
                //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一
                sp_billno = DateTime.Now.ToString("HHmmss") + TenPayV3Util.BuildRandomStr(28);
            }


            //创建支付应答对象
            OneGulp.WeChat.MP.TenPayLibV3.RequestHandler packageReqHandler = new OneGulp.WeChat.MP.TenPayLibV3.RequestHandler(null);
            //初始化
            packageReqHandler.Init();
            //packageReqHandler.SetKey(""/*TenPayV3Info.Key*/);

            timeStamp = TenPayV3Util.GetTimestamp();
            nonceStr  = TenPayV3Util.GetNoncestr();

            //设置package订单参数
            packageReqHandler.SetParameter("appid", uwEntity.AppId);                      //公众账号ID
            packageReqHandler.SetParameter("mch_id", MchId);                              //商户号
            packageReqHandler.SetParameter("nonce_str", nonceStr);                        //随机字符串
            packageReqHandler.SetParameter("body", busiBody);                             //商品描述
            packageReqHandler.SetParameter("attach", wid + "|" + busiBody);
            packageReqHandler.SetParameter("out_trade_no", sp_billno);                    //商家订单号
            packageReqHandler.SetParameter("total_fee", ((int)(ttFee * 100)).ToString()); //商品金额,以分为单位(money * 100).ToString()
            packageReqHandler.SetParameter("spbill_create_ip", Request.UserHostAddress);  //用户的公网ip,不是商户服务器IP
            packageReqHandler.SetParameter("notify_url", notify_url);                     //接收财付通通知的URL
            packageReqHandler.SetParameter("trade_type", TenPayV3Type.JSAPI.ToString());  //交易类型
            packageReqHandler.SetParameter("openid", openid);                             //用户的openId

            string sign = packageReqHandler.CreateMd5Sign("key", partnerKey);

            packageReqHandler.SetParameter("sign", sign);                           //签名

            string data = packageReqHandler.ParseXML();

            var result = TenPayV3Helper.Unifiedorder(data);

            var res = XDocument.Parse(result);

            prepayId = res.Element("xml").Element("prepay_id").Value;

            //设置支付参数
            OneGulp.WeChat.MP.TenPayLibV3.RequestHandler paySignReqHandler = new OneGulp.WeChat.MP.TenPayLibV3.RequestHandler(null);
            paySignReqHandler.SetParameter("appId", uwEntity.AppId);
            paySignReqHandler.SetParameter("timeStamp", timeStamp);
            paySignReqHandler.SetParameter("nonceStr", nonceStr);
            paySignReqHandler.SetParameter("package", string.Format("prepay_id={0}", prepayId));
            paySignReqHandler.SetParameter("signType", "MD5");
            paySign = paySignReqHandler.CreateMd5Sign("key", partnerKey);

            packageValue  = "";
            packageValue += " \"appId\": \"" + uwEntity.AppId + "\", ";
            packageValue += " \"timeStamp\": \"" + timeStamp + "\", ";
            packageValue += " \"nonceStr\": \"" + nonceStr + "\", ";
            packageValue += " \"package\": \"" + string.Format("prepay_id={0}", prepayId) + "\", ";
            packageValue += " \"signType\": \"MD5\", ";
            packageValue += " \"paySign\": \"" + paySign + "\"";
        }