wirecardBrasil / moip-encryption-sdk-android

Cliente Android para integração com as APIs v2 Moip, possibilita a criptografia de dados sensíveis de cartão de crédito.
20 stars 6 forks source link

Can not parse json response. [MoipErrorParser.java] #2

Closed asparmar closed 9 years ago

asparmar commented 9 years ago

I am trying to test Moip Android SDK in my android app. Below is my code.

Moip.createPayment(payment, new MoipCallback<Payment>() {
                    @Override
                    public void success(final Payment payment) {
                        Log.d("MOIP", "Payment success.....");
                        if (progressDialog != null){ try { progressDialog.dismiss(); }catch (Exception itsOkay){} }
                        new SavePaymentTask(getActivity(), ticketPayment).execute();
                    }

                    @Override
                    public void failure(final List<MoipError> moipErrorList) {
                        Log.e("MOIP", "Payment failed.....");
                        for (MoipError moipError : moipErrorList){
                            Log.d("MOIP", "ERR.getCode: " + moipError.getCode());
                            Log.d("MOIP", "ERR.getDescription: " + moipError.getDescription());
                            Log.d("MOIP", "ERR.getPath: " + moipError.getPath());
                            Log.d("MOIP", "ERR.moipError: " + moipError.toString());
                        }
                        if (progressDialog != null){ try { progressDialog.dismiss(); }catch (Exception itsOkay){} }
                    }
                });

When I ececute this code, failure callback method is called with all null values. And I am getting following in the log cat.

  E/Error while decoding﹕ 404 feign.Response$InputStreamBody@41f0ac38
 W/System.err﹕ org.json.JSONException: Value Not of type java.lang.String cannot be converted to JSONObject
 W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
 W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:158)
 W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:171)
 W/System.err﹕ at br.com.moip.sdk.parser.MoipErrorParser.fromString(MoipErrorParser.java:51)
 W/System.err﹕ at br.com.moip.sdk.Moip.createPayment(Moip.java:83)
 W/System.err﹕ at com.blackfoxweb.colaxi.CheckoutFragment$MoipPaymentTask.doInBackground(CheckoutFragment.java:354)
 W/System.err﹕ at com.blackfoxweb.colaxi.CheckoutFragment$MoipPaymentTask.doInBackground(CheckoutFragment.java:334)
 W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
 W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
 W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
 W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
 I/Errors﹕ Not Found
 E/MOIP﹕ Payment failed.....
 E/MOIP﹕ ERR.getCode: null
 E/MOIP﹕ ERR.getDescription: null
 E/MOIP﹕ ERR.getPath: null
 E/MOIP﹕ ERR.moipError: MoipError{code='null', path='null', description='null'}

I guess the MoipError object received in response contains a string which can not be converted to JSONObject. Is there any other way to see what response we are getting in the failure() method?

caueferreira commented 9 years ago

I seems you're no passing a valid Order. Are you creating an Order before creating a Payment? In order to create a Payment you must create an Order and add it id to the Payment, although I was not aware you wasn't receiving an error description.
We will see it fixed.

asparmar commented 9 years ago

Ohh yes. I was testing without setting order to the payment.

However still I am confused on how to set order id to the payment. Should I use setMoipOrderID() method? If yes how?

I have tried the following but getting the same result. Please help me in resolving this.

On view click:-

    try {
        Moip.initialize(getActivity());
        Moip.Environment(Environment.SANDBOX);
        //Moip.AppInfo(APP_ID, APP_SECRET);
        Moip.BasicAuth(TOKEN, KEY);
        Moip.PublicKey(PUBLIC_KEY);

        String moipOrderID = "1";

        Amount amount = new Amount();
        amount.setCurrency(Currency.BRL);

        amount.setTotal((long)ticketPayment.getTotal_amount());
        Log.d(MOIP_TAG, "amount.getTotal(): " + amount.getTotal());

        Order order = new Order();
        order.setOwnId(moipOrderID);
        order.setId(moipOrderID);
        ArrayList<Item> orderItems = new ArrayList<Item>();
        Item orderItem;
        for (Ticket ticket : purchasedTickets){
            orderItem = new Item();
            orderItem.setDetail(ticket.getTicket_type());
            orderItem.setPrice((int)ticket.getPrice());
            orderItem.setProduct(String.valueOf(ticket.getEvent_id()));
            orderItem.setQuantity(ticket.getNo_of_tickets());
            orderItems.add(orderItem);
            Log.d(MOIP_TAG, "Added item: ");
            Log.d(MOIP_TAG, "orderItem.getDetail(): " + orderItem.getDetail());
            Log.d(MOIP_TAG, "orderItem.getPrice(): " + orderItem.getPrice());
            Log.d(MOIP_TAG, "orderItem.getProduct(): " + orderItem.getProduct());
            Log.d(MOIP_TAG, "orderItem.getQuantity(): " + orderItem.getQuantity());
        }
        order.setItems(orderItems);
        order.setAmount(amount);

        CreditCard creditCard = new CreditCard();

        creditCard.setExpirationMonth(this.expMonth);
        Log.d(MOIP_TAG, "creditCard.getExpirationMonth(): " + creditCard.getExpirationMonth());

        creditCard.setExpirationYear(this.expYear);
        Log.d(MOIP_TAG, "creditCard.getExpirationYear(): " + creditCard.getExpirationYear());

        creditCard.setNumber(ticketPayment.getCredit_card_no());
        Log.d(MOIP_TAG, "creditCard.getNumber(): " + creditCard.getNumber());

        creditCard.setCvc(ticketPayment.getCvv_no());
        Log.d(MOIP_TAG, "creditCard.getCvc(): " + creditCard.getCvc());

        Holder holder = new Holder();
        holder.setFullname(userNameTextView.getText().toString());
        Log.d(MOIP_TAG, "holder.getFullname(): " + holder.getFullname());

        creditCard.setHolder(holder);

        FundingInstrument fundingInstrument = new FundingInstrument();
        fundingInstrument.setCreditCard(creditCard);
        fundingInstrument.setMethod("CREDIT_CARD");

        final Payment payment = new Payment();
        payment.setMoipOrderID(moipOrderID);
        Log.d(MOIP_TAG, "payment.getMoipOrderID(): " + payment.getMoipOrderID());
        payment.setInstallmentCount(1);
        payment.setAmount(amount);

        payment.setFundingInstrument(fundingInstrument);

        new MoipPaymentTask(payment, ticketPayment).execute();

    } catch (Exception e) {
        Log.e(MOIP_TAG, "Exception: " , e);
    } catch (java.lang.ExceptionInInitializerError e) {
        Log.e(MOIP_TAG, "ExceptionInInitializerError: " , e);
    }

MoipPaymentTask class:-

    private class MoipPaymentTask extends AsyncTask<Void, Void, Void>{
        private Payment payment;
        private TicketPayment ticketPayment;
        private ProgressDialog progressDialog;

        public MoipPaymentTask(Payment payment, TicketPayment ticketPayment){
            this.payment = payment;
            this.ticketPayment = ticketPayment;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = MainaActivity.getProgressDialog(getActivity(), "", "Processing payment.....");
            progressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            try{
                Moip.createPayment(payment, new MoipCallback<Payment>() {
                    @Override
                    public void success(final Payment payment) {
                        Log.e("MOIP", "Payment success.....");
                        if (progressDialog != null){ try { progressDialog.dismiss(); }catch (Exception itsOkay){} }
                        new SavePaymentTask(getActivity(), ticketPayment).execute();
                    }

                    @Override
                    public void failure(final List<MoipError> moipErrorList) {
                        Log.e("MOIP", "Payment failed.....");
                        for (MoipError moipError : moipErrorList){
                            Log.e("MOIP", "ERR.getCode: " + moipError.getCode());
                            Log.e("MOIP", "ERR.getDescription: " + moipError.getDescription());
                            Log.e("MOIP", "ERR.getPath: " + moipError.getPath());
                            Log.e("MOIP", "ERR.moipError: " + moipError.toString());
                        }
                        if (progressDialog != null){ try { progressDialog.dismiss(); }catch (Exception itsOkay){} }
                    }
                });
            }
            catch (Throwable throwable){
                Log.e("MoipPaymentTask", "doInBackground", throwable);
                if (progressDialog != null){ try { progressDialog.dismiss(); }catch (Exception itsOkay){} }
            }
            return null;
        }
    }

Logcat :-

 D/MOIP﹕ amount.getTotal(): 100
 D/MOIP﹕ Added item:
 D/MOIP﹕ orderItem.getDetail(): vip
 D/MOIP﹕ orderItem.getPrice(): 100
 D/MOIP﹕ orderItem.getProduct(): 1
 D/MOIP﹕ orderItem.getQuantity(): 1
 D/MOIP﹕ creditCard.getExpirationMonth(): 2
 D/MOIP﹕ creditCard.getExpirationYear(): 2020
 D/MOIP﹕ creditCard.getNumber(): 4111111111111111
 D/MOIP﹕ creditCard.getCvc(): 123
 D/MOIP﹕ holder.getFullname(): Ashoka Morya
 D/MOIP﹕ payment.getMoipOrderID(): 1
 D/ProgressBar﹕ setProgress = 0
 D/ProgressBar﹕ setProgress = 0, fromUser = false
 D/ProgressBar﹕ mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000
 D/ProgressBar﹕ setProgressDrawable mProgressDrawable = null, d = android.graphics.drawable.BitmapDrawable@42553d40needUpdate = false
 D/ProgressBar﹕ setProgressDrawable drawableHeight = 72
 D/ProgressBar﹕ updateDrawableBounds: left = 0
 D/ProgressBar﹕ updateDrawableBounds: top = 0
 D/ProgressBar﹕ updateDrawableBounds: right = 72
 D/ProgressBar﹕ updateDrawableBounds: bottom = 72
 D/ProgressBar﹕ updateDrawableBounds: mProgressDrawable.setBounds()
 D/AbsListView﹕ unregisterIRListener() is called
 E/Error while decoding﹕ 404 feign.Response$InputStreamBody@421076f8
 W/System.err﹕ org.json.JSONException: Value Not of type java.lang.String cannot be converted to JSONObject
 W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111)
 W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:158)
 W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:171)
 W/System.err﹕ at br.com.moip.sdk.parser.MoipErrorParser.fromString(MoipErrorParser.java:51)
 W/System.err﹕ at br.com.moip.sdk.Moip.createPayment(Moip.java:83)
 W/System.err﹕ at com.blackfoxweb.colaxi.CheckoutFragment$MoipPaymentTask.doInBackground(CheckoutFragment.java:352)
 W/System.err﹕ at com.blackfoxweb.colaxi.CheckoutFragment$MoipPaymentTask.doInBackground(CheckoutFragment.java:332)
 W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
 W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
 W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
 W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
 I/Errors﹕ Not Found
 E/MOIP﹕ Payment failed.....
 E/MOIP﹕ ERR.getCode: null
 E/MOIP﹕ ERR.getDescription: null
 E/MOIP﹕ ERR.getPath: null
 E/MOIP﹕ ERR.moipError: MoipError{code='null', path='null', description='null'}
caueferreira commented 9 years ago

I've updated the SDK if you can, please download it.

The Order must be created at Moip and not only at your app. To do so, you can use this method:

order = Moip.createMyOrder(order, "https://endpointdomeuecommerce.com/pedidos");

As for your safety, as well to ensure your business receive the Order you are creating, you should create the Order at Moip from your server, sending the json received to https://test.moip.com.br/v2/orders using the same authentication you use in your sdk, and then returning the Order to your app.

This step is needed to have a reliable communiction between you and Moip, and to make sure that the Order's data is the same as you sent to us.

asparmar commented 9 years ago

Okay. I created Order using the following method.

Moip.createOrder();

Still got some errors.

Finally I ended up by creating all the entities mentioned in readme (i.e. Customer, TaxDocument, Phone, ShippingAddress etc.) with dummy data and set to Order and Payment. Now it is working fine.

Is it necessary to create all these to perform payment transaction successfully? Can't we create Payment by just setting the Card and Holder to it? Customer, TaxDocument, Phone, ShippingAddress, BillingAddress should be optional.

caueferreira commented 9 years ago

As we solved the issue, I'm closing this issue. I'm sending you an email so we can further discuss your needs.