Flutter移动支付插件flutter_jiopay_pg的使用
Flutter移动支付插件flutter_jiopay_pg的使用
JioPay Flutter
Flutter 插件用于 JioPay SDK。
开始使用
此 Flutter 插件是围绕我们的 Android 和 iOS SDK 构建的包装器。
安装
在项目的 pubspec.yaml 文件中添加以下依赖:
flutter_jiopay_pg: ^0.0.1
注意(Android):确保应用程序的最低 API 级别为 19 或更高。
使用方法
示例代码可以在 example/lib/main.dart 中找到。
导入包
import 'package:flutter_jiopay_pg/flutter_jiopay_pg.dart';
创建 Jiopay 实例
JiopayPg jiopayFlutterPlugin = JiopayPg();
添加事件监听器
该插件使用基于事件的通信方式,并在支付失败或成功时发出事件。
事件名称通过 JiopayPg 类中的常量 EVENT_PAYMENT_SUCCESS 和 EVENT_PAYMENT_ERROR 暴露。
使用 JiopayPg 实例上的 on(String event, Function handler) 方法附加事件监听器。
jiopayFlutterPlugin.on(JiopayPg.EVENT_PAYMENT_SUCCESS, _handlePaymentSuccess);
jiopayFlutterPlugin.on(JiopayPg.EVENT_PAYMENT_ERROR, _handlePaymentError);
处理程序定义如下:
void _handlePaymentSuccess(PaymentSuccessResponse response) {
debugPrint(msg: "SUCCESS: " + response.tnxId!);
}
void _handlePaymentError(PaymentFailureResponse response) {
debugPrint('_handlePaymentError: $response');
}
设置选项
var options = {
'appaccesstoken': "APP_ACCESS_TOKEN",
'appidtoken': "APP_ID_TOKEN",
'intentid': "INTENT_ID",
'buildVariant': "prod", // 可选值:pp, sit, prod
'theme': {
'brandColor': "BRAND_COLOR_HEX",
'bodyBgColor': "BODY_BG_COLOR_HEX",
'bodyTextColor': "BODY_TEXT_COLOR",
'headingText': "HEADING_TEXT_COLOR",
},
};
打开支付页面
jiopayFlutterPlugin.open(options);
示例代码
以下是完整的示例代码,包含主应用文件和支付页面。
主应用文件 (example/lib/main.dart)
import 'package:flutter/material.dart';
import 'HomeScreen.dart'; // 引入支付页面
void main() {
runApp(const MyApp()); // 启动应用
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState(); // 初始化状态
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown'; // 平台版本信息
[@override](/user/override)
void initState() {
super.initState();
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp( // 应用主体
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'), // 应用标题
),
body: HomeScreen(), // 显示支付页面
),
);
}
}
支付页面 (example/lib/HomeScreen.dart)
import 'package:flutter/material.dart';
import 'package:flutter_jiopay_pg/flutter_jiopay_pg.dart';
class HomeScreen extends StatefulWidget {
[@override](/user/override)
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final JiopayPg _jiopayFlutterPlugin = JiopayPg(); // 创建 Jiopay 实例
[@override](/user/override)
void initState() {
super.initState();
// 添加事件监听器
_jiopayFlutterPlugin.on(JiopayPg.EVENT_PAYMENT_SUCCESS, _handlePaymentSuccess);
_jiopayFlutterPlugin.on(JiopayPg.EVENT_PAYMENT_ERROR, _handlePaymentError);
}
[@override](/user/override)
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
onPressed: () {
// 打开支付页面
_jiopayFlutterPlugin.open({
'appaccesstoken': "APP_ACCESS_TOKEN",
'appidtoken': "APP_ID_TOKEN",
'intentid': "INTENT_ID",
'buildVariant': "prod",
'theme': {
'brandColor': "#FF5722",
'bodyBgColor': "#FFFFFF",
'bodyTextColor': "#000000",
'headingText': "#000000",
},
});
},
child: Text("支付"),
),
);
}
// 处理支付成功事件
void _handlePaymentSuccess(PaymentSuccessResponse response) {
debugPrint("SUCCESS: " + response.tnxId!);
}
// 处理支付失败事件
void _handlePaymentError(PaymentFailureResponse response) {
debugPrint('_handlePaymentError: $response');
}
}
更多关于Flutter移动支付插件flutter_jiopay_pg的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter移动支付插件flutter_jiopay_pg的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_jiopay_pg 是一个用于集成 JioPay 支付网关的 Flutter 插件。通过这个插件,你可以在 Flutter 应用中轻松集成 JioPay 支付功能,从而实现移动支付。
以下是如何使用 flutter_jiopay_pg 插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml 文件中添加 flutter_jiopay_pg 插件的依赖。打开 pubspec.yaml 文件,在 dependencies 部分添加以下内容:
dependencies:
flutter:
sdk: flutter
flutter_jiopay_pg: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get 来获取依赖。
2. 导入插件
在你的 Dart 文件中导入 flutter_jiopay_pg 插件:
import 'package:flutter_jiopay_pg/flutter_jiopay_pg.dart';
3. 初始化支付
在调用支付功能之前,你需要初始化支付插件。通常,你需要在应用启动时或在支付页面初始化时调用初始化方法。
JioPayPG.initialize(
merchantId: 'YOUR_MERCHANT_ID',
merchantKey: 'YOUR_MERCHANT_KEY',
environment: JioPayEnvironment.test, // 使用测试环境或生产环境
);
4. 发起支付
使用 JioPayPG 类提供的 startPayment 方法来发起支付。你需要传递支付相关的参数,如订单号、金额、回调 URL 等。
JioPayPG.startPayment(
orderId: 'ORDER_ID',
amount: '100.00', // 金额
currency: 'INR', // 货币类型
description: 'Payment for order', // 订单描述
customerName: 'John Doe', // 客户姓名
customerEmail: 'john.doe@example.com', // 客户邮箱
customerPhone: '1234567890', // 客户电话
returnUrl: 'https://your-return-url.com', // 支付成功后的回调 URL
onSuccess: (response) {
// 支付成功处理
print('Payment Success: ${response.toString()}');
},
onFailure: (response) {
// 支付失败处理
print('Payment Failed: ${response.toString()}');
},
onCancel: () {
// 用户取消支付处理
print('Payment Cancelled');
},
);
5. 处理支付结果
支付结果会通过 onSuccess、onFailure 和 onCancel 回调返回。你可以在这些回调中处理支付成功、失败或取消的情况。
6. 处理回调 URL
在支付完成后,JioPay 会重定向到你提供的 returnUrl。你需要在这个 URL 对应的页面中处理支付结果,并更新订单状态。
7. 测试与生产环境
在开发阶段,你可以使用 JioPayEnvironment.test 来测试支付功能。在发布应用时,请切换到 JioPayEnvironment.production。
8. 处理异常
在实际使用中,可能会遇到网络问题或其他异常情况。你可以通过 try-catch 块来捕获异常并进行处理。
try {
await JioPayPG.startPayment(...);
} catch (e) {
print('Error: $e');
}
9. 调试与日志
在开发过程中,你可以启用调试日志来查看插件的运行情况。你可以在初始化时设置 debug 参数为 true。
JioPayPG.initialize(
merchantId: 'YOUR_MERCHANT_ID',
merchantKey: 'YOUR_MERCHANT_KEY',
environment: JioPayEnvironment.test,
debug: true,
);

