示例#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));
            }
        }