我已经在我的应用程序中实现了mercadopago模块,在这个模块中,从acitivity调用它是没有问题的,但是当我想在没有调用onActivityResult的片段上实现它时,我认为这是由于传递给它的上下文所致,但仍然无法调用
private void realizarPago() {
try {
JSONArray items = new JSONArray();
JSONObject detalle_linea_items = new JSONObject();
detalle_linea_items.put("id", "1234");
detalle_linea_items.put("title", "Servicio");
detalle_linea_items.put("description", "Servicio Express");
//detalle_linea_items.put("category_id", "others");
detalle_linea_items.put("quantity", 1);
//detalle_linea_items.put("currency_id", "MPE");
detalle_linea_items.put("currency_id", "PEN");
//detalle_linea_items.put("unit_price", 10.0);
detalle_linea_items.put("unit_price", Double.parseDouble(costoEnvioText.replace(",", ".")));
items.put(detalle_linea_items);
objetoCabecera.put("items", items);
JSONObject payer = new JSONObject();
payer.put("name", nombreUsuario.getText().toString());
payer.put("surname", apellidosUsuario.getText().toString());
payer.put("email", correoUsuario.getText().toString());
objetoCabecera.put("payer", payer);
JSONArray paymentArray = new JSONArray();
JSONObject detallePayment = new JSONObject();
detallePayment.put("id", "prepaid_card");
detallePayment.put("id", "atm");
detallePayment.put("id", "ticket");
detallePayment.put("id", "account_money");
detallePayment.put("id", "bank_transfer");
detallePayment.put("id", "pagoefectivo_atm");
paymentArray.put(detallePayment);
JSONArray paymentArray1 = new JSONArray();
JSONObject detallePayment1 = new JSONObject();
detallePayment1.put("id", "account_money");
detallePayment1.put("id", "pagoefectivo_atm");
paymentArray1.put(detallePayment1);
JSONObject payment = new JSONObject();
payment.put("excluded_payment_methods", paymentArray1);
//payment.put("excluded_payment_types", paymentArray);
objetoCabecera.put("payment_methods", payment);
} catch (JSONException e) {
e.printStackTrace();
}
RequestQueue requestQueue = Volley.newRequestQueue(view.getContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, rutaMercadoPago + tokenMercadoPago, objetoCabecera, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// do something...
Log.d("jaja", "Response " + response);
try {
Log.d("jaja", "response JSONObject: "+response);
String checkoutPreferenceId = response.getString("id");
new MercadoPagoCheckout.Builder(publicKeyMercadoPago, checkoutPreferenceId).build().startPayment(getContext(), REQUEST_CODE);
} catch (JSONException e) {
Log.d("jaja", "Error " + e.toString() + " / " + e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// do something...
Log.d("jaja", "Error " + error.toString() + " / " + error.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + rutaMercadoPago + tokenMercadoPago);
headers.put("Content-Type", "application/json");
return headers;
}
};
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
8000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(jsonObjectRequest);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("jaja", "Entro aquiiiiii request " + requestCode + " / " + resultCode);
if (requestCode == REQUEST_CODE) {
Log.d("jaja", "Entro aquiiiiii");
if (resultCode == MercadoPagoCheckout.PAYMENT_RESULT_CODE) {
final Payment payment = (Payment) data.getSerializableExtra(MercadoPagoCheckout.EXTRA_PAYMENT_RESULT);
Toast.makeText(getApplicationContext(), "PAGO REALIZADO", Toast.LENGTH_LONG).show();
Log.d("jaja", "Monto pagado " + payment.toString() + " / " + payment.getCurrencyId());
paymentId = payment.getCurrencyId();
//Done!
} else if (resultCode == RESULT_CANCELED) {
Log.d("jaja", "Acaaaa cancelo");
if (data != null && data.getExtras() != null
&& data.getExtras().containsKey(MercadoPagoCheckout.EXTRA_ERROR)) {
final MercadoPagoError mercadoPagoError =
(MercadoPagoError) data.getSerializableExtra(MercadoPagoCheckout.EXTRA_ERROR);
Toast.makeText(getApplicationContext(), "ERROR: " + mercadoPagoError.getMessage(), Toast.LENGTH_LONG).show();
Log.d("jaja", "Error en el pago " + mercadoPagoError.toString() + " / " + mercadoPagoError.toString());
//Resolve error in checkout
} else {
Log.d("jaja", "Error en el pago DATA " + data);
//Resolve canceled checkout
}
} else {
Log.d("jaja", "Error aqui " + MercadoPagoCheckout.PAYMENT_RESULT_CODE);
}
}
}发布于 2021-10-19 03:33:37
Después de muchos intentos,y consultas con Mercado pago,obtuve la respuesta de que no es es posible obtener una de su modulo en un fragment,asíque procedía generar un atajo,es decir,para que genere el Pago,coloque un botón en el fragment,el cual me lleva un obtuve y el obtuve recién se genera la solicitud de pago sin problemas。
https://stackoverflow.com/questions/69320665
复制相似问题