Flutter集成Remita支付插件remita_flutter_inline的使用

Flutter集成Remita支付插件remita_flutter_inline的使用

一个帮助你在Flutter应用中接受支付的Remita插件。

要求

  1. Remita API Keys
  2. 支持的Flutter版本 >= 1.17.0

安装

  1. 在你的pubspec.yaml文件中添加依赖项:
    remita_flutter_inline: 1.0.2
    
  2. 运行以下命令以获取依赖项:
    flutter pub get
    

使用

初始化一个RemitaPayment实例

要创建一个实例,你需要调用RemitaInlinePayment构造函数,并传递以下参数:

  • BuildContext
  • PaymentRequest
  • Customizer

这将返回一个RemitaInlinePayment实例。使用这个实例,我们调用异步方法.initiatePayment()

_handlePayment() async { 

    PaymentRequest request = PaymentRequest(
        environment: RemitaEnvironment.demo,
        rrr: 'pass your rrr here',
        key: 'enter your key here',
    );

    RemitaPayment remita = RemitaInlinePayment(
        buildContext: context,
        paymentRequest: request,
        customizer: Customizer(),
    );

    PaymentResponse response = await remita.initiatePayment();
}

处理响应

调用.initiatePayment()方法返回一个PaymentResponse的Future。

PaymentResponse response = await remita.initiatePayment();
if (response.code != null && response.code == '00') {
  // 交易成功
  // 在提供价值之前验证交易状态
} else {
  // 交易不成功
}

测试卡

CARD: 5178 6810 0000 0002,  
Expire Date : 05/30,  
CCV: 000, 
OTP: 123456

许可证

通过为Flutter库贡献,你同意你的贡献将在其MIT许可证下发布。

构建使用


示例代码

import 'dart:async';
import 'dart:developer';

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

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Example App',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const MyHomePage(title: 'Example Home'),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late GlobalKey<FormState> _formKey;
  late TextEditingController _controller;
  final _globalKey = GlobalKey<ScaffoldMessengerState>();

  [@override](/user/override)
  void initState() {
    super.initState();
    _formKey = GlobalKey<FormState>();
    _controller = TextEditingController();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Example App')),
      body: Form(
        key: _formKey,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              margin: EdgeInsets.symmetric(horizontal: MediaQuery.of(context).size.width * 0.1, vertical: 10),
              child: TextFormField(
                decoration: const InputDecoration(labelText: 'Enter RRR'),
                controller: _controller,
                validator: (_) => _controller.text.isEmpty ? 'RRR is required' : null,
              ),
            ),
            ElevatedButton(
              onPressed: () {
                final FormState? form = _formKey.currentState;
                if (form!.validate()) {
                  _initializePayment(context, _controller.text);
                }
              },
              child: const Text('Pay Now'),
            ),
          ],
        ),
      ),
    );
  }

  _initializePayment(BuildContext context, String rrr) async {
    PaymentRequest request = PaymentRequest(
      environment: RemitaEnvironment.demo,
      rrr: rrr,
      key: 'enter your key here',
    );

    RemitaPayment remita = RemitaInlinePayment(
      buildContext: context,
      paymentRequest: request,
      customizer: Customizer(),
    );

    PaymentResponse response = await remita.initiatePayment();
    if (response.code != null && response.code == '00') {
      // 交易成功
      // 在提供价值之前验证交易状态
    } else {
      // 交易不成功。
    }
    log(response.toString());
  }
}

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

1 回复

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


要在Flutter应用中集成Remita支付插件 remita_flutter_inline,你可以按照以下步骤进行操作:

1. 添加依赖

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

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

然后运行 flutter pub get 来获取依赖。

2. 初始化插件

在你的 main.dart 文件中,初始化 remita_flutter_inline 插件。你可以选择在 main 函数中初始化,或者在 initState 中初始化。

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

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  RemitaFlutterInline.initialize(
    publicKey: 'YOUR_PUBLIC_KEY',
    environment: RemitaEnvironment.TEST, // 或者 RemitaEnvironment.LIVE
  );
  runApp(MyApp());
}

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

3. 发起支付请求

在你的支付页面中,使用 RemitaFlutterInline 发起支付请求。你需要提供支付的相关信息,如 amountcustomerEmailcustomerName 等。

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

class PaymentScreen extends StatelessWidget {
  Future<void> initiatePayment() async {
    try {
      final response = await RemitaFlutterInline.makePayment(
        amount: '1000', // 金额
        customerEmail: 'customer@example.com', // 客户邮箱
        customerName: 'John Doe', // 客户姓名
        paymentDescription: 'Test Payment', // 支付描述
        transactionRef: 'TX123456789', // 交易参考号
        onSuccess: (response) {
          print('Payment Success: $response');
          // 处理支付成功逻辑
        },
        onError: (error) {
          print('Payment Error: $error');
          // 处理支付失败逻辑
        },
      );

      print('Payment Response: $response');
    } catch (e) {
      print('Error: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Remita Payment'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: initiatePayment,
          child: Text('Pay Now'),
        ),
      ),
    );
  }
}

4. 处理支付结果

onSuccessonError 回调中处理支付结果。你可以根据支付结果更新UI或执行其他操作。

5. 配置Android和iOS

确保在 AndroidManifest.xmlInfo.plist 文件中正确配置了必要的权限和设置。

Android

AndroidManifest.xml 中添加以下权限:

<uses-permission android:name="android.permission.INTERNET" />

iOS

Info.plist 中添加以下配置:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
回到顶部