Flutter QuickBooks集成插件quickbooks的使用
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:
- 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
andQUICKbookS_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