Flutter支付集成插件flutter_premiumpay_package的使用

发布于 1周前 作者 yuanlaile 来自 Flutter

Flutter支付集成插件flutter_premiumpay_package的使用

开始使用

  1. 连接到/创建您的Stripe账户

  2. 添加依赖项 在你的pubspec.yaml文件中添加flutter_premiumpay_package依赖项。

    dependencies:
      flutter_premiumpay_package: ^版本号
    
  3. 导入包 在你的 Dart 文件中导入 flutter_premiumpay_package 包。

    import 'package:flutter_premiumpay_package/flutter_premiumpay_package.dart';
    
  4. 使用premiumPayAPI变量 使用导出的变量 premiumPayAPI 并查看以下API使用摘要和流程。详细文档可以在这里找到。

API使用摘要与流程

  1. 生成随机安装ID并存储

    • 通常第一步是生成一个随机的安装ID,并在整个应用程序生命周期中保存该值(例如将其存储在安全存储中):
    premiumPayAPI.createInstallId()
    
    • 这里提供了一个用于生成安装ID的方法,但你需要确保其唯一性。你可以使用自己的生成器。
  2. 请求用户链接安装到其账户

    • 在应用程序的某个地方,你可能希望用户提供他们的电子邮件地址(你需要将此信息存储在某处)。
    • 发起一个 premiumPayAPI.connectRequest() 请求,需要提供一些参数(如安装ID和用户电子邮件),这将发送一封确认电子邮件给用户以确认其电子邮件并创建账户(如果有必要)。
  3. 激活或购买功能

    • 用户可以通过访问他们的账户来激活或购买功能,这将生成与该安装相关的令牌。
  4. 同步令牌

    • 你需要为用户提供一个“同步”按钮,点击后会发起一个 premiumPayAPI.syncRequest() 请求来检索相关令牌。
  5. 验证令牌

    • 你需要验证令牌是否与任何受限制的功能相关联,并在应用程序启动时或需要时进行存储和验证。
    • 使用 premiumPayAPI.verifyToken() 方法来验证令牌。

示例代码

以下是完整的示例代码,展示了如何使用 flutter_premiumpay_package 插件:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_premiumpay_package/flutter_premiumpay_package.dart';
import 'package:flutter/services.dart';
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:email_validator/email_validator.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:provider/provider.dart';

class Feature {
  final String feature_id;
  final String feature_name;
  bool activated;
  String token;

  Feature(this.feature_id, this.feature_name) {
    activated = false;
  }
}

class Data extends ChangeNotifier {
  String application_id;
  String install_id;
  Feature feature_1;
  Feature feature_2;
  List<String> features;
  String email;
  bool connected;
  String permanentLink;

  Data() {
    application_id = "jewish-time";
    feature_1 = new Feature("jewish-time#1", "Jewish Time Premium");
    feature_2 = new Feature("jewish-time#2", "Custom background");
    features = [feature_1.feature_id, feature_2.feature_id];
    init();
  }

  Future<bool> init() async {
    feature_1.activated = await _getFeatureActivationFromSharedPref(feature_1);
    feature_2.activated = await _getFeatureActivationFromSharedPref(feature_2);
    if (feature_1.activated) {
      feature_1.token = await _getTokenFromSharedPref(feature_1);
    }
    if (feature_2.activated) {
      feature_2.token = await _getTokenFromSharedPref(feature_2);
    }

    email = await _getEmailFromSharedPref();
    connected = await _getConnectedFromSharedPref();
    install_id = await _getInstallIdFromSharedPref();
    permanentLink = await _getPermanentLinkFromSharedPref();
    return true;
  }

  // Getter methods
  String get getAppId => application_id;
  String get getInstallId => install_id;
  Feature get getFeature_1 => feature_1;
  Feature get getFeature_2 => feature_2;

  // Setter methods
  void activateFeature_1(String token) {
    feature_1.activated = true;
    feature_1.token = token;
    _resetFeatureActivation(feature_1);
    _resetToken(feature_1, token);
    notifyListeners();
  }

  void activateFeature_2(String token) {
    feature_2.activated = true;
    feature_2.token = token;
    _resetFeatureActivation(feature_2);
    _resetToken(feature_2, token);
    notifyListeners();
  }

  String get getEmail => email;
  void setEmail(String address) {
    email = address;
    _resetEmail(address);
    notifyListeners();
  }

  bool get getConnected => connected;
  void setConnected(bool connect) {
    connected = connect;
    _resetConnected(connect);
    notifyListeners();
  }

  String get getPermanentLink => permanentLink;
  void setPermanentLink(String link) {
    permanentLink = link;
    _resetPermanentLink(link);
    notifyListeners();
  }
}

// Helper functions
Future<String> _getPermanentLinkFromSharedPref() async {
  final prefs = await SharedPreferences.getInstance();
  final link = prefs.getString('permanent_link');
  return link;
}

Future<void> _resetPermanentLink(String link) async {
  final prefs = await SharedPreferences.getInstance();
  await prefs.setString('permanent_link', link);
}

Future<String> _getEmailFromSharedPref() async {
  final prefs = await SharedPreferences.getInstance();
  final email = prefs.getString('email');
  if (email == null) {
    return "";
  }
  return email;
}

Future<void> _resetEmail(String email) async {
  final prefs = await SharedPreferences.getInstance();
  await prefs.setString('email', email);
}

Future<bool> _getConnectedFromSharedPref() async {
  final prefs = await SharedPreferences.getInstance();
  bool connected = prefs.getBool('connected');
  if (connected == null) {
    return false;
  }
  return connected;
}

Future<void> _resetConnected(bool connected) async {
  final prefs = await SharedPreferences.getInstance();
  await prefs.setBool('connected', connected);
}

Future<String> _getInstallIdFromSharedPref() async {
  final prefs = await SharedPreferences.getInstance();
  String installId = prefs.getString('install_id');
  if (installId == null) {
    installId = premiumPayAPI.createInstallId();
    await prefs.setString('install_id', installId);
  }
  return installId;
}

Future<bool> _getFeatureActivationFromSharedPref(Feature value) async {
  final prefs = await SharedPreferences.getInstance();
  bool activated = prefs.getBool(value.feature_id + "_activated");
  if (activated == null) {
    activated = false;
    await prefs.setBool(value.feature_id + "_activated", false);
  }
  return activated;
}

Future<String> _getTokenFromSharedPref(Feature value) async {
  final prefs = await SharedPreferences.getInstance();
  String token = prefs.getString(value.feature_id + "_token");
  return token;
}

Future<void> _resetFeatureActivation(Feature value) async {
  final prefs = await SharedPreferences.getInstance();
  await prefs.setBool(value.feature_id + "_activated", true);
}

Future<void> _resetToken(Feature value, String token) async {
  final prefs = await SharedPreferences.getInstance();
  await prefs.setString(value.feature_id + "_token", token);
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(
          value: Data(),
        ),
      ],
      child: MaterialApp(
        title: 'App Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: DemoAccessPage(),
      ),
    );
  }
}

class DemoAccessPage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          "Jewish Time",
          style: TextStyle(fontSize: 25),
        ),
      ),
      body: FutureBuilder<bool>(
        future: Provider.of<Data>(context).init(),
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
          if (snapshot.hasData) {
            Feature feat_1 = Provider.of<Data>(context).getFeature_1;
            Feature feat_2 = Provider.of<Data>(context).getFeature_2;
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      Container(
                        width: MediaQuery.of(context).size.width * 0.6,
                        child: Text(
                          feat_1.feature_name + " feature: ",
                          softWrap: true,
                          maxLines: 3,
                          style: TextStyle(color: Colors.blue[900], fontSize: 18),
                        ),
                      ),
                      feat_1.activated
                          ? Icon(
                              Icons.check_circle,
                              color: Colors.green,
                              size: 30,
                            )
                          : FlatButton(
                              child: Text(
                                'activate',
                                style: TextStyle(
                                    color: Colors.blue[900],
                                    fontSize: 20,
                                    fontStyle: FontStyle.italic,
                                    decoration: TextDecoration.underline),
                              ),
                              onPressed: () {
                                Navigator.push<Object>(
                                  context,
                                  MaterialPageRoute<dynamic>(
                                    builder: (context) => DemoConnectPage(),
                                  ),
                                );
                              },
                            )
                    ],
                  ),
                  SizedBox(
                    height: 60,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      Container(
                        width: MediaQuery.of(context).size.width * 0.6,
                        child: Text(
                          feat_2.feature_name + " feature: ",
                          softWrap: true,
                          maxLines: 3,
                          style: TextStyle(color: Colors.blue[900], fontSize: 18),
                        ),
                      ),
                      feat_2.activated
                          ? Icon(
                              Icons.check_circle,
                              color: Colors.green,
                              size: 30,
                            )
                          : FlatButton(
                              child: Text(
                                'activate',
                                style: TextStyle(
                                    color: Colors.blue[900],
                                    fontSize: 20,
                                    fontStyle: FontStyle.italic,
                                    decoration: TextDecoration.underline),
                              ),
                              onPressed: () {
                                Navigator.push<Object>(
                                  context,
                                  MaterialPageRoute<dynamic>(
                                    builder: (context) => DemoConnectPage(),
                                  ),
                                );
                              },
                            )
                    ],
                  ),
                  SizedBox(
                    height: 40,
                  ),
                  FlatButton(
                    child: Text(
                      'access to my account',
                      style: TextStyle(
                          color: Colors.blue[900],
                          fontSize: 17,
                          fontStyle: FontStyle.italic,
                          decoration: TextDecoration.underline),
                    ),
                    onPressed: () {
                      Navigator.push<Object>(
                        context,
                        MaterialPageRoute<dynamic>(
                          builder: (context) => DemoConnectPage(),
                        ),
                      );
                    },
                  ),
                ],
              ),
            );
          } else {
            return Center(
              child: Text(
                "Loading...",
                style: TextStyle(color: Colors.blue[900], fontSize: 18),
              ),
            );
          }
        },
      ),
    );
  }
}

class DemoConnectPage extends StatefulWidget {
  DemoConnectPage({Key key}) : super(key: key);

  [@override](/user/override)
  _DemoConnectPageState createState() => _DemoConnectPageState();
}

class _DemoConnectPageState extends State<DemoConnectPage> {
  TextEditingController email_controller = new TextEditingController();
  bool resend_email;
  bool accept_promo_offers;
  String msg;
  bool showIcon;
  bool accept_conditions_of_utilisation;
  bool need_to_accept_conditions;
  bool invalidToken_1;
  bool invalidToken_2;
  SyncResult syncResult;
  ConnectResult connectResult;
  TextEditingController token_controller_1;
  TextEditingController token_controller_2;

  [@override](/user/override)
  void initState() {
    super.initState();
    resend_email = false;
    accept_promo_offers = false;
    msg = "";
    showIcon = false;
    accept_conditions_of_utilisation = false;
    need_to_accept_conditions = false;
    invalidToken_1 = false;
    invalidToken_2 = false;
    token_controller_1 = new TextEditingController();
    token_controller_2 = new TextEditingController();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    bool connected = Provider.of<Data>(context).getConnected;
    String application_id = Provider.of<Data>(context).getAppId;
    String install_id = Provider.of<Data>(context).getInstallId;
    String email = Provider.of<Data>(context).getEmail;
    Feature feat_1 = Provider.of<Data>(context).getFeature_1;
    Feature feat_2 = Provider.of<Data>(context).getFeature_2;
    String permanentLink = Provider.of<Data>(context).getPermanentLink;
    List<String> features = [feat_1.feature_id, feat_2.feature_id];
    email_controller.text = Provider.of<Data>(context).getEmail;

    if (feat_1.activated) {
      token_controller_1.text = feat_1.token;
    }
    if (feat_2.activated) {
      token_controller_2.text = feat_2.token;
    }

    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Premium features',
          style: TextStyle(fontSize: 20),
        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            SizedBox(
              height: 20,
            ),
            Visibility(
              visible: !connected,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Container(
                    width: MediaQuery.of(context).size.width * 0.9,
                    child: Text(
                      "To activate premium features you need to link your installation to your account.",
                      softWrap: true,
                      maxLines: 3,
                      textAlign: TextAlign.center,
                      style: TextStyle(color: Colors.blue[900], fontSize: 18),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 20,
            ),
            (connected)
                ? Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      Container(
                        height: 400,
                        width: MediaQuery.of(context).size.width * 0.85,
                        decoration: BoxDecoration(
                          border: Border.all(width: 1.5, color: Colors.blue[700]),
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: <Widget>[
                            SizedBox(
                              height: 10,
                            ),
                            Text(
                              "Your install ID: \n$install_id",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                  fontSize: 18,
                                  fontWeight: FontWeight.normal,
                                  color: Colors.blue[900]),
                            ),
                            SizedBox(
                              height: 20,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                Container(
                                  height: 50,
                                  width: MediaQuery.of(context).size.width * 0.70,
                                  decoration: BoxDecoration(
                                    color: Colors.grey[300],
                                  ),
                                  child: TextField(
                                    keyboardType: TextInputType.emailAddress,
                                    readOnly: true,
                                    controller: email_controller,
                                    decoration: InputDecoration(
                                      prefixStyle: TextStyle(color: Colors.blue[900]),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                            SizedBox(
                              height: 30,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                SizedBox(
                                  width: 150,
                                  height: 45,
                                  child: RaisedButton(
                                    textColor: Colors.white,
                                    padding: const EdgeInsets.all(0.0),
                                    child: Center(
                                      child: Container(
                                        constraints: BoxConstraints.expand(),
                                        decoration: const BoxDecoration(
                                          gradient: LinearGradient(
                                            colors: <Color>[
                                              Color(0xFF0D47A1),
                                              Color(0xFF1976D2),
                                              Color(0xFF42A5F5),
                                            ],
                                          ),
                                        ),
                                        padding: const EdgeInsets.all(10.0),
                                        child: Center(
                                          child: Text(
                                            "Disconnect",
                                            style: TextStyle(fontSize: 20),
                                          ),
                                        ),
                                      ),
                                    ),
                                    onPressed: () {
                                      setState(() {
                                        Provider.of<Data>(context, listen: false).setConnected(false);
                                        Provider.of<Data>(context, listen: false).setPermanentLink("");
                                      });
                                    },
                                  ),
                                ),
                                SizedBox(
                                  width: 10,
                                ),
                                SizedBox(
                                  width: 150,
                                  height: 45,
                                  child: new RaisedButton(
                                    textColor: Colors.white,
                                    padding: const EdgeInsets.all(0.0),
                                    child: Center(
                                      child: Container(
                                        constraints: BoxConstraints.expand(),
                                        decoration: const BoxDecoration(
                                          gradient: LinearGradient(
                                            colors: <Color>[
                                              Color(0xFF0D47A1),
                                              Color(0xFF1976D2),
                                              Color(0xFF42A5F5),
                                            ],
                                          ),
                                        ),
                                        padding: const EdgeInsets.all(10.0),
                                        child: Center(
                                          child: Text(
                                            "Sync",
                                            style: TextStyle(fontSize: 20),
                                          ),
                                        ),
                                      ),
                                    ),
                                    onPressed: () async {
                                      setState(() {
                                        showIcon = true;
                                        msg = "";
                                      });

                                      syncResult = await premiumPayAPI.syncRequest(install_id, email);

                                      Provider.of<Data>(context, listen: false).setPermanentLink(syncResult.permanentLink);

                                      if (syncResult.tokens.isNotEmpty) {
                                        msg = (syncResult.tokens.length == 1)
                                            ? "${syncResult.tokens.length} token loaded."
                                            : "${syncResult.tokens.length} tokens loaded.";

                                        for (int i = 0; i < syncResult.tokens.length; i++) {
                                          if (feat_1.feature_id == syncResult.tokens[i].featureId) {
                                            Provider.of<Data>(context, listen: false).activateFeature_1(syncResult.tokens[i].token);
                                            token_controller_1.text = feat_1.token;
                                          }
                                          if (feat_2.feature_id == syncResult.tokens[i].featureId) {
                                            Provider.of<Data>(context, listen: false).activateFeature_2(syncResult.tokens[i].token);
                                            token_controller_2.text = feat_2.token;
                                          }
                                        }
                                      }

                                      setState(() {
                                        showIcon = false;
                                      });
                                    },
                                  ),
                                ),
                              ],
                            ),
                            SizedBox(
                              height: 20,
                            ),
                            Visibility(
                              visible: showIcon,
                              child: SizedBox(
                                child: CircularProgressIndicator(),
                                width: 30,
                                height: 30,
                              ),
                            ),
                            SizedBox(
                              height: 30,
                            ),
                            Padding(
                              padding: EdgeInsets.only(right: 12, left: 10),
                              child: Text(
                                msg,
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                    fontSize: 18,
                                    fontWeight: FontWeight.normal,
                                    color: ((syncResult != null && syncResult.status == SyncStatus.INSTALLATION_NOT_LINKED) ||
                                            (connectResult != null && connectResult.status != ConnectStatus.SUCCESSFUL_CONNECT))
                                        ? Colors.red
                                        : Colors.green),
                              ),
                            ),
                            Visibility(
                              visible: (permanentLink != null),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  SizedBox(
                                    height: 10,
                                  ),
                                  Text(
                                    "Activate premium features in",
                                    style: TextStyle(color: Colors.blue[900], fontSize: 18),
                                  ),
                                  FlatButton(
                                    onPressed: () async {
                                      String url = permanentLink;
                                      if (await canLaunch(url)) {
                                        await launch(url);
                                      } else {
                                        throw 'Could not launch $url';
                                      }
                                    },
                                    child: Text(
                                      "website.",
                                      style: TextStyle(
                                          color: Colors.blue[900],
                                          fontSize: 18,
                                          decoration: TextDecoration.underline),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  )
                : Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      Container(
                        height: 600,
                        width: MediaQuery.of(context).size.width * 0.85,
                        decoration: BoxDecoration(
                          border: Border.all(width: 1.5, color: Colors.blue[700]),
                        ),
                        child: SingleChildScrollView(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              SizedBox(
                                height: 10,
                              ),
                              Text(
                                "Your install ID: \n$install_id",
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                    fontSize: 18,
                                    fontWeight: FontWeight.normal,
                                    color: Colors.blue[900]),
                              ),
                              SizedBox(
                                height: 20,
                              ),
                              Text(
                                "Enter your email address:",
                                style: TextStyle(color: Colors.blue[900], fontSize: 18),
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: <Widget>[
                                  Container(
                                    height: 50,
                                    width: MediaQuery.of(context).size.width * 0.70,
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                    ),
                                    child: TextField(
                                      keyboardType: TextInputType.emailAddress,
                                      readOnly: false,
                                      controller: email_controller,
                                      onEditingComplete: () {
                                        if (!EmailValidator.validate(email_controller.text)) {
                                          setState(() {
                                            msg = "Please enter a valid email address.";
                                          });
                                        } else {
                                          setState(() {
                                            msg = "";
                                          });
                                        }
                                      },
                                      decoration: InputDecoration(
                                        prefixStyle: TextStyle(color: Colors.blue[900]),
                                        errorText: (msg != "") ? msg : null,
                                        hintText: 'address@mail.com',
                                        hintStyle: TextStyle(
                                            fontStyle: FontStyle.italic,
                                            color: Colors.grey,
                                            fontSize: 18),
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              SizedBox(
                                height: 15,
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: <Widget>[
                                  Checkbox(
                                    value: resend_email,
                                    onChanged: (newValue) {
                                      setState(() {
                                        resend_email = newValue;
                                      });
                                    },
                                  ),
                                  SizedBox(
                                    width: 5,
                                  ),
                                  Text("Resend email.", style: TextStyle(color: Colors.blue[900])),
                                ],
                              ),
                              LayoutBuilder(
                                builder: (context, constraints) => Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: <Widget>[
                                    Checkbox(
                                      value: accept_promo_offers,
                                      onChanged: (newValue) {
                                        setState(() {
                                          accept_promo_offers = newValue;
                                        });
                                      },
                                    ),
                                    SizedBox(
                                      width: 5,
                                    ),
                                    Container(
                                      width: constraints.maxWidth - 5 - 48,
                                      child: Text(
                                        "I accept to receive promotional offers emails.",
                                        softWrap: true,
                                        maxLines: 2,
                                        style: TextStyle(color: Colors.blue[900]),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: <Widget>[
                                  Checkbox(
                                    value: accept_conditions_of_utilisation,
                                    onChanged: (newValue) {
                                      setState(() {
                                        accept_conditions_of_utilisation = newValue;
                                        if (accept_conditions_of_utilisation) {
                                          need_to_accept_conditions = false;
                                        }
                                      });
                                    },
                                  ),
                                  SizedBox(
                                    width: 5,
                                  ),
                                  Text(
                                    "I accept the conditions of use.",
                                    style: TextStyle(
                                      color: (need_to_accept_conditions) ? Colors.red : Colors.blue[900],
                                    ),
                                  ),
                                ],
                              ),
                              SizedBox(
                                height: 15,
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: <Widget>[
                                  SizedBox(
                                    width: 150,
                                    height: 45,
                                    child: new RaisedButton(
                                      textColor: Colors.white,
                                      padding: const EdgeInsets.all(0.0),
                                      child: Center(
                                        child: Container(
                                          constraints: BoxConstraints.expand(),
                                          decoration: const BoxDecoration(
                                            gradient: LinearGradient(
                                              colors: <Color>[
                                                Color(0xFF0D47A1),
                                                Color(0xFF1976D2),
                                                Color(0xFF42A5F5),
                                              ],
                                            ),
                                          ),
                                          padding: const EdgeInsets.all(10.0),
                                          child: Center(
                                            child: Text(
                                              "Connect",
                                              style: TextStyle(fontSize: 20),
                                            ),
                                          ),
                                        ),
                                      ),
                                      onPressed: () async {
                                        if (!accept_conditions_of_utilisation) {
                                          setState(() {
                                            need_to_accept_conditions = true;
                                          });
                                        }
                                        if (EmailValidator.validate(email_controller.text) && accept_conditions_of_utilisation) {
                                          setState(() {
                                            showIcon = true;
                                          });
                                          Provider.of<Data>(context, listen: false).setEmail(email_controller.text);
                                          Install install = premiumPayAPI.createInstall(
                                              install_id,
                                              application_id,
                                              features);
                                          connectResult = await premiumPayAPI.connectRequest(
                                              install,
                                              email,
                                              resendEmail: resend_email,
                                              acceptPromoOffers: accept_promo_offers);
                                          setState(() {
                                            showIcon = false;
                                          });
                                          switch (connectResult.status) {
                                            case ConnectStatus.NEED_TO_VERIFY_EMAIL:
                                              setState(() {
                                                Provider.of<Data>(context, listen: false).setConnected(true);
                                                msg =
                                                    "Check your email and click on the link provided to link your installation.";
                                              });
                                              break;
                                            case ConnectStatus.SUCCESSFUL_CONNECT:
                                              setState(() {
                                                Provider.of<Data>(context, listen: false).setConnected(true);
                                              });
                                              break;
                                          }
                                        }
                                      },
                                    ),
                                  ),
                                ],
                              ),
                              SizedBox(
                                height: 20,
                              ),
                              Visibility(
                                visible: showIcon,
                                child: SizedBox(
                                  child: CircularProgressIndicator(),
                                  width: 30,
                                  height: 30,
                                ),
                              ),
                              SizedBox(
                                height: 30,
                              ),
                              Padding(
                                padding: EdgeInsets.all(10),
                                child: Text(
                                  "After connecting, an email will be sent to the address email given above.\nIn order to link your installation to your account, you need to click on the link in the email sent.",
                                  style: TextStyle(color: Colors.blue[900], fontSize: 18),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
            SizedBox(
              height: 10,
            ),
            Text("Tokens to deblock features:", style: TextStyle(color: Colors.blue[900], fontSize: 18)),
            SizedBox(
              height: 20,
            ),
            Text(feat_1.feature_name, style: TextStyle(color: Colors.blue[900], fontSize: 18)),
            SizedBox(
              height: 10,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  height: 50,
                  width: MediaQuery.of(context).size.width * 0.75,
                  decoration: BoxDecoration(
                    border: new Border.all(color: Colors.blue[600]),
                    color: feat_1.activated ? Colors.grey : Colors.white,
                  ),
                  child: TextField(
                    controller: token_controller_1,
                    readOnly: feat_1.activated,
                    onEditingComplete: () async {
                      bool verified = premiumPayAPI.checkTokenValidFormat(token_controller_1.text) &&
                          premiumPayAPI.verifyToken(install_id, feat_1.feature_id, token_controller_1.text);
                      if (verified) {
                        Provider.of<Data>(context, listen: false).activateFeature_1(token_controller_1.text);
                      } else {
                        setState(() {
                          invalidToken_1 = true;
                        });
                      }
                    },
                    decoration: InputDecoration(
                      prefixStyle: TextStyle(color: Colors.blue[900]),
                      hintText: 'Please enter the token here: ',
                      hintStyle: TextStyle(fontStyle: FontStyle.italic, color: Colors.grey, fontSize: 15),
                    ),
                  ),
                ),
                SizedBox(
                  width: 5,
                ),
                IconButton(
                  iconSize: 25,
                  icon: Icon(Icons.send),
                  color: Colors.blue[900],
                  onPressed: () async {
                    bool verified = premiumPayAPI.checkTokenValidFormat(token_controller_1.text) &&
                        premiumPayAPI.verifyToken(install_id, feat_1.feature_id, token_controller_1.text);
                    if (verified) {
                      Provider.of<Data>(context, listen: false).activateFeature_1(token_controller_1.text);
                    } else {
                      setState(() {
                        invalidToken_1 = true;
                      });
                    }
                  },
                ),
              ],
            ),
            SizedBox(
              height: 10,
            ),
            Visibility(
              visible: feat_1.activated,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Icon(
                    Icons.check_circle,
                    color: Colors.green,
                    size: 30,
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  Text(
                    "Feature added.",
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.normal, color: Colors.green),
                  ),
                ],
              ),
            ),
            Visibility(
              visible: invalidToken_1,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Icon(
                    Icons.error,
                    color: Colors.red,
                    size: 30,
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  Text(
                    "Invalid token.",
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.normal, color: Colors.red),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 10,
            ),
            Text(feat_2.feature_name, style: TextStyle(color: Colors.blue[900], fontSize: 18)),
            SizedBox(
              height: 10,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  height: 50,
                  width: MediaQuery.of(context).size.width * 0.75,
                  decoration: BoxDecoration(
                    border: new Border.all(color: Colors.blue[600]),
                    color: feat_2.activated ? Colors.grey : Colors.white,
                  ),
                  child: TextField(
                    controller: token_controller_2,
                    readOnly: feat_2.activated,
                    onEditingComplete: () async {
                      bool verified = premiumPayAPI.checkTokenValidFormat(token_controller_2.text) &&
                          premiumPayAPI.verifyToken(install_id, feat_2.feature_id, token_controller_2.text);
                      if (verified) {
                        Provider.of<Data>(context, listen: false).activateFeature_2(token_controller_2.text);
                      } else {
                        setState(() {
                          invalidToken_2 = true;
                        });
                      }
                    },
                    decoration: InputDecoration(
                      prefixStyle: TextStyle(color: Colors.blue[900]),
                      hintText: 'Please enter the token here: ',
                      hintStyle: TextStyle(fontStyle: FontStyle.italic, color: Colors.grey, fontSize: 15),
                    ),
                  ),
                ),
                SizedBox(
                  width: 5,
                ),
                IconButton(
                  iconSize: 25,
                  icon: Icon(Icons.send),
                  color: Colors.blue[900],
                  onPressed: () async {
                    bool verified = premiumPayAPI.checkTokenValidFormat(token_controller_2.text) &&
                        premiumPayAPI.verifyToken(install_id, feat_2.feature_id, token_controller_2.text);
                    if (verified) {
                      Provider.of<Data>(context, listen: false).activateFeature_2(token_controller_2.text);
                    } else {
                      setState(() {
                        invalidToken_2 = true;
                      });
                    }
                  },
                ),
              ],
            ),
            SizedBox(
              height: 10,
            ),
            Visibility(
              visible: feat_2.activated,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Icon(
                    Icons.check_circle,
                    color: Colors.green,
                    size: 30,
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  Text(
                    "Feature added.",
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.normal, color: Colors.green),
                  ),
                ],
              ),
            ),
            Visibility(
              visible: invalidToken_2,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Icon(
                    Icons.error,
                    color: Colors.red,
                    size: 30,
                  ),
                  SizedBox(
                    width: 10,
                  ),
                  Text(
                    "Invalid token.",
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.normal, color: Colors.red),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 20,
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter支付集成插件flutter_premiumpay_package的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter支付集成插件flutter_premiumpay_package的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter项目中集成并使用flutter_premiumpay_package插件进行支付的示例代码。请注意,由于flutter_premiumpay_package并非一个真实存在的插件(据我所知),我将提供一个假想的插件使用示例,并假设它具有一些基本功能,比如初始化支付、发起支付请求和处理支付结果。

首先,确保你已经在pubspec.yaml文件中添加了flutter_premiumpay_package依赖(假设它存在):

dependencies:
  flutter:
    sdk: flutter
  flutter_premiumpay_package: ^x.y.z  # 替换为实际的版本号

然后,运行flutter pub get来安装依赖。

接下来,在你的Flutter项目中,你可以按照以下步骤进行支付集成:

1. 初始化支付插件

在你的主应用文件(通常是main.dart)或者一个合适的初始化位置,初始化支付插件:

import 'package:flutter/material.dart';
import 'package:flutter_premiumpay_package/flutter_premiumpay_package.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  // 初始化支付插件
  PremiumPay.initialize('your_api_key_here'); // 替换为你的API密钥
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Payment Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: PaymentScreen(),
    );
  }
}

2. 创建支付屏幕

在你的PaymentScreen中,添加发起支付请求的逻辑:

import 'package:flutter/material.dart';
import 'package:flutter_premiumpay_package/flutter_premiumpay_package.dart';

class PaymentScreen extends StatefulWidget {
  @override
  _PaymentScreenState createState() => _PaymentScreenState();
}

class _PaymentScreenState extends State<PaymentScreen> {
  Future<void> _makePayment() async {
    try {
      // 发起支付请求
      var paymentResult = await PremiumPay.makePayment(
        amount: 100.0, // 支付金额
        currency: 'USD', // 货币类型
        description: 'Test Payment', // 支付描述
        // 其他可能的参数,如用户ID、商品ID等
      );

      // 处理支付结果
      if (paymentResult.success) {
        showDialog(
          context: context,
          builder: (context) => AlertDialog(
            title: Text('Payment Successful'),
            content: Text('Transaction ID: ${paymentResult.transactionId}'),
            actions: <Widget>[
              FlatButton(
                child: Text('OK'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          ),
        );
      } else {
        showDialog(
          context: context,
          builder: (context) => AlertDialog(
            title: Text('Payment Failed'),
            content: Text('Error: ${paymentResult.errorMessage}'),
            actions: <Widget>[
              FlatButton(
                child: Text('OK'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          ),
        );
      }
    } catch (e) {
      print('Error during payment: $e');
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: Text('Error'),
          content: Text('An unexpected error occurred: $e'),
          actions: <Widget>[
            FlatButton(
              child: Text('OK'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Payment Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _makePayment,
          child: Text('Make Payment'),
        ),
      ),
    );
  }
}

3. 处理支付结果

在上面的代码中,_makePayment方法发起了一个支付请求,并处理了支付结果。如果支付成功,它会显示一个包含交易ID的对话框;如果支付失败,它会显示一个包含错误信息的对话框。

注意事项

  • API密钥:确保你替换了your_api_key_here为实际的API密钥。
  • 支付金额和货币:根据你的需求调整支付金额和货币类型。
  • 错误处理:在实际应用中,你可能需要更详细的错误处理和用户反馈机制。

由于flutter_premiumpay_package是一个假想的插件,因此上述代码需要根据你的实际插件文档进行调整。通常,支付插件的文档会提供详细的初始化、支付请求和结果处理的示例代码。

回到顶部