Flutter支付集成插件esewa_flutter的使用
Flutter支付集成插件esewa_flutter的使用
eSewa Flutter是一个非官方的Flutter插件,用于集成eSewa支付网关。通过这个插件,您可以轻松地将eSewa支付网关集成到您的Flutter应用中,并开始接受客户的付款。
主要特点
- 简单易用
- 不需要复杂的设置
- 纯Dart代码实现
- 支持开发和生产环境
环境要求
- Android:
minSdkVersion 19
并支持androidx
- iOS: Xcode版本 >= 11
设置步骤
添加依赖
在您的pubspec.yaml
文件中添加esewa_flutter
作为依赖项:
dependencies:
esewa_flutter: ^1.0.0
导入包
在您的Dart代码中导入该包:
import 'package:esewa_flutter/esewa_flutter.dart';
创建配置实例
创建一个包含支付信息的ESewaConfig
实例:
final config = ESewaConfig.live(
amt: 100, // 支付金额
scd: 'merchant_id', // 商户代码
pid: 'product_id', // 商品ID
su: 'https://success.com.np', // 成功回调URL
fu: 'https://failure.com.np', // 失败回调URL
);
初始化支付
调用Esewa.init()
方法来初始化支付:
final result = await Esewa.i.init(
context: context,
eSewaConfig: config,
);
检查支付结果
根据返回的结果判断支付是否成功:
if (result.hasData) {
// 支付成功
final response = result.data!;
print('Payment successful. Ref ID: ${response.refId}');
} else {
// 支付失败或取消
final error = result.error!;
print('Payment failed or cancelled. Error: $error');
}
示例Demo
下面是一个完整的示例项目,展示了如何在Flutter应用中集成eSewa支付:
import 'package:esewa_flutter/esewa_flutter.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Esewa Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const EsewaApp(title: 'Esewa Payment'),
);
}
}
class EsewaApp extends StatefulWidget {
const EsewaApp({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
State<EsewaApp> createState() => _EsewaAppState();
}
class _EsewaAppState extends State<EsewaApp> {
String refId = '';
String hasError = '';
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
EsewaPayButton(
paymentConfig: ESewaConfig.dev(
su: 'https://www.marvel.com/hello', // 成功回调URL
amt: 10, // 支付金额
fu: 'https://www.marvel.com/hello', // 失败回调URL
pid: '1212', // 商品ID
),
width: 100,
onFailure: (result) async {
setState(() {
refId = '';
hasError = result;
});
print(result);
},
onSuccess: (result) async {
setState(() {
hasError = '';
refId = result.refId!;
});
print(result.toJson());
},
),
if (refId.isNotEmpty)
Text('Console: Payment Success, Ref Id: $refId'),
if (hasError.isNotEmpty)
Text('Console: Payment Failed, Message: $hasError'),
],
),
),
);
}
}
更多关于Flutter支付集成插件esewa_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复