Flutter商户服务插件beans_merchant_sdk的使用

Flutter商户服务插件beans_merchant_sdk的使用

Introduction

Beans Merchant Dart SDK 提供了一套全面的工具包,用于将 Beans 平台的高级支付功能直接集成到您的 Flutter 应用程序中。通过此 SDK,您可以简化集成过程,无需管理复杂的直接 API 调用即可充分利用 Beans Merchant API 的潜力。通过使用 Beans Merchant SDK,开发者可以解锁各种应用场景。

Use Cases

以下是几个典型的使用场景:

  • 构建实体店商户 POS 系统:通过生成二维码来处理店内销售点支付请求,结合线上交易的便利性和传统实体店购物体验。
  • Shopify 应用程序支持多币种支付:开发一个 Shopify 插件,使商家能够以自己选择的货币接收付款,同时为客户提供支付他们自己货币的灵活性,从而提升全球客户的购物体验。
  • 作为主通道合作伙伴集成 Beans:通过将 Beans 作为主要的入金和出金通道合作伙伴来简化应用程序的支付基础设施,节省大量的开发时间和资源。
  • 为基于 GPT 的应用实现货币化:对于使用生成式预训练转换器(GPT)的应用程序,集成 Beans 可以实现一种货币化策略,用户可以在达到一定限制后免费使用服务,超出部分则需要付费或购买积分,非常适合提供按次计费的优质内容或服务。

Getting Started

如何申请账户

欢迎来到 Beans App!要开始您的商户旅程,请通过以下步骤联系我们申请账户。

请发送邮件至 merchants@beansapp.com,并附上以下必要信息:

公司详情

  • 公司名称:提供公司的正式名称。
  • 公司网站:包含您的业务网站的 URL。
  • 公司电子邮件地址:这是您的业务官方联系邮箱。
  • 公司标志:上传一张高分辨率的标志图(最小尺寸为 500x500 像素)。

Stellar 账户详情

为了处理付款,请提供您的 Stellar 账户详细信息:

  • Beans App 账户(推荐)
    • 用户名:提供您的 Beans App 用户名。
    • 推荐理由:使用 Beans App 账户可以简化提现流程。
    • 新用户:要创建一个 Beans App 账户,请点击 这里 下载应用。
  • 自定义 Stellar 账户
    • 公钥:如果您希望接收付款到自定义的 Stellar 账户,请提供公钥。

感谢您选择 Beans App,我们期待帮助您处理业务交易!

安装

可以通过 npm 安装该包:

npm install beans-merchant-sdk

使用

以下是一个完整的示例代码,展示了如何使用 beans_merchant_sdk 进行支付请求生成、深链接生成以及二维码生成。

import 'package:beans_merchant_sdk/beans_merchant_sdk.dart';

void main() async {
  // 创建 BeansMerchantSdk 实例
  final sdk = BeansMerchantSdk(
    apiKey: 'your_api_key_here',
  );

  // 获取 Stellar 货币列表
  final stellarCurrenciesResponse = await sdk.fetchStellarCurrencies('your_stellar_account_id');
  print('可用 Stellar 货币: ${stellarCurrenciesResponse.stellarCurrencies}');

  // 生成深链接
  final deeplinkResponse = await sdk.generateDeeplink(
    'your_stellar_account_id',
    'stellar_currency_id',
    100,
    'memo_text',
    1,
    'https://your-domain.com/webhook',
  );
  print('生成的深链接: ${deeplinkResponse.deeplink}');

  // 生成 SVG 二维码
  final svgQrCodeResponse = await sdk.generateSvgQRCode(
    'your_stellar_account_id',
    'stellar_currency_id',
    100,
    'memo_text',
    1,
    'https://your-domain.com/webhook',
    250,
  );
  print('生成的深链接: ${svgQrCodeResponse.deeplink}');
  print('生成的 SVG 二维码: ${svgQrCodeResponse.svgQrCode}');

  // 生成 PNG 二维码
  final pngQrCodeResponse = await sdk.generatePngQRCode(
    'your_stellar_account_id',
    'stellar_currency_id',
    100,
    'memo_text',
    1,
    'https://your-domain.com/webhook',
    250,
  );
  print('生成的深链接: ${pngQrCodeResponse.deeplink}');
  print('生成的 PNG 二维码: ${pngQrCodeResponse.pngQrCodeBase64String}');
}

API Reference

BeansMerchantSdkDomain

BeansMerchantSdkDomain 类提供了设置 API 域名的常量。

Constants

  • production: 生产环境 API 域名 (api.beansapp.com)。
  • staging: 沙箱环境 API 域名 (api.staging.beansapp.com)。

BeansMerchantSdk

BeansMerchantSdk 类提供了与 Beans Merchant API 交互的方法。

Constructor

BeansMerchantSdk({
  String apiDomain = 'api.beansapp.com',
  required String apiKey,
});

参数说明:

  • apiDomain(可选):商户 API 的域名,默认值为 api.beansapp.com
  • apiKey:您的 Beans Merchant API 密钥。

Methods

Fetch Stellar Currencies

获取指定账户可用的 Stellar 货币列表。

方法签名:

Future<FetchStellarCurrenciesResponse> fetchStellarCurrencies(String stellarAccountId)

参数:

  • stellarAccountId:您的 Stellar 账户 ID。

返回值:

  • Future<FetchStellarCurrenciesResponse>:包含可用 Stellar 货币的响应对象。

返回对象属性:

  • stellarCurrencies:指定 Stellar 账户可用的 Stellar 货币数组。

示例:

final response = await sdk.fetchStellarCurrencies('stellarAccountId');
print('可用 Stellar 货币: ${response.stellarCurrencies}');
Generate Deeplink

生成支付请求的深链接。

方法签名:

Future<DeeplinkResponse> generateDeeplink(
  String stellarAccountId,
  String currencyId,
  double amount,
  String memo,
  int maxAllowedPayments = 1,
  String? webhookUrl,
)

参数:

  • stellarAccountId:您的 Stellar 账户 ID。
  • currencyId:Stellar 货币 ID。
  • amount:支付金额。
  • memo:支付备注。
  • maxAllowedPayments:允许的最大支付次数(默认值为 1,无限制时为 -1)。
  • webhookUrl:支付通知的回调 URL(可选)。

返回值:

  • Future<DeeplinkResponse>:包含支付请求深链接的响应对象。

返回对象属性:

  • id:支付请求的 ID。
  • deeplink:支付请求的 Beans App 深链接。

示例:

final response = await sdk.generateDeeplink(
  'stellarAccountId',
  'stellarCurrencyId',
  100,
  'memo_text',
  1,
  'https://your-domain.com/webhook',
);
print('生成的深链接: ${response.deeplink}');
Generate PNG QR Code

生成支付请求的 PNG 二维码。

方法签名:

Future<PngQrCodeResponse> generatePngQRCode(
  String stellarAccountId,
  String currencyId,
  double amount,
  String memo,
  int maxAllowedPayments = 1,
  String? webhookUrl,
  int? preferredSize,
)

参数:

  • stellarAccountId:您的 Stellar 账户 ID。
  • currencyId:Stellar 货币 ID。
  • amount:支付金额。
  • memo:支付备注。
  • maxAllowedPayments:允许的最大支付次数(默认值为 1,无限制时为 -1)。
  • webhookUrl:支付通知的回调 URL(可选)。
  • preferredSize:二维码的首选大小(可选)。

返回值:

  • Future<PngQrCodeResponse>:包含支付请求 PNG 二维码的响应对象。

返回对象属性:

  • id:支付请求的 ID。
  • deeplink:支付请求的 Beans App 深链接。
  • pngQrCodeBase64String:包含深链接的 Base64 编码 PNG 二维码字符串。

示例:

final response = await sdk.generatePngQRCode(
  'stellarAccountId',
  'stellarCurrencyId',
  100,
  'memo_text',
  1,
  'https://your-domain.com/webhook',
  250,
);
print('生成的深链接: ${response.deeplink}');
print('生成的 PNG 二维码: ${response.pngQrCodeBase64String}');
Generate SVG QR Code

生成支付请求的 SVG 二维码。

方法签名:

Future<SvgQrCodeResponse> generateSvgQRCode(
  String stellarAccountId,
  String currencyId,
  double amount,
  String memo,
  int maxAllowedPayments = 1,
  String? webhookUrl,
  int? size,
)

参数:

  • stellarAccountId:您的 Stellar 账户 ID。
  • currencyId:Stellar 货币 ID。
  • amount:支付金额。
  • memo:支付备注。
  • maxAllowedPayments:允许的最大支付次数(默认值为 1,无限制时为 -1)。
  • webhookUrl:支付通知的回调 URL(可选)。
  • size:二维码的大小(可选)。

返回值:

  • Future<SvgQrCodeResponse>:包含支付请求 SVG 二维码的响应对象。

返回对象属性:

  • id:支付请求的 ID。
  • deeplink:支付请求的 Beans App 深链接。
  • svgQrCode:包含深链接的 SVG 二维码。

示例:

final response = await sdk.generateSvgQRCode(
  'stellarAccountId',
  'stellarCurrencyId',
  100,
  'memo_text',
  1,
  'https://your-domain.com/webhook',
  250,
);
print('生成的深链接: ${response.deeplink}');
print('生成的 SVG 二维码: ${response.svgQrCode}');

Webhook Notifications

当支付收到时,Beans Merchant API 将向提供的 URL 发送一个 webhook 通知。

示例 Webhook Payload:

{
  "PaymentRequestId": "e3cfa903-548f-475c-a9f2-ebf3f4e2fa17",
  "Memo": "example",
  "TransactionHash": "b7f4e42935eb120e3a6f43cdae3c6a511a346da77b7e17299aff0f8c72dcf3c0"
}

更多关于Flutter商户服务插件beans_merchant_sdk的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


beans_merchant_sdk 是一个用于集成 Beans 商户服务的 Flutter 插件。Beans 是一个支付处理平台,允许商户接受和管理支付。通过 beans_merchant_sdk,你可以在 Flutter 应用中轻松集成 Beans 的支付功能。

以下是如何使用 beans_merchant_sdk 的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 beans_merchant_sdk 插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  beans_merchant_sdk: ^1.0.0  # 请使用最新的版本号

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

2. 初始化 SDK

在你的应用启动时,需要初始化 beans_merchant_sdk。通常,你可以在 main.dart 文件中进行初始化。

import 'package:beans_merchant_sdk/beans_merchant_sdk.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化 Beans Merchant SDK
  await BeansMerchantSdk.initialize(
    apiKey: 'YOUR_API_KEY',
    environment: BeansEnvironment.sandbox, // 或者 BeansEnvironment.production
  );

  runApp(MyApp());
}

确保将 YOUR_API_KEY 替换为你从 Beans 获取的实际 API 密钥。

3. 创建支付请求

在你的应用中,你可以创建一个支付请求,并调用 beans_merchant_sdk 来处理支付。

import 'package:beans_merchant_sdk/beans_merchant_sdk.dart';

void initiatePayment() async {
  try {
    // 创建支付请求
    PaymentRequest paymentRequest = PaymentRequest(
      amount: 1000, // 金额(以最小单位表示,例如 1000 表示 10.00 美元)
      currency: 'USD',
      orderId: 'ORDER12345',
      customerEmail: 'customer@example.com',
    );

    // 发起支付
    PaymentResponse paymentResponse = await BeansMerchantSdk.processPayment(paymentRequest);

    // 处理支付结果
    if (paymentResponse.status == PaymentStatus.success) {
      print('Payment successful: ${paymentResponse.transactionId}');
    } else {
      print('Payment failed: ${paymentResponse.errorMessage}');
    }
  } catch (e) {
    print('Error: $e');
  }
}

4. 处理支付结果

beans_merchant_sdk 会返回一个 PaymentResponse 对象,其中包含支付的状态、交易 ID 和错误信息(如果有)。你可以根据支付结果来更新 UI 或执行其他操作。

5. 处理回调(可选)

在某些情况下,你可能需要处理支付回调或通知。你可以在 BeansMerchantSdk 中设置回调监听器来处理这些事件。

BeansMerchantSdk.setPaymentListener((PaymentResponse response) {
  if (response.status == PaymentStatus.success) {
    print('Payment successful: ${response.transactionId}');
  } else {
    print('Payment failed: ${response.errorMessage}');
  }
});

6. 清理资源(可选)

如果你的应用中不再需要 beans_merchant_sdk,可以调用 dispose 方法来清理资源。

BeansMerchantSdk.dispose();
回到顶部