flutter-stripe/flutter_stripe

Can't set GooglePay Button type on android

Closed this issue · 0 comments

When setting the button type of google pay button inside a payment sheet, the following error happen

Error:

W/Bundle  (16907): Key buttonType expected Integer but value was a java.lang.String.  The default value 0 was returned.
W/Bundle  (16907): Attempt to cast generated internal exception:
W/Bundle  (16907): java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
W/Bundle  (16907): 	at android.os.BaseBundle.getInt(BaseBundle.java:1296)
W/Bundle  (16907): 	at android.os.BaseBundle.getInt(BaseBundle.java:1278)
W/Bundle  (16907): 	at com.reactnativestripesdk.PaymentSheetFragment$Companion.buildGooglePayConfig$stripe_android_debug(PaymentSheetFragment.kt:384)
W/Bundle  (16907): 	at com.reactnativestripesdk.PaymentSheetFragment.onViewCreated(PaymentSheetFragment.kt:68)
W/Bundle  (16907): 	at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3148)
W/Bundle  (16907): 	at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:600)
W/Bundle  (16907): 	at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
W/Bundle  (16907): 	at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2103)
W/Bundle  (16907): 	at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1998)
W/Bundle  (16907): 	at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1941)

Repro:

Copy paste the following into payment_sheet_screen and run the example

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:http/http.dart' as http;
import 'package:stripe_example/config.dart';
import 'package:stripe_example/screens/payment_sheet/payment_sheet_screen_custom_flow.dart';
import 'package:stripe_example/widgets/example_scaffold.dart';
import 'package:stripe_example/widgets/loading_button.dart';

class PaymentSheetScreen extends StatefulWidget {
  @override
  _PaymentSheetScreenState createState() => _PaymentSheetScreenState();
}

class _PaymentSheetScreenState extends State<PaymentSheetScreen> {
  int step = 0;

  @override
  Widget build(BuildContext context) {
    return ExampleScaffold(
      title: 'Payment Sheet',
      tags: ['Single Step'],
      children: [
        Stepper(
          controlsBuilder: emptyControlBuilder,
          currentStep: step,
          steps: [
            Step(
              title: Text('Init payment'),
              content: LoadingButton(
                onPressed: initPaymentSheet,
                text: 'Init payment sheet',
              ),
            ),
            Step(
              title: Text('Confirm payment'),
              content: LoadingButton(
                onPressed: confirmPayment,
                text: 'Pay now',
              ),
            ),
          ],
        ),
      ],
    );
  }

  Future<Map<String, dynamic>> _createTestPaymentSheet() async {
    final url = Uri.parse('$kApiUrl/payment-sheet');
    final response = await http.post(
      url,
      headers: {
        'Content-Type': 'application/json',
      },
      body: json.encode({
        'a': 'a',
      }),
    );
    final body = json.decode(response.body);
    if (body['error'] != null) {
      throw Exception(body['error']);
    }
    return body;
  }

  Future<void> initPaymentSheet() async {
    try {
      // 1. create payment intent on the server
      final data = await _createTestPaymentSheet();

      // create some billingdetails
      final billingDetails = BillingDetails(
        name: 'Flutter Stripe',
        email: 'email@stripe.com',
        phone: '+48888000888',
        address: Address(
          city: 'Houston',
          country: 'US',
          line1: '1459  Circle Drive',
          line2: '',
          state: 'Texas',
          postalCode: '77063',
        ),
      ); // mocked data for tests

      // 2. initialize the payment sheet
      await Stripe.instance.initPaymentSheet(
        paymentSheetParameters: SetupPaymentSheetParameters(
          // Main params
          paymentIntentClientSecret: data['paymentIntent'],
          merchantDisplayName: 'Flutter Stripe Store Demo',
          preferredNetworks: [CardBrand.Amex],
          // Customer params
          customerId: data['customer'],
          customerEphemeralKeySecret: data['ephemeralKey'],
          returnURL: 'flutterstripe://redirect',

          // Extra params
          primaryButtonLabel: 'Pay now',
          applePay: PaymentSheetApplePay(
            merchantCountryCode: 'DE',
          ),
          googlePay: PaymentSheetGooglePay(
            merchantCountryCode: 'DE',
            buttonType: PlatformButtonType.book,
            testEnv: true,
          ),
          style: ThemeMode.light,
          billingDetails: billingDetails,
        ),
      );
      setState(() {
        step = 1;
      });
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error: $e')),
      );
      rethrow;
    }
  }

  Future<void> confirmPayment() async {
    try {
      // 3. display the payment sheet.
      await Stripe.instance.presentPaymentSheet();

      setState(() {
        step = 0;
      });

      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('Payment succesfully completed'),
        ),
      );
    } on Exception catch (e) {
      if (e is StripeException) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Error from Stripe: ${e.error.localizedMessage}'),
          ),
        );
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Unforeseen error: ${e}'),
          ),
        );
      }
    }
  }
}