Flutter 适用于谷歌支付的应用程序内购买工具

Flutter 适用于谷歌支付的应用程序内购买工具,flutter,dart,in-app-purchase,google-pay,Flutter,Dart,In App Purchase,Google Pay,我正试图用flatter-Dart从Android Java更新我在google play store上的现有应用程序。在我现有的应用程序中,我有一个使用谷歌支付的应用程序内购买,它可以完美地工作(使用android java编码),但我正在寻找如何使用Flitter dart实现谷歌支付应用程序内购买。在应用程序内使用g支付应该没有问题。没有必要为此编写特定的代码。你只需要确保你正在谷歌计费平台上设置你的商户。查看本教程我发现google Pay不是一个支付网关,而是一个通往网关的阶梯,因此您

我正试图用flatter-Dart从Android Java更新我在google play store上的现有应用程序。在我现有的应用程序中,我有一个使用谷歌支付的应用程序内购买,它可以完美地工作(使用android java编码),但我正在寻找如何使用Flitter dart实现谷歌支付应用程序内购买。

在应用程序内使用g支付应该没有问题。没有必要为此编写特定的代码。你只需要确保你正在谷歌计费平台上设置你的商户。查看本教程

我发现google Pay不是一个支付网关,而是一个通往网关的阶梯,因此您必须将支付网关系统与google集成,并从我推荐的列表中选择。我将在下面解释

我遇到两个。实现这一目标的选择

  • -这将自动使用条带支付网关
  • -您可以使用多个自定义支付网关
  • 要求

    如果您尚未访问以设置帐户,请确保已设置您的条带帐户

    第一个插件 确保使用条带支付密钥初始化插件

    GooglePay.initializeGooglePay("pk_test_H5CJvRiPfCrRS44bZJLu46fM00UjQ0vtRN");
    
    这个插件的代码结构如下

    import 'package:flutter/material.dart';
    import 'dart:async';
    import 'package:flutter/services.dart';
    import 'package:google_pay/google_pay.dart';
    
    void main() => runApp(MyApp());
    
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      String _platformVersion = 'Unknown';
      String _googlePayToken = 'Unknown';
    
      @override
      void initState() {
        super.initState();
        initPlatformState();
      }
    
      // Platform messages are asynchronous, so we initialize in an async method.
      Future<void> initPlatformState() async {
        String platformVersion;
        // Platform messages may fail, so we use a try/catch PlatformException.
        try {
          platformVersion = await GooglePay.platformVersion;
        } on PlatformException {
          platformVersion = 'Failed to get platform version.';
        }
    
        await GooglePay.initializeGooglePay("pk_test_H5CJvRiPfCrRS44bZJLu46fM00UjQ0vtRN");
    
        // If the widget was removed from the tree while the asynchronous platform
        // message was in flight, we want to discard the reply rather than calling
        // setState to update our non-existent appearance.
        if (!mounted) return;
    
        setState(() {
          _platformVersion = platformVersion;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: const Text('Plugin example app'),
            ),
            body: Center(
              child: Column(
                children: <Widget>[
                  Text('Running on: $_platformVersion\n'),
                  Text('Google pay token: $_googlePayToken\n'),
                  FlatButton(
                    child: Text("Google Pay Button"),
                    onPressed: onButtonPressed,
                  )
              ]    
              ),
            ),
          ),
        );
      }
    
      void onButtonPressed() async{
        setState((){_googlePayToken = "Fetching";});
        try {
          await GooglePay.openGooglePaySetup(
              price: "5.0",
              onGooglePaySuccess: onSuccess,
              onGooglePayFailure: onFailure,
              onGooglePayCanceled: onCancelled);
          setState((){_googlePayToken = "Done Fetching";});
        } on PlatformException catch (ex) {
          setState((){_googlePayToken = "Failed Fetching";});
        }
        
      }
    
      void onSuccess(String token){ 
        setState((){_googlePayToken = token;});
      }
    
      void onFailure(){ 
        setState((){_googlePayToken = "Failure";});
      }
    
      void onCancelled(){ 
        setState((){_googlePayToken = "Cancelled";});
      }
    }
    
    用于其他支付网关

     _makeCustomPayment() async {
          var environment = 'rest'; // or 'production'
    
          if (!(await FlutterGooglePay.isAvailable(environment))) {
            _showToast(scaffoldContext, 'Google pay not available');
          } else {
            ///docs https://developers.google.com/pay/api/android/guides/tutorial
            PaymentBuilder pb = PaymentBuilder()
              ..addGateway("example")
              ..addTransactionInfo("1.0", "USD")
              ..addAllowedCardAuthMethods(["PAN_ONLY", "CRYPTOGRAM_3DS"])
              ..addAllowedCardNetworks(
                  ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"])
              ..addBillingAddressRequired(true)
              ..addPhoneNumberRequired(true)
              ..addShippingAddressRequired(true)
              ..addShippingSupportedCountries(["US", "GB"])
              ..addMerchantInfo("Example");
    
            FlutterGooglePay.makeCustomPayment(pb.build()).then((Result result) {
              if (result.status == ResultStatus.SUCCESS) {
                _showToast(scaffoldContext, 'Success');
              } else if (result.error != null) {
                _showToast(context, result.error);
              }
            }).catchError((error) {
              //TODO
            });
          }
        }
    

    你检查过这个链接了吗?是的,我检查过,但没有工作。我尝试了这项工作,并启动了谷歌支付,但G支付显示了无法识别的应用程序。我想我必须改变等待谷歌支付。初始化谷歌支付(“pk_test_H5CJvRiPfCrRS44bZJLu46fM00UjQ0vtRN”);要挖掘但不知道如何获取wait \“pk_test_H5CJvRiPfCrRS44bZJLu46fM00UjQ0vtRN”本机移动应用,在苹果应用商店和/或谷歌游戏商店上收集数字商品付款通常需要使用应用内购买API,这意味着Stripe和Paypal等服务是不可能的(除了实际装运货物的付款)。尝试此官方Flitter插件,我需要在美国注册一个stripe帐户,对吗?如果您没有谷歌支持的支付提供商或网关,您可以访问此url申请使用商户帐户(直接)
     _makeCustomPayment() async {
          var environment = 'rest'; // or 'production'
    
          if (!(await FlutterGooglePay.isAvailable(environment))) {
            _showToast(scaffoldContext, 'Google pay not available');
          } else {
            ///docs https://developers.google.com/pay/api/android/guides/tutorial
            PaymentBuilder pb = PaymentBuilder()
              ..addGateway("example")
              ..addTransactionInfo("1.0", "USD")
              ..addAllowedCardAuthMethods(["PAN_ONLY", "CRYPTOGRAM_3DS"])
              ..addAllowedCardNetworks(
                  ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"])
              ..addBillingAddressRequired(true)
              ..addPhoneNumberRequired(true)
              ..addShippingAddressRequired(true)
              ..addShippingSupportedCountries(["US", "GB"])
              ..addMerchantInfo("Example");
    
            FlutterGooglePay.makeCustomPayment(pb.build()).then((Result result) {
              if (result.status == ResultStatus.SUCCESS) {
                _showToast(scaffoldContext, 'Success');
              } else if (result.error != null) {
                _showToast(context, result.error);
              }
            }).catchError((error) {
              //TODO
            });
          }
        }