2
2
3
3
import androidx .appcompat .app .AppCompatActivity ;
4
4
5
+ import android .content .Context ;
6
+ import android .content .SharedPreferences ;
5
7
import android .os .Bundle ;
8
+ import android .text .Editable ;
9
+ import android .text .TextWatcher ;
10
+ import android .util .Log ;
6
11
import android .view .View ;
12
+ import android .widget .CompoundButton ;
7
13
import android .widget .TextView ;
8
14
9
15
import com .BugBazaar .R ;
16
+ import android .widget .Button ;
17
+ import android .widget .CheckBox ;
18
+ import android .widget .EditText ;
19
+ import android .widget .RadioButton ;
20
+ import android .widget .RadioGroup ;
21
+ import android .widget .Toast ;
10
22
11
- public class Wallet extends AppCompatActivity {
23
+ import com .BugBazaar .ui .payment .OrderSummary ;
24
+ import com .razorpay .Checkout ;
25
+ import com .razorpay .PaymentResultListener ;
26
+ import org .json .JSONObject ;
27
+
28
+ public class Wallet extends AppCompatActivity implements PaymentResultListener {
29
+ private static final String WALLET_BALANCE_KEY = "wallet_balance" ;
30
+ private TextView walletBalanceW ;
31
+ private RadioGroup rbGroupPaymentOptionsW ;
32
+
33
+ private SharedPreferences sharedPreferences ;
34
+ private int newAmount ;
35
+ private EditText enterAmountW ;
36
+ private TextView finalAmountW ;
37
+ private int edtWalletAmount =0 ;
38
+ int additionalAmount =0 ;
39
+ int promoCodeAmount =10000 ;
12
40
13
41
@ Override
14
42
protected void onCreate (Bundle savedInstanceState ) {
15
43
super .onCreate (savedInstanceState );
16
44
setContentView (R .layout .activity_wallet );
17
45
18
- //Toolbar title set
46
+ // Initialize SharedPreferences
47
+ sharedPreferences = getSharedPreferences ("MyWalletPrefs" , Context .MODE_PRIVATE );
48
+
49
+ // Toolbar title set
19
50
TextView toolbarTitle = findViewById (R .id .toolbarTitle );
20
51
toolbarTitle .setText ("Wallet" );
52
+
53
+ walletBalanceW = findViewById (R .id .walletBalanceW );
54
+ finalAmountW = findViewById (R .id .finalAmountW );
55
+ enterAmountW = findViewById (R .id .enterAmountW );
56
+ CheckBox promoCheckboxW = findViewById (R .id .promoCheckboxW );
57
+ RadioButton walletPayViaRazorpayW = findViewById (R .id .walletPayViaRazorpayW );
58
+ Button btnProceedWallet = findViewById (R .id .btnProceedWallet );
59
+ rbGroupPaymentOptionsW = findViewById (R .id .rbGroupPaymentOptionsW );
60
+
61
+ // Set the wallet balance from SharedPreferences
62
+ int initialBalance = sharedPreferences .getInt (WALLET_BALANCE_KEY , 12000 );
63
+ String wallBalanceStr = String .valueOf (initialBalance );
64
+ wallBalanceStr = formatPrice (initialBalance );
65
+ walletBalanceW .setText (wallBalanceStr ); // Display the balance
66
+
67
+ // Initialize Razorpay with your API key
68
+ Checkout .preload (getApplicationContext ());
69
+ Checkout checkout = new Checkout ();
70
+ checkout .setKeyID ("rzp_test_YEExgm42Uvy0u1" );
71
+
72
+ // Add a text change listener to the EditText
73
+ enterAmountW .addTextChangedListener (new TextWatcher () {
74
+ @ Override
75
+ public void beforeTextChanged (CharSequence s , int start , int count , int after ) {
76
+ }
77
+
78
+ @ Override
79
+ public void onTextChanged (CharSequence s , int start , int before , int count ) {
80
+ // Update finalAmountW as the user types or modifies the amount
81
+ updateFinalAmount (s .toString ());
82
+ }
83
+
84
+ @ Override
85
+ public void afterTextChanged (Editable s ) {
86
+ }
87
+ });
88
+
89
+ // Add a listener for the promoCheckbox
90
+ promoCheckboxW .setOnCheckedChangeListener (new CompoundButton .OnCheckedChangeListener () {
91
+ @ Override
92
+ public void onCheckedChanged (CompoundButton buttonView , boolean isChecked ) {
93
+ try {
94
+ int enteredAmount = Integer .parseInt (enterAmountW .getText ().toString ());
95
+ if (isChecked ) {
96
+ enteredAmount += 10000 ;
97
+ }
98
+ // Calculate the new amount in paise
99
+ newAmount = enteredAmount * 100 ;
100
+ updateFinalAmount (Integer .toString (enteredAmount ));
101
+ } catch (NumberFormatException e ) {
102
+ Toast .makeText (getApplicationContext (), "Invalid amount entered." , Toast .LENGTH_SHORT ).show ();
103
+ }
104
+ }
105
+ });
106
+ enterAmountW .addTextChangedListener (new TextWatcher () {
107
+ @ Override
108
+ public void beforeTextChanged (CharSequence s , int start , int count , int after ) {
109
+ }
110
+
111
+ @ Override
112
+ public void onTextChanged (CharSequence s , int start , int before , int count ) {
113
+ try {
114
+ int enteredAmount = Integer .parseInt (s .toString ());
115
+ if (promoCheckboxW .isChecked ()) {
116
+ enteredAmount += 10000 ;
117
+ }
118
+ // Calculate the new amount in paise
119
+ newAmount = enteredAmount * 100 ;
120
+ updateFinalAmount (Integer .toString (enteredAmount ));
121
+ } catch (NumberFormatException e ) {
122
+ Toast .makeText (getApplicationContext (), "Invalid amount entered." , Toast .LENGTH_SHORT ).show ();
123
+ }
124
+ }
125
+
126
+ @ Override
127
+ public void afterTextChanged (Editable s ) {
128
+ }
129
+ });
130
+ int selectedRadioButtonId = rbGroupPaymentOptionsW .getCheckedRadioButtonId ();
131
+
132
+ if (selectedRadioButtonId == R .id .walletPayViaRazorpayW ) {
133
+
134
+ int amountInPaise = edtWalletAmount * 100 ;
135
+ additionalAmount =edtWalletAmount ;
136
+ newAmount = amountInPaise ; // Store the amount for later use
137
+
138
+ // Calculate the final amount
139
+ int finalAmount = amountInPaise ;
140
+
141
+ // Update the final amount TextView
142
+ finalAmountW .setText (formatPrice (finalAmount ));
143
+ }
144
+
145
+ btnProceedWallet .setOnClickListener (new View .OnClickListener () {
146
+ @ Override
147
+ public void onClick (View view ) {
148
+ String enteredValue = enterAmountW .getText ().toString ();
149
+ if (!enteredValue .isEmpty ()) {
150
+ try {
151
+ edtWalletAmount = Integer .parseInt (enteredValue );
152
+ if (promoCheckboxW .isChecked ()) {
153
+ edtWalletAmount += promoCodeAmount ;
154
+ }
155
+ int amountInPaise = edtWalletAmount * 100 ;
156
+ newAmount = amountInPaise ;
157
+ // Continue with the rest of your code to open Razorpay with the newAmount
158
+ } catch (NumberFormatException e ) {
159
+ Toast .makeText (getApplicationContext (), "Invalid amount entered." , Toast .LENGTH_SHORT ).show ();
160
+ }
161
+ } else {
162
+ Toast .makeText (getApplicationContext (), "Please enter an amount." , Toast .LENGTH_SHORT ).show ();
163
+ }
164
+ try {
165
+ // You need to pass a JSONObject with payment details to Razorpay
166
+ JSONObject options = new JSONObject ();
167
+ options .put ("name" , "BugBazaar Private Limited" ); // Replace with your company name
168
+ options .put ("description" , "Add money to wallet" );
169
+ options .put ("currency" , "INR" ); // Replace with the appropriate currency code
170
+ Log .d ("newAmount2" ,String .valueOf (newAmount ));
171
+ options .put ("amount" , newAmount ); // Amount should be in paise
172
+ options .put ("prefill.email" , "customer@example.com" );
173
+ options .put ("prefill.contact" , "1234567890" );
174
+
175
+ // Callback URL (optional, can be used for handling payment success or failure)
176
+ // options.put("callback_url", "your_callback_url");
177
+ checkout .open (Wallet .this , options );
178
+
179
+ } catch (Exception e ) {
180
+ e .printStackTrace ();
181
+ }
182
+ }
183
+ });
21
184
}
22
185
23
- //Code to handle backbutton
186
+ public void onPaymentSuccess (String s ) {
187
+ // Clear the EditText after a successful payment
188
+ EditText enterAmountEditText = findViewById (R .id .enterAmountW );
189
+ enterAmountEditText .setText ("" );
190
+ enterAmountEditText .clearFocus ();
191
+ CheckBox promoCheckboxW =findViewById (R .id .promoCheckboxW );
192
+ promoCheckboxW .clearFocus ();
193
+ promoCheckboxW .setChecked (false );
194
+
195
+ // Retrieve the current wallet balance from SharedPreferences
196
+ int currentBalance = sharedPreferences .getInt (WALLET_BALANCE_KEY , 12000 );
197
+
198
+ // Calculate the new wallet balance
199
+ int updatedBalance = currentBalance + edtWalletAmount ;
200
+
201
+
202
+ // Save the updated wallet balance back to SharedPreferences
203
+ SharedPreferences .Editor editor = sharedPreferences .edit ();
204
+ editor .putInt (WALLET_BALANCE_KEY , updatedBalance );
205
+ editor .apply ();
206
+
207
+ // Update the displayed wallet balance
208
+ walletBalanceW .setText (formatPrice (updatedBalance ));
209
+
210
+ Toast .makeText (this , "Added money to the wallet." , Toast .LENGTH_SHORT ).show ();
211
+ }
212
+
213
+ public void onPaymentError (int code , String response ) {
214
+ // Clear the EditText after a successful payment
215
+ EditText enterAmountEditText = findViewById (R .id .enterAmountW );
216
+ enterAmountEditText .setText ("" );
217
+
218
+ switch (code ) {
219
+ case Checkout .NETWORK_ERROR :
220
+ Toast .makeText (this , "Network Error: Please check the internet connection." , Toast .LENGTH_LONG ).show ();
221
+ break ;
222
+ case Checkout .INVALID_OPTIONS :
223
+ // Handle invalid payment options
224
+ break ;
225
+ case Checkout .PAYMENT_CANCELED :
226
+ Toast .makeText (this , "Payment Error: Payment Canceled by the user." , Toast .LENGTH_LONG ).show ();
227
+ break ;
228
+ // Add more cases for specific error codes as needed
229
+ }
230
+ }
231
+
232
+ private String formatPrice (int price ) {
233
+ return String .format ("₹%,d" , price );
234
+ }
235
+
236
+ // Code to handle the back button
24
237
public void onBackButtonClick (View view ) {
25
238
onBackPressed (); // Navigate back to the previous activity
26
239
}
27
- }
240
+
241
+ // Update finalAmountW with the entered amount
242
+ private void updateFinalAmount (String enteredValue ) {
243
+ if (!enteredValue .isEmpty ()) {
244
+ try {
245
+ int edtWalletAmount = Integer .parseInt (enteredValue );
246
+ // Perform any calculations if needed
247
+ finalAmountW .setText (formatPrice (edtWalletAmount ));
248
+ } catch (NumberFormatException e ) {
249
+ finalAmountW .setText ("Invalid amount" );
250
+ }
251
+ } else {
252
+ finalAmountW .setText ("₹" );
253
+ }
254
+ }
255
+ }
0 commit comments