skip to Main Content

I am testing handling payments using Google Pay, and I use the pay package for this.

I made a simple app and set it for the test environment, I have now a working Google Pay button, which shows the sheet to fill with the card information.
screen shot of the google pay button

I am using this button code:

  GooglePayButton(
          paymentConfigurationAsset:
              'default_payment_profile_google_pay.json',
          paymentItems: _paymentItems,
          type: GooglePayButtonType.buy,
          margin: const EdgeInsets.only(top: 15.0),
          onPaymentResult: (Map<String, dynamic> paymentResult) {
            print(paymentResult); // The question starts here
          },
           loadingIndicator: const Center(
            child: CircularProgressIndicator(),
          ),
        ),

and the default_payment_profile_google_pay.json contains this:

{
  "provider": "google_pay",
  "data": {
    "environment": "TEST",
    "apiVersion": 2,
    "apiVersionMinor": 0,
    "allowedPaymentMethods": [
      {
        "type": "CARD",
        "tokenizationSpecification": {
          "type": "PAYMENT_GATEWAY",
          "parameters": {
            "gateway": "example",
            "gatewayMerchantId": "gatewayMerchantId"
          }
        },
        "parameters": {
          "allowedCardNetworks": ["VISA", "MASTERCARD"],
          "allowedAuthMethods": ["PAN_ONLY", "CRYPTOGRAM_3DS"],
          "billingAddressRequired": true,
          "billingAddressParameters": {
            "format": "FULL",
            "phoneNumberRequired": true
          }
        }
      }
    ],
    "merchantInfo": {
      "merchantId": "01234567890123456789",
      "merchantName": "Example Merchant Name"
    },
    "transactionInfo": {
      "countryCode": "US",
      "currencyCode": "USD"
    }
  }
}

the onPaymentResult method will print the response triggered after the payment is done, it’s Map<String, dynamic>.

I am using the test suite provided by Google Pay, after a successful payment, the response is this:

    {
  "apiVersionMinor": "0",
  "apiVersion": "2",
  "paymentMethodData": {
    "description": "Mastercard •••• 4444",
    "tokenizationData": {
      "type": "PAYMENT_GATEWAY",
      "token": "examplePaymentMethodToken"
    },
    "type": "CARD",
    "info": {
      "cardNetwork": "MASTERCARD",
      "cardDetails": "4444",
      "billingAddress": {
        "phoneNumber": "+1 650-555-5555",
        "address3": "",
        "sortingCode": "",
        "address2": "",
        "countryCode": "US",
        "address1": "1600 Amphitheatre Parkway",
        "postalCode": "94043",
        "name": "Card Holder Name",
        "locality": "Mountain View",
        "administrativeArea": "CA"
      }
    }
  }
}

My question is:

What should I do now to complete the payment process, where should I see the payments or manage them?

I have no experience dealing with payments, so any pieces of information or ways of what I should do from this point is really really welcome.

2

Answers


  1. From there, you have to use a payment processor to process your payment. A good example is Stripe. You can do it by using this plugin flutter_stripe then using something like this (From the plugin description)

    Future<void> onGooglePayResult(paymentResult) async {
    final response = await fetchPaymentIntentClientSecret();
    final clientSecret = response['clientSecret'];
    final token = paymentResult['paymentMethodData']['tokenizationData']['token'];
    final tokenJson = Map.castFrom(json.decode(token));
    
    final params = PaymentMethodParams.cardFromToken(
      token: tokenJson['id'],
    );
    // Confirm Google pay payment method
    await Stripe.instance.confirmPayment(
      clientSecret,
      params,
    );
    

    }

    Of course you have to get a Stripe account and so and so

    Login or Signup to reply.
  2. Inside the onGooglePayResult method, you can use this:

    Future<void> onGooglePayResult(paymentResult) async {
    final response = await fetchPaymentIntentClientSecret(); //creates stripe payment intent
    final clientSecret = response['client_secret'];
    
    await stripe.Stripe.instance.confirmPlatformPayPaymentIntent(
          clientSecret: clientSecret,
          confirmParams: stripe.PlatformPayConfirmParams.googlePay(
            googlePay: stripe.GooglePayParams(
              amount: amount, //int
              testEnv: true, //default false
              merchantName: merchantName,
              merchantCountryCode: merchantCountryCode,
              currencyCode: currencyCode,
            ),
          )
      );
    

    I was having trouble with the accepted answer because the [‘paymentMethodData’][‘tokenizationData’][‘token’] was returning "examplePaymentMethodToken" which was breaking decoding

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search