예제 #1
0
        //
        // GET: /V3Jsapi/


        /// <summary>
        ///
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public ActionResult Pay()
        {
            string code        = "";                               //网页授权获得的code
            string orderNo     = "";                               //文档中的out_trade_no
            string description = "";                               //商品描述
            string totalFee    = "1";                              //订单金额(单位:分)
            string createIp    = "127.0.0.1";
            string notifyUrl   = "";                               //通知url
            string openId      = WeiXinHelper.GetUserOpenId(code); //通过网页授权code获取用户openid(或者之前有存储用户的openid 也可以直接拿来用)

            //prepayid 只有第一次支付时生成,如果需要再次支付,必须拿之前生成的prepayid。
            //也就是说一个orderNo 只能对应一个prepayid
            string prepayid = string.Empty;

            #region 之前生成过 prepayid,此处可略过

            //创建Model
            UnifiedWxPayModel model = UnifiedWxPayModel.CreateUnifiedModel(WeiXinConst.AppId, WeiXinConst.PartnerId, WeiXinConst.PartnerKey);

            //预支付
            UnifiedPrePayMessage result = WeiXinHelper.UnifiedPrePay(model.CreatePrePayPackage(description, orderNo, totalFee, createIp, notifyUrl, openId));

            if (result == null ||
                !result.ReturnSuccess ||
                !result.ResultSuccess ||
                string.IsNullOrEmpty(result.Prepay_Id))
            {
                throw new Exception("获取PrepayId 失败");
            }

            //预支付订单
            prepayid = result.Prepay_Id;

            #endregion

            //JSAPI 支付参数的Model
            PayModel payModel = new PayModel()
            {
                AppId     = model.AppId,
                Package   = string.Format("prepay_id={0}", prepayid),
                Timestamp = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString(),
                Noncestr  = CommonUtil.CreateNoncestr(),
            };

            Dictionary <string, string> nativeObj = new Dictionary <string, string>();
            nativeObj.Add("appId", payModel.AppId);
            nativeObj.Add("package", payModel.Package);
            nativeObj.Add("timeStamp", payModel.Timestamp);
            nativeObj.Add("nonceStr", payModel.Noncestr);
            nativeObj.Add("signType", payModel.SignType);
            payModel.PaySign = model.GetCftPackage(nativeObj); //生成JSAPI 支付签名


            return(View(payModel));
        }
예제 #2
0
        private UnifiedPrePayMessage UnifiedPrePay(UnifiedWxPayModel model, TradeType tradeType)
        {
            //JSAPI支付, 追加客户openid
            if (tradeType == TradeType.JSAPI && gatewayInfo.DataList.Count > 4)
            {
                this.openid = gatewayInfo.DataList[4];
            }
            string payPack = model.CreatePrePayPackage(body, out_trade_no, total_fee,
                                                       notify_url, tradeType, openid);
            //预支付
            UnifiedPrePayMessage result = Utils.WeiXinHelper.UnifiedPrePay(payPack);

            //Core.Globals.WriteText(new System.Text.StringBuilder(gatewayInfo.NotifyUrl));
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (result == null)
            {
                sb.AppendFormat("获取PrepayId 失败 result is NULL");
            }
            else if (!result.ReturnSuccess)
            {
                sb.AppendFormat(
                    "获取PrepayId 失败 result.ReturnSuccess:{0}|result.Return_Code:{1}|result.Return_Msg:{2}",
                    result.ReturnSuccess, result.Return_Code, result.Return_Msg);
            }
            else if (!result.ResultSuccess && result.Err_Code_Des == "该订单已支付")
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Write("该订单已支付!");
                HttpContext.Current.Response.End();
                return(result);
            }
            else if (!result.ResultSuccess)
            {
                sb.AppendFormat(
                    "获取PrepayId 失败 result.ResultSuccess:{0}|result.Result_Code:{1}|result.Err_Code:{2}|result.Err_Code_Des:{3}",
                    result.ResultSuccess, result.Result_Code, result.Err_Code, result.Err_Code_Des);
            }
            else if (string.IsNullOrEmpty(result.Prepay_Id))
            {
                sb.AppendFormat("获取PrepayId 失败 result.Prepay_Id is NULL");
            }
            if (sb.Length > 0)
            {
                Core.Globals.WriteText(sb);
                return(result);
            }
            return(result);
        }
예제 #3
0
        public void ProcessRequest(HttpContext context)
        {
            string prepayid = string.Empty;

            if (context.Request["openid"] != null && context.Request["price"] != null && context.Request["title"] != null)
            {
                string orderNo     = DateTime.Now.ToString("yyyyMMddhhmmssfff");                                   //文档中的out_trade_no
                string description = context.Request["title"].ToString();                                          //商品描述
                string totalFee    = (Convert.ToDecimal(context.Request["price"].ToString()) * 100).ToString("0"); //订单金额(单位:分)
                string createIp    = "127.0.0.1";
                string notifyUrl   = "https://api.uifang.com/payway.ashx";                                         //通知url
                // string openId = WeiXinHelper.GetUserOpenId(code);//通过网页授权code获取用户openid(或者之前有存储用户的openid 也可以直接拿来用)
                createIp = "127.0.0.1";
                //   code = "031937cca83477222807bef025b247L";
                string openId = "";

                //openId = context.Request["openid"].ToString();
                openId = context.Request["openid"].ToString();
                // string openId = WeiXinHelper.GetUserOpenId(code);
                //prepayid 只有第一次支付时生成,如果需要再次支付,必须拿之前生成的prepayid。
                //也就是说一个orderNo 只能对应一个prepayid


                //创建Model
                UnifiedWxPayModel model = UnifiedWxPayModel.CreateUnifiedModel("wx65cd51f5b9c0cac6", "1491450372", "asd57491714asd57491714asd5749171");

                //预支付
                UnifiedPrePayMessage result = WeiXinHelper.UnifiedPrePay(model.CreatePrePayPackage(description, orderNo, totalFee, createIp, notifyUrl, openId));

                if (result == null ||
                    !result.ReturnSuccess ||
                    !result.ResultSuccess ||
                    string.IsNullOrEmpty(result.Prepay_Id))
                {
                    prepayid = result.Return_Msg.ToString();
                    //  viewdata.state = false;
                    //    viewdata.error = openId + "," + code + "," + "," + result.Return_Msg.ToString();
                }
                else
                {
                    //预支付订单
                    prepayid = result.Prepay_Id;
                    PayModel payModel = new PayModel()
                    {
                        AppId     = model.AppId,
                        Package   = string.Format("prepay_id={0}", prepayid),
                        Timestamp = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString(),
                        Noncestr  = CommonUtil.CreateNoncestr(),
                    };
                    Dictionary <string, string> nativeObj = new Dictionary <string, string>();
                    nativeObj.Add("appId", payModel.AppId);
                    nativeObj.Add("package", payModel.Package);
                    nativeObj.Add("timeStamp", payModel.Timestamp);
                    nativeObj.Add("nonceStr", payModel.Noncestr);
                    nativeObj.Add("signType", payModel.SignType);
                    payModel.PaySign = model.GetCftPackage(nativeObj); //生成JSAPI 支付签名
                    payModel.OrderId = orderNo;
                    prepayid         = JsonConvert.SerializeObject(payModel);
                }
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(prepayid);
        }
예제 #4
0
        /// <summary>
        /// 创建用户入会申请订单
        /// </summary>
        /// <param name="context"></param>
        private void LeaguerApplyCreateOrder(HttpContext context)
        {
            string goodsID       = context.Request["goodsid"];
            string goodsName     = context.Request["goodsname"];
            string goodsPrice    = context.Request["goodsprice"];
            string phone         = context.Request["phone"];
            string realName      = context.Request["realName"];
            string address       = context.Request["address"];
            string birthday      = context.Request["birthday"];
            string email         = context.Request["email"];
            string idCard        = context.Request["idcard"];
            string sex           = context.Request["sex"];
            string childName     = context.Request["childname"];
            string childBirthday = context.Request["childbirthday"];
            string childSex      = context.Request["childsex"];
            string openId        = context.Request["openId"];
            string avatarUrl     = context.Request["avatarUrl"];
            string wxNickName    = context.Request["wxNickName"];

            //验证参数完整性
            if (string.IsNullOrEmpty(goodsID) || goodsID == "undefined" || string.IsNullOrEmpty(goodsName) || goodsName == "undefined" || string.IsNullOrEmpty(goodsPrice) || goodsPrice == "undefined" || string.IsNullOrEmpty(phone) ||
                string.IsNullOrEmpty(realName) || string.IsNullOrEmpty(birthday) || string.IsNullOrEmpty(address) || string.IsNullOrEmpty(idCard) ||
                string.IsNullOrEmpty(sex))
            {
                context.Response.Write("{\"errMsg\":\"参数错误\"}");
                return;
            }

            IMallClient _client = new DefaultMallClient(Url, secretkey, appId);
            DTOLeaguerApplyCreateOrderVer1_2 rCreateOrder = new DTOLeaguerApplyCreateOrderVer1_2
            {
                BussinessID   = bussinessID,
                GoodsID       = goodsID,
                GoodsPrice    = decimal.Parse(goodsPrice),
                GoodsName     = goodsName,
                Address       = address,
                Birthday      = birthday,
                Email         = email,
                IDCard        = idCard,
                ChildName     = childName,
                ChildSex      = Int32.Parse(childSex),
                ChildBirthday = childBirthday,
                TPOrderNo     = Guid.NewGuid().ToString().Substring(0, 8),
                UserPass      = "******",
                Sex           = Int32.Parse(sex),
                RealName      = realName,
                Phone         = phone,
                OrderMoney    = decimal.Parse(goodsPrice),
                Summary       = "openid:" + openId + " wxNickName:" + wxNickName //+ " avatarUrl:" + avatarUrl,
            };

            //创建签名
            rCreateOrder.SetSign(rCreateOrder, appId, secretkey);
            //创建入会订单
            var rCreateOrderresponse = _client.Execute(rCreateOrder);

            if (rCreateOrderresponse.IsSuccess == false)
            {
                context.Response.Write("{\"errMsg\":\"" + rCreateOrderresponse.ResponseStatus.Message + "\"}");
                return;
            }

            #region 创建微信支付
            string orderNo     = rCreateOrder.TPOrderNo; //文档中的out_trade_no
            string description = rCreateOrder.GoodsName; //商品描述
            // Convert.ToDecimal(context.Request["price"].ToString()) * 100).ToString("0");//订单金额(单位:分)
            string totalFee = "1";                       //(rCreateOrderresponse.TotleMoney*100 ).ToString("0") ;

            string createIp  = "127.0.0.1";
            string notifyUrl = "https://api.uifang.com/payway.ashx"; //通知url
                                                                     // string openId = WeiXinHelper.GetUserOpenId(code);//通过网页授权code获取用户openid(或者之前有存储用户的openid 也可以直接拿来用)
            createIp = "127.0.0.1";
            //   code = "031937cca83477222807bef025b247L";
            // string openId = WeiXinHelper.GetUserOpenId(code);
            //prepayid 只有第一次支付时生成,如果需要再次支付,必须拿之前生成的prepayid。
            //也就是说一个orderNo 只能对应一个prepayid


            //创建Model
            UnifiedWxPayModel model = UnifiedWxPayModel.CreateUnifiedModel("wx65cd51f5b9c0cac6", partnerId, "asd57491714asd57491714asd5749171");

            //预支付
            UnifiedPrePayMessage result = WeiXinHelper.UnifiedPrePay(model.CreatePrePayPackage(description, orderNo, totalFee, createIp, notifyUrl, openId));
            string prepayid             = string.Empty;
            string resultStr            = string.Empty;
            if (result == null || !result.ReturnSuccess || !result.ResultSuccess || string.IsNullOrEmpty(result.Prepay_Id))
            {
                resultStr = result.Return_Msg.ToString();
                //  viewdata.state = false;
                //  viewdata.error = openId + "," + code + "," + "," + result.Return_Msg.ToString();
            }
            else
            {
                //预支付订单
                prepayid = result.Prepay_Id;
                PayModel payModel = new PayModel()
                {
                    AppId     = model.AppId,
                    Package   = string.Format("prepay_id={0}", prepayid),
                    Timestamp = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString(),
                    Noncestr  = CommonUtil.CreateNoncestr(),
                };
                Dictionary <string, string> nativeObj = new Dictionary <string, string>();
                nativeObj.Add("appId", payModel.AppId);
                nativeObj.Add("package", payModel.Package);
                nativeObj.Add("timeStamp", payModel.Timestamp);
                nativeObj.Add("nonceStr", payModel.Noncestr);
                nativeObj.Add("signType", payModel.SignType);
                payModel.PaySign = model.GetCftPackage(nativeObj); //生成JSAPI 支付签名
                payModel.OrderId = orderNo;
                //之前返回的订单号
                payModel.MallOrderId = rCreateOrderresponse.OrderID;
                resultStr            = JsonConvert.SerializeObject(payModel);
            }
            #endregion
            context.Response.ContentType = "text/plain";
            context.Response.Write(resultStr);

            //context.Response.Write(JsonMapper.ToJson(rCreateOrderresponse));
        }