Flutter支付沙盒测试插件auropay_payments_sandbox的使用

Flutter支付沙盒测试插件auropay_payments_sandbox的使用

Auropay沙盒插件

开始使用

AuroPay插件允许您在iOS和Android应用中接受应用内付款,为您提供创建无缝结账体验所需的构建块。

pubspec.yaml 设置

要从Pub.dev将AuroPay Flutter插件添加到您的项目,请按照以下步骤操作:

步骤1. 在项目的pubspec.yaml文件中添加以下代码:

dependencies:
  auropay_payments_sandbox: latest_version
  
// 或者在终端运行
$ flutter pub add auropay_payments_sandbox

步骤2. 在终端运行:

$ flutter pub get

现在,AuroPay插件已添加到您的项目中。

注意:对于Android,需要在manifest文件中添加互联网权限:

<uses-permission android:name="android.permission.INTERNET"/>

对于iOS,在info.plist文件中添加相机权限:

<key>NSCameraUsageDescription</key>
<string>需要访问您的相机来扫描卡片。</string>

使用SDK

要使用AuroPay Flutter插件进行支付交易,应在发起任何支付请求之前初始化SDK。AuroPay插件可以从main.dart文件初始化。

初始化SDK

构造所需的模型类以初始化插件,并在您的dart文件中添加以下行:

// 创建客户资料。
CustomerProfile customerProfileobj = CustomerProfile(
    title: "Some Title",
    firstName: "First Name",
    middleName: "Middle Name",
    lastName: "Last Name",
    phone: "01234567890",
    email: "Test@gmail.com");

// 准备带有所需信息的构建器
final builder = AuropayBuilder(
    subDomainId: keys.merchantId, // 您的商户域名
    accessKey: keys.accessKey, // 您的访问密钥
    secretKey: keys.secretKey, // 您的秘密密钥
    customerProfile: customerProfile)
    .setAutoContrast(true) // 设置应用程序栏的颜色主题
    .setCountry(Country.IN)
    .setShowReceipt(true)
    .askForCustomerDetail(false)
    .getDetailedResponse(false)
    .build();

您还可以选择为AuroPay插件UI元素指定一个主题,以匹配您的应用程序主题。

// 创建主题模型。
Theme myTheme = AuropayTheme(
    primaryColor: const Color(0xff236C6C),
    colorOnPrimary: Colors.black,
    colorOnSecondary: Colors.redAccent,
    secondaryColor: Colors.white);

builder.setTheme(myTheme);

发起支付请求并处理结果

以下代码片段演示了如何发起支付请求并接收回调事件。

auropayResponse = await _auropay.doPayment(
    builder: builder,
    amount: double.parse(amountController.text),
    referenceNumber: "mock_reference");

final paymentStatus = ResponseType.success == auropayResponse?.type
    ? PaymentStatus.success 
    : PaymentStatus.failed; 

String? message;
if (auropayResponse?.data is FailureData) {
  FailureData data = auropayResponse?.data as FailureData;
  message = '支付失败:\n${data.message}';
}

if (auropayResponse?.data is SuccessData) {
  SuccessData data = auropayResponse?.data as SuccessData;
  message = '支付成功\n交易ID:${data.transactionId}';
}

通过在构建器属性中设置以下参数为true,您可以从SDK获取详细的支付响应:

builder.getDetailedResponse(true) // 默认为false

按照本文档中的步骤,您应该能够成功地将AuroPay Flutter插件集成到您的移动应用程序中,并实现无缝的支付处理。

如果您遇到任何问题,请联系我们:support@auropay.net

示例代码

以下是示例代码:

import 'package:flutter/material.dart';

import 'simple_app.dart';

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

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

1 回复

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


auropay_payments_sandbox 是一个用于 Flutter 应用的支付沙盒测试插件,通常用于模拟支付流程,以便开发者在应用上线前进行充分的测试。这个插件可能模拟支付网关的行为,允许你在不涉及真实支付的情况下测试你的支付流程。

以下是如何在 Flutter 项目中使用 auropay_payments_sandbox 插件的一般步骤:


1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  auropay_payments_sandbox: ^版本号 # 替换为实际的版本号

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


2. 初始化插件

在你的 Dart 代码中,导入并初始化插件。

import 'package:auropay_payments_sandbox/auropay_payments_sandbox.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await AuropayPaymentsSandbox.initialize(
    apiKey: '你的沙盒API密钥',
    environment: Environment.sandbox, // 设置为沙盒环境
  );
  runApp(MyApp());
}

3. 创建支付请求

使用插件提供的 API 创建支付请求。例如:

Future<void> makePayment() async {
  try {
    PaymentResponse response = await AuropayPaymentsSandbox.createPayment(
      amount: 100.0, // 支付金额
      currency: 'USD', // 货币类型
      description: 'Test payment', // 支付描述
      customerEmail: 'test@example.com', // 客户邮箱
    );

    if (response.status == PaymentStatus.success) {
      print('支付成功: ${response.transactionId}');
    } else {
      print('支付失败: ${response.errorMessage}');
    }
  } catch (e) {
    print('支付异常: $e');
  }
}

4. 处理支付结果

根据支付结果更新 UI 或执行其他逻辑。例如:

ElevatedButton(
  onPressed: () async {
    await makePayment();
  },
  child: Text('支付测试'),
);

5. 模拟不同支付场景

在沙盒环境中,你可以模拟不同的支付场景,例如:

  • 成功支付
  • 支付失败(例如余额不足)
  • 支付取消 通过更改插件配置或使用测试数据来模拟这些场景。

6. 调试和日志

如果遇到问题,可以启用插件提供的调试模式或查看日志,帮助排查问题。

AuropayPaymentsSandbox.setDebugMode(true); // 启用调试模式

7. 切换至生产环境

测试完成后,将插件配置切换至生产环境,使用真实的 API 密钥和支付网关。

await AuropayPaymentsSandbox.initialize(
  apiKey: '你的生产API密钥',
  environment: Environment.production, // 切换为生产环境
);
回到顶部