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.
example/main.dart
import 'package:fyber/fyber.dart';
/// Fyber Flutter SDK Examples
///
/// Run with: dart run example/main.dart
void main() async {
// Initialize the client
final fyber = Fyber(
apiKey: 'sk_test_your_api_key',
environment: 'test', // or 'live' for production
);
try {
// Example 1: Create a customer
print('Creating customer...');
final customer = await fyber.customers.create(CreateCustomerRequest(
email: 'john@example.com',
name: 'John Doe',
phone: '+1234567890',
));
print('Customer created: ${customer.id}');
// Example 2: Tokenize a card
print('\nTokenizing card...');
final token = await fyber.tokens.create(CreateTokenRequest(
customerId: customer.id,
card: CardSource(
number: '4111111111111111',
expMonth: '12',
expYear: '2025',
cvv: '123',
holderName: 'John Doe',
),
purpose: TokenPurpose.recurring,
verifyCard: true,
));
print('Token created: ${token.id}');
print('Card: ${token.card?.brand} ****${token.card?.last4}');
// Example 3: Create a payment with token
print('\nCreating payment...');
final payment = await fyber.payments.create(CreatePaymentRequest(
amount: 1000, // $10.00
currency: 'USD',
tokenId: token.id,
customerId: customer.id,
description: 'Test payment',
));
print('Payment created: ${payment.id}');
print('Status: ${payment.status}');
// Example 4: Create a checkout session
print('\nCreating checkout session...');
final session = await fyber.checkout.create(CreateCheckoutRequest(
currency: 'USD',
lineItems: [
CheckoutLineItem(
name: 'Premium Plan',
amount: 2999,
quantity: 1,
),
],
successUrl: 'https://example.com/success',
cancelUrl: 'https://example.com/cancel',
customerId: customer.id,
));
print('Checkout URL: ${session.url}');
// Example 5: Create a subscription
print('\nCreating subscription...');
final subscription = await fyber.subscriptions.create(CreateSubscriptionRequest(
customerId: customer.id,
tokenId: token.id,
amount: 999, // $9.99/month
currency: 'USD',
interval: SubscriptionInterval.month,
trialDays: 14,
));
print('Subscription created: ${subscription.id}');
print('Status: ${subscription.status}');
// Example 6: Check BNPL eligibility
print('\nChecking installment eligibility...');
final eligibility = await fyber.installments.checkEligibility(
CheckEligibilityRequest(
customerId: customer.id,
amount: 50000, // $500.00
currency: 'USD',
),
);
print('Eligible: ${eligibility.eligible}');
if (eligibility.eligible) {
print('Available installments: ${eligibility.availableInstallments}');
}
// Example 7: List payments
print('\nListing payments...');
final payments = await fyber.payments.list(ListPaymentsOptions(
limit: 10,
customerId: customer.id,
));
print('Found ${payments.total} payments');
// Example 8: Refund a payment
print('\nRefunding payment...');
final refund = await fyber.refunds.create(CreateRefundRequest(
paymentId: payment.id,
amount: 500, // Partial refund of $5.00
reason: 'Customer request',
));
print('Refund created: ${refund.id}');
} on FyberException catch (e) {
print('Error: ${e.message}');
print('Code: ${e.code}');
} finally {
fyber.close();
}
}
/// Example: Webhook verification
void webhookExample() {
const payload = '{"id":"evt_123","type":"payment.succeeded"}';
const signature = 't=1234567890,v1=abc123...';
const secret = 'whsec_your_webhook_secret';
try {
final event = Fyber.verifyWebhook(
payload: payload,
signature: signature,
secret: secret,
);
print('Webhook verified: ${event['type']}');
} on FyberException catch (e) {
print('Webhook verification failed: ${e.message}');
}
}