Flutter google_pay的实现

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

因此,已经通过整个互联网搜索,看看是否可以得到任何代码,以帮助与谷歌支付集成flutter,以取代我现有的Java Android代码,但壁橱是 https:/pub.devpackagesgoogle_pay。 加载谷歌播放用户界面,但支付不成功,因为它显示一个错误,未识别的应用程序。

flutter in-app-billing google-pay flutter-in-app-purchase
1个回答
0
投票

使用谷歌支付flutter包 此处.

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";});
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.