public static void Thread1(object sock) { string gatewayPublicKey; string privateKeyMerchant = File.ReadAllText("d:/file/MerchantPrivateKey.xml"); Common c = new Common(); Socket socket = (Socket)sock; //nhận init request string receiveMessage = c.receive(socket); string[] initREQ = receiveMessage.Split('-'); InitiateRequest initiateRequest = new InitiateRequest(initREQ[0], initREQ[1], initREQ[2], initREQ[3], initREQ[4]); //tạo init response X509Certificate2 certificateMerchant = new X509Certificate2("d:/file/merchant.crt", "123456"); X509Certificate2 certificateGateway = new X509Certificate2("d:/file/gateway.crt", "123456"); gatewayPublicKey = certificateGateway.GetRSAPublicKey().ToXmlString(false); InitiateResponse initiateResponse = new InitiateResponse(initiateRequest.getLIDC(), initiateRequest.getLanguage(), initiateRequest.getRRPID(), initiateRequest.getBrandID(), c.ByteArrayToString(certificateMerchant.GetRawCertData()), c.ByteArrayToString(certificateGateway.GetRawCertData())); string sendMessage = initiateResponse.ToMessage(privateKeyMerchant); c.send(sendMessage, socket); //nhận purchase request receiveMessage = c.receive(socket); string[] purchase = receiveMessage.Split('-'); PurchaseRequest purchaseRequest = new PurchaseRequest(purchase[0], purchase[1], purchase[2], purchase[3], purchase[4], purchase[5]); Console.WriteLine("purchase verify" + purchaseRequest.verify()); //tạo ủy quyền request gửi tới gateway AuthorizationRequest authorizationRequest = new AuthorizationRequest(purchaseRequest.getTransID(), Convert.ToDouble(purchaseRequest.getTien()), privateKeyMerchant, gatewayPublicKey, purchaseRequest.getCustommerCertificate(), c.ByteArrayToString(certificateMerchant.GetRawCertData()), purchaseRequest.getMessageToGateway(), purchaseRequest.getDigitalEnvelop()); IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1235); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(iep); c.send(authorizationRequest.ToMessage(), client); //nhận auth res receiveMessage = c.receive(client); string[] splitAuthRES = receiveMessage.Split('-'); AuthorizationResponse authorizationResponse = new AuthorizationResponse(splitAuthRES[0], splitAuthRES[1], splitAuthRES[2], splitAuthRES[3], splitAuthRES[4], splitAuthRES[5], splitAuthRES[6]); Console.WriteLine("verify authorization response: " + authorizationResponse.verifyMessage()); //lưu token SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = "localhost"; builder.UserID = "sa"; builder.Password = "******"; builder.InitialCatalog = "Bank"; using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { connection.Open(); string sql; StringBuilder sb = new StringBuilder(); sb.Clear(); sb.Append("INSERT Token (TransID, SignToken, EncryptToken, EncryptKey) "); sb.Append("VALUES (@id, @sign, @token, @key);"); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@id", authorizationResponse.getTransID()); command.Parameters.AddWithValue("@sign", authorizationResponse.getSignToken()); command.Parameters.AddWithValue("@token", authorizationResponse.getEncryptToken()); command.Parameters.AddWithValue("@key", authorizationResponse.getEncryptKeyToken()); int rowsAffected = command.ExecuteNonQuery(); } connection.Close(); } //tạo purchase response và gởi customer string[] messageRES = authorizationResponse.getMessage().Split(':'); PurchaseResponse purchaseResponse = new PurchaseResponse(messageRES[0] + ":" + purchaseRequest.getRRPID() + ":" + messageRES[2] + ":" + messageRES[3]); c.send(purchaseResponse.ToMessage(), socket); //tạo capture request string merchantCard = "012541AR09O5"; string merchantCVV = "012345"; string merchantDateValid = "25062019"; //---->lấy token string signToken = "", encryptToken = "", encryptKeyToken = ""; using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { connection.Open(); string sql; sql = "SELECT TransID, SignToken, EncryptToken, EncryptKey FROM Token;"; using (SqlCommand command = new SqlCommand(sql, connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { if (reader.GetString(0).CompareTo(authorizationResponse.getTransID()) == 0) { signToken = reader.GetString(1); encryptToken = reader.GetString(2); encryptKeyToken = reader.GetString(3); } } } } connection.Close(); } CaptureRequest captureRequest = new CaptureRequest(purchaseRequest.getTransID(), merchantCard, merchantCVV, merchantDateValid, Convert.ToDouble(purchaseRequest.getTien()), gatewayPublicKey, signToken, encryptToken, encryptKeyToken); c.send(captureRequest.ToMessage(), client); //nhận capture response từ gateway receiveMessage = c.receive(client); string[] splitCaptureResponse = receiveMessage.Split('-'); CaptureResponse captureResponse = new CaptureResponse(splitCaptureResponse[0], splitCaptureResponse[1], splitCaptureResponse[2], splitCaptureResponse[3]); Console.WriteLine("verify capture response: " + captureResponse.verify()); using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { connection.Open(); string sql; StringBuilder sb = new StringBuilder(); sb.Clear(); sb.Append("INSERT LogCaptureResponse (SignMessage, EncryptMessage, EncryptKey) "); sb.Append("VALUES (@sign, @encrypt, @key);"); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@sign", captureResponse.getSignMessage()); command.Parameters.AddWithValue("@encrypt", captureResponse.getEncryptMessage()); command.Parameters.AddWithValue("@key", captureResponse.getEncryptKey()); int rowsAffected = command.ExecuteNonQuery(); } connection.Close(); } Console.Read(); }
public static void Thread1(object sock) { int maDH; Console.WriteLine("start connect with customer client"); X509Certificate2 caCertificate = new X509Certificate2("d:/file/ca.crt"); string gatewayPublicKey; string merchantPrivateKey = File.ReadAllText("d:/file/MerchantPrivateKey.xml"); X509Certificate2 merchantCertificate = new X509Certificate2("d:/file/merchant.crt"); X509Certificate2 gatewayCertificate = new X509Certificate2("d:/file/gateway.crt"); X509Certificate2 customerCertificate; Common c = new Common(); Socket socket = (Socket)sock; string receiveMessage = c.receive(ref socket); string[] firstMessage = receiveMessage.Split('-'); string s; //thông điệp nhận được là Hủy đơn hàng hoặc init request if (firstMessage[0].CompareTo("HUYDONHANG") == 0) { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = "localhost"; builder.UserID = "sa"; builder.Password = "******"; builder.InitialCatalog = "QuanLyBanSach"; using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { connection.Open(); string sql; StringBuilder sb = new StringBuilder(); sb.Append("DELETE FROM ChiTietDonHang WHERE MaDonHang = @maDH;"); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@maDH", firstMessage[1]); int rowsAffected = command.ExecuteNonQuery(); } sb.Clear(); sb.Append("DELETE FROM DonHang WHERE MaDonHang = @maDH;"); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@maDH", firstMessage[1]); int rowsAffected = command.ExecuteNonQuery(); } connection.Close(); } } else { InitiateRequest initiateRequest = new InitiateRequest(firstMessage[0], firstMessage[1], firstMessage[2], firstMessage[3], firstMessage[4]); //tạo init response gatewayPublicKey = gatewayCertificate.GetRSAPublicKey().ToXmlString(false); InitiateResponse initiateResponse = new InitiateResponse(initiateRequest.getLIDC(), initiateRequest.getLanguage(), initiateRequest.getRRPID(), initiateRequest.getBrandID(), c.ByteArrayToString(merchantCertificate.GetRawCertData()), c.ByteArrayToString(gatewayCertificate.GetRawCertData())); string sendMessage = initiateResponse.ToMessage(merchantPrivateKey); c.send(sendMessage, ref socket); //nhận purchase request receiveMessage = c.receive(ref socket); string[] purchase = receiveMessage.Split('-'); customerCertificate = new X509Certificate2(c.StringToByteArray(purchase[5])); if (c.VerifyCertificate(caCertificate, customerCertificate) == false) { Console.WriteLine("verify purchase request certificate false"); s = initiateResponse.getTransID() + ":" + c.Random(2) + ":" + 4 + ":" + "xac thuc that bai"; PurchaseResponse purchaseResponse = new PurchaseResponse(s); c.send(purchaseResponse.ToMessage(), ref socket); } else { PurchaseRequest purchaseRequest = new PurchaseRequest(purchase[0], purchase[1], purchase[2], purchase[3], purchase[4], purchase[5]); if (purchaseRequest.verify() == false)//xác thực purchase request { Console.WriteLine("verify purchase request false"); s = initiateResponse.getTransID() + ":" + purchaseRequest.getRRPID() + ":" + 4 + ":" + "xac thuc that bai"; PurchaseResponse purchaseResponse = new PurchaseResponse(s); c.send(purchaseResponse.ToMessage(), ref socket); } else { Console.WriteLine("verify purchase request true"); maDH = purchaseRequest.getMaDH(); //tạo authorization request gửi tới gateway AuthorizationRequest authorizationRequest = new AuthorizationRequest(purchaseRequest.getTransID(), Convert.ToDouble(purchaseRequest.getTien()), merchantPrivateKey, gatewayPublicKey, purchaseRequest.getCustommerCertificate(), c.ByteArrayToString(merchantCertificate.GetRawCertData()), purchaseRequest.getMessageToGateway(), purchaseRequest.getDigitalEnvelop()); IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1235); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(iep); c.send(authorizationRequest.ToMessage(), ref client); //nhận authorization response receiveMessage = c.receive(ref client); string[] splitAuthRES = receiveMessage.Split('-'); AuthorizationResponse authorizationResponse; if (splitAuthRES.Length < 5)//trường hợp nhận thông báo lỗi từ isuuer { gatewayCertificate = new X509Certificate2(c.StringToByteArray(splitAuthRES[3])); if (c.VerifyCertificate(caCertificate, gatewayCertificate) == true)//kiểm tra chứng chỉ nhận từ gateway { Console.WriteLine("verify authorization response certificate true"); authorizationResponse = new AuthorizationResponse(splitAuthRES[0], splitAuthRES[1], splitAuthRES[2], splitAuthRES[3]); if (authorizationResponse.verifyMessage() == true) { Console.WriteLine("verify authorization response true"); //tạo purchase response và gởi customer string[] messageRES = authorizationResponse.getMessage().Split(':'); PurchaseResponse purchaseResponse = new PurchaseResponse(messageRES[0] + ":" + purchaseRequest.getRRPID() + ":" + messageRES[2] + ":" + messageRES[3]); c.send(purchaseResponse.ToMessage(), ref socket); } } } else { gatewayCertificate = new X509Certificate2(c.StringToByteArray(splitAuthRES[6])); if (c.VerifyCertificate(caCertificate, gatewayCertificate) == true)//kiểm tra chứng chỉ nhận từ gateway { Console.WriteLine("verify authorization response certificate true"); authorizationResponse = new AuthorizationResponse(splitAuthRES[0], splitAuthRES[1], splitAuthRES[2], splitAuthRES[3], splitAuthRES[4], splitAuthRES[5], splitAuthRES[6]); if (authorizationResponse.verifyMessage() == true) { Console.WriteLine("verify authorization response true"); //lưu token SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = "localhost"; builder.UserID = "sa"; builder.Password = "******"; builder.InitialCatalog = "Bank"; using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { connection.Open(); string sql; StringBuilder sb = new StringBuilder(); sb.Clear(); sb.Append("INSERT Token (TransID, SignToken, EncryptToken, EncryptKey) "); sb.Append("VALUES (@id, @sign, @token, @key);"); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@id", authorizationResponse.getTransID()); command.Parameters.AddWithValue("@sign", authorizationResponse.getSignToken()); command.Parameters.AddWithValue("@token", authorizationResponse.getEncryptToken()); command.Parameters.AddWithValue("@key", authorizationResponse.getEncryptKeyToken()); int rowsAffected = command.ExecuteNonQuery(); } connection.Close(); } //tạo purchase response và gởi customer string[] messageRES = authorizationResponse.getMessage().Split(':'); PurchaseResponse purchaseResponse = new PurchaseResponse(messageRES[0] + ":" + purchaseRequest.getRRPID() + ":" + messageRES[2] + ":" + messageRES[3]); c.send(purchaseResponse.ToMessage(), ref socket); //Console.WriteLine(purchaseResponse.getMessage()); //tạo capture request gửi tới gateway string merchantCard = "012541AR09O5"; string merchantCVV = "012345"; string merchantDateValid = "25062019"; //---->lấy token string signToken = "", encryptToken = "", encryptKeyToken = ""; using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { connection.Open(); string sql; sql = "SELECT TransID, SignToken, EncryptToken, EncryptKey FROM Token;"; using (SqlCommand command = new SqlCommand(sql, connection)) { using (SqlDataReader sqlReader = command.ExecuteReader()) { while (sqlReader.Read()) { if (sqlReader.GetString(0).CompareTo(authorizationResponse.getTransID()) == 0) { signToken = sqlReader.GetString(1); encryptToken = sqlReader.GetString(2); encryptKeyToken = sqlReader.GetString(3); } } } } connection.Close(); } CaptureRequest captureRequest = new CaptureRequest(purchaseRequest.getTransID(), merchantCard, merchantCVV, merchantDateValid, Convert.ToInt64(purchaseRequest.getTien()), gatewayPublicKey, signToken, encryptToken, encryptKeyToken); c.send(captureRequest.ToMessage(), ref client); //nhận capture response từ gateway receiveMessage = c.receive(ref client); string[] splitCaptureResponse = receiveMessage.Split('-'); gatewayCertificate = new X509Certificate2(c.StringToByteArray(splitCaptureResponse[3])); if (c.VerifyCertificate(caCertificate, gatewayCertificate) == true) { Console.WriteLine("verify capture response certificate true"); CaptureResponse captureResponse = new CaptureResponse(splitCaptureResponse[0], splitCaptureResponse[1], splitCaptureResponse[2], splitCaptureResponse[3]); if (captureResponse.verify() == true) { Console.WriteLine("verify capture response true"); using (SqlConnection connection = new SqlConnection(builder.ConnectionString))//lưu capture response { connection.Open(); string sql; StringBuilder sb = new StringBuilder(); sb.Clear(); //lưu capture response sb.Append("INSERT LogCaptureResponse (SignMessage, EncryptMessage, EncryptKey) "); sb.Append("VALUES (@sign, @encrypt, @key);"); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@sign", captureResponse.getSignMessage()); command.Parameters.AddWithValue("@encrypt", captureResponse.getEncryptMessage()); command.Parameters.AddWithValue("@key", captureResponse.getEncryptKey()); int rowsAffected = command.ExecuteNonQuery(); } //xác nhận tình trạng thanh toán của đơn hàng connection.Close(); } builder.InitialCatalog = "QuanLyBanSach"; using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { connection.Open(); string sql; StringBuilder sb = new StringBuilder(); sb.Clear(); sb.Append("UPDATE DonHang SET DaThanhToan = @thanhtoan WHERE MaDonHang = @id"); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@thanhtoan", 1); command.Parameters.AddWithValue("@id", maDH); int rowsAffected = command.ExecuteNonQuery(); } connection.Close(); } } } } } } client.Close(); } } } socket.Close(); }