Flutter集成STK插件ilebora_stk的使用

Flutter集成STK插件ilebora_stk的使用

特性

  • STK推送支付: 通过STK推送发起M-Pesa支付,提供用户友好的体验。

安装

pubspec.yaml文件中添加以下依赖:

dependencies:
  ilebora_stk: ^1.0.0

然后运行以下命令来获取依赖:

flutter pub get

使用

首先,导入插件:

import 'package:ilebora_stk/ilebora_stk.dart';

初始化并使用插件:

void main() {
  // 初始化BoraSTK
  final boraSTK = BoraSTK(
    userID: 'developer_user_id', // 提供的用户ID
    apiKey: 'developer_api_key', // 提供的API密钥
    phone: '254712345678', // 客户手机号码(Safaricom)
    amount: '100.00', // 支付金额
    orderID: 'unique_order_id', // 唯一订单ID
    displayName: 'Display Name', // 在STK推送中显示的名称
    onSuccess: (BoraPushTransaction response) {
      // 成功回调
      print('Payment Successful!');
      print(response);
    },
    onFailure: (String err) {
      // 失败回调
      print('Payment Failed!');
      print('Error: $err');
    },
  );

  // 显示STK屏幕
  // boraSTK.showSTKScreen(
  //   context,
  //   backgroundColor: Colors.white,
  //   buttonColor: Colors.green,
  //   textColor: Colors.white,
  // );

  // 显示STK对话框
  // boraSTK.showSTKDialog(
  //   context,
  //   backgroundColor: Colors.white,
  //   buttonColor: Colors.green,
  //   textColor: Colors.white,
  // );
}

API参考

BoraSTK 类

  • 构造函数

    BoraSTK({
      required String userID,
      required String apiKey,
      required String phone,
      required String amount,
      required String orderID,
      required String displayName,
      required Function(BoraPushTransaction) onSuccess,
      required Function(String) onFailure,
    }): 初始化BoraSTK实例所需的参数。
    
  • 方法

    generateToken(): 生成用于API请求的身份验证令牌。
    
    showSTKScreen(BuildContext context, {...}): 以新路由的形式显示STK推送屏幕。
    
    showSTKDialog(BuildContext context, {...}): 在AlertDialog中显示STK推送屏幕。
    

许可证

该项目采用MIT许可证 - 详情见LICENSE文件。


示例代码

以下是完整的示例代码:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('STK Push Payment Example')),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              final BoraSTK boraSTK = BoraSTK(
                userID: 'developer_user_id', // 提供的用户ID
                apiKey: 'developer_api_key', // 提供的API密钥
                phone: '254712345678', // 客户手机号码(Safaricom)
                amount: '100.00', // 支付金额
                orderID: 'unique_order_id', // 唯一订单ID
                displayName: 'Display Name', // 在STK推送中显示的名称
                onSuccess: (BoraPushTransaction response) {
                  // 成功回调
                  print('Payment Successful!');
                  print(response);
                },
                onFailure: (String err) {
                  // 失败回调
                  print('Payment Failed!');
                  print('Error: $err');
                },
              );

              // 显示STK屏幕
              boraSTK.showSTKScreen(
                context,
                backgroundColor: Colors.white,
                buttonColor: Colors.green,
                textColor: Colors.white,
              );

              // 显示STK对话框
              // boraSTK.showSTKDialog(
              //   context,
              //   backgroundColor: Colors.white,
              //   buttonColor: Colors.green,
              //   textColor: Colors.white,
              // );
            },
            child: Text('Make Payment'),
          ),
        ),
      ),
    );
  }
}

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

1 回复

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


在Flutter中集成 ilebora_stk 插件(用于处理M-Pesa STK Push支付)的步骤如下:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  ilebora_stk: ^1.0.0  # 请确保使用最新版本

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

2. 配置Android和iOS项目

Android

确保在 android/app/build.gradle 文件中,minSdkVersion 设置为至少 16:

defaultConfig {
    applicationId "com.example.yourapp"
    minSdkVersion 16
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"
}

iOS

ios/Podfile 中,确保 platform 设置为至少 9.0:

platform :ios, '9.0'

然后运行 pod install 来更新 iOS 项目。

3. 使用 ilebora_stk 插件

在 Flutter 项目中,你可以使用 ilebora_stk 插件来发起 STK Push 请求。以下是一个简单的示例:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('STK Push Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              try {
                // 初始化 STK 配置
                final stk = IleboraStk(
                  businessShortCode: "YOUR_BUSINESS_SHORT_CODE",
                  password: "YOUR_PASSWORD",
                  timestamp: "YOUR_TIMESTAMP",
                  transactionType: "CustomerPayBillOnline",
                  amount: "10", // 金额
                  phoneNumber: "YOUR_PHONE_NUMBER",
                  callBackURL: "YOUR_CALLBACK_URL",
                  accountReference: "YOUR_ACCOUNT_REFERENCE",
                  transactionDesc: "Payment for services",
                );

                // 发起 STK Push 请求
                final response = await stk.initiateSTKPush();

                // 处理响应
                if (response['ResponseCode'] == '0') {
                  print('STK Push initiated successfully');
                } else {
                  print('Failed to initiate STK Push: ${response['ResponseDescription']}');
                }
              } catch (e) {
                print('Error: $e');
              }
            },
            child: Text('Initiate STK Push'),
          ),
        ),
      ),
    );
  }
}
回到顶部