pinpad is getting signal and amount from websocket and navigate to PaymentActivity
This commit is contained in:
parent
a75f5b9a6f
commit
30055a1a14
|
@ -16,12 +16,10 @@ import com.dspread.pos_android_app.databinding.FragmentPinpadBinding;
|
||||||
import me.goldze.mvvmhabit.utils.ToastUtils;
|
import me.goldze.mvvmhabit.utils.ToastUtils;
|
||||||
|
|
||||||
public class PinpadFragment extends BaseFragment<FragmentPinpadBinding, PinpadViewModel> implements TitleProviderListener {
|
public class PinpadFragment extends BaseFragment<FragmentPinpadBinding, PinpadViewModel> implements TitleProviderListener {
|
||||||
|
|
||||||
private static final String TAG = "PinpadFragment";
|
private static final String TAG = "PinpadFragment";
|
||||||
private boolean canShow = true;
|
private boolean canShow = true;
|
||||||
private CountDownTimer showTimer;
|
private CountDownTimer showTimer;
|
||||||
private String amountToPass;
|
private String amountToPass;
|
||||||
private CountDownTimer autoNavigationTimer;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int initContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public int initContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
|
@ -48,8 +46,7 @@ public class PinpadFragment extends BaseFragment<FragmentPinpadBinding, PinpadVi
|
||||||
Log.d(TAG, "No arguments, using default amount: " + amountToPass);
|
Log.d(TAG, "No arguments, using default amount: " + amountToPass);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up auto-navigation after 10 seconds
|
// REMOVED: Auto-navigation timer - we'll use signal instead
|
||||||
startAutoNavigationTimer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initTimer() {
|
private void initTimer() {
|
||||||
|
@ -64,25 +61,7 @@ public class PinpadFragment extends BaseFragment<FragmentPinpadBinding, PinpadVi
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startAutoNavigationTimer() {
|
private void navigateToPayment(String amount) {
|
||||||
autoNavigationTimer = new CountDownTimer(10000, 1000) { // 10 seconds
|
|
||||||
@Override
|
|
||||||
public void onTick(long millisUntilFinished) {
|
|
||||||
// Update UI with countdown
|
|
||||||
long secondsRemaining = millisUntilFinished / 1000;
|
|
||||||
viewModel.updateCountdown(secondsRemaining);
|
|
||||||
Log.d(TAG, "Countdown: " + secondsRemaining + " seconds");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFinish() {
|
|
||||||
Log.d(TAG, "Auto-navigation timer finished, navigating to payment");
|
|
||||||
navigateToPayment();
|
|
||||||
}
|
|
||||||
}.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void navigateToPayment() {
|
|
||||||
if (!canShow) {
|
if (!canShow) {
|
||||||
Log.d(TAG, "Navigation blocked - canShow is false");
|
Log.d(TAG, "Navigation blocked - canShow is false");
|
||||||
return;
|
return;
|
||||||
|
@ -102,10 +81,10 @@ public class PinpadFragment extends BaseFragment<FragmentPinpadBinding, PinpadVi
|
||||||
canShow = false;
|
canShow = false;
|
||||||
showTimer.start();
|
showTimer.start();
|
||||||
|
|
||||||
Log.d(TAG, "Navigating to PaymentActivity with amount: " + amountToPass);
|
Log.d(TAG, "Navigating to PaymentActivity with amount: " + amount);
|
||||||
|
|
||||||
Intent intent = new Intent(getActivity(), PaymentActivity.class);
|
Intent intent = new Intent(getActivity(), PaymentActivity.class);
|
||||||
intent.putExtra("amount", amountToPass);
|
intent.putExtra("amount", amount);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
|
|
||||||
// Optional: add animation
|
// Optional: add animation
|
||||||
|
@ -114,8 +93,11 @@ public class PinpadFragment extends BaseFragment<FragmentPinpadBinding, PinpadVi
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initViewObservable() {
|
public void initViewObservable() {
|
||||||
// Remove this observer if you don't need manual payment start
|
// Observe the payment start event (signal-based)
|
||||||
// viewModel.paymentStartEvent.observe(this, amount -> navigateToPayment());
|
viewModel.paymentStartEvent.observe(this, amount -> {
|
||||||
|
Log.d(TAG, "Payment start signal received with amount: " + amount);
|
||||||
|
navigateToPayment(amount);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -126,21 +108,6 @@ public class PinpadFragment extends BaseFragment<FragmentPinpadBinding, PinpadVi
|
||||||
if (showTimer != null) {
|
if (showTimer != null) {
|
||||||
showTimer.cancel();
|
showTimer.cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (autoNavigationTimer != null) {
|
|
||||||
autoNavigationTimer.cancel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
Log.d(TAG, "onPause called");
|
|
||||||
|
|
||||||
// Cancel timers to prevent memory leaks
|
|
||||||
if (autoNavigationTimer != null) {
|
|
||||||
autoNavigationTimer.cancel();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,15 +1,24 @@
|
||||||
package com.dspread.pos.ui.pinpad;
|
package com.dspread.pos.ui.pinpad;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
|
import android.util.Log;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.databinding.ObservableField;
|
import androidx.databinding.ObservableField;
|
||||||
import com.dspread.pos.common.base.BaseAppViewModel;
|
import com.dspread.pos.common.base.BaseAppViewModel;
|
||||||
|
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class PinpadViewModel extends BaseAppViewModel {
|
public class PinpadViewModel extends BaseAppViewModel {
|
||||||
|
private static final String TAG = "PinpadVM";
|
||||||
|
|
||||||
|
public ObservableField<String> currencyCode = new ObservableField<>("RUB");
|
||||||
public ObservableField<String> amount = new ObservableField<>("10.00");
|
public ObservableField<String> amount = new ObservableField<>("10.00");
|
||||||
public ObservableField<String> countdown = new ObservableField<>("10");
|
public ObservableField<String> countdown = new ObservableField<>("10");
|
||||||
|
|
||||||
|
// Add this SingleLiveEvent for signal-based navigation
|
||||||
|
public SingleLiveEvent<String> paymentStartEvent = new SingleLiveEvent<>();
|
||||||
|
|
||||||
public PinpadViewModel(@NonNull Application application) {
|
public PinpadViewModel(@NonNull Application application) {
|
||||||
super(application);
|
super(application);
|
||||||
}
|
}
|
||||||
|
@ -33,4 +42,56 @@ public class PinpadViewModel extends BaseAppViewModel {
|
||||||
return "0.00";
|
return "0.00";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPaymentRedirected(String jsonString) {
|
||||||
|
Log.d(TAG, "onPaymentRedirected - processing redirect payment");
|
||||||
|
Log.d(TAG, "jsonString" + jsonString);
|
||||||
|
|
||||||
|
try {
|
||||||
|
JSONObject results = new JSONObject(jsonString);
|
||||||
|
String status = results.optString("status", "");
|
||||||
|
|
||||||
|
switch (status) {
|
||||||
|
case "signal":
|
||||||
|
Log.d(TAG, "Signal Redirect Payment received");
|
||||||
|
|
||||||
|
// Get the amount from the signal (or use the current amount)
|
||||||
|
String signalAmount = results.optString("amount", null);
|
||||||
|
if (signalAmount != null) {
|
||||||
|
amount.set(formatAmount(signalAmount));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trigger navigation via the event
|
||||||
|
String amountToPass = results.optString("rawAmount", getRawAmount());
|
||||||
|
paymentStartEvent.postValue(amountToPass);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
Log.w(TAG, "Unknown JSON type: " + status);
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
Log.e(TAG, "Error parsing JSON", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper method to get raw amount (without formatting)
|
||||||
|
private String getRawAmount() {
|
||||||
|
try {
|
||||||
|
// Convert formatted amount back to raw cents
|
||||||
|
String formatted = amount.get();
|
||||||
|
if (formatted != null) {
|
||||||
|
double amountDouble = Double.parseDouble(formatted);
|
||||||
|
return String.valueOf((int)(amountDouble * 100));
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
Log.e(TAG, "Error converting amount back to raw", e);
|
||||||
|
}
|
||||||
|
return "1000"; // default fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method to manually trigger payment (if needed)
|
||||||
|
public void triggerPayment() {
|
||||||
|
paymentStartEvent.postValue(getRawAmount());
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -17,7 +17,7 @@
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Waiting Payment..."
|
android:text="Waiting for Payment Signal..."
|
||||||
android:textSize="20sp"
|
android:textSize="20sp"
|
||||||
android:layout_marginBottom="20dp"/>
|
android:layout_marginBottom="20dp"/>
|
||||||
|
|
||||||
|
@ -32,17 +32,16 @@
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Auto-processing in:"
|
android:text="Currency:"
|
||||||
android:textSize="16sp"
|
android:textSize="16sp"
|
||||||
android:layout_marginBottom="10dp"/>
|
android:layout_marginBottom="5dp"/>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="@{viewModel.countdown}"
|
android:text="@{viewModel.currencyCode}"
|
||||||
android:textSize="24sp"
|
android:textSize="18sp"
|
||||||
android:textStyle="bold"
|
android:textStyle="bold"/>
|
||||||
android:textColor="@android:color/holo_red_dark"/>
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</layout>
|
</layout>
|
Loading…
Reference in New Issue