Flutter QuickBooks集成插件quickbooks的使用

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

Flutter QuickBooks集成插件quickbooks的使用

quickbooks

The quickbooks package provides you with all the toolsing you need to get data from QuickBooksPlatform in Dart.

Installation

In the dependencies: section of your pubspec.yaml, add the following line:

dependencies:
  quickbooks: &latest_version

To import it in your Dart file, add the following line:

import 'package:quickbooks/quickbooks.dart';

Getting Started

To use the different features of this project, you will need:

  1. A QuickBooks developer account: https://developer.intuit.com/app/developer/qbdesktop/docs/get-started/create-an-intuit-developer-account
  • You can set these values in the environment variables as QUICKBOOKS_CLIENT_ID and QUICKbookS_CLIENT_SECRET or set it directly on the OAuth2 authentication service.

Your QuickBooks app will give you a client_id and a client_secret keys: https://developer.intuit.com/app/developer/qbo/docs/get-started/get-client-id-and-client-secret

Environment

This package is set to use automatically the environment variables of your system if you didn’t specify values to services that uses them.

The environment variables used by this package are:

  • QUICKBOOKS_CLIENT_ID: The id of the app you use.
  • QUICKbookS_CLIENT_SECRET: The secret key of the app you use.
  • QUICKbookS_REDIRECT_URI: The uri OAuth2 will redirect after connection to send informations.
  • QUICKbookS_STATE: The confirmation string used by OAuth2 to confirm a connection is valid.
  • QUICKbookS_IS_PRODUCTION: Boolean that specifies if the app is in production or in sandbox mode. Will be automatically set to production if no value is given.

Features

You can manage these types of QuickBooks data:

  • Products
  • Categories
  • Your own company
  • Customers
  • Invoices
  • Payments

Each of these data types have their own service that can get, create, update and delete data.

Example:

var service = QuickbooksPaymentService();

var value = await service.getAll(
    accessToken: tokens.accessToken,
    companyId: tokens.companyId!,
    conditions: '&your conditions in MySql',
);

If you need to get a data that is not covered by the prebuilt services, you can use the QuickbooksQueryService to build your own custom service easily.

Example:

var service = QuickbooksQueryService(
    postEndpoint: '&post endpoint of your date',
    baseQuery: '&base query to get your data in MySql',
    baseConditions: '&base conditions to get your data in Mysql', // Is null by default
    isProduction: true //Will be set with the environment variable by default
);

By example, to set a QuickbooksQueryService to get Payments, you need this code:

var service = QuickbooksQueryService(
    postEndpoint: '&post endpoint of your date',
    baseQuery: '&base query to get your data in MySql',
    baseConditions: '&base conditions to get your data in Mysql', // Is null by default
    isProduction: true //Will be set with the environment variable by default
);

示例代码

import 'dart:io';

import 'package:quickbooks/entities/entity.export.dart';
import 'package:quickbooks/environment/environment.export.dart';
import 'package:quickbooks/services/quickbooks_tax_rates.service.dart';
import 'package:quickbooks/services/services.export.dart';

/// Note that to run this example, you must have your environment variables set
void main() async {
  var tokens = await oauthConnectExample();
  await getCompanyExample(tokens);
  await getCustomersExample(tokens);
  await getCategoriesExample(tokens);
  await getProductsExample(tokens);
  await getInvoicesExample(tokens);
  await getPaymentsExample(tokens);
  await getAccountsExample(tokens);
  await getPurchasesExample(tokens);
  await getVendorsExample(tokens);
  await getTaxRatesExample(tokens);
}

/// Example of how to use the oauth connection with Quickbooks.
///
/// If you don't provide the needed values, it will take it from environment variables.
Future<QuickbooksOauth2Tokens> oauthConnectExample(
    {String? clientId,
    String? clientSecret,
    String? redirectUri,
    bool? isProduction}) async {
  //Init of the Oauth2 service
  var service = QuickbooksOauth2Service(
    clientId: clientId ?? QuickbooksEnvironment.clientId,
    clientSecret: clientSecret ?? QuickbooksEnvironment.clientSecret,
    redirectUrl: redirectUri ?? QuickbooksEnvironment.redirectUri,
    isProduction: isProduction ?? QuickbooksEnvironment.isProduction,
  );

  String authUrl = await service.getAuthUrl();

  // Prints the connection instructions in the console and
  // gets the informations from the console entry
  print('Please connect on the link below:\n');
  print(authUrl);
  print(
      "\nAfter connecting, enter the code in the console. It is in que query parameters.");
  print("The code:");
  String? code = stdin.readLineSync();
  print('You entered: $code');
  print("Now, enter the realmId. it is also in the query parameters.");
  print("The realmId:");
  String? realmId = stdin.readLineSync();
  print('You entered: $realmId');
  print("Retrieving data");

  // Gets the tokens from Quickbooks
  QuickbooksOauth2Tokens value =
      await service.getTokens(authorizationCode: code ?? "", realmId: realmId);
  print('Oauth2 tokens');
  print(value);
  print('');
  return value;
}

/// Gets and prints all the customers
Future getCustomersExample(QuickbooksOauth2Tokens tokens) async {
  var accessToken = tokens.accessToken;
  var service = QuickbooksCustomerService();

  var value = await service.getAll(
    accessToken: accessToken,
    companyId: tokens.companyId!,
  );
  print('Customers');
  print(value);
  print('');
}

/// Gets and prints all the categories
Future getCategoriesExample(QuickbooksOauthhe Tokens tokens) async {
  var accessToken = tokens.accessToken;
  var service = QuickbooksCategoriesService();

  var value = await service.getAll(
    accessToken: accessToken,
    companyId: tokens.companyId!,
  );
  print('Categories');
  print(value);
  print('');
}

/// Gets and prints your company
Future getCompanyExample(QuickbooksOauthhe Tokens tokens) async {
  var accessToken = tokens.accessToken;
  var service = QuickbooksCompanyService();

  var value =
      await service.get(accessToken: accessToken, companyId: tokens.companyId!);
  print('Company');
  print(value);
  print('');
}

/// Gets and prints all the invoices
Future getInvoicesExample(QuickbooksOauthhe Tokens tokens) async {
  var accessToken = tokens.accessToken;
  var service = QuickbooksInvoiceService();

  var value = await service.getAll(
    accessToken: accessToken,
    companyId: tokens.companyId!,
  );
  print('Invoices');
  print(value);
  print('');
}

/// Gets and prints all the payments
Future getPaymentsExample(QuickbooksOauthhe Tokens tokens) async {
  var accessToken = tokens.accessToken;
  var service = QuickbooksPaymentService();

  var value = await service.getAll(
    accessToken: accessToken,
    companyId: tokens.companyId!,
  );
  print('Payments');
  print(value);
  print('');
}

/// Gets and prints all the productses
Future getProductsExample(QuickbooksOauthhe Tokens tokens) async {
  var accessToken = tokens.accessToken;
  var service = QuickbooksProductsService();

  var value = await service.getAll(
    accessToken: accessToken,
    companyId: tokens.companyId!,
  );
  print('Products');
  print(value);
  print('');
}

/// Gets and prints all the accounts
Future getAccountsExample(QuickbooksOauthhe Tokens tokens) async {
  var accessToken = tokens.accessToken;
  var service = QuickbooksAccountsService();

  var value = await service.getAll(
    accessToken: accessToken,
    companyId: tokens.companyId!,
  );
  print('Accounts');
  print(value);
  print('');
}

/// Gets and prints all the purchases
Future getPurchasesExample(QuickbooksOauthhe Tokens tokens) async {
  var accessToken = tokens.accessToken;
  var service = QuickbooksPurchasesService();

  var value = await service.getAll(
    accessToken: accessToken,
    companyId: tokens.companyId!,
  );
  print('Purchases');
  print(value);
  print('');
}

/// Gets and prints all the vendors
Future getVendorsExample(QuickbooksOauthhe Tokens tokens) async {
  var accessToken = tokens.accessToken;
  var service = QuickbooksVendorsService();

  var value = await service.getAll(
    accessToken: accessToken,
    companyId: tokens.companyId!,
  );
  print('Vendors');
  print(value);
  print('');
}

/// Gets and prints all the tax rates
Future getTaxRatesExample(QuickbooksOauthhe Tokens tokens) async {
  var accessToken = tokens.accessToken;
  var service = QuickbooksTaxRatesService();

  var value = await service.getAll(
    accessToken: accessToken,
    companyId: tokens.companyId!,
  );
  print('Tax Rates');
  print(value);
  print('');
}

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

1 回复

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


在Flutter项目中集成QuickBooks并使用quickbooks插件,通常涉及几个关键步骤:安装插件、配置QuickBooks OAuth、以及执行API调用。以下是一个简化的代码案例,展示如何在Flutter中实现这些功能。

1. 安装插件

首先,你需要在pubspec.yaml文件中添加quickbooks插件的依赖。由于quickbooks并非一个官方或广泛认可的Flutter插件名称,这里假设有一个名为flutter_quickbooks的假设插件(实际使用时请替换为真实存在的插件名称或官方推荐的方法)。

dependencies:
  flutter:
    sdk: flutter
  flutter_quickbooks: ^x.y.z  # 替换为实际版本号

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

2. 配置OAuth

为了与QuickBooks进行交互,你需要设置一个OAuth流程来获取访问令牌。这通常涉及到设置重定向URI、客户端ID和客户端密钥,并在QuickBooks开发者门户中注册你的应用。

import 'package:flutter_quickbooks/flutter_quickbooks.dart';
import 'dart:async';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 替换为你的QuickBooks客户端ID、客户端密钥和重定向URI
  final clientId = 'YOUR_CLIENT_ID';
  final clientSecret = 'YOUR_CLIENT_SECRET';
  final redirectUri = 'YOUR_REDIRECT_URI';
  final environment = QuickBooksEnvironment.sandbox; // 或 QuickBooksEnvironment.production

  // 配置QuickBooks OAuth
  QuickBooksOAuthConfig config = QuickBooksOAuthConfig(
    clientId: clientId,
    clientSecret: clientSecret,
    redirectUri: redirectUri,
    environment: environment
  );

  // 初始化QuickBooks客户端
  QuickBooksClient client = QuickBooksClient(config: config);

  // 获取访问令牌
  try {
    var accessToken = await client.getAccessToken(code: 'AUTHORIZATION_CODE_OBTAINED_FROM_REDIRECT');
    print('Access Token: ${accessToken.accessToken}');
    
    // 你可以在这里保存令牌,并在后续请求中使用
  } catch (e) {
    print('Error getting access token: $e');
  }

  runApp(MyApp());
}

注意:AUTHORIZATION_CODE_OBTAINED_FROM_REDIRECT是你从OAuth流程中的重定向URI获取到的授权码。在实际应用中,这个代码通常是通过一个Web视图或服务器回调获得的。

3. 执行API调用

一旦你有了访问令牌,就可以使用它来对QuickBooks API进行请求。以下是一个获取公司信息的示例:

void fetchCompanyInfo(String accessToken) async {
  QuickBooksClient client = QuickBooksClient(accessToken: accessToken);

  try {
    var response = await client.get('/v3/company/YOUR_COMPANY_ID', headers: {
      'Content-Type': 'application/json'
    });

    if (response.statusCode == 200) {
      var companyInfo = await response.json();
      print('Company Info: $companyInfo');
    } else {
      print('Error fetching company info: ${response.statusCode}');
    }
  } catch (e) {
    print('Error: $e');
  }
}

在这个例子中,你需要替换YOUR_COMPANY_ID为你的QuickBooks公司ID。这个ID通常可以在QuickBooks API文档或开发者门户中找到。

注意事项

  1. 安全性:不要将敏感信息(如客户端ID和密钥)硬编码到你的Flutter应用中。考虑使用环境变量或安全的密钥管理服务。
  2. 错误处理:在生产环境中,确保有适当的错误处理和日志记录。
  3. API限制:了解QuickBooks API的速率限制和配额,以避免服务中断。

由于QuickBooks的集成可能涉及复杂的OAuth流程和API调用,建议查阅QuickBooks开发者文档以获取最新的信息和最佳实践。

回到顶部