Flutter支付集成插件payable_ipg的使用

Flutter支付集成插件payable_ipg的使用

PAYable IPG - Flutter Integration

Flutter Package - ipg.flutter.payable.lk | Create Issue

Pub
Build Status


初始化

1. 修改 minSdkVersion

在您的应用级 build.gradle 文件中将 minSdkVersion 修改为以下内容:

android {
    defaultConfig {
        minSdkVersion 20
    }
}

2. 在 pubspec.yaml 中添加依赖包

在项目的 pubspec.yaml 文件中添加以下内容:

payable_ipg: ^1.1.1

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


使用

1. 导入 PAYable IPG SDK 包

在 Dart 文件中导入 payable_ipg 包:

import 'package:payable_ipg/payable_ipg.dart';

2. 创建 PAYable IPG 客户端

创建一个 PAYableIPGClient 实例,并传入商户密钥、商户令牌、回调 URL 等信息:

PAYableIPGClient ipgClient = PAYableIPGClient(
  merchantKey: "YOUR_MERCHANT_KEY", // 商户密钥
  merchantToken: "YOUR_MERCHANT_TOKEN", // 商户令牌
  refererUrl: "YOUR_REQUEST_URL", // 回调 URL
  logoUrl: "YOUR_COMPANY_LOGO", // 公司 LOGO URL
  environment: IPGEnvironment.sandbox, // 可选:沙盒环境
);

3. 调用 PAYable IPG

在应用中使用 PAYableIPG 组件完成支付操作:

PAYableIPG(
  ipgClient: ipgClient, // 之前创建的客户端实例
  amount: 100.45, // 支付金额
  currencyCode: "LKR", // 币种
  orderDescription: "Netflix", // 订单描述
  customerFirstName: "Aslam", // 用户名
  customerLastName: "Kasun",
  customerEmail: "test@org.lk",
  customerMobilePhone: "0777123456",
  billingAddressStreet: "Hill Street", // 账单地址
  billingAddressCity: "Dehiwala",
  billingAddressCountry: "LK",
  billingAddressPostcodeZip: "10350",
  onPaymentCompleted: (data) { // 支付成功回调
    print("onPaymentCompleted: $data");
  },
  onPaymentCancelled: () { // 支付取消回调
    print("onPaymentCancelled");
  },
  onPaymentError: (data) { // 支付错误回调
    print("onPaymentError: $data");
  },
)

注意:配送信息和通知 URL 是可选的。


示例代码

以下是完整的示例代码:

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

void main() {
  // 初始化 PAYable IPG 客户端
  PAYableIPGClient ipgClient = PAYableIPGClient(
    merchantKey: "A75BCD8EF30E529A", // 替换为实际的商户密钥
    merchantToken: "B8727C74D29E210F9A297B65690C0140", // 替换为实际的商户令牌
    refererUrl: "https://www.sandboxmerdev.payable.lk", // 替换为实际的回调 URL
    logoUrl: "https://i.imgur.com/l21F5us.png", // 替换为实际的公司 LOGO URL
    environment: IPGEnvironment.sandbox, // 沙盒环境
  );

  // 创建 PAYable IPG 组件
  PAYableIPG payableIPG = PAYableIPG(
    ipgClient: ipgClient,
    amount: 100.45, // 支付金额
    currencyCode: "LKR", // 币种
    orderDescription: "Netflix", // 订单描述
    customerFirstName: "Aslam", // 用户名
    customerLastName: "Kasun",
    customerEmail: "test@org.lk",
    customerMobilePhone: "0777123456",
    billingAddressStreet: "Hill Street", // 账单地址
    billingAddressCity: "Dehiwala",
    billingAddressCountry: "LK",
    billingAddressPostcodeZip: "10350",
    onPaymentCompleted: (data) { // 支付成功回调
      print("onPaymentCompleted: $data");
    },
    onPaymentCancelled: () { // 支付取消回调
      print("onPaymentCancelled");
    },
    onPaymentError: (data) { // 支付错误回调
      print("onPaymentError: $data");
    },
  );

  // 启动应用
  runApp(
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text("PAYable IPG Demo"), // 应用标题
        ),
        body: payableIPG, // PAYable IPG 组件
      ),
    ),
  );
}

高级用法

1. 使用会话 ID (uid) 进行支付

如果已知会话 ID (uid),可以直接通过 PAYableIPGSession 进行支付:

PAYableIPGSession payableIPGSession = PAYableIPGSession(
  uid: "9727F698-C1CE-4E22-96C6-9635EE600BA3", // 会话 ID
  onPaymentCompleted: (data) { // 支付成功回调
    print("onPaymentCompleted: $data");
  },
  onPaymentCancelled: () { // 支付取消回调
    print("onPaymentCancelled");
  },
  onPaymentError: (data) { // 支付错误回调
    print("onPaymentError: $data");
  },
  environment: IPGEnvironment.sandbox, // 环境设置
);

2. 查询交易状态

可以使用 getStatus 方法查询交易状态:

var data = await ipgClient.getStatus("uid", "resultIndicator");
print(data); // 打印返回结果

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

1 回复

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


payable_ipg 是一个用于 Flutter 应用的支付集成插件,主要支持 IPG(Internet Payment Gateway)支付。IPG 是一种在线支付网关,允许用户通过信用卡、借记卡或其他支付方式进行支付。payable_ipg 插件简化了在 Flutter 应用中集成 IPG 支付的过程。

以下是如何在 Flutter 项目中使用 payable_ipg 插件的基本步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 payable_ipg 插件的依赖:

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

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

2. 导入插件

在需要使用支付功能的 Dart 文件中导入 payable_ipg 插件:

import 'package:payable_ipg/payable_ipg.dart';

3. 初始化支付

在使用支付功能之前,通常需要初始化支付网关。这可以通过 PayableIPG 类的 initialize 方法来完成:

PayableIPG.initialize(
  merchantId: 'YOUR_MERCHANT_ID',
  apiKey: 'YOUR_API_KEY',
  baseUrl: 'https://your-ipg-gateway-url.com', // IPG 网关的基础 URL
);

4. 发起支付

使用 PayableIPG 类的 pay 方法来发起支付。你需要提供支付相关的参数,例如订单号、金额、货币类型等:

try {
  final response = await PayableIPG.pay(
    orderId: 'ORDER12345',
    amount: 100.0, // 支付金额
    currency: 'USD', // 货币类型
    customerEmail: 'customer@example.com',
    customerPhone: '1234567890',
    description: 'Payment for order ORDER12345',
  );

  if (response.status == 'success') {
    // 支付成功
    print('Payment successful: ${response.transactionId}');
  } else {
    // 支付失败
    print('Payment failed: ${response.message}');
  }
} catch (e) {
  // 处理异常
  print('Payment error: $e');
}

5. 处理支付结果

支付结果通常会通过 response 对象返回。你可以根据 response.status 来判断支付是否成功,并获取交易 ID 或其他相关信息。

6. 处理回调(可选)

某些 IPG 网关可能支持回调功能,用于在支付完成后通知你的服务器。你可以在初始化时指定回调 URL,并在服务器上处理支付结果。

7. 测试支付

在开发过程中,确保使用测试环境和测试账户来进行支付测试,以避免产生实际的支付交易。

8. 发布应用

在发布应用之前,确保所有支付相关的配置(如商户 ID、API 密钥等)都已正确设置为生产环境的值。

示例代码

以下是一个完整的示例代码,展示了如何在 Flutter 应用中使用 payable_ipg 插件进行支付:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: PaymentScreen(),
    );
  }
}

class PaymentScreen extends StatelessWidget {
  Future<void> initiatePayment() async {
    PayableIPG.initialize(
      merchantId: 'YOUR_MERCHANT_ID',
      apiKey: 'YOUR_API_KEY',
      baseUrl: 'https://your-ipg-gateway-url.com',
    );

    try {
      final response = await PayableIPG.pay(
        orderId: 'ORDER12345',
        amount: 100.0,
        currency: 'USD',
        customerEmail: 'customer@example.com',
        customerPhone: '1234567890',
        description: 'Payment for order ORDER12345',
      );

      if (response.status == 'success') {
        print('Payment successful: ${response.transactionId}');
      } else {
        print('Payment failed: ${response.message}');
      }
    } catch (e) {
      print('Payment error: $e');
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Payable IPG Demo'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: initiatePayment,
          child: Text('Pay Now'),
        ),
      ),
    );
  }
}
回到顶部