Flutter支付网关插件xpresspayments_gateway的使用
Flutter支付网关插件xpresspayments_gateway的使用
Xpresspayments 支付网关
这是一个用于轻松集成 Xpresspayments 支付网关到您的 Flutter 应用程序的库。它提供了多种支付方式:
- 卡交易
- Tokenization(令牌化)
- 银行
- 转账
开始使用
步骤 1
在 这里 注册以获取公钥。
步骤 2
void pay(BuildContext context) {
int date = DateTime.now().microsecondsSinceEpoch;
int transactionId = Random().nextInt(1000000).floor().toInt();
String publicKey = "yourvalidpublickey"; // 替换为您的有效公钥
InitialisePayment initialisePayment = InitialisePayment(
transactionId: (date + transactionId).toString(),
amount: 200.0,
email: "yourvalidemail@mail.com", // 替换为有效的电子邮件地址
productId: "12",
productDescription: "some description"
);
Xpay xpay = Xpay(context, publicKey, isLive: false); // 设置为 false 时为测试模式
xpay.transact(initialisePayment, (transaction) {
debugPrint("result: ${transaction.toString()}"); // 打印交易结果
}, (exception, transaction) {
debugPrint("error: " + exception.message); // 打印错误信息
});
}
截图
测试
我们提供测试卡供您使用,而不是使用自己的借记卡或信用卡。
5399 8300 0000 0008
MM/YY: 05/30
CVV: 000
PIN: 1234
TOKEN: 123456
PublicKey: XPPUBK-e634d14d9ded04eaf05d5b63a0a06d2f-X
完整示例代码
以下是一个完整的示例代码,展示如何在 Flutter 应用中使用 xpresspayments_gateway
插件。
示例代码
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:xpresspayments_gateway/xpresspayments_gateway.dart';
void main() {
runApp(const PaymentGateWayApp());
}
class PaymentGateWayApp extends StatelessWidget {
const PaymentGateWayApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Xpay',
theme: ThemeData(
primaryColor: Colors.blue,
),
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
// 获取当前时间戳作为交易 ID 的一部分
int get date => DateTime.now().microsecondsSinceEpoch;
// 随机生成交易 ID
int get transactionId => Random().nextInt(1000000).floor().toInt();
// 替换为您在 Xpresspayments 上注册的公钥
String get publicKey => "XPPUBK-e634d14d9ded04eaf05d5b63a0a06d2f-X";
// 初始化支付参数
InitialisePayment get initialisePayment => InitialisePayment(
transactionId: (date + transactionId).toString(),
amount: 100.0, // 替换为您要收取的实际金额
email: "adegbitefemisamson@gmail.com", // 替换为有效的电子邮件地址
productId: "12",
productDescription: "some description",
);
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.white,
child: Center(
child: TextButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(Colors.green),
),
onPressed: () {
pay(context);
},
child: const Text(
"Pay",
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
// 处理支付逻辑
void pay(BuildContext context) {
Xpay xpay = Xpay(context, publicKey, isLive: false); // 设置为 false 时为测试模式
xpay.transact(initialisePayment, (transaction) {
debugPrint("result: ${transaction.toString()}"); // 打印交易结果
}, (exception, transaction) {
debugPrint("error: " + exception.message); // 打印错误信息
});
}
}
更多关于Flutter支付网关插件xpresspayments_gateway的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter支付网关插件xpresspayments_gateway的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
xpresspayments_gateway
是一个用于在 Flutter 应用中集成 Xpress Payments 支付网关的插件。通过这个插件,你可以轻松地将支付功能集成到你的 Flutter 应用中,并处理用户的支付请求。
以下是如何在 Flutter 项目中使用 xpresspayments_gateway
插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 xpresspayments_gateway
插件的依赖项:
dependencies:
flutter:
sdk: flutter
xpresspayments_gateway: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 初始化支付网关
在你的 Dart 代码中,首先需要初始化支付网关。通常,你可以在 main.dart
或某个专门的支付服务类中进行初始化。
import 'package:xpresspayments_gateway/xpresspayments_gateway.dart';
void main() {
// 初始化支付网关
XpressPaymentsGateway.initialize(
publicKey: 'your_public_key',
secretKey: 'your_secret_key',
isLiveMode: false, // 设置为 true 以使用生产环境
);
runApp(MyApp());
}
3. 发起支付请求
接下来,你可以在需要发起支付的地方调用支付方法。通常,你需要提供订单信息、用户信息等。
import 'package:xpresspayments_gateway/xpresspayments_gateway.dart';
Future<void> makePayment() async {
try {
final paymentResponse = await XpressPaymentsGateway.makePayment(
amount: 1000, // 金额(以最小单位,例如 1000 表示 10.00 美元)
currency: 'USD', // 货币代码
email: 'user@example.com', // 用户邮箱
reference: 'order_12345', // 订单号
callbackUrl: 'https://example.com/callback', // 回调 URL
);
if (paymentResponse.success) {
// 支付成功
print('Payment successful: ${paymentResponse.message}');
} else {
// 支付失败
print('Payment failed: ${paymentResponse.message}');
}
} catch (e) {
// 处理异常
print('Error making payment: $e');
}
}