Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 6.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using Newtonsoft.Json;
  10. using Website9D.Models;
  11.  
  12. namespace Website9D.Controllers
  13. {
  14.     public class ForCardController : Controller
  15.     {
  16.         DataMemberAccountDataContext memberPasswordContext = new DataMemberAccountDataContext();
  17.         GMSDataContext cisContext = new GMSDataContext();
  18.         Common common = new Common();
  19.         //
  20.         // GET: /ForCard/
  21.         private string merchant_id = "36251";
  22.         private string api_user = "5865b9213448b";
  23.         private string api_password = "159ead7a6ece57f331727ee0449332d8";
  24.         private string note = "dungorange";
  25.         private string type;
  26.         //private string conversion_rate = "10000:10000; 20000:20000; 30000:30000; 50000:500; 100000:100000; 200000:200000; 300000:300000; 500000:500000; 1000000:1000000";
  27.  
  28.  
  29.         public ActionResult Index()
  30.         {
  31.             if (Session["login"] == null) {
  32.                 return RedirectToAction("Index", "Home");
  33.             }
  34.            
  35.             return View();
  36.         }
  37.  
  38.         public ActionResult KTC()
  39.         {
  40.             List<_9d_KTCItem> listItem = memberPasswordContext._9d_KTCItems.Where(c => c.created_by == "Admin").OrderByDescending(c => c.created_at).ToList();
  41.             return View(listItem);
  42.         }
  43.  
  44.         [HttpPost]
  45.         public ActionResult Index(FormCollection collection)
  46.         {
  47.             try
  48.             {
  49.                 if (Session["login"] == null)
  50.                 {
  51.                     return RedirectToAction("Index", "Home");
  52.                 }
  53.                 if (collection["seritxt"].ToString() == "" || collection["passtxt"].ToString() == "") {
  54.                     ViewBag.error = "Vui lòng nhập thông tin thẻ!";
  55.                     return View();
  56.                 }
  57.  
  58.                 // Create a new request to the mentioned URL.    
  59.                 string url = "http://sv.gamebank.vn/api/card";
  60.                
  61.                 if (collection["option"].ToString() == "Viettel")
  62.                 {
  63.                     type = "1";
  64.                 }
  65.                 else if (collection["option"].ToString() == "Mobile")
  66.                 {
  67.                     type = "2";
  68.                 }
  69.                 else if (collection["option"].ToString() == "Vina")
  70.                 {
  71.                     type = "3";
  72.                 }
  73.                 else if (collection["option"].ToString() == "Gate")
  74.                 {
  75.                     type = "4";
  76.                 }
  77.                
  78.                 string seri = collection["seritxt"].ToString();
  79.                 string pin = collection["passtxt"].ToString();
  80.                 HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(url);
  81.  
  82.                 string POST_DATA = "merchant_id=" + merchant_id + "&card_type=" + type + "&seri=" + seri + "&pin=" + pin + "&note=" + note;
  83.  
  84.                 ASCIIEncoding encoding = new ASCIIEncoding();
  85.                 byte[] data = encoding.GetBytes(POST_DATA);
  86.                 httpWReq.Method = "POST";
  87.                 httpWReq.ContentType = "application/x-www-form-urlencoded";
  88.                 httpWReq.ContentLength = data.Length;
  89.                 httpWReq.Credentials = new NetworkCredential(api_user, api_password);
  90.                 using (Stream stream = httpWReq.GetRequestStream())
  91.                 {
  92.                     stream.Write(data, 0, data.Length);
  93.                 }
  94.                 HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
  95.                 string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
  96.  
  97.                 dynamic results = JsonConvert.DeserializeObject<dynamic>(responseString);
  98.                 string i = results.code;
  99.                 string e = results.code;
  100.                 string ic;
  101.                 if (results.code == 0)
  102.                 {
  103.                     _9d_user user = memberPasswordContext._9d_users.Where(c => c.user_name == Session["login"].ToString()).FirstOrDefault();
  104.                     ic = results.info_card;
  105.  
  106.                     int coutncar = Convert.ToInt32(ic);
  107.                     user.balance = user.balance + coutncar;
  108.                     memberPasswordContext.SubmitChanges();
  109.  
  110.                     User_History addhistory = new User_History();
  111.                     addhistory.username = Session["login"].ToString();
  112.                     addhistory.timeinto = DateTime.Now;
  113.                     addhistory.carinfo = type + "," + coutncar;
  114.                     memberPasswordContext.User_Histories.InsertOnSubmit(addhistory);
  115.                     memberPasswordContext.SubmitChanges();
  116.                     //ViewBag.balance = user.balance;
  117.                     Session["xu"] = user.balance;
  118.                     return RedirectToAction("Index", "Home");
  119.                 }
  120.                 else {
  121.                     ViewBag.error = "Nạp thẻ thất bại, vui lòng kiểm tra lại";
  122.                     return View();
  123.                 }
  124.             }
  125.             catch (Exception e){}
  126.             return View();
  127.         }
  128.  
  129.         public ActionResult Buy(int id)
  130.         {
  131.             if (Session["login"] == null)
  132.             {
  133.                 return RedirectToAction("Index", "Home");
  134.             }
  135.  
  136.             _9d_user u = common.getUserInfo(Session["login"].ToString());
  137.             _9d_KTCItem item = memberPasswordContext._9d_KTCItems.Where(c => c.id == id).FirstOrDefault();
  138.  
  139.             if (item == null)
  140.             {
  141.                 TempData["error"] = "Item này không tồn tại";
  142.                 return RedirectToAction("KTC", "ForCard");
  143.             }
  144.  
  145.             if (u.balance < item.itemprice)
  146.             {
  147.                 TempData["error"] = "Bạn không đủ tiền mua vật phẩm này";
  148.                 return RedirectToAction("KTC", "ForCard");
  149.             }
  150.  
  151.             try
  152.             {
  153.                 //chuyen item vao tk nguoi choi
  154.                 string userId = u.user_name;
  155.                 int? itemId = Convert.ToInt32(item.itemid);
  156.                 byte? server = Convert.ToByte(0);
  157.                 int? orderId = 0;
  158.                 byte? err = 0;
  159.  
  160.                 cisContext.Sp_Purchase_Using(userId, Convert.ToInt32(item.itemid), server, 0, ref orderId, ref err);
  161.                 //add vao transaction
  162.                 cisContext.SubmitChanges();
  163.  
  164.                 int curr = u.balance;
  165.                 _9d_user user = memberPasswordContext._9d_users.Where(c => c.user_name == userId).FirstOrDefault();
  166.                 user.balance = curr - item.itemprice;
  167.                 memberPasswordContext.SubmitChanges();
  168.  
  169.                 Session["xu"] = user.balance;
  170.                 TempData["error"] = "Mua hàng thành công!!!";
  171.             }
  172.             catch (Exception)
  173.             {
  174.  
  175.             }
  176.  
  177.             return RedirectToAction("KTC", "ForCard");
  178.         }
  179.  
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement