예제 #1
0
        public void BuildDailyRandomStrTest()
        {
            var result = TenPayV3Util.BuildDailyRandomStr(10);

            Assert.IsNotNull(result);
            Console.WriteLine(result);
        }
예제 #2
0
        public WxChatPayResult Pay(decimal amount, string openID, string title, out string orderID)
        {
            var m     = mpSetting.Value;
            var price = Convert.ToInt32(amount * 100);

            orderID = TenPayV3Util.BuildDailyRandomStr(32);
            //var amount = 1;
            //var openid = "ohYxb0-EBw7Vr2_H5WhqJRW6k62A";
            var param = new TenPayV3UnifiedorderRequestData(
                m.ParentAppID, m.ParentMchID, m.AppID, m.MchID, title, orderID, price,
                "127.0.0.1", m.NotifyUrl, TenPayV3Type.JSAPI, "", openID, m.Key, TenPayV3Util.GetNoncestr());
            var q = TenPayV3.Unifiedorder(param);

            if (q.IsResultCodeSuccess() && q.IsReturnCodeSuccess())
            {
                var re = new WxChatPayResult(m.Key, q.appid, q.prepay_id);
                return(re);
            }
            return(null);
        }
예제 #3
0
        public HttpResponseMessage CreatePayment(string orderCode)
        {
            LogHelper.Payment(orderCode, "准备请求微信支付接口...");
            var orderEntity = _repositoryFactory.IOrders.Single(x => x.OrderCode == orderCode);

            if (orderEntity == null)
            {
                LogHelper.Payment(orderCode, "订单不存在");
                return(ApiResponse(ResultStatus.ParamError, "订单不存在"));
            }
            if (orderEntity.OrderStatus != (int)EnumHepler.OrderStatus.Created || orderEntity.PayStatus != (int)EnumHepler.OrderPayStatus.Unpay)
            {
                LogHelper.Payment(orderCode, "订单非待支付状态");
                return(ApiResponse(ResultStatus.ParamError, "订单非待支付状态"));
            }
            TenPayV3Info TenPayV3Info = new TenPayV3Info(appId, appSecrect, wxmchId, wxmchKey, notifyUrl, notifyUrl);

            TenPayV3Info.TenPayV3Notify = notifyUrl;
            //创建支付应答对象
            //RequestHandler packageReqHandler = new RequestHandler(null);
            var    sp_billno = DateTime.Today.ToString("yyMMdd") + TenPayV3Util.BuildDailyRandomStr(4);//最多32位
            var    nonceStr  = TenPayV3Util.GetNoncestr();
            string clientIp  = CommonTools.GetIpAddress();
            //创建请求统一订单接口参数
            var xmlDataInfo = new TenPayV3UnifiedorderRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, "订单支付", orderCode, (int)(orderEntity.ActualPrice * 100), clientIp, TenPayV3Info.TenPayV3Notify, Senparc.Weixin.TenPay.TenPayV3Type.JSAPI, orderEntity.OpenId, TenPayV3Info.Key, nonceStr);

            //返回给微信的请求
            //RequestHandler res = new RequestHandler(null);
            try
            {
                //调用统一订单接口
                var result = TenPayV3.Unifiedorder(xmlDataInfo);
                LogHelper.Payment(orderCode, $"微信支付接口返回:{JsonConvert.SerializeObject(result)}");

                if (result != null && result.return_code == "SUCCESS")
                {
                    //return_code是通信标识,非交易标识,交易是否成功需要查看result_code来判断
                    if (result.result_code == "SUCCESS")
                    {
                        LogHelper.Payment(orderCode, $"请求微信支付成功,预支付单号:{result.prepay_id}");
                        string rtimeStamp = CommonTools.GetTimeStamp();
                        //string nativeReqSign = res.CreateMd5Sign("key", TenPayV3Info.Key);
                        string paySign = SecurityHelper.MD5($"appId={TenPayV3Info.AppId}&nonceStr={nonceStr}&package=prepay_id={result.prepay_id}&signType=MD5&timeStamp={rtimeStamp}&key={TenPayV3Info.Key}").ToUpper();
                        //返回前端唤起微信支付收银台的参数
                        var detail = new { timeStamp = rtimeStamp, nonceStr = nonceStr, package = $"prepay_id={result.prepay_id}", signType = "MD5", paySign = paySign };
                        return(ApiResponse(ResultStatus.Success, "下单成功", detail));
                    }
                    else
                    {
                        LogHelper.Payment(orderCode, $"请求微信支付失败,err_code:{result.err_code},err_code_des:{result.err_code_des}");
                    }
                }
                return(ApiResponse(ResultStatus.Failed, result.return_msg));
            }
            catch (Exception ex)
            {
                //res.SetParameter("return_code", "FAIL");
                //res.SetParameter("return_msg", "统一下单失败");
                LogHelper.Payment(orderCode, $"创建微信支付异常:{ex.Message}");
                LogHelper.Exception(ex);
                return(ApiResponse(ResultStatus.Failed, "创建微信支付请求失败,请联系管理员!"));
            }
        }