Flutter支付集成插件telepay_dio的使用

Flutter支付集成插件telepay_dio的使用

TelePay Dart

style: very_good_analysis pub package License: MIT

TelePay 客户端用于 Dart 语言,通过使用 http 客户端 Dio 轻松处理加密货币支付。

开始使用

查看完整的示例 这里

安装

pubspec.yaml 文件中添加依赖:

dependencies:
    telepay_dio: <version>

首次请求

首先,创建一个 TelePayDio 实例以供后续使用。该实例接收两个参数:您的 API 私钥和一个 Dio 实例。

final telepay = TelePayDio(secretApiKey: 'your_secret_api_key', dio: Dio());

🚨 警告 🚨
您的 secretApiKey 是机密信息,仅限您使用。任何人拥有您的项目密钥都可以访问您的商户。请勿分享此密钥或将其提交到代码中。

方法

getMe

获取当前商户信息。

final me = await telepay.getMe();
getAssets

获取 TelePay 支持的所有资产。

final assets = await telepay.getAssets();
getBalance

获取所有钱包的余额。

final balance = await telepay.getBalance();
getInvoices

获取您的商户的所有发票。

final invoices = await telepay.getInvoices();
createInvoice

创建与您的商户关联的发票。

final invoiceCreate = await telepay.createInvoice(
    const CreateInvoice(
        asset: 'TON',
        blockchain: 'TON',
        network: 'testnet',
        amount: 3.5,
    ),
);
getInvoice

通过发票编号获取特定发票详情。

final invoice = await telepay.getInvoice('YYXZT1U7Q2P8');
cancelInvoice

通过发票编号取消发票。

final invoiceCancel = await telepay.cancelInvoice('FNGUA7LR6B');
deleteInvoice

通过发票编号删除发票。

final isDelete = await telepay.deleteInvoice('FNGUA7LR6BYYY');
transfer

在内部钱包之间创建转账,这是一个链下操作。

final transfer = await telepay.transfer(
    const CreateTransfer(
        asset: 'TON',
        blockchain: 'TON',
        network: 'testnet',
        amount: 3.5,
        username: 'yeikel16',
    ),
);
getWithdrawMinimum

获取特定资产的提现最低费用。

final withdrawMin =
    await telepay.getWithdrawMinimum('TON', 'TON', 'testnet');
getWithdrawFee

获取估计的提现费用,由区块链费用和处理费用组成。

final withdrawFee = await telepay.getWithdrawFee(
    const CreateWithdraw(
        asset: 'TON',
        blockchain: 'TON',
        network: 'testnet',
        amount: 5.2,
        toAddress: 'EQAwEl_ExMqFJIjfitPRPTdV_B9KTgHG-YognX6iKRWHdpX1',
    ),
);
withdraw

将资金提现到特定地址。

final withdraw = await telepay.withdraw(
    const CreateWithdraw(
        asset: 'TON',
        blockchain: 'TON',
        network: 'testnet',
        amount: 5.2,
        toAddress: 'EQAwEl_ExMqFJIjfitPRPTdV_B9KTgHG-YognX6iKRWHdpX1',
    ),
);

运行测试 🧪

要运行所有单元测试,请使用以下命令:

dart test --coverage="/coveraga" --test-randomize-ordering-seed random

要查看生成的覆盖率报告,可以使用 coverde

# 过滤测试文件。
coverde filter -f \.g\.dart

# 生成覆盖率报告并打开浏览器。
coverde report -l

报告问题或请求功能

如果您想报告问题或希望添加新功能,请自由地在 GitHub 上 打开一个 issue。欢迎提交 pull 请求。


示例代码

// ignore_for_file: avoid_print

import 'package:dio/dio.dart';
import 'package:telepay_dio/telepay_dio.dart';

const secretApiKey = 'you_secret_api_key';

Future<void> main(List<String> args) async {
  final telepay = TelePayDio(secretApiKey: secretApiKey, dio: Dio());
  try {
    // 获取当前商户信息。
    final me = await telepay.getMe();
    print(me);

    // 获取 TelePay 支持的所有资产。
    final asset = await telepay.getAssets();
    print(asset);

    // 获取所有钱包的余额。
    final balance = await telepay.getBalance();
    print(balance);

    // 获取所有发票。
    final invoices = await telepay.getInvoices();
    print(invoices);

    // 创建与商户关联的发票。
    final invoiceCreate = await telepay.createInvoice(
      const CreateInvoice(
        asset: 'TON',
        blockchain: 'TON',
        network: 'testnet',
        amount: 3.5,
      ),
    );
    print(invoiceCreate.number);

    // 通过发票编号获取特定发票详情。
    final invoice = await telepay.getInvoice(invoiceCreate.number);
    print(invoice);

    // 取消发票。
    final invoiceCancel = await telepay.cancelInvoice(invoice.number);
    print(invoiceCancel);

    // 删除发票。
    final isDelete = await telepay.deleteInvoice(invoiceCancel.number);
    print(isDelete);

    // 在内部钱包之间创建转账。
    final transfer = await telepay.transfer(
      const CreateTransfer(
        asset: 'TON',
        blockchain: 'TON',
        network: 'testnet',
        amount: 3.5,
        username: 'yeikel16',
      ),
    );
    print(transfer);

    // 获取特定资产的提现最低费用。
    final withdrawMin =
        await telepay.getWithdrawMinimum('TON', 'TON', 'testnet');
    print(withdrawMin);

    // 获取估计的提现费用。
    final withdrawFee = await telepay.getWithdrawFee(
      const CreateWithdraw(
        asset: 'TON',
        blockchain: 'TON',
        network: 'testnet',
        amount: 5.2,
        toAddress: 'EQAwEl_ExMqFJIjfitPRPTdV_B9KTgHG-YognX6iKRWHdpX1',
      ),
    );
    print(withdrawFee);

    // 将资金提现到特定地址。
    final withdraw = await telepay.withdraw(
      const CreateWithdraw(
        asset: 'TON',
        blockchain: 'TON',
        network: 'testnet',
        amount: 5.2,
        toAddress: 'EQAwEl_ExMqFJIjfitPRPTdV_B9KTgHG-YognX6iKRWHdpX1',
      ),
    );
    print(withdraw);
  } on TelePayException catch (e) {
    print(e.toString());
  }
}

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

1 回复

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


当然,以下是一个关于如何在Flutter项目中集成和使用telepay_dio插件的示例代码。请注意,由于telepay_dio可能是一个虚构或特定的支付插件,具体的API和功能可能会有所不同,以下代码仅为示例,你可能需要根据实际插件文档进行调整。

1. 添加依赖

首先,在你的pubspec.yaml文件中添加telepay_dio依赖:

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

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

2. 导入插件

在你的Dart文件中导入telepay_dio插件:

import 'package:telepay_dio/telepay_dio.dart';

3. 初始化支付插件

通常,支付插件需要一些初始化步骤,比如设置API密钥或配置其他支付参数。以下是一个假设的初始化示例:

void initTelepay() async {
  try {
    // 假设初始化需要API密钥
    String apiKey = "your_api_key_here";
    await TelepayDio.instance.initialize(apiKey: apiKey);
    print("Telepay initialized successfully.");
  } catch (e) {
    print("Error initializing Telepay: $e");
  }
}

4. 发起支付请求

接下来,你可以使用插件提供的方法来发起支付请求。以下是一个假设的支付请求示例:

void makePayment() async {
  try {
    // 假设支付请求需要一些参数,如订单ID、金额等
    String orderId = "123456";
    double amount = 100.0;
    String currency = "USD";
    
    // 构建支付请求
    Map<String, dynamic> paymentDetails = {
      'orderId': orderId,
      'amount': amount,
      'currency': currency,
      // 可能还有其他参数
    };
    
    // 发起支付请求
    var response = await TelepayDio.instance.createPayment(paymentDetails);
    
    // 处理响应
    if (response['status'] == 'success') {
      print("Payment successful. Payment ID: ${response['paymentId']}");
      // 处理支付成功逻辑
    } else {
      print("Payment failed. Error: ${response['error']}");
      // 处理支付失败逻辑
    }
  } catch (e) {
    print("Error making payment: $e");
  }
}

5. 在UI中调用支付功能

最后,在你的Flutter UI中调用上述初始化和支付函数。例如,在按钮点击事件中调用:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Telepay Integration Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              // 初始化Telepay
              await initTelepay();
              
              // 发起支付请求
              await makePayment();
            },
            child: Text('Make Payment'),
          ),
        ),
      ),
    );
  }
}

注意事项

  1. API文档:务必参考telepay_dio插件的官方文档,因为实际插件的API可能与上述示例有所不同。
  2. 错误处理:在实际应用中,应添加更详细的错误处理和用户反馈机制。
  3. 安全性:确保敏感信息(如API密钥)的安全存储和传输。

希望这个示例能帮你快速集成telepay_dio插件到你的Flutter项目中。如果有任何具体问题或需要进一步的帮助,请查阅插件的官方文档或联系插件开发者。

回到顶部