コード例 #1
0
        public ActionResult WxQrcodePay(int?CompanyId, string storeId, string paySN, decimal totalPrice)
        {
            var op   = new OpResult();
            var data = new qrcode.wx.WxPayData();

            data.SetValue("body", "订单编号" + paySN);                                                //商品描述
            data.SetValue("attach", CompanyId + "_" + paySN);                                     //附加数据
            data.SetValue("total_fee", Convert.ToInt32(totalPrice * 100));                        //总金额
            data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));                 //交易起始时间
            data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); //交易结束时间
            data.SetValue("goods_tag", "");                                                       //商品标记
            data.SetValue("trade_type", "NATIVE");                                                //交易类型
            data.SetValue("product_id", paySN);                                                   //商品ID
            try
            {
                var setting = paySetBLL.GetPaymentSettingBystoreId(2, storeId, CompanyId);
                data.SetValue("out_trade_no", qrcode.wx.WxPayApi.GenerateOutTradeNo(setting.AppId)); //随机字符串
                data.SetValue("notify_url", qrcode.wx.WxPayConfig.NOTIFY_URL);
                data.SetValue("spbill_create_ip", qrcode.wx.WxPayConfig.IP);                         //终端ip
                data.SetValue("appid", setting.PartnerId);                                           //公众账号ID
                data.SetValue("mch_id", setting.AppId);                                              //商户号
                data.SetValue("nonce_str", qrcode.wx.WxPayApi.GenerateNonceStr());                   //随机字符串,放置顺序
                //签名
                data.SetValue("sign", data.MakeSign(setting.CheckKey));
                var result = qrcode.wx.WxPayApi.UnifiedOrder(data);//调用统一下单接口

                if (Convert.ToString(result.GetValue("result_code")) == "SUCCESS" && Convert.ToString(result.GetValue("return_code")) == "SUCCESS")
                {
                    string createUrl = result.GetValue("code_url").ToString();//获得统一下单接口返回的二维码链接
                    createUrl = Url.Action("GenerateQrcode", new { codeUrl = createUrl });
                    //op.Message = Request.Url.Scheme + "://" + Request.Url.Authority + Url.Action("CreateQRCode", new { url = createUrl, paySN = paySN, price = totalPrice });
                    op.Message   = Request.Url.Scheme + "://" + Request.Url.Authority + createUrl;
                    op.Successed = true;
                }
                else if (Convert.ToString(result.GetValue("return_code")) == "SUCCESS")
                {
                    op.Message = result.GetValue("err_code_des").ToString();
                }
                else
                {
                    op.Message = Convert.ToString(result.GetValue("return_msg"));
                }
            }
            catch (Exception ex)
            {
                qrcode.Log.Error(this.GetType().Name, "微信扫码," + ex.Message);
                op.Message = ex.Message;
                new Sys.LogEngine().WriteError("微信扫码支付失败!", ex);
            }

            return(new JsonNetResult(op));
        }
コード例 #2
0
        public ActionResult WxQrcodePay(string paySN, decimal totalPrice)
        {
            var data = new qrcode.wx.WxPayData();

            data.SetValue("body", "东本-订单编号" + paySN);                                             //商品描述
            data.SetValue("attach", paySN);                                                       //附加数据
            data.SetValue("out_trade_no", qrcode.wx.WxPayApi.GenerateOutTradeNo());               //随机字符串
            data.SetValue("total_fee", Convert.ToInt32(totalPrice * 100));                        //总金额
            data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));                 //交易起始时间
            data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); //交易结束时间
            data.SetValue("goods_tag", "");                                                       //商品标记
            data.SetValue("trade_type", "NATIVE");                                                //交易类型
            data.SetValue("product_id", paySN);                                                   //商品ID
            var op = new OpResult();

            try
            {
                var result = qrcode.wx.WxPayApi.UnifiedOrder(data);//调用统一下单接口

                if (Convert.ToString(result.GetValue("result_code")) == "SUCCESS" && Convert.ToString(result.GetValue("return_code")) == "SUCCESS")
                {
                    string createUrl = result.GetValue("code_url").ToString();//获得统一下单接口返回的二维码链接
                    createUrl = Url.Action("GenerateQrcode", new { codeUrl = createUrl });
                    //op.Message = Request.Url.Scheme + "://" + Request.Url.Authority + Url.Action("CreateQRCode", new { url = createUrl, paySN = paySN, price = totalPrice });
                    op.Message   = Request.Url.Scheme + "://" + Request.Url.Authority + createUrl;
                    op.Successed = true;
                }
                else if (Convert.ToString(result.GetValue("return_code")) == "SUCCESS")
                {
                    op.Message = result.GetValue("err_code_des").ToString();
                }
                else
                {
                    op.Message = Convert.ToString(result.GetValue("return_msg"));
                }
            }
            catch (Exception ex)
            {
                qrcode.Log.Error(this.GetType().Name, ex.Message);
                op.Message = ex.Message;
                new Sys.LogEngine().WriteError(ex);
            }
            return(new JsonNetResult(op));
        }
コード例 #3
0
        public ActionResult WxMicroPay(int?CompanyId, string storeId, string autoCode, string paySN, decimal totalPrice)
        {
            var data = new qrcode.wx.WxPayData();

            data.SetValue("auth_code", autoCode);                          //授权码
            data.SetValue("body", "订单编号 " + paySN);                        //商品描述
            data.SetValue("total_fee", Convert.ToInt32(totalPrice * 100)); //总金额
            var op = new OpResult();

            qrcode.wx.WxPayData result = null;
            try
            {
                if (autoCode.IsNullOrEmpty() || paySN.IsNullOrEmpty() || totalPrice == 0)
                {
                    throw new ArgumentException("参数传入为空!");
                }
                var setting = paySetBLL.GetPaymentSettingBystoreId(2, storeId, CompanyId);
                data.SetValue("out_trade_no", qrcode.wx.WxPayApi.GenerateOutTradeNo(setting.AppId)); //产生随机的商户订单号
                data.SetValue("spbill_create_ip", qrcode.wx.WxPayConfig.IP);                         //终端ip
                data.SetValue("appid", setting.PartnerId);                                           //公众账号ID
                data.SetValue("mch_id", setting.AppId);                                              //商户号
                data.SetValue("nonce_str", qrcode.wx.WxPayApi.GenerateNonceStr());                   //随机字符串,放置顺序
                //签名
                data.SetValue("sign", data.MakeSign(setting.CheckKey));

                result = qrcode.wx.WxPayApi.Micropay(data, 10); //提交被扫支付,接收返回结果

                //如果提交被扫支付接口调用失败,则抛异常
                if (!result.IsSet("return_code") || result.GetValue("return_code").ToString() == "FAIL")
                {
                    throw new Exception("支付接口调用失败!returnCode:FAIL," + result.GetValue("return_msg"));
                }

                //签名验证
                result.CheckSign(setting.CheckKey);
                //刷卡支付直接成功
                if (result.GetValue("return_code").ToString() == "SUCCESS" &&
                    result.GetValue("result_code").ToString() == "SUCCESS")
                {
                    op.Message   = result.GetValue("transaction_id").ToString();
                    op.Successed = true;
                }
                else
                {
                    /******************************************************************
                    * 剩下的都是接口调用成功,业务失败的情况
                    * ****************************************************************/
                    //1)业务结果明确失败
                    if (result.GetValue("err_code").ToString() != "USERPAYING" &&
                        result.GetValue("err_code").ToString() != "SYSTEMERROR")
                    {
                        throw new Exception("业务处理失败!" + result.GetValue("err_code_des"));
                    }

                    //2)不能确定是否失败,需查单
                    //用商户订单号去查单
                    string out_trade_no = data.GetValue("out_trade_no").ToString();

                    //确认支付是否成功,每隔一段时间查询一次订单,共查询10次

                    for (int i = 0; i < retryCount; i++)
                    {
                        int succResult  = 0;//查询结果
                        var queryResult = qrcode.wx.WxPayApi.Query(data, setting.CheckKey, out succResult);
                        //如果需要继续查询,则等待2s后继续
                        if (succResult == 2)
                        {
                            Thread.Sleep(3000);
                            continue;
                        }
                        //查询成功,返回订单查询接口返回的数据
                        else if (succResult == 1)
                        {
                            op.Message   = queryResult.GetValue("transaction_id").ToString();
                            op.Successed = true;
                            break;
                        }
                        //订单交易失败,直接返回刷卡支付接口返回的结果,失败原因会在err_code中描述
                        else
                        {
                            throw new Exception("订单交易失败!" + result.GetValue("err_code_des"));
                        }
                    }
                    //确认失败,则撤销订单
                    if (!op.Successed && !qrcode.wx.WxPayApi.Cancel(data, setting.CheckKey, setting.AppId))
                    {
                        throw new Exception("撤销订单失败!");
                    }
                }
            }
            catch (Exception ex)
            {
                qrcode.Log.Error(this.GetType().Name, "微信当面付," + ex.Message);
                op.Message = ex.Message;
                new Sys.LogEngine().WriteError("微信当面付失败,返回结果:" + result.ToXml(), ex);
            }
            return(new JsonNetResult(op));
        }