【问题标题】:Flutter google_pay implementationFlutter google_pay 实现
【发布时间】:2020-09-13 07:12:06
【问题描述】:

所以已经搜索了整个互联网,看看我是否可以获得任何代码来帮助整合谷歌支付以替换我现有的 Java android 代码,但壁橱是 https://pub.dev/packages/google_pay,它加载了谷歌播放 UI,但支付不成功因为它显示了无法识别的应用程序的错误。

【问题讨论】:

    标签: flutter in-app-billing google-pay flutter-in-app-purchase


    【解决方案1】:

    使用 Google Pay Flutter 包here

    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";});
      }
    }
    

    【讨论】:

    • 谢谢@zavora。正如您在我的问题中看到的那样,我使用了您之前发送给我的相同链接,但是我在说“无法识别的应用程序”时出错,我不知道我哪里弄错了。我需要在 await GooglePay.initializeGooglePay("pk_test_H5CJvRiPfCrRS44bZJLu46fM00UjQ0vtRN") 中更改此“pk_test_H5CJvRiPfCrRS44bZJLu46fM00UjQ0vtRN”吗?如果是,我从哪里获得令牌,它是否必须以 pk_test 开头?
    • @yodeveloper 和 Zavora,你终于可以让这个工作了吗?我现在在同一条船上 - 寻找资源以将 Google Pay 集成到 Flutter 应用程序中。我的用例不是应用内支付,而是电子商务支付。如果您现在能够分享您对这个主题的任何想法,我将不胜感激..
    猜你喜欢
    • 1970-01-01
    • 2019-02-25
    • 2019-01-21
    • 2017-10-26
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2019-02-07
    相关资源
    最近更新 更多