-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPayment.cs
184 lines (149 loc) · 6.01 KB
/
Payment.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Coders_Space
{
public partial class Payment : Form
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
string userEmail;
decimal amount;
public Payment(string email, decimal amount)
{
InitializeComponent();
userEmail = email;
this.amount = amount;
}
private void Payment_Load(object sender, EventArgs e)
{
}
private void guna2HtmlLabel1_Click(object sender, EventArgs e)
{
}
private bool PaymentInformationIsValid()
{
string defaultAccountName = "Mirza Saikat Ahmmed";
string defaultCardNumber = "123456789012";
string defaultExpirationMonth = "12";
string defaultExpirationYear = "35";
string defaultCVV = "354";
return textBoxAccountName.Text.Trim() == defaultAccountName &&
textBoxCardNumber.Text.Trim() == defaultCardNumber &&
textBoxMM.Text.Trim() == defaultExpirationMonth &&
textBoxYY.Text.Trim() == defaultExpirationYear &&
textBoxCVV.Text.Trim() == defaultCVV;
}
private void buttonPay_Click(object sender, EventArgs e)
{
if (PaymentInformationIsValid())
{
UpdateUserRoleToPremium();
InsertTransactionRecord();
MessageBox.Show("Payment complete!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
Login login = new Login();
login.Show();
this.Close();
}
else
{
MessageBox.Show("Payment information is not valid.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpdateUserRoleToPremium()
{
using (SqlConnection connection = new SqlConnection(cs))
{
connection.Open();
string query = "UPDATE USERS SET Role = 'PREMIUM_USER', UPDATE_DATE = GETDATE() WHERE Email = @Email";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@Email", userEmail);
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
Console.WriteLine("User role updated to premium_user.");
}
else
{
Console.WriteLine("Failed to update user role.");
}
}
}
private void InsertTransactionRecord()
{
int nextID;
int maxID = GetMaxIDFromDatabase();
nextID = maxID + 1;
using (SqlConnection connection = new SqlConnection(cs))
{
connection.Open();
string queryUser = "SELECT ID, Name FROM USERS WHERE Email = @Email";
SqlCommand commandUser = new SqlCommand(queryUser, connection);
commandUser.Parameters.AddWithValue("@Email", userEmail);
SqlDataReader reader = commandUser.ExecuteReader();
int userId = 0;
string userName = string.Empty;
if (reader.Read())
{
object idValue = reader["ID"];
if (idValue != null && idValue != DBNull.Value)
{
userId = Convert.ToInt32(idValue);
}
userName = reader["NAME"].ToString(); ;
}
reader.Close();
string queryTransaction = "INSERT INTO TRANSACTIONS (ID, USERID, USERNAME, AMOUNT, TRXID, SUB_VALID_MONTH, TRANSACTION_DATE) " +
"VALUES (@ID, @UserId, @UserName, @Amount, @TrxId, @SubValidMonth, GETDATE())";
using (SqlCommand commandTransaction = new SqlCommand(queryTransaction, connection))
{
commandTransaction.Parameters.AddWithValue("@ID", nextID);
commandTransaction.Parameters.AddWithValue("@UserId", userId);
commandTransaction.Parameters.AddWithValue("@UserName", userName);
commandTransaction.Parameters.AddWithValue("@Amount", amount);
commandTransaction.Parameters.AddWithValue("@TrxId", GenerateTrxId());
commandTransaction.Parameters.AddWithValue("@SubValidMonth", (amount == 5000) ? 12 : 1);
commandTransaction.ExecuteNonQuery();
}
}
}
private int GetMaxIDFromDatabase()
{
int maxID = 0;
using (SqlConnection con = new SqlConnection(cs))
{
string query = "SELECT MAX(ID) FROM TRANSACTIONS";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
object result = cmd.ExecuteScalar();
if (result != null && result != DBNull.Value)
{
maxID = Convert.ToInt32(result);
}
con.Close();
}
return maxID;
}
private string GenerateTrxId()
{
string timestamp = DateTime.Now.ToString("yyyyMMddHHmmssfff");
string randomPart = Guid.NewGuid().ToString("N").Substring(0, 6);
return $"{timestamp}{randomPart}";
}
private void exitBTN_Click(object sender, EventArgs e)
{
this.Close();
}
private void minimizeBTN_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
}
}