authnet_core 1.0.2
authnet_core: ^1.0.2 copied to clipboard
Authorize.Net SDK for Dart/Flutter: card, eCheck, PayPal payments, CIM profiles, ARB subscriptions, reporting. Not affiliated with or endorsed by Authorize.Net or Visa.
example/authnet_core_example.dart
// Runnable example: a card charge, an eCheck charge, and a save-then-charge
// CIM flow against a sandbox account.
//
// Reads sandbox credentials from the environment so this script never has
// real credentials hardcoded in it:
// AUTHNET_API_LOGIN_ID=... AUTHNET_TRANSACTION_KEY=... dart run example/authnet_core_example.dart
//
// Get free sandbox credentials at https://developer.authorize.net/hello_world/sandbox.html
// Sandbox test card numbers: https://developer.authorize.net/hello_world/testing_guide.html
import 'dart:io';
import 'package:authnet_core/authnet_core.dart';
Future<void> main() async {
final apiLoginId = Platform.environment['AUTHNET_API_LOGIN_ID'] ?? '';
final transactionKey = Platform.environment['AUTHNET_TRANSACTION_KEY'] ?? '';
final config =
AuthNetConfig(apiLoginId: apiLoginId, transactionKey: transactionKey);
if (!config.isConfigured) {
stdout.writeln(
'Set AUTHNET_API_LOGIN_ID and AUTHNET_TRANSACTION_KEY (sandbox) to run this example.\n'
'Get free sandbox credentials: https://developer.authorize.net/hello_world/sandbox.html',
);
return;
}
final client = AuthNetClient(config: config, onLog: stdout.writeln);
try {
await _chargeCard(client);
await _chargeECheck(client);
await _saveThenChargeSavedMethod(client);
} finally {
client.close();
}
}
Future<void> _chargeCard(AuthNetClient client) async {
stdout.writeln('\n--- Card charge ---');
final result = await client.charge(PaymentRequest(
amount: 1.00,
method: PaymentMethod.creditCard,
billing: BillingDetails(
firstName: 'Test', lastName: 'Buyer', email: 'test@example.com'),
// Authorize.Net's published sandbox test card (always approves in the sandbox).
card: CardDetails(
number: '4111111111111111',
expMonth: '12',
expYear: '2030',
cvv: '900'),
orderDescription: 'authnet_core example: card charge',
));
stdout.writeln('${result.status}: ${result.message}');
}
Future<void> _chargeECheck(AuthNetClient client) async {
stdout.writeln('\n--- eCheck (bank account) charge ---');
final result = await client.charge(PaymentRequest(
amount: 5.00,
method: PaymentMethod.bankAccount,
billing: BillingDetails(firstName: 'Test', lastName: 'Buyer'),
// Authorize.Net's published sandbox test bank account.
bank: BankDetails(
accountType: BankAccountType.checking,
routingNumber: '071000013',
accountNumber: '123456789',
nameOnAccount: 'Test Buyer',
),
orderDescription: 'authnet_core example: eCheck charge',
));
stdout.writeln('${result.status}: ${result.message}');
if (result.isPendingSettlement) {
stdout.writeln(
'note: approved but not yet settled. Funds move over 3-5 business '
'days and can still be returned. Do not treat as final.');
}
}
Future<void> _saveThenChargeSavedMethod(AuthNetClient client) async {
stdout.writeln('\n--- Save a card, then charge the saved method ---');
final charge = await client.charge(PaymentRequest(
amount: 1.00,
method: PaymentMethod.creditCard,
billing: BillingDetails(firstName: 'Test', lastName: 'Buyer'),
card: CardDetails(
number: '4111111111111111',
expMonth: '12',
expYear: '2030',
cvv: '900'),
orderDescription: 'authnet_core example: save this card',
));
if (!charge.isApproved || charge.transactionId == null) {
stdout.writeln('Could not charge a card to save, skipping.');
return;
}
final saved = await client.saveMethodFromTransaction(
transactionId: charge.transactionId!);
stdout.writeln('Saved as customerProfileId=${saved.customerProfileId}, '
'paymentProfileId=${saved.customerPaymentProfileId}');
final again = await client.chargeSavedMethod(
customerProfileId: saved.customerProfileId!,
paymentProfileId: saved.customerPaymentProfileId!,
amount: 1.00,
);
stdout.writeln('Charged saved method: ${again.status}: ${again.message}');
}