Flutter支付网关插件telr_payment_gateway的使用

Flutter支付网关插件telr_payment_gateway的使用

Getting Started(开始)

本项目是一个用于Flutter的插件包,包含适用于Android和/或iOS的平台特定实现代码。

对于如何开始使用Flutter,您可以查看我们的在线文档,其中提供了教程、示例、移动开发指南以及完整的API参考。

使用示例

下面是一个完整的示例代码,展示了如何在Flutter应用中使用telr_payment_gateway插件进行支付操作。

import 'dart:math';

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

import 'package:flutter/services.dart';
import 'package:telr_payment_gateway/telr_payment_gateway.dart';

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

class MyApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = '未知';

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 平台消息是异步的,所以我们通过一个异步方法来初始化。
  Future<void> initPlatformState() async {
    String platformVersion;
    // 平台消息可能会失败,所以我们使用try/catch来处理PlatformException。
    // 我们还处理了消息可能返回null的情况。
    try {
      platformVersion =
          await TelrPaymentGateway.platformVersion ?? '未知平台版本';
    } on PlatformException {
      platformVersion = '获取平台版本失败。';
    }

    // 如果在异步平台消息飞行期间小部件从树中移除,我们希望丢弃回复而不是调用setState来更新我们不存在的外观。
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('插件示例应用'),
        ),
        body: Column(
          children: [
            Text('运行在: $_platformVersion\n'),
            SizedBox(height: 10),
            RaisedButton(
              child: Text('TelR 提交'),
              onPressed: () async {
                String message;
                // 平台消息可能会失败,所以我们使用try/catch来处理PlatformException。
                // 我们还处理了消息可能返回null的情况。
                try {
                  message = await TelrPaymentGateway.callTelRForTransaction(
                    store_id: "25798",
                    key: "Nbsw5^mDR5@3m9Nc",
                    amount: "20",
                    app_install_id: "123456",
                    app_name: "TelR",
                    app_user_id: "12345",
                    app_version: "1.0.0",
                    sdk_version: "123",
                    mode: "1",
                    tran_type: "sale",
                    tran_cart_id: "1003",
                    desc: "第一笔交易",
                    tran_lang: "EN",
                    tran_currency: "AED",
                    bill_city: "迪拜",
                    bill_country: "AE",
                    bill_region: "迪拜",
                    bill_address: "SIT GTower",
                    bill_first_name: "Deep",
                    bill_last_name: "Amin",
                    bill_title: "先生",
                    bill_email: "deep@innovuratech.com",
                    bill_phone: "528636596",
                  ) ?? '未知消息';
                } on PlatformException {
                  message = '获取消息失败。';
                }
                print("点击的消息 == $message");
              },
            ),
          ],
        ),
      ),
    );
  }
}

代码解释

  1. 导入必要的库

    import 'dart:math';
    import 'package:flutter/material.dart';
    import 'dart:async';
    import 'package:flutter/services.dart';
    import 'package:telr_payment_gateway/telr_payment_gateway.dart';
    
  2. 定义主应用类

    void main() {
      runApp(MyApp());
    }
    
  3. 初始化状态

    class MyApp extends StatefulWidget {
      [@override](/user/override)
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      String _platformVersion = '未知';
    
      [@override](/user/override)
      void initState() {
        super.initState();
        initPlatformState();
      }
    
  4. 获取平台版本

    Future<void> initPlatformState() async {
      String platformVersion;
      try {
        platformVersion =
            await TelrPaymentGateway.platformVersion ?? '未知平台版本';
      } on PlatformException {
        platformVersion = '获取平台版本失败。';
      }
    
      if (!mounted) return;
    
      setState(() {
        _platformVersion = platformVersion;
      });
    }
    
  5. 构建应用界面

    [@override](/user/override)
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('插件示例应用'),
          ),
          body: Column(
            children: [
              Text('运行在: $_platformVersion\n'),
              SizedBox(height: 10),
              RaisedButton(
                child: Text('TelR 提交'),
                onPressed: () async {
                  String message;
                  try {
                    message = await TelrPaymentGateway.callTelRForTransaction(
                      store_id: "25798",
                      key: "Nbsw5^mDR5@3m9Nc",
                      amount: "20",
                      app_install_id: "123456",
                      app_name: "TelR",
                      app_user_id: "12345",
                      app_version: "1.0.0",
                      sdk_version: "123",
                      mode: "1",
                      tran_type: "sale",
                      tran_cart_id: "1003",
                      desc: "第一笔交易",
                      tran_lang: "EN",
                      tran_currency: "AED",
                      bill_city: "迪拜",
                      bill_country: "AE",
                      bill_region: "迪拜",
                      bill_address: "SIT GTower",
                      bill_first_name: "Deep",
                      bill_last_name: "Amin",
                      bill_title: "先生",
                      bill_email: "deep@innovuratech.com",
                      bill_phone: "528636596",
                    ) ?? '未知消息';
                  } on PlatformException {
                    message = '获取消息失败。';
                  }
                  print("点击的消息 == $message");
                },
              ),
            ],
          ),
        ),
      );
    }
    

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

1 回复

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


telr_payment_gateway 是一个用于 Flutter 的支付网关插件,允许开发者集成 Telr 支付网关到他们的应用程序中。Telr 是一个支持多种支付方式的支付网关,包括信用卡、借记卡、Apple Pay、Google Pay 等。

以下是如何在 Flutter 项目中使用 telr_payment_gateway 插件的步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 telr_payment_gateway 插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  telr_payment_gateway: ^1.0.0  # 使用最新版本

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

2. 初始化支付网关

在你的 Dart 代码中,首先需要初始化 telr_payment_gateway 插件。

import 'package:telr_payment_gateway/telr_payment_gateway.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: PaymentScreen(),
    );
  }
}

class PaymentScreen extends StatefulWidget {
  [@override](/user/override)
  _PaymentScreenState createState() => _PaymentScreenState();
}

class _PaymentScreenState extends State<PaymentScreen> {
  final TelrPaymentGateway _telrPaymentGateway = TelrPaymentGateway();

  [@override](/user/override)
  void initState() {
    super.initState();
    _initializePaymentGateway();
  }

  void _initializePaymentGateway() async {
    await _telrPaymentGateway.initialize(
      storeId: 'YOUR_STORE_ID',
      authKey: 'YOUR_AUTH_KEY',
      testMode: true, // 设置为 false 在生产环境中使用
    );
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Telr Payment Gateway'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _startPayment,
          child: Text('Pay Now'),
        ),
      ),
    );
  }

  void _startPayment() async {
    final response = await _telrPaymentGateway.startPayment(
      amount: '100.00',
      currency: 'USD',
      orderId: 'ORDER12345',
      description: 'Sample Payment',
      customerEmail: 'customer@example.com',
      customerName: 'John Doe',
      customerPhone: '+1234567890',
      returnUrl: 'https://yourwebsite.com/return',
    );

    if (response.success) {
      // 支付成功
      print('Payment successful: ${response.transactionId}');
    } else {
      // 支付失败
      print('Payment failed: ${response.errorMessage}');
    }
  }
}
回到顶部