Exemplo n.º 1
0
        /// <summary>
        /// 微信设置
        /// </summary>
        /// <param name="response"></param>
        public virtual void SetWeixiResult(WeixinDataHelper response)
        {
            var returnCode = !response.IsSet("return_code") || response.GetValue("return_code").ToString() == "FAIL" ? "FAIL" : response.GetValue("return_code").ToString();
            var resultCode = !response.IsSet("result_code") || response.GetValue("result_code").ToString() == "FAIL" ? "FAIL":response.GetValue("result_code").ToString();


            var isSuccess = returnCode.ToUpper().Contains("SUCCESS") && resultCode.ToUpper().Contains("SUCCESS");

            this._isError = !isSuccess;
            this._errCode = response.IsSet("err_code") ? response.GetValue("err_code").ToString() : "";

            if (!isSuccess)
            {
                var errMsg = response.IsSet("return_msg") ? response.GetValue("return_msg").ToString() : "";
                if (string.IsNullOrEmpty(errMsg))
                {
                    errMsg = response.IsSet("err_code_des") ? response.GetValue("err_code_des").ToString() : "";
                }
                this._errMsg = errMsg;
            }
            this._responsebody   = response.ToJson();
            this._apiType        = "weixinpay";
            this._requestUrl     = response.IsSet("requestUrl") ? response.GetValue("requestUrl").ToString() : "";
            this._elapsedTime    = response.IsSet("elapsedTime") ? response.GetValue("elapsedTime").ToString() : "";
            this._httpMethod     = response.IsSet("httpMethod") ? response.GetValue("httpMethod").ToString() : "";
            this._requestContent = response.IsSet("requestContent") ? response.GetValue("requestContent").ToString() : "";
        }
Exemplo n.º 2
0
 /// <summary>
 /// 微信结果设置
 /// </summary>
 /// <param name="response"></param>
 public override void SetWeixiResult(WeixinDataHelper response)
 {
     base.SetWeixiResult(response);
     if (!IsError)
     {
         this.OutTradeNo    = response.IsSet("out_trade_no") ? response.GetValue("out_trade_no").ToString() : "";
         this.OnlineTradeNo = response.IsSet("transaction_id") ? response.GetValue("transaction_id").ToString():"";
         //在微信中订单生成时间,格式为yyyyMMddHHmmss
         this.TradeTime = response.IsSet("time_end") ? DateTime.ParseExact(response.GetValue("time_end").ToString(), "yyyyMMddHHmmss", null):DateTime.Now;
     }
 }
Exemplo n.º 3
0
 public override void SetWeixiResult(WeixinDataHelper response)
 {
     base.SetWeixiResult(response);
     if (!IsError)
     {
         this.OnlineTradeNo = response.IsSet("transaction_id") ? response.GetValue("transaction_id").ToString() : "";
         this.OutRefundNo   = response.IsSet("out_refund_no") ? response.GetValue("out_refund_no").ToString() : "";
         this.OutTradeNo    = response.IsSet("out_trade_no") ? response.GetValue("out_trade_no").ToString() : "";
         this.TotalAmount   = response.IsSet("total_fee") ? response.GetValue("total_fee").ToString() : "";
         this.RefundAmount  = response.IsSet("refund_fee") ? response.GetValue("refund_fee").ToString() : "";
     }
 }
Exemplo n.º 4
0
 public override void SetWeixiResult(WeixinDataHelper response)
 {
     base.SetWeixiResult(response);
     if (!this.IsError)
     {
         this.OnlineTradeNo = response.IsSet("transaction_id") ?response.GetValue("transaction_id").ToString():"";
         this.OutTradeNo    = response.IsSet("out_trade_no") ?response.GetValue("out_trade_no").ToString():"";
         this.TradeStatus   = response.IsSet("trade_state") ?response.GetValue("trade_state").ToString():"";
         this.TotalAmount   = response.IsSet("total_fee") ?response.GetValue("total_fee").ToString():"";
         if (response.IsSet("time_end"))
         {
             this.TradeTime = DateTime.ParseExact(response.GetValue("time_end").ToString(), "yyyyMMddHHmmss", null);
         }
     }
 }
Exemplo n.º 5
0
 public override void SetWeixiResult(WeixinDataHelper response)
 {
     base.SetWeixiResult(response);
     if (!IsError)
     {
         this.OnlineTradeNo = response.IsSet("transaction_id") ? response.GetValue("transaction_id").ToString() : "";
         this.OutTradeNo    = response.IsSet("out_trade_no") ? response.GetValue("out_trade_no").ToString() : "";
         this.TotalAmout    = response.IsSet("total_fee") ? response.GetValue("total_fee").ToString() : "";
         //this.RefundReason =;//微信不支持
         //请完成一次退款
         //这里不处理部分退款,https://pay.weixin.qq.com/wiki/doc/api/micropay_sl.php?chapter=9_5
         this.RefundAmount = response.IsSet("refund_fee_1") ? response.GetValue("refund_fee_1").ToString() : "";
         this.RefundStatus = response.IsSet("refund_status_1") ? response.GetValue("refund_status_1").ToString() : "";
         this.OutRefundNo  = response.IsSet("out_refund_no_1") ? response.GetValue("out_refund_no_1").ToString() : "";
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// 扫码支付
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public OrderPayResult OrderPay(OrderPayDTO dto)
        {
            WeixinDataHelper data = new WeixinDataHelper(this._weixinKey);

            data.SetValue("auth_code", dto.AuthCode);                                                      //授权码
            data.SetValue("body", dto.Body);                                                               //商品描述
            data.SetValue("total_fee", int.Parse(((Convert.ToDouble(dto.TotalAmount)) * 100).ToString())); //总金额
            data.SetValue("out_trade_no", dto.OutTradeNo);                                                 //产生随机的商户订单号
            data.SetValue("spbill_create_ip", dto.TerminalId);                                             //终端ip
            WeixinDataHelper response = weixinPayApi.Micropay(data, 10);                                   //提交被扫支付,接收返回结果
            var result = new OrderPayResult();

            //如果提交被扫支付接口调用失败,则抛异常
            if (!response.IsSet("return_code") || response.GetValue("return_code").ToString() == "FAIL")
            {
                result.SetWeixiResult(response);
                return(result);
                //string returnMsg = response.IsSet("return_msg") ? response.GetValue("return_msg").ToString() : "";
                //throw new F2FPayException("Micropay API interface call failure, return_msg : " + returnMsg);
            }

            //签名验证
            response.CheckSign();
            //刷卡支付直接成功
            if (response.GetValue("return_code").ToString() == "SUCCESS" &&
                response.GetValue("result_code").ToString() == "SUCCESS")
            {
                result.SetWeixiResult(response);
                return(result);
            }

            /******************************************************************
            * 剩下的都是接口调用成功,业务失败的情况
            * ****************************************************************/
            //1)业务结果明确失败
            if (response.GetValue("err_code").ToString() != "USERPAYING" &&
                response.GetValue("err_code").ToString() != "SYSTEMERROR")
            {
                result.SetWeixiResult(response);
                return(result);
            }

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

            //确认支付是否成功,每隔一段时间查询一次订单,共查询10次
            int queryTimes = 10;//查询次数计数器

            while (queryTimes-- > 0)
            {
                int succResult = 0;//查询结果
                WeixinDataHelper queryResult = Query(out_trade_no, out succResult);
                //如果需要继续查询,则等待2s后继续
                if (succResult == 2)
                {
                    Thread.Sleep(2000);
                    continue;
                }
                //查询成功,返回订单查询接口返回的数据
                else if (succResult == 1)
                {
                    result.SetWeixiResult(queryResult);
                    return(result);
                }
                //订单交易失败,直接返回刷卡支付接口返回的结果,失败原因会在err_code中描述
                else
                {
                    result.SetWeixiResult(response);
                    return(result);
                }
            }

            //确认失败,则撤销订单
            if (!Cancel(out_trade_no))
            {
                throw new WeixinPayException("撤销订单失败!");
            }
            var errmsg = response.GetValue("err_code_des");

            response.SetValue("err_code_des", errmsg + "(已自动完成了撤销订单)");
            result.SetWeixiResult(response);
            return(result);
        }