58 public View
onCreateView(LayoutInflater inflater, ViewGroup container,
59 Bundle savedInstanceState) {
61 View view = inflater.inflate(R.layout.fragment_success, container,
false);
64 textMessage = view.findViewById(R.id.textView);
65 Button button = view.findViewById(R.id.button);
68 ViewGroup ticketLayout =
new android.widget.LinearLayout(requireContext());
69 ((android.widget.LinearLayout) ticketLayout).setOrientation(android.widget.LinearLayout.VERTICAL);
70 ticketLayout.setPadding(32, 32, 32, 32);
71 ticketLayout.setBackgroundResource(android.R.color.white);
72 ticketLayout.setElevation(8f);
73 ticketLayout.setClickable(
true);
74 ticketLayout.setFocusable(
true);
75 ticketLayout.setForeground(requireContext().getDrawable(android.R.drawable.list_selector_background));
76 android.widget.TextView tvTitulo =
new android.widget.TextView(requireContext());
77 tvTitulo.setText(
"Ticket de compra");
78 tvTitulo.setTextSize(18);
79 tvTitulo.setTextColor(android.graphics.Color.BLACK);
80 tvTitulo.setTypeface(
null, android.graphics.Typeface.BOLD);
81 ticketLayout.addView(tvTitulo);
83 String fecha =
new java.text.SimpleDateFormat(
"dd/MM/yyyy HH:mm").format(
new java.util.Date());
84 android.widget.TextView tvFecha =
new android.widget.TextView(requireContext());
85 tvFecha.setText(
"Fecha: " + fecha);
86 tvFecha.setTextColor(android.graphics.Color.DKGRAY);
87 ticketLayout.addView(tvFecha);
89 int nroPedido = (int) (Math.random() * 90000 + 10000);
90 android.widget.TextView tvNro =
new android.widget.TextView(requireContext());
91 tvNro.setText(
"N° Pedido: " + nroPedido);
92 tvNro.setTextColor(android.graphics.Color.DKGRAY);
93 ticketLayout.addView(tvNro);
95 android.widget.TextView tvPago =
new android.widget.TextView(requireContext());
97 if (paymentMethod !=
null) {
98 String paymentMethodLower = paymentMethod.toLowerCase();
99 if (paymentMethodLower.contains(
"mercado") || paymentMethodLower.equals(
"mercadopago")) {
100 metodo =
"Mercado Pago";
101 }
else if (paymentMethodLower.contains(
"paypal") || paymentMethodLower.equals(
"pay pal")) {
103 }
else if (paymentMethodLower.contains(
"credit") || paymentMethodLower.contains(
"tarjeta") ||
104 paymentMethodLower.contains(
"card")) {
105 metodo =
"Tarjeta de crédito";
107 metodo = paymentMethod;
112 tvPago.setText(
"Pago: " + metodo);
113 tvPago.setTextColor(android.graphics.Color.DKGRAY);
114 ticketLayout.addView(tvPago);
116 android.widget.TextView tvTotal =
new android.widget.TextView(requireContext());
117 String totalSimulado = String.valueOf((
int)(Math.random()*2000+2000));
118 tvTotal.setText(
"Total: $ " + totalSimulado);
119 tvTotal.setTextColor(android.graphics.Color.DKGRAY);
120 ticketLayout.addView(tvTotal);
122 android.view.View sep =
new android.view.View(requireContext());
123 sep.setBackgroundColor(android.graphics.Color.LTGRAY);
124 sep.setLayoutParams(
new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, 2));
125 ticketLayout.addView(sep);
127 android.widget.TextView tvGracias =
new android.widget.TextView(requireContext());
128 tvGracias.setText(
"¡Gracias por tu compra!");
129 tvGracias.setTextColor(android.graphics.Color.parseColor(
"#388E3C"));
130 tvGracias.setTextSize(16);
131 tvGracias.setPadding(0, 16, 0, 0);
132 ticketLayout.addView(tvGracias);
134 FrameLayout ticketContainer = view.findViewById(R.id.ticket_container);
135 ticketContainer.removeAllViews();
136 ticketContainer.addView(ticketLayout);
138 if (paymentMethod !=
null && paymentMethod.equalsIgnoreCase(
"mercadopago")) {
139 textMessage.setText(
"¡Compra Finalizada con Éxito!\n\nTu pago con MercadoPago ha sido procesado correctamente.");
141 textMessage.setText(
"¡Compra Finalizada con Éxito!");
145 button.setOnClickListener(
new View.OnClickListener() {
147 public void onClick(View v) {
150 if (getActivity() instanceof com.example.food_front.MainActivity) {
151 ((com.example.food_front.MainActivity) getActivity()).mostrarHomeLimpiandoBackStack();
154 FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
155 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
156 fragmentTransaction.replace(R.id.fragment_container_view, new HomeFragment());
157 fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
158 fragmentTransaction.commit();
160 } catch (Exception e) {
161 Log.e(TAG,
"Error al navegar al HomeFragment: " + e.getMessage());
162 Toast.makeText(requireContext(),
"Error al volver a inicio. Intente nuevamente.", Toast.LENGTH_SHORT).show();
167 String token = sessionManager.getToken();
170 public void onSuccess(org.json.JSONArray pedidos) {
171 if (pedidos.length() > 0) {
173 org.json.JSONObject ultimoPedido = pedidos.getJSONObject(0);
174 StringBuilder productosBuilder = new StringBuilder();
175 if (ultimoPedido.has(
"detalles")) {
176 org.json.JSONArray detalles = ultimoPedido.getJSONArray(
"detalles");
177 for (int i = 0; i < detalles.length(); i++) {
178 org.json.JSONObject detalle = detalles.getJSONObject(i);
179 String nombre = detalle.optString(
"nombre_producto",
"Producto");
180 int cantidad = detalle.optInt(
"cantidad_productos", 1);
181 productosBuilder.append(
"- ").append(nombre).append(
" x").append(cantidad).append(
"\n");
184 String productos = productosBuilder.toString().trim();
186 ticketLayout.setOnClickListener(new View.OnClickListener() {
188 public void onClick(View v) {
189 TicketDetailDialogFragment dialog = TicketDetailDialogFragment.newInstance(
191 String.valueOf(nroPedido),
196 dialog.show(getParentFragmentManager(),
"TicketDetailDialog");
199 } catch (Exception e) {
201 setTicketClickDefault(ticketLayout, fecha, nroPedido, metodo, totalSimulado);
205 setTicketClickDefault(ticketLayout, fecha, nroPedido, metodo, totalSimulado);
209 public void onError(String error) {
211 setTicketClickDefault(ticketLayout, fecha, nroPedido, metodo, totalSimulado);