Flutter伊拉克支付插件fib_iraq_payment的使用

Flutter伊拉克支付插件fib_iraq_payment的使用

《FIB IRAQ Payment Flutter Package》

《FIB IRAQ Payment》插件为你的Flutter应用提供了与FIB伊拉克支付网关的便捷集成。它允许开发者创建支付、检查支付状态、生成付款二维码等。此插件简化了将FIB伊拉克支付系统集成到你的应用中的过程,且所需的设置最少。

特性

  • 创建支付:通过指定金额、描述和回调URL来发起支付。
  • 检查支付状态:使用支付ID来验证支付的状态。
  • 生成二维码:生成可以被FIB应用扫描的支付二维码。
  • 退款支付:对特定支付进行退款。
  • 启动应用链接:直接从支付界面打开个人或商业的FIB应用。
  • 应用模式:支持stagedevprod模式。
    • 模式由FIB在测试后确定。
    • 在部署生产环境前,确保将模式设置为prod(或其他由FIB指定的模式)。

安装

在你的pubspec.yaml文件中添加fib_iraq_payment作为依赖项:

dependencies:
  fib_iraq_payment: ^0.0.2

然后运行flutter pub get以安装该插件。

使用

1. 初始化支付服务

你需要通过设置clientIdclientSecret来初始化FIBService。这些凭证由FIB伊拉克提供。

import 'package:fib_iraq_payment/fib_iraq_payment.dart';

final FIBService fibService = FIBService();

void main() {
  fibService.clientId = 'your-client-id';
  fibService.clientSecret = 'your-client-secret';
  fibService.mode = 'stage'; // 设置模式为`dev`、`stage`或`prod`(由FIB确定)
  runApp(const MyApp());
}

2. 创建支付

要创建支付,需要调用createPayment方法并传递金额、描述和回调URL。回调URL是FIB系统将发送支付状态更新的地方。

Future<void> _createPayment() async {
  final payment = await fibService.createPayment(
    250, // 金额(单位:IQD)
    '支付服务费用',
    'https://your-callback-url.com', // 支付状态回调URL
  );

  setState(() {
    _paymentId = payment['paymentId'];
    _qrCodeImage = fibService.base64ToImage(payment['qrCode'].split(',')[1]);
    personalAppLink = payment['personalAppLink'];
    businessAppLink = payment['businessAppLink'];
  });
}

3. 检查支付状态

创建支付后,你可以使用checkPaymentStatus方法来检查其状态。传递支付ID以获取状态。

void _checkPaymentStatus() async {
  final status = await fibService.checkPaymentStatus(_paymentId);
  setState(() {
    _status = status['status'];
  });
}

4. 退款支付

可以通过调用refundPayment方法并传递支付ID来对特定支付进行退款:

void _refundPayment(String paymentId) async {
  final refundStatus = await fibService.refundPayment(paymentId);
  print('退款状态: $refundStatus');
}

5. 启动个人或商业应用链接

一旦支付创建成功,用户可以被重定向到FIB个人或商业应用来完成支付。你可以使用url_launcher包来启动应用:

void _launchPersonalApp(String url) async {
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw '无法启动$url';
  }
}

完整示例

以下是展示如何使用FIB IRAQ Payment插件的完整示例:

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

final FIBService fibService = FIBService();

void main() {
  fibService.clientId = 'your-client-id';
  fibService.clientSecret = 'your-client-secret';
  fibService.mode = 'stage'; // 设置模式为`dev`、`stage`或`prod`(由FIB确定)
  runApp(const FIBPaymentApp());
}

class FIBPaymentApp extends StatelessWidget {
  const FIBPaymentApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'FIB支付应用',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const PaymentScreen(),
    );
  }
}

class PaymentScreen extends StatefulWidget {
  const PaymentScreen({super.key});

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

class _PaymentScreenState extends State<PaymentScreen> {
  final FIBService fibService = FIBService();
  String _paymentId = '';
  String _status = '尚未支付';
  Uint8List? _qrCodeImage;
  String personalAppLink = '';
  String businessAppLink = '';

  Future _createPayment() async {
    final payment = await fibService.createPayment(
      250,
      '支付服务费用',
      'https://your-callback-url.com',
    );

    setState(() {
      _paymentId = payment['paymentId'];
      _qrCodeImage = fibService.base64ToImage(payment['qrCode'].split(',')[1]);
      personalAppLink = payment['personalAppLink'];
      businessAppLink = payment['businessAppLink'];
    });

    return payment;
  }

  void _checkPaymentStatus() async {
    if (_paymentId.isEmpty) return;
    final status = await fibService.checkPaymentStatus(_paymentId);
    setState(() {
      _status = status['status'];
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    Future.delayed(const Duration(seconds: 5), () {
      _checkPaymentStatus();
    });

    return Scaffold(
      appBar: AppBar(
        title: const Text('FIB支付应用'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            _qrCodeImage != null
                ? Image.memory(_qrCodeImage!)
                : const Text('尚未生成二维码'),
            const SizedBox(height: 10),
            Text('支付ID: $_paymentId'),
            const SizedBox(height: 10),
            Text('支付状态: $_status'),
            const SizedBox(height: 10),
            ElevatedButton(
              onPressed: _createPayment,
              child: const Text('通过个人应用支付'),
            ),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


当然,以下是如何在Flutter项目中集成和使用fib_iraq_payment插件的示例代码。这个插件假定是为了处理伊拉克地区的支付功能。由于我无法直接访问最新的插件文档或源代码,以下代码是基于一般Flutter插件的使用习惯编写的,实际使用时请根据插件的官方文档进行调整。

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  fib_iraq_payment: ^最新版本号  # 请替换为实际可用的最新版本号

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

2. 导入插件

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

import 'package:fib_iraq_payment/fib_iraq_payment.dart';

3. 初始化插件并设置支付参数

在使用插件之前,你需要初始化它并设置必要的支付参数。这通常包括API密钥、商户ID、支付金额等。

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('伊拉克支付示例'),
        ),
        body: PaymentScreen(),
      ),
    );
  }
}

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

class _PaymentScreenState extends State<PaymentScreen> {
  @override
  void initState() {
    super.initState();
    // 初始化支付插件(假设需要API密钥和商户ID)
    FibIraqPayment.instance.initialize(
      apiKey: '你的API密钥',
      merchantId: '你的商户ID',
    );
  }

  void _startPayment() async {
    try {
      // 设置支付参数
      var paymentDetails = PaymentDetails(
        amount: 100.0,  // 支付金额
        currency: 'IQD',  // 货币代码
        description: '商品描述',
      );

      // 发起支付请求
      var paymentResult = await FibIraqPayment.instance.startPayment(paymentDetails);

      // 处理支付结果
      if (paymentResult.status == PaymentStatus.success) {
        print('支付成功: ${paymentResult.transactionId}');
      } else {
        print('支付失败: ${paymentResult.errorMessage}');
      }
    } catch (e) {
      print('支付过程中发生错误: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        onPressed: _startPayment,
        child: Text('发起支付'),
      ),
    );
  }
}

// 假设的支付详情和结果类(根据插件实际API调整)
class PaymentDetails {
  final double amount;
  final String currency;
  final String description;

  PaymentDetails({required this.amount, required this.currency, required this.description});
}

class PaymentResult {
  final PaymentStatus status;
  final String? transactionId;
  final String? errorMessage;

  PaymentResult({required this.status, this.transactionId, this.errorMessage});
}

enum PaymentStatus { success, failure, pending, unknown }

注意

  1. 依赖版本:确保使用最新的fib_iraq_payment插件版本。
  2. API密钥和商户ID:替换为你实际的API密钥和商户ID。
  3. 支付详情和结果类:这些类(PaymentDetailsPaymentResult)是假设的,具体字段和方法应根据插件的实际API进行调整。
  4. 错误处理:在实际应用中,应该添加更详细的错误处理和用户反馈。

结论

以上代码提供了一个基本的框架,展示了如何在Flutter项目中集成和使用fib_iraq_payment插件进行支付。请根据插件的官方文档和API参考进行进一步的调整和完善。

回到顶部