buildGooglePayConfigurationJson function

String? buildGooglePayConfigurationJson({
  1. required CheckoutCustomizationData? customization,
  2. required String currencyCode,
  3. required String totalPrice,
  4. required String googlePayEnvironment,
  5. String gatewayFallback = 'gpaymob',
})

Builds a JSON string for the pay package's Google Pay PaymentConfiguration.

Uses the enabled GOOGLE_PAY / ANDROID_PAY row from checkout customization. Resolves gateway vs Google seller ids when the API puts the Paymob numeric id in merchant_id and the BCR… id in identifier (or the reverse).

googlePayEnvironment should be TEST (sandbox) or PRODUCTION.

Implementation

String? buildGooglePayConfigurationJson({
  required CheckoutCustomizationData? customization,
  required String currencyCode,
  required String totalPrice,
  required String googlePayEnvironment,
  String gatewayFallback = 'gpaymob',
}) {
  if (customization == null) return null;
  final method = findVisibleGooglePayMethod(customization);
  if (method == null) return null;

  final gatewayMerchantId = _pickGatewayMerchantId(method);
  final googleSellerId = _pickGoogleSellerId(method);
  if (gatewayMerchantId == null ||
      gatewayMerchantId.isEmpty ||
      googleSellerId == null ||
      googleSellerId.isEmpty) {
    return null;
  }

  final md = customization.merchantDetails;
  final merchantLabel = _nonEmpty(md?.merchantName);
  final companyLabel = _nonEmpty(md?.companyName);
  final merchantName = merchantLabel.isNotEmpty
      ? merchantLabel
      : (companyLabel.isNotEmpty ? companyLabel : 'Pay');

  final schemeSources = method.schemes.isNotEmpty
      ? method.schemes
      : (md?.supportedCardSchemes ?? const <String>[]);
  final networks = _mapSchemesToGoogleCardNetworks(schemeSources);
  // final allowedCardNetworks =
  //     networks.isNotEmpty ? networks : const ['VISA', 'MASTERCARD'];
  final allowedCardNetworks = const ['VISA', 'MASTERCARD'];

  final cc = currencyCode.trim().toUpperCase();
  if (cc.isEmpty) return null;

  final price = _formatDecimalTotal(totalPrice);
  final env = googlePayEnvironment.trim().toUpperCase();
  final environment =
      env == 'PRODUCTION' || env == 'PROD' ? 'PRODUCTION' : 'TEST';

  final gateway = _resolveGateway(method, gatewayFallback);

  final json = jsonEncode({
    'provider': 'google_pay',
    'data': {
      'environment': environment,
      'apiVersion': 2,
      'apiVersionMinor': 0,
      'allowedPaymentMethods': [
        {
          'type': 'CARD',
          'parameters': {
            'allowedAuthMethods': ['PAN_ONLY', 'CRYPTOGRAM_3DS'],
            'allowedCardNetworks': allowedCardNetworks,
          },
          'tokenizationSpecification': {
            'type': 'PAYMENT_GATEWAY',
            'parameters': {
              'gateway': gateway,
              'gatewayMerchantId': gatewayMerchantId,
            },
          },
        },
      ],
      'merchantInfo': {
        'merchantId': googleSellerId,
        'merchantName': "Orc Technologies FZE",
      },
      'transactionInfo': {
        'totalPriceStatus': 'FINAL',
        'totalPriceLabel': 'Total',
        'totalPrice': price,
        "countryCode": "AE",
        'currencyCode': cc,
        'checkoutOption': 'COMPLETE_IMMEDIATE_PURCHASE',
      },
    },
  });

  if (kDebugMode) {
    debugPrint('Google Pay Configuration JSON: $json');
  }

  return json;
}