Пример #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
        public override void SendRequest()
        {
            string action = HttpContext.Current.Request.QueryString["action"];

            if (string.IsNullOrWhiteSpace(action))
            {
                action = "show";
                //HttpContext.Current.Response.Write("NO ACTION");
                //HttpContext.Current.Response.End();
                //return;
            }
            action = action.ToLower();

            //Utils.WxPayHelper wxPayHelper = new Utils.WxPayHelper();
            ////先设置基本信息
            //wxPayHelper.SetAppId(this.appid);
            //wxPayHelper.SetAppKey(this.key2);
            //wxPayHelper.SetPartnerKey(this.key);
            //wxPayHelper.SetSignType("SHA1");
            ////设置请求package信息
            //wxPayHelper.SetParameter("bank_type", bank_type);
            //wxPayHelper.SetParameter("body", body); //商品描述
            //wxPayHelper.SetParameter("partner", partner); //partner 商户号
            //wxPayHelper.SetParameter("out_trade_no", out_trade_no); //商家订单号
            //wxPayHelper.SetParameter("total_fee", total_fee); //商品金额,以分为单位
            //wxPayHelper.SetParameter("fee_type", fee_type); //币种,1人民币
            //wxPayHelper.SetParameter("notify_url", notify_url);
            //wxPayHelper.SetParameter("spbill_create_ip", spbill_create_ip);//用户的公网ip,不是商户服务器IP
            //wxPayHelper.SetParameter("input_charset", input_charset);

            //System.Console.Out.WriteLine("生成app支付package:");
            //System.Console.Out.WriteLine(wxPayHelper.CreateAppPackage("test"));
            //System.Console.Out.WriteLine("生成jsapi支付package:");
            //string jsApiPackage = wxPayHelper.CreateBizPackage();
            //System.Console.Out.WriteLine(jsApiPackage);
            //System.Console.Out.WriteLine("生成原生支付url:");
            //System.Console.Out.WriteLine(wxPayHelper.CreateNativeUrl("abc"));
            //System.Console.Out.WriteLine("生成原生支付package:");
            //System.Console.Out.WriteLine(wxPayHelper.CreateNativePackage("0", "ok"));

            //DONE: 输出JS进行网关交互
            string resultJson;

            switch (action)
            {
            case "show":
                Configuration.GatewayProvider provider =
                    Configuration.PayConfiguration.GetConfig().Providers["wechat_v3"] as
                    Configuration.GatewayProvider;
                if (provider == null)
                {
                    resultJson = "{\"STATUS\":\"ERROR\",\"DATA\":\"GATEWAY PROVIDER NOT FOND\"}";
                    break;
                }

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

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

                //创建Model
                UnifiedWxPayModel    model  = UnifiedWxPayModel.CreateUnifiedModel(this.appid, this.partner, this.key);
                UnifiedPrePayMessage result = UnifiedPrePay(model, TradeType.JSAPI);

                //预支付订单
                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 支付签名
                nativeObj.Add("paySign", payModel.PaySign);

                var    entries = nativeObj.Select(d => string.Format("\"{0}\":\"{1}\"", d.Key, d.Value));
                string data    = "{" + string.Join(",", entries.ToArray()) + "}";

                resultJson = "{\"STATUS\":\"SUCCESS\",\"DATA\":" + data + "}";

                this.RedirectToGateway(string.Format(CultureInfo.InvariantCulture,
                                                     HttpContext.Current.Server.HtmlDecode(provider.Attributes["urlFormat"]),
                                                     this.out_trade_no, this.gatewayInfo.Data, resultJson));
                return;

            case "app":
                Configuration.GatewayProvider providerApp =
                    Configuration.PayConfiguration.GetConfig().Providers["wechat_v3"] as
                    Configuration.GatewayProvider;
                if (providerApp == null)
                {
                    resultJson = "{\"STATUS\":\"ERROR\",\"DATA\":\"GATEWAY PROVIDER NOT FOND\"}";
                    break;
                }

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

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

                //创建Model
                UnifiedWxPayModel    modelApp  = UnifiedWxPayModel.CreateUnifiedModel(this.appid, this.partner, this.key);
                UnifiedPrePayMessage resultApp = UnifiedPrePay(modelApp, TradeType.APP);

                //预支付订单
                prepayidApp = resultApp.Prepay_Id;

                #endregion

                //APP 支付参数的Model
                PayModel payModelApp = new PayModel()
                {
                    AppId = modelApp.AppId
                };
                Dictionary <string, string> nativeObjApp = new Dictionary <string, string>();
                nativeObjApp.Add("appid", modelApp.AppId);
                nativeObjApp.Add("partnerid", this.partner);
                nativeObjApp.Add("prepayid", prepayidApp);
                nativeObjApp.Add("package", "Sign=WXPay");
                nativeObjApp.Add("noncestr", CommonUtil.CreateNoncestr());
                nativeObjApp.Add("timestamp", ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString());
                payModelApp.PaySign = modelApp.GetCftPackage(nativeObjApp);     //生成APP 支付签名
                nativeObjApp.Add("sign", payModelApp.PaySign);

                var    entriesApp = nativeObjApp.Select(d => string.Format("\"{0}\":\"{1}\"", d.Key, d.Value));
                string dataApp    = "{" + string.Join(",", entriesApp.ToArray()) + "}";

                resultJson = "{\"STATUS\":\"SUCCESS\",\"DATA\":" + dataApp + "}";

                this.RedirectToGateway(string.Format(CultureInfo.InvariantCulture,
                                                     HttpContext.Current.Server.HtmlDecode(providerApp.Attributes["urlFormat"]),
                                                     this.out_trade_no, this.gatewayInfo.Data, resultJson));
                return;

            case "bizpackage":
                resultJson = "{\"STATUS\":\"ERROR\",\"DATA\":\"NotImplemented\"}";
                break;

            case "qr":
                Configuration.GatewayProvider providerQR =
                    Configuration.PayConfiguration.GetConfig().Providers["wechat_v3"] as
                    Configuration.GatewayProvider;
                if (providerQR == null)
                {
                    resultJson = "{\"STATUS\":\"ERROR\",\"DATA\":\"GATEWAY PROVIDER NOT FOND\"}";
                    break;
                }

                #region 生成统一支付

                //创建Model
                UnifiedWxPayModel    modelQR  = UnifiedWxPayModel.CreateUnifiedModel(this.appid, this.partner, this.key);
                UnifiedPrePayMessage resultQR = UnifiedPrePay(modelQR, TradeType.NATIVE);

                //返回二维码支付URL
                resultJson = "{\"STATUS\":\"SUCCESS\",\"DATA\":\"" + resultQR.Code_Url + "\",\"Result_Code\":\"" + resultQR.Result_Code + "\"}";
                #endregion
                break;

            default:
                resultJson = "{\"STATUS\":\"ERROR\",\"DATA\":\"NotImplemented\"}";
                break;
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/json";
            HttpContext.Current.Response.Write(resultJson);
        }
Пример #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));
        }