Ejemplo n.º 1
0
        public Response GetOpenId(string token, [FromBody] WeChatInfo param)
        {
            Response response = new Response();

            string[] strAry   = token.Split(',');
            string   tmpToken = strAry[0];
            string   appName  = strAry[1];

            if (string.IsNullOrEmpty(tmpToken) || !tmpToken.Equals(_token))
            {
                response.code    = "404";
                response.message = "Invild token";
            }
            else
            {
                var data = WeChatPayHelper.GetOpenId(param, appName);
                if (data == null)
                {
                    response.code    = "500";
                    response.message = "Get data failed";
                }
                else
                {
                    response.code    = "200";
                    response.content = data;
                }
            }

            return(response);
        }
Ejemplo n.º 2
0
        private static IWeChatPayConfig GetConfig()
        {
            var config = WeChatPayHelper.GetPayConfigFunc();

            if (config == null)
            {
                throw new ArgumentNullException("请配置支付配置信息!", "PayConfig");
            }

            if (string.IsNullOrWhiteSpace(config.PayAppId))
            {
                throw new ArgumentNullException("PayAppId必须配置!", nameof(config.PayAppId));
            }

            if (string.IsNullOrWhiteSpace(config.MchId))
            {
                throw new ArgumentNullException("MchId(商户Id)必须配置!", nameof(config.MchId));
            }

            if (string.IsNullOrWhiteSpace(config.PayNotifyUrl))
            {
                throw new ArgumentNullException("PayNotifyUrl(支付回调地址)必须配置!", nameof(config.PayNotifyUrl));
            }

            if (string.IsNullOrWhiteSpace(config.TenPayKey))
            {
                throw new ArgumentNullException("TenPayKey(支付密钥)必须配置!", nameof(config.TenPayKey));
            }

            return(config);
        }
Ejemplo n.º 3
0
        public HttpResponseMessage WechatBarCode(string token, string data)
        {
            HttpResponseMessage response = new HttpResponseMessage();

            try
            {
                Dictionary <string, string> keyDic =
                    Aden.Util.Common.JsonHelper.JSDeserialize <Dictionary <string, string> >(data);

                if (string.IsNullOrWhiteSpace(token) || string.IsNullOrWhiteSpace(_token))
                {
                    throw new Exception("Invalid Token");
                }

                byte[] barcode = WeChatPayHelper.CreateWxCode(keyDic);
                //appName,keyDic["guid"], keyDic["costCenterCode"],
                //eyDic["cardno"], int.Parse(keyDic["barCodeSize"]));

                response.Content = new ByteArrayContent(barcode);

                response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpg");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(response);
        }
Ejemplo n.º 4
0
        public Response GetWxPayConfig(string token, [FromBody] SingleField param)
        {
            Response response = new Response();

            string[] strAry   = token.Split(',');
            string   tmpToken = strAry[0];
            string   appName  = strAry[1];

            if (string.IsNullOrEmpty(tmpToken) || !tmpToken.Equals(_token))
            {
                response.code    = "404";
                response.message = "Invild token";
            }
            else
            {
                string value = WeChatPayHelper.GetWxPayConfig(param, appName);
                if (string.IsNullOrEmpty(value))
                {
                    response.code    = "500";
                    response.message = "Update failed";
                }
                else
                {
                    response.code    = "200";
                    response.content = value;
                }
            }
            return(response);
        }
Ejemplo n.º 5
0
        public Response GetAccessToken(string token)
        {
            Response response = new Response();

            string[] strAry   = token.Split(',');
            string   tmpToken = strAry[0];
            string   appName  = strAry[1];
            string   isNew    = strAry.Length > 2 ? strAry[2] : string.Empty;

            if (string.IsNullOrEmpty(tmpToken) || !tmpToken.Equals(_token))
            {
                response.code    = "404";
                response.message = "Invild token";
            }
            else
            {
                var data = WeChatPayHelper.GetAccessToken(appName, isNew);
                if (data == null)
                {
                    response.code    = "500";
                    response.message = "Get data failed";
                }
                else
                {
                    response.code    = "200";
                    response.content = data;
                }
            }
            return(response);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 确认退款
        /// </summary>
        /// <param name="refundParam"></param>
        /// <param name="certPath"></param>
        /// <param name="certPwd"></param>
        /// <param name="paykey"></param>
        /// <param name="return_msg"></param>
        /// <returns></returns>
        public StateCode WeChatConfirmRefund(RefundPay refundParam, long order_id, string certPath, string certPwd, string paykey, out string return_msg)
        {
            using (ISession s = SessionFactory.Instance.CreateSession())
            {
                return_msg = string.Empty;
                try
                {
                    //回调
                    RefundMessage rmessage = WeChatPayHelper.CallRefund(refundParam, certPath, certPwd, paykey);
                    if (rmessage.return_code.Equals("FAIL", StringComparison.OrdinalIgnoreCase))
                    {
                        return_msg = rmessage.return_msg;
                        return(StateCode.State_500);
                    }
                    else if (rmessage.result_code.Equals("FAIL", StringComparison.OrdinalIgnoreCase))
                    {
                        return_msg = rmessage.err_code + " " + rmessage.err_code_des;
                        return(StateCode.State_500);
                    }

                    //开启事务
                    s.StartTransaction();

                    //更新订单状态
                    s.ExcuteUpdate("update tb_odr_order set refund_status = @0,status = @1 where id = @2 ", OrderRefundStatus.Pass, OrderStatus.Close, order_id);

                    //更新退款单状态
                    s.ExcuteUpdate("update tb_odr_refund set status = @0 where order_serial_no = @1 ", OrderRefundStatus.Pass, refundParam.out_trade_no);

                    //提交事务
                    s.Commit();

                    return(StateCode.State_200);
                }
                catch (Exception ex)
                {
                    return_msg = ex.ToString();
                    s.RollBack();
                    return(StateCode.State_500);
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     小程序支付
        ///     小程序统一下单接口
        ///     https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1
        /// </summary>
        /// <returns></returns>
        public MiniProgramPayOutput MiniProgramPay(MiniProgramPayInput input)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            if (input.TotalFee <= 0)
            {
                throw new ArgumentException("金额不能小于0!", nameof(input.TotalFee));
            }

            if (string.IsNullOrWhiteSpace(input.OpenId))
            {
                throw new ArgumentNullException("OpenId必须填写!", nameof(input.OpenId));
            }

            if (string.IsNullOrWhiteSpace(input.Body))
            {
                throw new ArgumentNullException("商品描述必须填写!", nameof(input.Body));
            }

            if (string.IsNullOrWhiteSpace(input.SpbillCreateIp))
            {
                throw new ArgumentNullException("终端IP必须填写!", nameof(input.SpbillCreateIp));
            }

            var url    = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            var config = GetConfig();

            var model = new UnifiedorderRequest
            {
                AppId          = config.PayAppId,
                MchId          = config.MchId,
                Attach         = input.Attach,
                Body           = input.Body,
                Detail         = input.Detail,
                DeviceInfo     = input.DeviceInfo,
                FeeType        = input.FeeType,
                GoodsTag       = input.GoodsTag,
                LimitPay       = input.LimitPay,
                OpenId         = input.OpenId,
                OutTradeNo     = input.OutTradeNo ?? weChatPayHelper.GenerateOutTradeNo(),
                SpbillCreateIp = input.SpbillCreateIp,
                TimeExpire     = input.TimeExpire,
                TimeStart      = input.TimeStart,
                TotalFee       = ((int)(input.TotalFee * 100)).ToString(),
                NonceStr       = weChatPayHelper.GetNoncestr(),
                NotifyUrl      = config.PayNotifyUrl
            };
            var dictionary = weChatPayHelper.GetDictionaryByType(model);

            model.Sign = weChatPayHelper.CreateMd5Sign(dictionary, config.TenPayKey); //生成Sign

            var result = weChatPayHelper.PostXML <UnifiedorderResult>(url, model);

            if (result.IsSuccess())
            {
                var data = new
                {
                    appId     = result.AppId,
                    nonceStr  = result.NonceStr,
                    package   = "prepay_id=" + result.PrepayId,
                    signType  = "MD5",
                    timeStamp = weChatPayHelper.GetTimestamp()
                };
                return(new MiniProgramPayOutput
                {
                    AppId = data.appId,
                    Package = data.package,
                    NonceStr = data.nonceStr,
                    PaySign =
                        weChatPayHelper.CreateMd5Sign(weChatPayHelper.GetDictionaryByType(data), config.TenPayKey),
                    SignType = data.signType,
                    TimeStamp = data.timeStamp
                });
            }

            WeChatPayHelper.LoggerAction("Error", "支付错误:" + result.GetFriendlyMessage());
            throw new WeChatPayException("支付错误,请联系客服或重新支付!");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //LogHelper.WriteLog(typeof(NativeNotifyPage), "可以运行1-1");

            String xmlData = getPostStr();//获取请求数据

            if (!string.IsNullOrWhiteSpace(xmlData))
            {
                #region Step1.先创建回执XML start
                var dic = new Dictionary <string, string>
                {
                    { "return_code", "SUCCESS" },
                    { "return_msg", "OK" }
                };
                var sb = new StringBuilder();
                sb.Append("<xml>");

                foreach (var d in dic)
                {
                    sb.Append("<" + d.Key + ">" + d.Value + "</" + d.Key + ">");
                }
                sb.Append("</xml>");
                #endregion

                /***将xml转换成实体类***/
                WxPayNotifyData wxData = WeChatPayHelper.GetWxPayNotifyData(xmlData);

                #region step2.检查XML是否合法
                if (WeChatPayHelper.CheckSign(xmlData, wxData.attch))
                {
                    // 创建回写订单的实体类
                    WxOrder order = new WxOrder();
                    // 取得PaymentCode长度
                    int          len = 0;
                    WeChatConfig wxc = WxMastDataHelper.GetWeChatConfig(wxData.attch);
                    if (wxc != null && !string.IsNullOrWhiteSpace(wxc.paymentCode))
                    {
                        len = wxc.paymentCode.Length;
                    }

                    order.appName        = wxData.attch;
                    order.out_trade_no   = wxData.out_trade_no.Substring(len, wxData.out_trade_no.Length - len);
                    order.transaction_id = wxData.transaction_id;
                    order.cash_fee       = wxData.cash_fee;
                    order.fee_type       = wxData.fee_type;
                    order.bank_type      = wxData.bank_type;
                    order.time_end       = wxData.time_end;
                    order.result_code    = wxData.result_code;
                    order.return_code    = wxData.return_code;

                    // 执行回写
                    if (WxOrderHelper.UpdatePayOrder(order) > 0)
                    {
                        // 调用数据导入Exe
                        this.DoExe();
                        // 回写成功时返回SUCCESS
                        return_result = sb.ToString();
                    }
                }
                #endregion
            }
            Response.Write(return_result);
            Response.End();
        }