Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Web;
- using System.Web.Mvc;
- using Newtonsoft.Json;
- using Website9D.Models;
- namespace Website9D.Controllers
- {
- public class ForCardController : Controller
- {
- DataMemberAccountDataContext memberPasswordContext = new DataMemberAccountDataContext();
- GMSDataContext cisContext = new GMSDataContext();
- Common common = new Common();
- //
- // GET: /ForCard/
- private string merchant_id = "36251";
- private string api_user = "5865b9213448b";
- private string api_password = "159ead7a6ece57f331727ee0449332d8";
- private string note = "dungorange";
- private string type;
- //private string conversion_rate = "10000:10000; 20000:20000; 30000:30000; 50000:500; 100000:100000; 200000:200000; 300000:300000; 500000:500000; 1000000:1000000";
- public ActionResult Index()
- {
- if (Session["login"] == null) {
- return RedirectToAction("Index", "Home");
- }
- return View();
- }
- public ActionResult KTC()
- {
- List<_9d_KTCItem> listItem = memberPasswordContext._9d_KTCItems.Where(c => c.created_by == "Admin").OrderByDescending(c => c.created_at).ToList();
- return View(listItem);
- }
- [HttpPost]
- public ActionResult Index(FormCollection collection)
- {
- try
- {
- if (Session["login"] == null)
- {
- return RedirectToAction("Index", "Home");
- }
- if (collection["seritxt"].ToString() == "" || collection["passtxt"].ToString() == "") {
- ViewBag.error = "Vui lòng nhập thông tin thẻ!";
- return View();
- }
- // Create a new request to the mentioned URL.
- string url = "http://sv.gamebank.vn/api/card";
- if (collection["option"].ToString() == "Viettel")
- {
- type = "1";
- }
- else if (collection["option"].ToString() == "Mobile")
- {
- type = "2";
- }
- else if (collection["option"].ToString() == "Vina")
- {
- type = "3";
- }
- else if (collection["option"].ToString() == "Gate")
- {
- type = "4";
- }
- string seri = collection["seritxt"].ToString();
- string pin = collection["passtxt"].ToString();
- HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(url);
- string POST_DATA = "merchant_id=" + merchant_id + "&card_type=" + type + "&seri=" + seri + "&pin=" + pin + "¬e=" + note;
- ASCIIEncoding encoding = new ASCIIEncoding();
- byte[] data = encoding.GetBytes(POST_DATA);
- httpWReq.Method = "POST";
- httpWReq.ContentType = "application/x-www-form-urlencoded";
- httpWReq.ContentLength = data.Length;
- httpWReq.Credentials = new NetworkCredential(api_user, api_password);
- using (Stream stream = httpWReq.GetRequestStream())
- {
- stream.Write(data, 0, data.Length);
- }
- HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
- string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
- dynamic results = JsonConvert.DeserializeObject<dynamic>(responseString);
- string i = results.code;
- string e = results.code;
- string ic;
- if (results.code == 0)
- {
- _9d_user user = memberPasswordContext._9d_users.Where(c => c.user_name == Session["login"].ToString()).FirstOrDefault();
- ic = results.info_card;
- int coutncar = Convert.ToInt32(ic);
- user.balance = user.balance + coutncar;
- memberPasswordContext.SubmitChanges();
- User_History addhistory = new User_History();
- addhistory.username = Session["login"].ToString();
- addhistory.timeinto = DateTime.Now;
- addhistory.carinfo = type + "," + coutncar;
- memberPasswordContext.User_Histories.InsertOnSubmit(addhistory);
- memberPasswordContext.SubmitChanges();
- //ViewBag.balance = user.balance;
- Session["xu"] = user.balance;
- return RedirectToAction("Index", "Home");
- }
- else {
- ViewBag.error = "Nạp thẻ thất bại, vui lòng kiểm tra lại";
- return View();
- }
- }
- catch (Exception e){}
- return View();
- }
- public ActionResult Buy(int id)
- {
- if (Session["login"] == null)
- {
- return RedirectToAction("Index", "Home");
- }
- _9d_user u = common.getUserInfo(Session["login"].ToString());
- _9d_KTCItem item = memberPasswordContext._9d_KTCItems.Where(c => c.id == id).FirstOrDefault();
- if (item == null)
- {
- TempData["error"] = "Item này không tồn tại";
- return RedirectToAction("KTC", "ForCard");
- }
- if (u.balance < item.itemprice)
- {
- TempData["error"] = "Bạn không đủ tiền mua vật phẩm này";
- return RedirectToAction("KTC", "ForCard");
- }
- try
- {
- //chuyen item vao tk nguoi choi
- string userId = u.user_name;
- int? itemId = Convert.ToInt32(item.itemid);
- byte? server = Convert.ToByte(0);
- int? orderId = 0;
- byte? err = 0;
- cisContext.Sp_Purchase_Using(userId, Convert.ToInt32(item.itemid), server, 0, ref orderId, ref err);
- //add vao transaction
- cisContext.SubmitChanges();
- int curr = u.balance;
- _9d_user user = memberPasswordContext._9d_users.Where(c => c.user_name == userId).FirstOrDefault();
- user.balance = curr - item.itemprice;
- memberPasswordContext.SubmitChanges();
- Session["xu"] = user.balance;
- TempData["error"] = "Mua hàng thành công!!!";
- }
- catch (Exception)
- {
- }
- return RedirectToAction("KTC", "ForCard");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement