jonasbark/flutter_stripe_payment

My flutter app kept crashing with the error: Java.lang.NoSuchFieldError: No field type of type Ljava/lang/String;

AyeshaIftikhar opened this issue · 1 comments

I have followed all the instructions described in the dependency documentation. I am using this dependency to integrate stripe into my flutter project and it is throwing the following error:

FATAL EXCEPTION: DefaultDispatcher-worker-1
E/AndroidRuntime( 5692): Process: com.github.love_easy_giving, PID: 5692
E/AndroidRuntime( 5692): java.lang.NoSuchFieldError: No field type of type Ljava/lang/String; in class Lcom/stripe/android/model/PaymentMethod; or its superclasses (declaration of 'com.stripe.android.model.PaymentMethod' appears in /data/app/~~VO5aj2yr8kwr96XX9YId_A==/com.github.love_easy_giving-8pPD2CBi6ztF0vTAUvqM3w==/base.apk!classes3.dex)
E/AndroidRuntime( 5692): 	at com.gettipsi.stripe.util.Converters.convertPaymentMethodToWritableMap(Converters.java:291)
E/AndroidRuntime( 5692): 	at com.gettipsi.stripe.StripeModule$9.onSuccess(StripeModule.java:385)
E/AndroidRuntime( 5692): 	at com.gettipsi.stripe.StripeModule$9.onSuccess(StripeModule.java:376)
E/AndroidRuntime( 5692): 	at com.stripe.android.ApiOperation.dispatchResult(ApiOperation.kt:40)
E/AndroidRuntime( 5692): 	at com.stripe.android.ApiOperation.access$dispatchResult(ApiOperation.kt:14)
E/AndroidRuntime( 5692): 	at com.stripe.android.ApiOperation$execute$1$1.invokeSuspend(ApiOperation.kt:33)
E/AndroidRuntime( 5692): 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/AndroidRuntime( 5692): 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
E/AndroidRuntime( 5692): 	at android.os.Handler.handleCallback(Handler.java:938)
E/AndroidRuntime( 5692): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 5692): 	at android.os.Looper.loop(Looper.java:239)
E/AndroidRuntime( 5692): 	at android.app.ActivityThread.main(ActivityThread.java:8179)
E/AndroidRuntime( 5692): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 5692): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626)
E/AndroidRuntime( 5692): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1015)
I/Process ( 5692): Sending signal. PID: 5692 SIG: 9
Lost connection to device.

I am using the following piece of code to create the to create payment:

static String apiBase = 'https://api.stripe.com/v1';
  static Uri paymentApiUrl = Uri.parse('$apiBase/payment_intents');
  static Map<String, String> headers = {
    'Authorization': 'Bearer $secret',
    'Content-Type': 'application/x-www-form-urlencoded'
  };
  static Future<StripeTransactionResponse> payWithCard({
    @required String amount,
    @required String currency,
    @required CreditCard card,
    @required String description,
    @required String name,
    @required String phone,
    @required String email,
    @required BillingAddress billingAddress,
  }) async {
    try {
      PaymentMethodRequest paymentMethodRequest = PaymentMethodRequest(
        billingAddress: billingAddress,
        card: card,
      );
      var paymentMethod =
          await StripePayment.createPaymentMethod(paymentMethodRequest);
      var paymentIntent = await createPaymentIntent(
        amount: amount,
        currency: currency,
        description: description,
        card: card,
        paymentMethod: paymentMethod,
      );
      BillingDetails billingDetails = BillingDetails(
        address: billingAddress,
        email: email,
        name: name,
        phone: phone,
      );
      // ignore: unused_local_variable
      PaymentMethod paymentMethodToPass = PaymentMethod(
          billingDetails: billingDetails,
          card: card,
          created: paymentMethod.created,
          id: paymentMethod.id,
          customerId: paymentMethod.customerId,
          livemode: false,
          type: 'card');

      var response = await StripePayment.confirmPaymentIntent(PaymentIntent(
        paymentMethod: paymentMethodRequest,
        clientSecret: paymentIntent['client_secret'],
        paymentMethodId: paymentMethod.id,
      ));
      if (response.status == 'succeeded') {
        return new StripeTransactionResponse(
            message: 'Transaction successful', success: true);
      } else {
        return new StripeTransactionResponse(
            message: 'Transaction failed', success: false);
      }
    } on PlatformException catch (err) {
      return getPlatformExceptionErrorResult(err);
    } catch (e) {
      print('Pay with card $e');
      return null;
    }
  }

And other functions used by this are:

static getPlatformExceptionErrorResult(err) {
    String message = 'Something went wrong';
    if (err.code == 'cancelled') {
      message = 'Transaction cancelled';
    }
    return new StripeTransactionResponse(message: message, success: false);
  }

  static Future<Map<String, dynamic>> createPaymentIntent({
    @required String amount,
    @required String currency,
    @required String description,
    @required PaymentMethod paymentMethod,
    @required CreditCard card,
  }) async {
    try {
      Map<String, dynamic> body = {
        'amount': amount,
        'currency': currency,
        'payment_method_types[]': 'card',
        // 'shipping': jObjshipping,
        //'payment_method': paymentMethodToPass,
        // 'customer': '',
        'description': description,
      };
      var response =
          await http.post(paymentApiUrl, body: body, headers: headers);
      return jsonDecode(response.body);
    } catch (e) {
      print('Create Payment Intent: $e');
      return null;
    }
  }

And the StripeTransactionResponse class is:

class StripeTransactionResponse {
  String message;
  bool success;

  StripeTransactionResponse({this.message, this.success});
}

This has taken days and still, I am not able to resolve it and I have my project due next week.
Please help me.

Nevermind, after searching the internet upside down for days. Finally, I resolved it.
The pubspec.lock file in my project was creating the issue. Appearntly, I tried multiple plugins to for stripe payment and all of those instances were being saved in pubspec.lock and causing the conflict between dependencies. So, I just delete the pubspec.lock file and get the dependencies again using

flutter pub get

And then run the project again and it just resolved the crashing problem.