Flutter支付插件paymentez的使用

发布于 1周前 作者 phonegap100 来自 Flutter

Flutter支付插件paymentez的使用

安装

要使用 paymentez 插件,请遵循以下步骤:

  1. 在你的 pubspec.yaml 文件中添加 paymentez 依赖:

    dependencies:
      paymentez: ^版本号
    
  2. 运行 flutter pub get 来获取新安装的包。

使用示例

在使用示例之前,你需要从 Paymentez 获取 paymentClientAppCodepaymentClientAppKey。这些凭据用于验证你对 Paymentez 的访问权限。将它们添加到你的 main.dart 文件中。

示例代码

import 'package:flutter/material.dart';
import 'package:paymentez/paymentez.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Paymentez',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Add card Paymentez'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            MaterialButton(
              elevation: 10,
              color: Colors.amber,
              child: Text('添加卡片'),
              onPressed: () {
                showDialogPaymentez(
                  context: context,
                  isDebug: true,
                  uid: 'ec774d32-8c3d-4df5-af5e-77e5177f4d1d',
                  email: 'user@softnux.io',
                  environment: 'stg', // stg -> 开发者环境 或 prod -> 生产环境
                  paymentClientAppCode: 'TPP3-EC-CLIENT',
                  paymentClientAppKey: 'ZfapAKOk4QFXheRNvndVib9XU3szzg',
                  background: 'F3F3F5',
                  btnBackground1: 'f02b6c',
                  btnBackground2: 'f02b6c',
                  textAddCard: '添加卡片',
                  textProcessingCard: '处理卡片',
                  funReturnData: (DataModel data, controller) {
                    switch (data.status) {
                      case 'success':
                        print('成功添加卡片');
                        print(data.cvc); // 打印卡片安全码

                        // 关闭WebView窗口
                        controller.goBack();

                        // 关闭对话框
                        Navigator.of(context).pop();
                        break;

                      case 'pending':
                        print('待审核卡片');

                        print(data.cvc);

                        controller.goBack();
                        Navigator.of(context).pop();
                        break;

                      case 'failure':
                        print('失败添加卡片');
                        controller.goBack();
                        Navigator.of(context).pop();
                        break;

                      case 'error':
                        print(data.error.type);
                        // 可以在这里添加一个通知,提示添加卡片时发生错误。
                        break;
                    }
                  }
                );
              },
            )
          ],
        ),
      )
    );
  }
}

克隆并运行示例项目

  1. 克隆示例项目:

    git clone https://github.com/Krysthyan/paymentez.git
    cd paymentez/example
    
  2. 运行项目:

    flutter run
    

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

1 回复

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


当然,下面是一个关于如何在Flutter项目中使用Paymentez支付插件的示例代码。请注意,这只是一个基本示例,实际项目中可能需要更多的错误处理和配置。

首先,你需要在Flutter项目中添加Paymentez插件。假设你已经有了一个Flutter项目,你可以通过以下步骤添加Paymentez插件。

  1. 添加依赖

    打开你的pubspec.yaml文件,并在dependencies部分添加以下依赖:

    dependencies:
      flutter:
        sdk: flutter
      paymentez_flutter: ^x.y.z  # 请使用最新版本号替换x.y.z
    

    然后运行flutter pub get来安装依赖。

  2. 配置Android

    打开android/app/src/main/AndroidManifest.xml文件,确保你有必要的权限配置(如互联网权限)。

    <uses-permission android:name="android.permission.INTERNET"/>
    
  3. 配置iOS

    对于iOS,确保你的Info.plist文件中有必要的配置(如网络权限)。

  4. 初始化Paymentez

    在你的Flutter项目中,初始化Paymentez支付插件。通常你会在main.dart或类似的文件中进行初始化。

    import 'package:flutter/material.dart';
    import 'package:paymentez_flutter/paymentez_flutter.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Paymentez Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: PaymentScreen(),
        );
      }
    }
    
    class PaymentScreen extends StatefulWidget {
      @override
      _PaymentScreenState createState() => _PaymentScreenState();
    }
    
    class _PaymentScreenState extends State<PaymentScreen> {
      final PaymentezFlutter paymentez = PaymentezFlutter();
    
      void makePayment() async {
        try {
          // 配置支付参数
          var paymentParams = PaymentezPaymentParams(
            publicKey: 'your_public_key',  // 替换为你的公钥
            privateKey: 'your_private_key', // 替换为你的私钥
            amount: 100.0,  // 支付金额
            currency: 'USD',  // 货币类型
            orderId: 'order_123456',  // 订单ID
            description: 'Payment for product X',  // 支付描述
            successUrl: 'https://yourwebsite.com/success',  // 支付成功后的回调URL
            failureUrl: 'https://yourwebsite.com/failure',  // 支付失败后的回调URL
          );
    
          // 发起支付
          var response = await paymentez.startPayment(paymentParams);
          print('Payment response: ${response.toJson()}');
    
          // 处理支付结果
          if (response.status == 'APPROVED') {
            // 支付成功处理逻辑
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  title: Text("Payment Successful"),
                  content: Text("Transaction ID: ${response.transactionId}"),
                  actions: <Widget>[
                    TextButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: Text("OK"),
                    ),
                  ],
                );
              },
            );
          } else {
            // 支付失败处理逻辑
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  title: Text("Payment Failed"),
                  content: Text("Error: ${response.errorMessage}"),
                  actions: <Widget>[
                    TextButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: Text("OK"),
                    ),
                  ],
                );
              },
            );
          }
        } catch (e) {
          print('Error: $e');
          showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text("Payment Error"),
                content: Text("An error occurred while processing payment."),
                actions: <Widget>[
                  TextButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: Text("OK"),
                  ),
                ],
              );
            },
          );
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Paymentez Payment Demo'),
          ),
          body: Center(
            child: ElevatedButton(
              onPressed: makePayment,
              child: Text('Make Payment'),
            ),
          ),
        );
      }
    }
    
  5. 注意事项

    • 请确保你替换了your_public_keyyour_private_key为你的实际Paymentez API密钥。
    • orderId应该是唯一的,以避免重复支付。
    • successUrlfailureUrl是支付成功或失败后重定向的URL。

这个示例展示了如何在Flutter应用中集成Paymentez支付插件,并处理支付结果。实际应用中,你可能需要添加更多的错误处理和用户体验优化。

回到顶部