首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在片段中实现mercadopago时出错,未调用onActivityResult

在片段中实现mercadopago时出错,未调用onActivityResult
EN

Stack Overflow用户
提问于 2021-09-24 20:12:49
回答 1查看 36关注 0票数 0

我已经在我的应用程序中实现了mercadopago模块,在这个模块中,从acitivity调用它是没有问题的,但是当我想在没有调用onActivityResult的片段上实现它时,我认为这是由于传递给它的上下文所致,但仍然无法调用

代码语言:javascript
复制
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);

        }
    }

}
EN

回答 1

Stack Overflow用户

发布于 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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69320665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档