Flutter支付功能插件gkash_payment的使用
Flutter支付功能插件gkash_payment的使用
本文将详细介绍如何在Flutter应用中集成Gkash支付插件。我们将通过一个完整的示例来演示整个过程。
使用
首先,我们需要导入必要的包并设置支付请求。以下是一个简单的示例代码:
import 'package:flutter/material.dart';
import 'package:gkash_payment/gkash_webview.dart';
import 'package:gkash_payment/model/payment_callback.dart';
import 'package:gkash_payment/model/payment_request.dart';
import 'package:gkash_payment/model/payment_response.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> implements PaymentCallback {
String? amountInput = "0.10";
final navigatorKey = GlobalKey<NavigatorState>();
String? _status = "",
_cartId = "",
_amount = "",
_currency = "",
_poId = "",
_paymentType = "",
_description = "",
_cid = "";
[@override](/user/override)
void getPaymentStatus(PaymentResponse response) {
setState(() {
_status = response.status;
_cartId = response.cartid;
_amount = response.amount;
_currency = response.currency;
_description = response.description;
_paymentType = response.PaymentType;
_poId = response.POID;
_cid = response.cid;
});
}
requestPayment() {
PaymentRequest request = PaymentRequest(
version: '1.5.0',
cid: "M102-U-xxx",
currency: 'MYR',
amount: amountInput,
cartid: DateTime.now().millisecondsSinceEpoch.toString(),
signatureKey: "YourSignatureKey",
isProd: false, // 如果是在生产环境中则设置为true
callbackUrl: "https://YourCallbackUrl.com/callback", // 设置你的回调URL(可选)
paymentCallback: this);
Navigator.of(navigatorKey.currentState!.context).push(
MaterialPageRoute(
builder: (_) {
return GkashWebView(request);
},
),
);
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: navigatorKey,
home: Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: const Text("GkashPayment"),
),
body: Container(
padding: const EdgeInsets.all(10),
child: Column(
children: <Widget>[
TextFormField(
validator: (value) {
if (value == null || value.isEmpty) {
return '请输入金额';
}
return null;
},
onChanged: (amount) {
amountInput = amount;
},
keyboardType:
const TextInputType.numberWithOptions(decimal: true),
decoration: const InputDecoration(
hintText: "金额",
),
),
const SizedBox(height: 30),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {
setState(() {
_status = "";
_cartId = "";
_amount = "";
_currency = "";
_description = "";
_paymentType = "";
_poId = "";
_cid = "";
});
requestPayment();
},
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('提交'),
),
),
),
const SizedBox(
height: 100,
),
Column(
children: [
Text("状态: ${_status!}"),
Text("描述: ${_description!}"),
Text("POID: ${_poId!}"),
Text("CID: ${_cid!}"),
Text("购物车ID: ${_cartId!}"),
Text("金额: ${_currency! + _amount!}"),
Text("支付类型: ${_paymentType!}"),
],
),
],
),
),
),
);
}
}
获取支付结果
当WebView活动结束时,需要实现GkashPaymentCallback
接口来处理支付响应。以下是具体的实现方法:
class MyHomePage extends StatefulWidget {
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> implements PaymentCallback {
[@override](/user/override)
void getPaymentStatus(PaymentResponse response) {
// 处理支付响应
setState(() {
_status = response.status;
_cartId = response.cartid;
_amount = response.amount;
_currency = response.currency;
_description = response.description;
_paymentType = response.PaymentType;
_poId = response.POID;
_cid = response.cid;
});
}
}
更多关于Flutter支付功能插件gkash_payment的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter支付功能插件gkash_payment的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
gkash_payment
是一个用于在 Flutter 应用中集成 Gkash 支付功能的插件。Gkash 是一个在马来西亚广泛使用的支付网关,支持多种支付方式,如信用卡、借记卡、网上银行等。以下是如何在 Flutter 应用中使用 gkash_payment
插件的详细步骤。
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 gkash_payment
插件的依赖。
dependencies:
flutter:
sdk: flutter
gkash_payment: ^1.0.0 # 请确保使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 配置 Android 和 iOS 项目
Android
在 android/app/build.gradle
文件中,确保 minSdkVersion
至少为 21。
android {
defaultConfig {
minSdkVersion 21
}
}
iOS
在 ios/Podfile
中,确保 platform
设置为 ios 11.0
或更高版本。
platform :ios, '11.0'
3. 初始化 Gkash 支付
在你的 Flutter 项目中,导入 gkash_payment
插件并初始化支付。
import 'package:gkash_payment/gkash_payment.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: PaymentScreen(),
);
}
}
class PaymentScreen extends StatefulWidget {
[@override](/user/override)
_PaymentScreenState createState() => _PaymentScreenState();
}
class _PaymentScreenState extends State<PaymentScreen> {
final GkashPayment _gkashPayment = GkashPayment();
[@override](/user/override)
void initState() {
super.initState();
_initializeGkash();
}
void _initializeGkash() async {
try {
await _gkashPayment.initialize(
clientId: 'your_client_id', // 替换为你的 Gkash 客户端 ID
clientSecret: 'your_client_secret', // 替换为你的 Gkash 客户端密钥
isSandbox: true, // 设置为 false 以使用生产环境
);
} catch (e) {
print('Failed to initialize Gkash: $e');
}
}
void _makePayment() async {
try {
final response = await _gkashPayment.makePayment(
amount: '100.00', // 支付金额
currency: 'MYR', // 货币类型
orderId: 'order123', // 订单 ID
description: 'Test Payment', // 支付描述
);
if (response['status'] == 'success') {
print('Payment successful: ${response['transactionId']}');
} else {
print('Payment failed: ${response['message']}');
}
} catch (e) {
print('Failed to make payment: $e');
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Gkash Payment'),
),
body: Center(
child: ElevatedButton(
onPressed: _makePayment,
child: Text('Pay with Gkash'),
),
),
);
}
}