Beispiel #1
0
        /// <summary>
        /// 创建支付提交的表单
        /// </summary>
        /// <param name="orderInfo"></param>
        /// <returns></returns>
        public static string BuildWapRequestForm(PayOrderInfo orderInfo)
        {
            var data = new AllinpayData(AllinpayDataType.PayRequest);

            //页面跳转同步通知页面路径
            data.SetValue("pickupUrl", string.Format("http://{0}/Allinpay/OnlinePayReturn", AppConfig.Global.Domain));
            //服务器接受支付结果的后台地址
            data.SetValue("receiveUrl", string.Format("http://{0}/Allinpay/OnlinePayNotify", AppConfig.Global.Domain));
            //商户号
            data.SetValue("merchantId", AppConfig.Global.AllinpayMerchantId);
            //商户订单号
            data.SetValue("orderNo", orderInfo.OrderId.Trim());
            //商户订单金额
            data.SetValue("orderAmount", ((int)(Convert.ToDecimal(orderInfo.PaymentAmt) * 100)).ToString());
            //商户订单提交时间
            data.SetValue("orderDatetime", orderInfo.OrderTime);

            int diffMinutes = (int)((DateTime.ParseExact(orderInfo.OrderTime, "yyyyMMddHHmmss", new CultureInfo("zh-CN", true)).AddHours(24) - DateTime.Now).TotalMinutes);

            //订单过期时间
            data.SetValue("orderExpireDatetime", Math.Max(diffMinutes, 1).ToString()); //默认下单后的24小时
            //支付方式
            data.SetValue("payType", "0");                                             //接入手机网关时,该值填固定填0
            //发卡方代码
            data.SetValue("issuerId", "");                                             //payType为0时,issuerId必须为空
            //签名字符串
            data.SetValue("signMsg", AllinpayCore.RequestSign(data));
            //建立请求
            string formHtml = data.ToForm(AppConfig.Global.AllinpayWapServerUrl, "post");

            return(formHtml);
        }
Beispiel #2
0
        /// <summary>
        /// 支付时获取SO详细信息,从CenterDB获取
        /// </summary>
        /// <param name="SOSysNo">订单号</param>
        /// <returns>订单详细信息</returns>
        public static PayOrderInfo PayGetCenterDBOrderInfoBySOSysNo(int SOSysNo)
        {
            PayOrderInfo orderInfo = null;
            DataCommand  command   = DataCommandManager.GetDataCommand("Order_PayGetCenterDBOrderInfoBySOSysNo");

            command.SetParameterValue("@SOID", SOSysNo);

            DataSet result = command.ExecuteDataSet();

            if (result != null && result.Tables.Count > 0)
            {
                DataTable masterTable = result.Tables[0];

                if (masterTable.Rows != null && masterTable.Rows.Count > 0)
                {
                    orderInfo = DataMapper.GetEntity <PayOrderInfo>(masterTable.Rows[0]);
                }
                if (result.Tables != null && result.Tables.Count > 1)
                {
                    DataTable itemTable = result.Tables[1];
                    if (itemTable.Rows != null && itemTable.Rows.Count > 0 && orderInfo != null)
                    {
                        orderInfo.SOItemList = DataMapper.GetEntityList <PaySOItemInfo, List <PaySOItemInfo> >(itemTable.Rows);
                    }
                }
            }

            return(orderInfo);
        }
Beispiel #3
0
        /// <summary>
        /// 执行条码支付
        /// </summary>
        /// <param name="orderInfo">支付订单</param>
        /// <param name="resultInfo">支付结果记录</param>
        /// <returns></returns>
        public override ExecuteResult <PayNotifyInfo> ExecuteBarcodePay(PayOrderInfo orderInfo, PayResultInfo resultInfo)
        {
            var result = new ExecuteResult <PayNotifyInfo>();

            try
            {
                int       paymentAmt = (int)(Convert.ToDecimal(orderInfo.PaymentAmt) * 100);//微信支付金额的单位为“分”,所以要乘以100
                WxPayData runData    = MicroPay.Run(orderInfo.OrderId, paymentAmt, orderInfo.Barcode);
                if (runData.GetValue("return_code").ToString() != "SUCCESS")
                {
                    resultInfo.ExecuteResult = (int)ResultStatus.Failure;
                    resultInfo.ResultDesc    = "微信条码支付请求失败:" + runData.GetValue("return_msg").ToString();
                    PayResultDAL.Update(resultInfo);

                    result.Status  = ResultStatus.Failure;
                    result.Message = resultInfo.ResultDesc;
                    return(result);
                }
                else if (runData.GetValue("result_code").ToString() != "SUCCESS")
                {
                    resultInfo.ExecuteResult = (int)ResultStatus.Failure;
                    resultInfo.ResultDesc    = "微信条码支付失败:" + runData.GetValue("err_code_des").ToString();
                    PayResultDAL.Update(resultInfo);

                    result.Status  = ResultStatus.Failure;
                    result.Message = resultInfo.ResultDesc;
                    return(result);
                }

                //支付成功,更新支付结果记录
                resultInfo.OrderId       = orderInfo.OrderId;
                resultInfo.TradeNo       = runData.GetValue("transaction_id").ToString();
                resultInfo.PaymentAmt    = Convert.ToDecimal(runData.GetValue("total_fee")) / 100; //微信支付金额的单位为“分”,所以要除以100
                resultInfo.ExecuteResult = (int)ResultStatus.Success;

                result.Data   = this.GetPayNotifyInfo(resultInfo);
                result.Status = ResultStatus.Success;
            }
            catch (WxPayException wex)
            {
                resultInfo.ExecuteResult = (int)ResultStatus.Error;
                resultInfo.ResultDesc    = wex.Message;
                PayResultDAL.Update(resultInfo);

                result.Status  = ResultStatus.Error;
                result.Message = resultInfo.ResultDesc;
            }
            catch (Exception ex)
            {
                resultInfo.ExecuteResult = (int)ResultStatus.Error;
                resultInfo.ResultDesc    = ex.ToString();
                PayResultDAL.Update(resultInfo);

                result.Status  = ResultStatus.Error;
                result.Message = ex.Message;
            }
            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// 支付时获取SO详细信息,优先从QueryDB读取,若QueryDB不存在,则从CenterDB读取
        /// </summary>
        /// <param name="SOSysNo">订单号</param>
        /// <returns>订单详细信息</returns>
        public static PayOrderInfo PayGetOrderInfoBySOSysNo(int SOSysNo)
        {
            //优先从QueryDB读取,若QueryDB不存在,则从CenterDB读取
            PayOrderInfo result = PayGetQueryDBOrderInfoBySOSysNo(SOSysNo);

            if (result == null)
            {
                result = PayGetCenterDBOrderInfoBySOSysNo(SOSysNo);
            }
            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// 执行条码支付
        /// </summary>
        /// <param name="orderInfo">支付订单</param>
        /// <param name="resultInfo">支付结果记录</param>
        /// <returns></returns>
        public override ExecuteResult <PayNotifyInfo> ExecuteBarcodePay(PayOrderInfo orderInfo, PayResultInfo resultInfo)
        {
            var result = new ExecuteResult <PayNotifyInfo>();

            resultInfo.OrderId       = orderInfo.OrderId;
            resultInfo.TradeNo       = DateTime.Now.ToString("yyyyMMddHHmmssfff") + RandomHelper.CreateRandomCode(3);
            resultInfo.PaymentAmt    = Convert.ToDecimal(orderInfo.PaymentAmt);
            resultInfo.ExecuteResult = (int)ResultStatus.Success;

            result.Data   = this.GetPayNotifyInfo(resultInfo);
            result.Status = ResultStatus.Success;
            return(result);
        }
Beispiel #6
0
        public ActionResult WeChatBarcodePay(string orderId, string paymentAmt, string barcode)
        {
            PayOrderInfo info = new PayOrderInfo()
            {
                OrderId    = orderId,
                PaymentAmt = paymentAmt,
                NotifyUrl  = string.Format("http://{0}/Test/PayNotify?type=0", AppConfig.Global.Domain),
                Barcode    = barcode,
            };
            var setting = JsonHelper.GetDefaultSettings();

            setting.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            string data = JsonHelper.Serialize(info, setting);
            string sign = SignManager.CreateSign(_appId, data).Data;

            ViewBag.AppId = _appId;
            ViewBag.Sign  = sign;
            ViewBag.Data  = data;
            ViewBag.Title = "微信支付(测试)";
            ViewBag.Url   = "/wechatpay/barcodepay";
            return(View("Pay"));
        }
Beispiel #7
0
        public ActionResult SwiftPassWeChatPay(string orderId, string paymentAmt)
        {
            PayOrderInfo info = new PayOrderInfo()
            {
                OrderId    = orderId,
                PaymentAmt = paymentAmt,
                OrderTime  = DateTime.Now.ToString("yyyyMMddHHmmss"),
                NotifyUrl  = string.Format("http://{0}/Test/PayNotify?type=0", AppConfig.Global.Domain),
                ReturnUrl  = string.Format("http://{0}/Test/PayReturn?type=0", AppConfig.Global.Domain),
            };
            var setting = JsonHelper.GetDefaultSettings();

            setting.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            string data = JsonHelper.Serialize(info, setting);
            string sign = SignManager.CreateSign(_appId, data).Data;

            ViewBag.AppId = _appId;
            ViewBag.Sign  = sign;
            ViewBag.Data  = data;
            ViewBag.Title = "微信支付(测试)";
            ViewBag.Url   = "/swiftpasswechatpay/onlinepay";
            return(View("Pay"));
        }
Beispiel #8
0
        /// <summary>
        /// 创建移动端支付提交的表单
        /// </summary>
        /// <param name="orderInfo"></param>
        /// <returns></returns>
        public static string BuildWapRequestForm(PayOrderInfo orderInfo)
        {
            var paramsDict = new SortedDictionary <string, string>();

            //接口名称 不可空
            paramsDict.Add("service", "alipay.wap.create.direct.pay.by.user");
            //合作者身份ID 签约的支付宝账号对应的支付宝唯一用户号 不可空
            paramsDict.Add("partner", AlipayConfig.partner);
            //卖家支付宝用户号 seller_id是以2088开头的纯16位数字 不可空
            paramsDict.Add("seller_id", AlipayConfig.seller_id);
            //参数编码字符集 商户网站使用的编码格式,如utf-8、gbk、gb2312等 不可空
            paramsDict.Add("_input_charset", AlipayConfig.input_charset.ToLower());
            //支付类型 只支持取值为1(商品购买) 不可空
            paramsDict.Add("payment_type", AlipayConfig.payment_type);
            //服务器异步通知页面路径 支付宝服务器主动通知商户网站里指定的页面http路径 可空
            paramsDict.Add("notify_url", string.Format("http://{0}/Alipay/OnlinePayNotify", AppConfig.Global.Domain));
            //页面跳转同步通知页面路径 支付宝处理完请求后,当前页面自动跳转到商户网站里指定页面的http路径 可空
            paramsDict.Add("return_url", string.Format("http://{0}/Alipay/OnlinePayReturn", AppConfig.Global.Domain));
            //商户网站唯一订单号 不可空
            paramsDict.Add("out_trade_no", orderInfo.OrderId);
            //商品的标题/交易标题/订单标题/订单关键字等,最长为128个汉字 不可空
            paramsDict.Add("subject", string.Format("{0}订单:{1}", AppConfig.Global.WebSiteName, orderInfo.OrderId));
            //交易金额 该笔订单的资金总额,单位为RMB-Yuan。取值范围为[0.01,100000000.00],精确到小数点后两位 不可空
            paramsDict.Add("total_fee", Convert.ToDecimal(orderInfo.PaymentAmt).ToString("#########0.00"));
            //商品描述 对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body 可空
            paramsDict.Add("body", orderInfo.OrderId);

            int diffMinutes = (int)((DateTime.ParseExact(orderInfo.OrderTime, "yyyyMMddHHmmss", new CultureInfo("zh-CN", true)).AddHours(24) - DateTime.Now).TotalMinutes);

            //超时时间 设置未付款交易的超时时间,一旦超时,该笔交易就会自动被关闭。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭) 可空
            paramsDict.Add("it_b_pay", Math.Max(diffMinutes, 1) + "m");

            //建立请求
            string formHtml = AlipaySubmit.BuildRequest(paramsDict, "get", "确认");

            return(formHtml);
        }
Beispiel #9
0
 /// <summary>
 /// 执行条码支付
 /// </summary>
 /// <param name="orderInfo">支付订单</param>
 /// <param name="resultInfo">支付结果记录</param>
 /// <returns></returns>
 public virtual ExecuteResult <PayNotifyInfo> ExecuteBarcodePay(PayOrderInfo orderInfo, PayResultInfo resultInfo)
 {
     return(new ExecuteResult <PayNotifyInfo>());
 }
Beispiel #10
0
        /// <summary>
        /// 在线支付
        /// </summary>
        /// <param name="appId">业务系统ID</param>
        /// <param name="sign">签名</param>
        /// <param name="data">数据报文</param>
        /// <returns></returns>
        public ActionResult OnlinePay(string appId, string sign, string data)
        {
            try
            {
                var onlinePay = Builder.BuildOnlinePay();

                //校验支付模式
                if (onlinePay.CheckModeIsSimulate())
                {
                    return(RedirectToActionPermanent("OnlinePay", "Simulate", new { payType = (int)this._payType, appId = appId, sign = sign, data = data }));
                }

                //保存请求报文
                var requestInfo = onlinePay.SaveRequest(appId, data, this._payType);

                //校验签名
                var checkResult = onlinePay.CheckSign(appId, sign, data, requestInfo);
                if (checkResult.Status != ResultStatus.Success)
                {
                    ViewBag.ErrorMsg = checkResult.Message;
                    return(View());
                }

                //校验支付环境
                if (RequestHelper.IsWapRequest())
                {
                    checkResult = onlinePay.CheckBrowserType(AppEnum.BrowserType.Others, requestInfo);
                    if (checkResult.Status != ResultStatus.Success)
                    {
                        ViewBag.ErrorMsg = checkResult.Message;
                        return(View());
                    }
                }

                //解析支付请求
                checkResult = onlinePay.ResolveRequest(data, requestInfo);
                if (checkResult.Status != ResultStatus.Success)
                {
                    ViewBag.ErrorMsg = checkResult.Message;
                    return(View());
                }

                //校验支付参数
                var checkResult2 = onlinePay.CheckParamaters(data, this._payType, requestInfo);
                if (checkResult2.Status != ResultStatus.Success)
                {
                    ViewBag.ErrorMsg = checkResult2.Message;
                    return(View());
                }
                PayOrderInfo orderInfo = checkResult2.Data;

                //组装支付请求表单数据
                if (!RequestHelper.IsWapRequest())
                {
                    ViewBag.Form = AllinpayManager.BuildRequestForm(orderInfo);//PC端访问
                }
                else
                {
                    ViewBag.Form = AllinpayManager.BuildWapRequestForm(orderInfo);//移动端访问
                }
                //支付请求执行成功
                onlinePay.ExecuteSuccess(requestInfo);
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMsg = "系统执行时发生异常:" + ex.Message;
                string log = string.Format(@"支付发生异常!{0}异常描述:{1}{0}异常堆栈:{2}{0}请求参数:appId={3} sign={4} data={5}",
                                           Environment.NewLine, ex.Message, ex.StackTrace, appId, sign, data);
                LogWriter.WriteLog(log, AppConfig.Global.AllinpayLogFolder, ExceptionHelper.ExceptionLevel.Exception);
            }
            return(View());
        }
        /// <summary>
        /// PC在线支付
        /// </summary>
        /// <param name="appId">业务系统ID</param>
        /// <param name="sign">签名</param>
        /// <param name="data">数据报文</param>
        public ActionResult OnlinePay(string appId, string sign, string data)
        {
            try
            {
                var onlinePay = Builder.BuildOnlinePay();

                //校验支付模式
                if (onlinePay.CheckModeIsSimulate())
                {
                    return(RedirectToActionPermanent("OnlinePay", "Simulate", new { payType = (int)this._payType, appId = appId, sign = sign, data = data }));
                }

                //移动端访问,跳转至微信Wap支付
                if (RequestHelper.IsWapRequest())
                {
                    return(RedirectToAction("OnlineWapPay", new { appId = appId, sign = sign, data = data }));
                }

                //保存请求报文
                var requestInfo = onlinePay.SaveRequest(appId, data, this._payType);

                //校验签名
                var checkResult = onlinePay.CheckSign(appId, sign, data, requestInfo);
                if (checkResult.Status != ResultStatus.Success)
                {
                    ViewBag.ErrorMsg = checkResult.Message;
                    return(View());
                }

                //解析支付请求
                checkResult = onlinePay.ResolveRequest(data, requestInfo);
                if (checkResult.Status != ResultStatus.Success)
                {
                    ViewBag.ErrorMsg = checkResult.Message;
                    return(View());
                }

                //校验支付参数
                var checkResult2 = onlinePay.CheckParamaters(data, this._payType, requestInfo);
                if (checkResult2.Status != ResultStatus.Success)
                {
                    ViewBag.ErrorMsg = checkResult2.Message;
                    return(View());
                }
                PayOrderInfo orderInfo = checkResult2.Data;

                #region 生成支付二维码链接code_url
                try
                {
                    //获取订单支付金额
                    int paymentAmt = (int)(requestInfo.PaymentAmt * 100);//微信支付金额的单位为“分”,所以要乘以100
                    //异步通知url
                    string notifyUrl = string.Format("http://{0}/SwiftPassWeChatPay/OnlinePayNotify", AppConfig.Global.Domain);

                    DateTime orderEndTime  = DateTime.ParseExact(orderInfo.OrderTime, "yyyyMMddHHmmss", new CultureInfo("zh-CN", true)).AddHours(24);
                    DateTime minExpireTime = DateTime.Now.AddMinutes(6);//为保险,多加1分钟
                    //交易过期时间(最短过期时间间隔必须大于5分钟)
                    string expireTime = (orderEndTime > minExpireTime ? orderEndTime : minExpireTime).ToString("yyyyMMddHHmmss");
                    //向微信发起统一下单请求
                    string codeUrl = SwiftPassPayApi.WeChatNativePay(requestInfo.OrderId, paymentAmt, notifyUrl, expireTime);

                    ViewBag.CodeUrl     = codeUrl;
                    ViewBag.RequestInfo = requestInfo;
                }
                catch (BizException bex)
                {
                    requestInfo.ResultDesc    = bex.Message;
                    requestInfo.ExecuteResult = (int)ResultStatus.Error;
                    PayRequestDAL.Update(requestInfo);

                    ViewBag.ErrorMsg = bex.Message;
                    return(View());
                }
                catch (Exception ex)
                {
                    requestInfo.ExecuteResult = (int)ResultStatus.Error;
                    requestInfo.ResultDesc    = ex.ToString();
                    PayRequestDAL.Update(requestInfo);

                    ViewBag.ErrorMsg = "系统执行时发生异常:" + ex.Message;
                    return(View());
                }
                #endregion

                //支付请求执行成功
                onlinePay.ExecuteSuccess(requestInfo);
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMsg = "系统执行时发生异常:" + ex.Message;
                string log = string.Format(@"PC支付请求发生异常!{0}异常描述:{1}{0}异常堆栈:{2}{0}请求参数:appId={3} sign={4} data={5}",
                                           Environment.NewLine, ex.Message, ex.StackTrace, appId, sign, data);
                LogWriter.WriteLog(log, AppConfig.Global.SwiftPassWeChatPayLogFolder, ExceptionHelper.ExceptionLevel.Exception);
            }
            return(View());
        }
Beispiel #12
0
        /// <summary>
        /// 条码支付
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="sign"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public string BarcodePay(string appId, string sign, string data)
        {
            try
            {
                #region 处理支付请求
                var barcodePay = Builder.BuildBarcodePay();

                //保存请求报文
                var requestInfo = barcodePay.SaveRequest(appId, data, this._payType);

                //校验签名
                var checkResult = barcodePay.CheckSign(appId, sign, data, requestInfo);
                if (checkResult.Status != ResultStatus.Success)
                {
                    return(JsonHelper.Serialize(new { status = 0, msg = checkResult.Message }));
                }

                //解析支付请求
                checkResult = barcodePay.ResolveRequest(data, requestInfo);
                if (checkResult.Status != ResultStatus.Success)
                {
                    return(JsonHelper.Serialize(new { status = 0, msg = checkResult.Message }));
                }

                //校验支付参数
                var checkResult2 = barcodePay.CheckParamaters(data, this._payType, requestInfo);
                if (checkResult2.Status != ResultStatus.Success)
                {
                    return(JsonHelper.Serialize(new { status = 0, msg = checkResult2.Message }));
                }

                //支付请求执行成功
                barcodePay.ExecuteSuccess(requestInfo);
                #endregion

                #region 执行支付申请
                var resultInterface = Builder.BuildWeChatPayResult();

                //校验模式
                if (barcodePay.CheckModeIsSimulate())
                {
                    resultInterface = Builder.BuildSimulatePayResult();
                }

                //保存支付结果请求
                var resultInfo = resultInterface.SaveRequest(data, this._payType);
                resultInfo.OrderId = requestInfo.OrderId;

                //执行条码支付
                PayOrderInfo orderInfo     = checkResult2.Data;
                var          executeResult = resultInterface.ExecuteBarcodePay(orderInfo, resultInfo);
                if (executeResult.Status != ResultStatus.Success)
                {
                    return(JsonHelper.Serialize(new { status = 0, msg = executeResult.Message }));
                }

                //更新支付结果记录并关联支付请求记录
                resultInterface.RelateRequestInfo(resultInfo);

                //异步线程通知业务系统支付结果
                var thread = new System.Threading.Thread(() => resultInterface.NotifyBack(resultInfo, requestInfo));
                thread.Start();

                var notifyInfo = executeResult.Data;
                return(JsonHelper.Serialize(new { status = 1, data = notifyInfo }));

                #endregion
            }
            catch (Exception ex)
            {
                string log = string.Format(@"条码支付发生异常!{0}异常描述:{1}{0}异常堆栈:{2}{0}请求参数:appId={3} sign={4} data={5}",
                                           Environment.NewLine, ex.Message, ex.StackTrace, appId, sign, data);
                LogWriter.WriteLog(log, AppConfig.Global.WeChatPayLogFolder, ExceptionHelper.ExceptionLevel.Exception);
                return(JsonHelper.Serialize(new { status = -1, msg = "系统执行时发生异常:" + ex.Message }));
            }
        }