fyber 1.0.1
fyber: ^1.0.1 copied to clipboard
Official Fyber Payment SDK for Flutter. Process payments, manage customers, handle subscriptions, and more.
Fyber Flutter SDK #
Official Fyber Payment SDK for Flutter. Process payments, manage customers, handle subscriptions, and more.
Installation #
Add to your pubspec.yaml:
dependencies:
fyber: ^1.0.0
Then run:
flutter pub get
Quick Start #
import 'package:fyber/fyber.dart';
// Initialize the client
final fyber = Fyber(
apiKey: 'sk_test_your_api_key',
environment: 'test', // or 'live'
);
// Create a payment
final payment = await fyber.payments.create(CreatePaymentRequest(
amount: 1000, // $10.00 in cents
currency: 'USD',
source: CardSource(
number: '4111111111111111',
expMonth: '12',
expYear: '2025',
cvv: '123',
),
));
print('Payment ${payment.id}: ${payment.status}');
Features #
Payments #
// Create a payment
final payment = await fyber.payments.create(CreatePaymentRequest(
amount: 1000,
currency: 'USD',
source: CardSource(...),
));
// Capture an authorized payment
await fyber.payments.capture(payment.id);
// Cancel a payment
await fyber.payments.cancel(payment.id);
// List payments
final payments = await fyber.payments.list(ListPaymentsOptions(
limit: 20,
status: 'succeeded',
));
Customers #
// Create a customer
final customer = await fyber.customers.create(CreateCustomerRequest(
email: 'john@example.com',
name: 'John Doe',
));
// Update a customer
await fyber.customers.update(customer.id, UpdateCustomerRequest(
name: 'John Smith',
));
// List customers
final customers = await fyber.customers.list();
Tokens (Saved Cards) #
// Tokenize a card
final token = await fyber.tokens.create(CreateTokenRequest(
customerId: customer.id,
card: CardSource(
number: '4111111111111111',
expMonth: '12',
expYear: '2025',
cvv: '123',
),
purpose: TokenPurpose.recurring,
));
// Use token for payment
final payment = await fyber.payments.create(CreatePaymentRequest(
amount: 1000,
currency: 'USD',
tokenId: token.id,
));
// Set as default
await fyber.tokens.setDefault(token.id);
// List customer's tokens
final tokens = await fyber.tokens.listByCustomer(customer.id);
Checkout Sessions #
final session = await fyber.checkout.create(CreateCheckoutRequest(
currency: 'USD',
lineItems: [
CheckoutLineItem(name: 'Product', amount: 2999, quantity: 1),
],
successUrl: 'https://example.com/success',
cancelUrl: 'https://example.com/cancel',
));
// Redirect to session.url
Subscriptions #
// Create subscription
final subscription = await fyber.subscriptions.create(CreateSubscriptionRequest(
customerId: customer.id,
tokenId: token.id,
amount: 999,
currency: 'USD',
interval: SubscriptionInterval.month,
trialDays: 14,
));
// Cancel subscription
await fyber.subscriptions.cancel(subscription.id, atPeriodEnd: true);
// Pause/resume
await fyber.subscriptions.pause(subscription.id);
await fyber.subscriptions.resume(subscription.id);
Installments (BNPL) #
// Check eligibility
final eligibility = await fyber.installments.checkEligibility(
CheckEligibilityRequest(
customerId: customer.id,
amount: 50000,
currency: 'USD',
),
);
if (eligibility.eligible) {
// Create installment plan
final plan = await fyber.installments.create(CreateInstallmentRequest(
customerId: customer.id,
tokenId: token.id,
amount: 50000,
currency: 'USD',
installments: 4,
frequency: InstallmentFrequency.biweekly,
));
}
Webhooks #
// Verify webhook signature
try {
final event = Fyber.verifyWebhook(
payload: rawBody,
signature: request.headers['fyber-signature']!,
secret: 'whsec_your_secret',
);
switch (event['type']) {
case 'payment.succeeded':
// Handle successful payment
break;
case 'subscription.canceled':
// Handle cancellation
break;
}
} on FyberException catch (e) {
print('Invalid webhook: ${e.message}');
}
Error Handling #
try {
final payment = await fyber.payments.create(...);
} on FyberException catch (e) {
print('Error: ${e.message}');
print('Code: ${e.code}');
print('Status: ${e.statusCode}');
if (e.isAuthError) {
// Invalid API key
} else if (e.isValidationError) {
// Invalid request data
} else if (e.isRateLimitError) {
// Too many requests
}
}
Configuration #
final fyber = Fyber(
apiKey: 'sk_test_xxx',
environment: 'test', // 'test' or 'live'
timeout: Duration(seconds: 30),
);
// Don't forget to close when done
fyber.close();
Requirements #
- Dart SDK >= 3.0.0
- Flutter >= 3.0.0
License #
MIT License - see LICENSE file for details.