Flutter支付插件esewa的使用

Flutter支付插件esewa的使用

特性

  • 使用eSewa支付

开始使用

此应用依赖于flutter_inappwebview插件。因此,你需要配置以下内容。

要求

  • Dart SDK: “>=2.15.0 <4.0.0”
  • Flutter: “>=3.0.0”
  • Android: minSdkVersion 19 并添加对androidx的支持(查看AndroidX迁移以迁移现有应用)
  • iOS 9.0+: --ios-language swift, Xcode版本 >= 14.3
  • MacOS 10.11+: Xcode版本 >= 14.3

安装

在你的pubspec.yaml文件中添加esewa作为依赖项。

使用方法

Navigator.push(
    context,
    MaterialPageRoute(
    builder: (context) => PayWithEsewaScreen(
      config: EsewaConfigModel.sandbox(
        amount: 10,
        taxAmount: 0,
        successUrl: 'https://example.com/success',
        failureUrl: 'https://example.com/failure',
        productId: 'product_id',
      ),
      onSuccess: (config, refId) {
        if (kDebugMode) {
          print('Payment successful');
        }
        if (kDebugMode) {
          print('RefId: $refId');
        }
        showSnackBar(context, 'Payment successful RefId: $refId');
        },
    onFailure: (errorMessage) {
        Navigator.pop(context);
        showSnackBar(context, errorMessage);
        if (kDebugMode) {
          print('Error: $errorMessage');
        }
      },
    ),
  ),
);

示例代码

import 'package:esewa/esewa.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // 这个小部件是你的应用的根
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Pay with Esewa',
      theme: ThemeData(
        useMaterial3: true,
        primarySwatch: Colors.green,
      ),
      home: const MyHomePage(title: 'Flutter Pay with Esewa'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const [
            Text(
              '点击浮动操作按钮使用eSewa付款',
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () {
          // 点击按钮时调用支付逻辑
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => PayWithEsewaScreen(
                config: EsewaConfigModel.sandbox(
                  amount: 10,
                  taxAmount: 0,
                  successUrl: 'https://example.com/success',
                  failureUrl: 'https://example.com/failure',
                  productId: 'product_id',
                ),
                onSuccess: (config, refId) {
                  if (kDebugMode) {
                    print('Payment successful');
                  }
                  if (kDebugMode) {
                    print('RefId: $refId');
                  }
                  showSnackBar(context, 'Payment successful RefId: $refId');
                },
                onFailure: (errorMessage) {
                  Navigator.pop(context);
                  showSnackBar(context, errorMessage);
                  if (kDebugMode) {
                    print('Error: $errorMessage');
                  }
                },
              ),
            ),
          );
        },
        isExtended: true,
        label: const Text('使用eSewa付款'),
        icon: const Icon(Icons.payment),
      ),
    );
  }
}

void showSnackBar(BuildContext context, String message) {
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(
      content: Text(message),
      duration: const Duration(seconds: 3),
      behavior: SnackBarBehavior.floating,
    ),
  );
}

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

1 回复

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


esewa 是尼泊尔广泛使用的一种支付方式,Flutter 开发者可以通过集成 esewa 插件来实现支付功能。以下是使用 esewa 插件的步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 esewa_flutter_sdk 依赖项:

dependencies:
  flutter:
    sdk: flutter
  esewa_flutter_sdk: ^1.0.0  # 请使用最新版本

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

2. 配置 esewa 支付参数

在使用 esewa 插件之前,你需要准备好以下参数:

  • Client ID: 你的 esewa 商户 ID。
  • Secret Key: 你的 esewa 商户密钥。
  • Environment: 选择环境 (testlive),test 用于测试,live 用于生产环境。

3. 初始化 esewa SDK

在你的 Dart 文件中,导入 esewa_flutter_sdk 并初始化 SDK:

import 'package:esewa_flutter_sdk/esewa_flutter_sdk.dart';

void initializeEsewa() {
  EsewaFlutterSdk.init(
    clientId: 'your_client_id',
    secretKey: 'your_secret_key',
    environment: Environment.test,  // 或 Environment.live
  );
}

4. 发起支付请求

使用 EsewaFlutterSdk 发起支付请求。以下是一个简单的支付请求示例:

import 'package:esewa_flutter_sdk/esewa_flutter_sdk.dart';

void makePayment() {
  final payment = EsewaPayment(
    productId: 'product_id',
    productName: 'Product Name',
    productPrice: '100',  // 金额
    callbackUrl: 'https://your-callback-url.com',
  );

  EsewaFlutterSdk.startPayment(
    payment: payment,
    onSuccess: (Map<String, dynamic> data) {
      print('Payment Successful: $data');
    },
    onFailure: (Map<String, dynamic> data) {
      print('Payment Failed: $data');
    },
    onCancellation: () {
      print('Payment Cancelled');
    },
  );
}
回到顶部