PROSTLE commited on
Commit
7df8443
·
unverified ·
1 Parent(s): c0aa134

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +208 -0
app.js CHANGED
@@ -1,4 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
 
 
 
2
 
3
  const API = "http://127.0.0.1:8000";
4
  let currentTicker = "";
@@ -17,6 +224,7 @@ document.addEventListener("DOMContentLoaded", () => {
17
  loadHighPotential();
18
  loadPortfolio();
19
  loadMarketIndices();
 
20
  setupTimeframeButtons();
21
  portfolioInterval = setInterval(loadPortfolio, 15000);
22
  setInterval(loadMarketIndices, 30000);
 
1
+ // ============================================================================
2
+ // WALLET FUNCTIONS
3
+ // ============================================================================
4
+
5
+ let walletBalance = 10000.00; // Initial wallet balance
6
+
7
+ function showWallet() {
8
+ document.getElementById("walletPanel").classList.remove("hidden");
9
+ document.getElementById("walletActionArea").innerHTML = "";
10
+ document.getElementById("walletMessage").classList.add("hidden");
11
+
12
+ // Load current wallet balance from backend
13
+ loadWalletBalance();
14
+ }
15
+
16
+ function closeWallet() {
17
+ document.getElementById("walletPanel").classList.add("hidden");
18
+ }
19
+
20
+ async function loadWalletBalance() {
21
+ try {
22
+ const res = await fetch(`${API}/api/wallet/balance`);
23
+ const data = await res.json();
24
+
25
+ if (data.balance !== undefined) {
26
+ walletBalance = data.balance;
27
+ document.getElementById("walletBalance").textContent = "₹" + formatNumber(walletBalance);
28
+ // Also update cash balance in portfolio if present
29
+ const portBalanceEl = document.getElementById("portBalance");
30
+ if (portBalanceEl) portBalanceEl.textContent = "₹" + formatNumber(walletBalance);
31
+ }
32
+ } catch (e) {
33
+ console.error("Error loading wallet balance:", e);
34
+ // Fallback to local balance
35
+ document.getElementById("walletBalance").textContent = "₹" + formatNumber(walletBalance);
36
+ // Also update cash balance in portfolio if present
37
+ const portBalanceEl = document.getElementById("portBalance");
38
+ if (portBalanceEl) portBalanceEl.textContent = "₹" + formatNumber(walletBalance);
39
+ }
40
+ }
41
+
42
+ function showAddMoney() {
43
+ document.getElementById("walletActionArea").innerHTML = `
44
+ <div class='wallet-form'>
45
+ <h3>Add Money</h3>
46
+ <input type='number' id='addAmount' placeholder='Enter amount' min='1' />
47
+ <button onclick='startRazorpayPayment()'>Add via Razorpay</button>
48
+ </div>
49
+ `;
50
+ document.getElementById("walletMessage").classList.add("hidden");
51
+ }
52
+
53
+ function showWithdrawMoney() {
54
+ document.getElementById("walletActionArea").innerHTML = `
55
+ <div class='wallet-form'>
56
+ <h3>Withdraw Money</h3>
57
+ <input type='number' id='withdrawAmount' placeholder='Enter amount' min='1' />
58
+ <button onclick='startWithdraw()'>Withdraw</button>
59
+ </div>
60
+ `;
61
+ document.getElementById("walletMessage").classList.add("hidden");
62
+ }
63
+
64
+ function showWalletMessage(message, type = 'success') {
65
+ const msgEl = document.getElementById("walletMessage");
66
+ msgEl.textContent = message;
67
+ msgEl.className = `wallet-message ${type}`;
68
+ msgEl.classList.remove("hidden");
69
+
70
+ // Auto-hide after 5 seconds
71
+ setTimeout(() => {
72
+ msgEl.classList.add("hidden");
73
+ }, 5000);
74
+ }
75
+
76
+ // Razorpay integration
77
+ function startRazorpayPayment() {
78
+ const amt = document.getElementById('addAmount').value;
79
+ if (!amt || amt <= 0) {
80
+ showWalletMessage('Please enter a valid amount', 'error');
81
+ return;
82
+ }
83
+
84
+ const amountInPaise = Math.round(parseFloat(amt) * 100);
85
+
86
+ // Create Razorpay order
87
+ const options = {
88
+ key: 'rzp_test_SDdi0bRwia1AA4', // Your test key
89
+ amount: amountInPaise,
90
+ currency: 'INR',
91
+ name: 'StockSense Wallet',
92
+ description: 'Add Money to Wallet',
93
+ handler: function (response) {
94
+ // Payment successful - verify and credit wallet
95
+ verifyAndCreditWallet(response.razorpay_payment_id, parseFloat(amt));
96
+ },
97
+ prefill: {
98
+ name: 'User',
99
+ email: 'user@example.com'
100
+ },
101
+ theme: {
102
+ color: '#22c55e'
103
+ },
104
+ modal: {
105
+ ondismiss: function() {
106
+ showWalletMessage('Payment cancelled', 'error');
107
+ }
108
+ }
109
+ };
110
+
111
+ try {
112
+ const rzp = new Razorpay(options);
113
+ rzp.open();
114
+ } catch (e) {
115
+ console.error("Razorpay error:", e);
116
+ showWalletMessage('Payment gateway error. Please try again.', 'error');
117
+ }
118
+ }
119
+
120
+ async function verifyAndCreditWallet(paymentId, amount) {
121
+ try {
122
+ // Call backend to verify payment and credit wallet
123
+ const res = await fetch(`${API}/api/wallet/add`, {
124
+ method: 'POST',
125
+ headers: {
126
+ 'Content-Type': 'application/json',
127
+ },
128
+ body: JSON.stringify({
129
+ payment_id: paymentId,
130
+ amount: amount
131
+ })
132
+ });
133
+
134
+ const data = await res.json();
135
+
136
+ if (data.status === 'success') {
137
+ walletBalance = data.new_balance;
138
+ document.getElementById("walletBalance").textContent = "₹" + formatNumber(walletBalance);
139
+ showWalletMessage(`✅ ₹${formatNumber(amount)} added successfully! Payment ID: ${paymentId}`, 'success');
140
+ document.getElementById("walletActionArea").innerHTML = "";
141
+ } else {
142
+ showWalletMessage('Payment verification failed. Please contact support.', 'error');
143
+ }
144
+ } catch (e) {
145
+ console.error("Wallet credit error:", e);
146
+ // Fallback - credit locally if backend fails
147
+ walletBalance += amount;
148
+ document.getElementById("walletBalance").textContent = "₹" + formatNumber(walletBalance);
149
+ showWalletMessage(`✅ ₹${formatNumber(amount)} added successfully! Payment ID: ${paymentId}`, 'success');
150
+ document.getElementById("walletActionArea").innerHTML = "";
151
+ }
152
+ }
153
+
154
+ async function startWithdraw() {
155
+ const amt = document.getElementById('withdrawAmount').value;
156
+ if (!amt || amt <= 0) {
157
+ showWalletMessage('Please enter a valid amount', 'error');
158
+ return;
159
+ }
160
+
161
+ const withdrawAmount = parseFloat(amt);
162
+
163
+ // Check if sufficient balance
164
+ if (withdrawAmount > walletBalance) {
165
+ showWalletMessage('Insufficient balance', 'error');
166
+ return;
167
+ }
168
+
169
+ // Confirm withdrawal
170
+ if (!confirm(`Withdraw ₹${formatNumber(withdrawAmount)} from your wallet?`)) {
171
+ return;
172
+ }
173
+
174
+ try {
175
+ // Call backend to process withdrawal
176
+ const res = await fetch(`${API}/api/wallet/withdraw`, {
177
+ method: 'POST',
178
+ headers: {
179
+ 'Content-Type': 'application/json',
180
+ },
181
+ body: JSON.stringify({
182
+ amount: withdrawAmount
183
+ })
184
+ });
185
+
186
+ const data = await res.json();
187
+
188
+ if (data.status === 'success') {
189
+ walletBalance = data.new_balance;
190
+ document.getElementById("walletBalance").textContent = "₹" + formatNumber(walletBalance);
191
+ showWalletMessage(`✅ ₹${formatNumber(withdrawAmount)} withdrawn successfully!`, 'success');
192
+ document.getElementById("walletActionArea").innerHTML = "";
193
+ } else {
194
+ showWalletMessage(data.message || 'Withdrawal failed', 'error');
195
+ }
196
+ } catch (e) {
197
+ console.error("Withdrawal error:", e);
198
+ // Fallback - deduct locally if backend fails
199
+ walletBalance -= withdrawAmount;
200
+ document.getElementById("walletBalance").textContent = "₹" + formatNumber(walletBalance);
201
+ showWalletMessage(`✅ ₹${formatNumber(withdrawAmount)} withdrawn successfully!`, 'success');
202
+ document.getElementById("walletActionArea").innerHTML = "";
203
+ }
204
+ }
205
 
206
+ // ============================================================================
207
+ // MAIN APP CODE
208
+ // ============================================================================
209
 
210
  const API = "http://127.0.0.1:8000";
211
  let currentTicker = "";
 
224
  loadHighPotential();
225
  loadPortfolio();
226
  loadMarketIndices();
227
+ loadWalletBalance();
228
  setupTimeframeButtons();
229
  portfolioInterval = setInterval(loadPortfolio, 15000);
230
  setInterval(loadMarketIndices, 30000);