Exemplo n.º 1
0
        public ActionResult ConfirmPayment(double?accountNumber, double?total)
        {
            List <CartItem> lstCart = (List <CartItem>)Session[CartSession];
            var             acc     = _db.AccountPayments.Where(x => x.accountNumber == accountNumber).SingleOrDefault();

            //Chuyển thành chuỗi để băm
            string str_cart = acc.accountName + " " + acc.accountNumber + " " + acc.accountBalance;

            foreach (var item in lstCart)
            {
                str_cart += item.Product;
                str_cart += item.Quantity;
            }

            //Băm chuối
            BigInteger arrayhash = sig.HamBam(str_cart);


            //mã hóa chuỗi
            string encrypt_message = sig.Encrypt_MD5(arrayhash.ToString());

            BigInteger pri_key = BigInteger.Parse(sig.Decrypt_MD5(acc.pri_key));

            //Sinh chữ ký
            List <BigInteger> lstRS = sig.SinhChuKy(BigInteger.Parse(acc.so_p), BigInteger.Parse(acc.so_t), BigInteger.Parse(acc.so_q), pri_key, encrypt_message);

            //sinh khóa công khai
            BigInteger pub_key = sig.publicKey(pri_key, BigInteger.Parse(acc.so_p.Trim()), BigInteger.Parse(acc.so_t.Trim()));

            //Gửi xác thực chữ ký
            var res = new AccountPaymentDAO().KiemTraChuKy(encrypt_message, lstRS[0], lstRS[1], pub_key, BigInteger.Parse(acc.so_p.Trim()));

            if (!res)//Bản tin đã bị thay đổi
            {
                return(Redirect("/thanh-toan-loi"));
            }
            else
            {
                //Lưu hóa đơn
                var order = new GioHang();
                order.TenKhachHang = acc.accountName;
                order.NgayTao      = DateTime.Now;
                order.NoiDung      = "Ok";
                order.PayFormat    = 2;
                order.SDTKhachHang = acc.phone;
                order.status       = true;
                try
                {
                    _db.GioHangs.Add(order);
                    _db.SaveChanges();

                    //Lưu vào chi tiết hóa đơn
                    var cart = (List <CartItem>)Session[CartSession];
                    foreach (var i in cart)
                    {
                        var orderDetail = new ChiTietGioHang();
                        orderDetail.IDSanPham = i.Product.id;
                        orderDetail.IDGioHang = order.id;
                        if (i.Product.newprice != null)
                        {
                            orderDetail.Tien = i.Product.newprice * i.Quantity;
                        }
                        else
                        {
                            orderDetail.Tien = i.Product.price * i.Quantity;
                        }

                        orderDetail.SoLuong = i.Quantity;

                        _db.ChiTietGioHangs.Add(orderDetail);
                        _db.SaveChanges();
                    }
                    //thanh toán thành công thì cho giỏ hàng bằng null
                    Session[CartSession] = null;
                }
                catch
                {
                    return(Redirect("/thanh-toan-loi"));
                }

                //Trừ tiền trong tài khoản thanh toán
                acc.accountBalance -= total;

                //Cộng tiền cho admin
                var admin = _db.AccountPayments.Find(1);
                admin.accountBalance += total;
                _db.SaveChanges();

                return(Redirect("/thanh-toan-thanh-cong"));
            }
        }
Exemplo n.º 2
0
        public ActionResult ConfirmPayment(double?accountNumber, double?total)
        {
            List <CartItem> lstCart = (List <CartItem>)Session[CartSession];
            var             acc     = _db.AccountPayments.Where(x => x.accountNumber == accountNumber).SingleOrDefault();

            byte[] arrayhash;

            //Chuyển thành chuỗi để băm
            string str_cart = String.Join("", lstCart);

            //Băm chuối
            arrayhash = sig.hash(str_cart);
            string str_arrayhash = "";

            foreach (byte a in arrayhash)
            {
                str_arrayhash += a.ToString();
            }

            //mã hóa chuỗi
            string encrypt_message = sig.Encrypt_MD5(str_arrayhash);

            //Tạo chữ ký
            List <long> mang1 = new List <long>();

            foreach (byte k in arrayhash)
            {
                long i = Convert.ToInt64(k);
                long j = sig.TINHA(i, Convert.ToInt64(sig.Decrypt_MD5(acc.pri_key)), Convert.ToInt64(acc.so_n));
                mang1.Add(j);
            }
            //Chuyển thành chuỗi để truyền đi
            string str_signal = "";

            foreach (long k in mang1)
            {
                str_signal += k.ToString() + ' ';
            }

            //Gửi xác thực chữ ký
            var res = new AccountPaymentDAO().CheckSignal(encrypt_message, str_signal, Convert.ToInt64(acc.so_n), Convert.ToInt64(acc.so_e));

            if (res == 0)//Bản tin đã bị thay đổi
            {
                return(Redirect("/thanh-toan-loi"));
            }
            else
            {
                //Lưu hóa đơn
                var order = new GioHang();
                order.TenKhachHang = acc.accountName;
                order.NgayTao      = DateTime.Now;
                order.NoiDung      = "Ok";
                order.PayFormat    = 2;
                order.SDTKhachHang = acc.phone;
                order.status       = true;
                try
                {
                    _db.GioHangs.Add(order);
                    _db.SaveChanges();

                    //Lưu vào chi tiết hóa đơn
                    var cart = (List <CartItem>)Session[CartSession];
                    foreach (var i in cart)
                    {
                        var orderDetail = new ChiTietGioHang();
                        orderDetail.IDSanPham = i.Product.id;
                        orderDetail.IDGioHang = order.id;
                        if (i.Product.newprice != null)
                        {
                            orderDetail.Tien = i.Product.newprice * i.Quantity;
                        }
                        else
                        {
                            orderDetail.Tien = i.Product.price * i.Quantity;
                        }

                        orderDetail.SoLuong = i.Quantity;

                        _db.ChiTietGioHangs.Add(orderDetail);
                        _db.SaveChanges();
                    }
                    //thanh toán thành công thì cho giỏ hàng bằng null
                    Session[CartSession] = null;
                }
                catch
                {
                    return(Redirect("/thanh-toan-loi"));
                }

                //Trừ tiền trong tài khoản thanh toán
                acc.accountBalance -= total;

                //Cộng tiền cho admin
                var admin = _db.AccountPayments.Find(1);
                admin.accountBalance += total;
                _db.SaveChanges();

                return(Redirect("/nhap-ma-otp-token"));
            }
        }