如何在 Flutter 中检查 Apple Pay 和 Google Pay 可用性

问题描述 投票:0回答:2

我需要在我的应用程序中列出所有可用的付款方式,因此如果配置了 Apple Pay,我需要在我的付款方式列表中显示它,对于 Google Pay 也是如此。

有没有办法检查用户的设备上是否配置了Apple Pay / Google Pay或尚未配置?

我看到了这个库(付费),但它看起来没有这个功能,有什么想法吗?

flutter applepay google-pay
2个回答
3
投票

我知道怎么做了,所以我分享答案..

实际上我可以使用这个库来做到这一点,它也受到google.dev

的支持
  1. 首先将库

    pay: ^1.1.0
    添加到您的
    pubspecs.yaml
    的依赖项中。

  2. 添加配置文件,新的 dart 文件将其命名为

    defaultConfigPayJson.dart
    并将以下代码粘贴到其中,并确保您需要替换商家 ID 和其他一些内容:

    const String defaultApplePay = '''{
      "provider": "apple_pay",
      "data": {
        "merchantIdentifier": "merchant.com.domain.project",
        "displayName": "App name",
        "merchantCapabilities": ["3DS", "debit", "credit"],
        "supportedNetworks": ["amex", "visa", "discover", "masterCard"],
        "countryCode": "QA",
        "currencyCode": "QAR",
        "requiredBillingContactFields": [],
        "requiredShippingContactFields": [],
        "shippingMethods": []
      }
    }''';
    
    const String defaultGooglePay = '''{
      "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": "QA",
          "currencyCode": "QAR"
        }
      }
    }''';
    
  3. 将此代码添加到您的目标页面(应该在其中检查可用性):

    import 'defaultConfigPayJson.dart' as payment_configurations;
    ...
    Pay _payClient;
    bool _hasApplePay = false;
    bool _hasGooglePay = false;
    ...
    @override
    void initState() {
      super.initState();
    
      _payClient = Pay({
        PayProvider.google_pay: PaymentConfiguration.fromJsonString(
            payment_configurations.defaultGooglePay),
        PayProvider.apple_pay: PaymentConfiguration.fromJsonString(
            payment_configurations.defaultApplePay),
      });
    
      _checkIfGooglePayInstalled();
      _checkIfApplePayInstalled();
    }
    
  4. 最后添加从您需要的任何地方调用可用性检查:

    Future <void> _checkIfApplePayInstalled() async {
      _hasApplePay = await _payClient.userCanPay(PayProvider.apple_pay);
    
      if (_hasApplePay) {
        setState(() {
          // Write here your code..
        });
        print('Apple Pay is available on this device!');
      } else {
        print('Apple Pay is not available on this device!');
      }
    }
    
    Future<void> _checkIfGooglePayInstalled() async {
      _hasGooglePay = await _payClient.userCanPay(PayProvider.google_pay);
      if (_hasGooglePay) {
        setState(() {
          // Write here your code..
        });
        print('Google Pay is available on this device!');
      } else {
        print('Google Pay is not available on this device!');
      }
    }
    

0
投票

此代码可以用于 Upi 付款并打开已安装的 Googlepay 应用程序并重定向以提供付款状态吗?

© www.soinside.com 2019 - 2024. All rights reserved.