alfredoqt / flutter_conekta

Flutter plugin for Conekta, enabling Android and iOS card tokenization.
MIT License
3 stars 8 forks source link

Can't catch error on failed tokenization #2

Open mauriciorzm opened 5 years ago

mauriciorzm commented 5 years ago

When trying to tokenize a Card if it doesn't succeed I only get and the execution of the method the tokenization was called on ends, having no way to handle the exception.

E/Volley ( 7990): [296] BasicNetwork.performRequest: Unexpected response code 422 for https://api.conekta.io/tokens

                 await FlutterConekta.tokenizeCard(
                    publicKey: Constants.conektaPublicKey,
                    cardNumber: cardRequest.cardNumber,
                    cardholderName: cardRequest.cardHolderName,
                    expiryMonth: cardRequest.expiryMonth,
                    expiryYear: cardRequest.expiryYear,
                    cvv: cardRequest.cvv)
                    .then((token) => {
                      print(token)
                    })
                    .catchError((a) => {
                      print("Error")
                    });

                  print("Continue");

In the code above if the tokenizations succeeds the then statement is reached and the parent method execution continues, while if it doesn't get tokenized, the execution of the parent method gets terminated and the print("Continue") is never reached.

joseluisg1 commented 5 years ago

Tengo el mismo problema, el error BE/Volley ( 7990): [296] BasicNetwork.performRequest: Unexpected response code 422 for https://api.conekta.io/tokens no puede ser capturado de forma correcta y termina la ejecución. Una mejora considerable sería poder capturarlo con un catch

alfredoqt commented 5 years ago

Hello, I will fix this issue tomorrow.

joseluisg1 commented 5 years ago

I handled to making work following conekta SDK documentation and with some native code, here is my java main file:

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import android.app.Activity;
import android.util.Log;
import io.conekta.conektasdk.Conekta;
import io.conekta.conektasdk.Card;
import io.conekta.conektasdk.Token;
import org.json.JSONObject;
import java.util.Map;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;

public class MainActivity extends FlutterActivity {
  private static final String CHANNEL = "example/conektaSDK";
  private Activity activity = this;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
      new MethodCallHandler() {
        @Override
        public void onMethodCall(MethodCall call, Result result) {
            if (call.method.equals("tokenizeCard")) {
                handleTokenizeCard(call, result);
            } else {
                result.notImplemented();
            }
        }
      });
  }

 private void handleTokenizeCard(MethodCall call, final Result result) {
    Map<String, String> arguments = call.arguments();
    String publicKey = arguments.get("publicKey");
    String cardholderName = arguments.get("cardholderName");
    String cardNumber = arguments.get("cardNumber");
    String cvv = arguments.get("cvv");
    String expiryMonth = arguments.get("expiryMonth");
    String expiryYear = arguments.get("expiryYear");

    Conekta.setPublicKey(publicKey);

    Card card = new Card(cardholderName, cardNumber, cvv, expiryMonth, expiryYear);

    Token token = new Token(activity);

    token.onCreateTokenListener(new Token.CreateToken() {
      @Override
      public void onCreateTokenReady(JSONObject data) {
        try {
          result.success(data.getString("id"));
        } catch (Exception err) {
          result.error("ERROR_UNABLE_TO_TOKENIZE", err.getMessage(), null);
        }
      }
    });

    token.create(card);
  }
}

Now I can try-catch this: (flutterClass.dart)

static const platform = const MethodChannel('example/conektaSDK');

try{
final String result = await platform.invokeMethod('tokenizeCard', {
          "cardholderName": _name,
          "cardNumber": _number,
          "cvv": _cvc,
          "expiryMonth": _expMonth,
          "expiryYear": _expYear,
          "publicKey": tokenConekta,
        });
}
catch(e){
 //do something
}

I don't know if it helps but this is how I made it work.

RaulUrtecho commented 3 years ago

@alfredoqt
Hasn't this problem been fixed yet? It does not throw me an exception when the call is made without internet connection.

RaulUrtecho commented 3 years ago

Saludos nuevamente @alfredoqt, han pasado 6 meses y aun no veo algún mantenimiento en este plug-in, o es que ya no piensas darle mas seguimiento?

Me encantaría ayudar pero nativo no es mi fuerte.

Por favor si encuentras un tiempo libre un fin de semana para este plug-in seria genial.

De ser posible también migrarlo a null-safety esa parte no creo sea difícil (desde que todo el SDK en Dart solo es un método).