Exemple #1
0
        /// <summary>
        /// 支付宝转账支付
        /// </summary>
        public static bool PayMentForAlipayTransfer(string uid, string pid, string pwd, string pcode, out string info)
        {
            info = "";
            decimal amount = 0;

            using (var DB = new SSMMEntities())
            {
                var user    = DB.User.SingleOrDefault(x => x.Email == uid);
                var product = DB.Product.Find(pid);
                if (product == null)
                {
                    info = "该产品不存在!请重新支付!";
                    return(false);
                }
                amount = product.Price;
                //优惠码
                var couponcode = new CouponCodeDto();
                if (CouponCodeService.Query(pcode, out couponcode))
                {
                    amount = product.Price - couponcode.Amount;
                    amount = amount < 0 ? 0 : amount;
                }
                else
                {
                    if (!string.IsNullOrEmpty(pcode))
                    {
                        info = "优惠码已过期或已超过最大使用次数!请重新支付!";
                        return(false);
                    }
                }
                var tradeno = FormatHelper.GenerateOutTradeNo();
                //生成订单
                DB.Order.Add(new Order()
                {
                    TradeNumber = tradeno,
                    ProductId   = product.Id,
                    ProductName = product.Name,
                    Price       = product.Price,
                    Amount      = amount,
                    CreateTime  = DateTime.Now,
                    Status      = 1,
                    Type        = PaymentType.支付宝转账.ToString(),
                    UserId      = user.Id
                });
                //ss信息
                var ss = DB.SS.SingleOrDefault(x => x.userid == user.Id);
                if (ss == null)
                {
                    DB.SS.Add(new SS()
                    {
                        id = tradeno,
                        t  = FormatHelper.ConvertDateTimeInt(DateTime.Now),   //ss服务端更新时间
                        u  = 0,                                               //上传流量
                        d  = 0,                                               //下载流量
                        transfer_enable = FormatHelper.GB2B(product.Traffic), //long 总套餐流量
                        port            = SSService.GetNextPort(),            //端口
                        password        = pwd,                                //密码
                        @switch         = 1,
                        enable          = 1,
                        isrest          = product.IsRest,                                                               //是否重置流量
                        last_rest_time  = FormatHelper.ConvertDateTimeInt(DateTime.Now.AddDays(30)),                    //重置流量时间
                        expiration_time = FormatHelper.ConvertDateTimeInt(DateTime.Now.AddDays(product.ExpirationDay)), //过期时间
                        create_time     = FormatHelper.ConvertDateTimeInt(DateTime.Now),
                        userid          = user.Id
                    });
                }
                else
                {
                    ss.t = FormatHelper.ConvertDateTimeInt(DateTime.Now);    //ss服务端更新时间
                    ss.u = 0;                                                //上传流量
                    ss.d = 0;                                                //下载流量
                    ss.transfer_enable = FormatHelper.GB2B(product.Traffic); //long 总套餐流量
                    ss.password        = pwd;                                //密码
                    ss.@switch         = 1;
                    ss.enable          = 1;
                    ss.isrest          = product.IsRest;                                                               //是否重置流量
                    ss.last_rest_time  = FormatHelper.ConvertDateTimeInt(DateTime.Now.AddDays(30));                    //重置流量时间
                    ss.expiration_time = FormatHelper.ConvertDateTimeInt(DateTime.Now.AddDays(product.ExpirationDay)); //过期时间
                    ss.create_time     = FormatHelper.ConvertDateTimeInt(DateTime.Now);
                }
                //返佣
                if (user.ParentId.HasValue)
                {
                    var parent = DB.User.Find(user.ParentId.Value);
                    if (parent != null)
                    {
                        var num          = Convert.ToInt32(SettingCache.Cache.Get(SettingFlag.RebateNum)) * 0.01;
                        var agentamounts = amount * (decimal)num;//返佣金额为最终优惠后的实际交易价格的返佣百分比
                        parent.Balance += agentamounts;
                        DB.Record.Add(new Record()
                        {
                            Amount     = agentamounts,
                            CreateTime = DateTime.Now,
                            Type       = RecordType.返佣.ToString(),
                            Info       = $"用户[{user.Id}]{user.Email}购买了{product.Name},产品价格{product.Price.ToString("0.00")}元,最终交易价格{amount.ToString("0.00")}元,返佣百分比{num * 100}%,返佣金额{agentamounts.ToString("0.00")}元",
                            UserId     = parent.Id,
                            Remark     = PaymentType.支付宝转账.ToString()
                        });
                    }
                }
                if (DB.SaveChanges() > 0)
                {
                    info = "支付成功!";
                    return(true);
                }
                else
                {
                    info = "发现错误!支付失败!";
                    return(false);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 校验参数签名,返回最终订单金额
        /// </summary>
        public static bool CheckParameterSign(string uid, string pid, string pwd, string pcode, int timestamp, string sign, PaymentType type, out string info, out decimal amount)
        {
            info   = "";
            amount = 0;
            //校验参数
            var ServerSign = ParameterSign(uid, pid, pwd, pcode, timestamp);

            if (sign != ServerSign)
            {
                info = "签名错误!请重新支付!";
                return(false);
            }
            var time = FormatHelper.GetTime(timestamp);

            if ((DateTime.Now - time).TotalMinutes > 30)
            {
                info = "支付已过期!请重新支付!";
                return(false);
            }
            using (var DB = new SSMMEntities())
            {
                var user = DB.User.SingleOrDefault(x => x.Email == uid);
                if (user == null)
                {
                    info = "该用户不存在!请重新支付!";
                    return(false);
                }
                var product = DB.Product.Find(pid);
                if (product == null)
                {
                    info = "该产品不存在!请重新支付!";
                    return(false);
                }
                amount = product.Price;
                //优惠码
                var couponcode = new CouponCodeDto();
                if (CouponCodeService.Query(pcode, out couponcode))
                {
                    amount = product.Price - couponcode.Amount;
                    amount = amount < 0 ? 0 : amount;
                }
                else
                {
                    if (!string.IsNullOrEmpty(pcode))
                    {
                        info = "优惠码已过期或已超过最大使用次数!请重新支付!";
                        return(false);
                    }
                }
                //支付方式为 支付宝转账 优惠后的价格为0
                if (type == PaymentType.支付宝转账)
                {
                    if (amount == 0)
                    {
                        info = "支付成功!";
                        return(true);
                    }
                }
                //支付方式为 账户余额支付 需要校验余额
                if (type == PaymentType.账户余额)
                {
                    if (user.Balance < amount)
                    {
                        info = "账户余额不足!";
                        return(false);
                    }
                }

                return(true);
            }
        }