buck_box_sdk 1.0.3
buck_box_sdk: ^1.0.3 copied to clipboard
Flutter plugin for BuckBox SDK. To know more about BuckBox, visit https://bustto.com/
example/lib/main.dart
import 'dart:io';
import 'package:buck_box_sdk/buck_box_sdk.dart';
import 'package:flutter/material.dart';
void main() async {
runApp(const _MyApp());
}
class _MyApp extends StatelessWidget {
const _MyApp();
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'BuckBox Payment',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: const _CheckoutTestScreen(),
);
}
}
class _CheckoutTestScreen extends StatefulWidget {
const _CheckoutTestScreen();
@override
State<_CheckoutTestScreen> createState() => _CheckoutTestScreenState();
}
class _CheckoutTestScreenState extends State<_CheckoutTestScreen> {
bool isLoading = false;
String checkoutState = "Idle";
String callbackText = "No callback yet";
final amountController = TextEditingController(text: "10");
BuckBoxEnvironment selectedEnvironment = BuckBoxEnvironment.production;
@override
void dispose() {
amountController.dispose();
super.dispose();
}
Future<void> startCheckout() async {
final amount = double.tryParse(amountController.text.trim());
if (amount == null || amount <= 0) {
setState(() {
checkoutState = "Invalid amount";
callbackText = "Please enter a valid amount.";
});
return;
}
setState(() {
isLoading = true;
checkoutState = "Initializing";
callbackText = "Initializing BuckBox SDK...";
});
final checkoutSdk = BuckBoxSDK();
//Need to remove in next version
String apiKey = '';
String token = '';
if(selectedEnvironment == BuckBoxEnvironment.staging) {
//Dev
apiKey = "nvfZ2hSdwqDCStLRsIaS3xCR";
token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjaGFudF9pZCI6IkJNMTg3NzcyIiwibmFtZSI6IkJ1c3R0byBkZXZzIiwiZW1haWwiOiJ0ZXN0bWVyY2hhbnRkZXZAeW9wbWFpbC5jb20iLCJleHAiOjE3ODMwNjU5NTV9.94nTgzwQioFDTWC0LrzlH0ubd-v8tgyWhIazurtnY7Q";
// Staging
// apiKey = "Z1aeI8El5BoLuFPnnfqlFc1m";
// token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjaGFudF9pZCI6IkJNNjg5NDkiLCJuYW1lIjoiQnVja0JveCBJbmZvdGVjaCBQdnQgTHRkIiwiZW1haWwiOiJidXN0dG8udGVzdEB5b3BtYWlsLmNvbSIsImV4cCI6MTc4MzMxODk3NH0.7cb8igdtbL0DAgW9P7rMBTXE5RZP6nDkFORldJ2-VN4";
} else {
apiKey = "Z1aeI8El5BoLuFPnnfqlFc1m";
token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjaGFudF9pZCI6IkJNNjg5NDkiLCJuYW1lIjoiQnVja0JveCBJbmZvdGVjaCBQdnQgTHRkIiwiZW1haWwiOiJidXN0dG8udGVzdEB5b3BtYWlsLmNvbSIsImV4cCI6MTc4MzUwNjEzNn0.X4bHFgVu-vkurTD0ufvSd00CnvEfgBP37mBt74EDSUw";
}
try {
final checkoutStarted = await checkoutSdk.initSdk(
context,
BuckBoxInitConfig(
apiKey: apiKey,
authorization: token,
amount: amount,
merchantTxnId: 'ORD-${DateTime.now().millisecondsSinceEpoch}-TEST',
environment: selectedEnvironment,
platform: Platform.isAndroid
? BuckBoxPlatform.android
: BuckBoxPlatform.ios,
payerDetails: const BuckBoxPayerDetails(
firstName: 'Andy',
lastName: 'Rover',
email: 'andy.rover@example.com',
phone: '9696332351',
),
onSuccess: (response) {
debugPrint('BuckBox success: $response');
updateCheckoutState(
state: "Success callback received",
details: "Success callback:\n$response",
);
},
onFailure: (response) {
debugPrint('BuckBox failure: $response');
updateCheckoutState(
state: "Failure callback received",
details: "Failure callback:\n$response",
);
},
onProcessing: (response) {
debugPrint('BuckBox processing: $response');
updateCheckoutState(
state: "Processing callback received",
details: "Processing callback:\n$response",
);
},
note: 'Transaction Note test',
),
);
if (!mounted) {
return;
}
setState(() {
isLoading = false;
checkoutState = checkoutStarted
? "Checkout opened"
: "Checkout did not start";
callbackText = checkoutStarted
? "Waiting for success or failure callback..."
: "Checkout could not be started.";
});
} catch (error) {
if (!mounted) {
return;
}
setState(() {
isLoading = false;
checkoutState = "Initialization failed";
callbackText = "Unable to initialize BuckBox SDK.\n$error";
});
}
}
void updateCheckoutState({
required String state,
required String details,
}) {
debugPrint(details);
if (!mounted) {
return;
}
setState(() {
checkoutState = state;
callbackText = details;
isLoading = false;
});
}
Color get statusColor {
if (checkoutState.contains("Success")) {
return Colors.green;
}
if (checkoutState.contains("Failure") ||
checkoutState.contains("failed")) {
return Colors.red;
}
if (checkoutState.contains("Processing")) {
return Colors.blueGrey;
}
if (isLoading || checkoutState == "Checkout opened") {
return Colors.orange;
}
return Colors.grey;
}
IconData get statusIcon {
if (checkoutState.contains("Success")) {
return Icons.check_circle;
}
if (checkoutState.contains("Failure") ||
checkoutState.contains("failed")) {
return Icons.cancel;
}
if (checkoutState.contains("Processing")) {
return Icons.sync;
}
if (isLoading || checkoutState == "Checkout opened") {
return Icons.hourglass_top;
}
return Icons.info;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Checkout Test")),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(24),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
"Merchant APP",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.w700,
),
),
const SizedBox(height: 24),
DropdownButtonFormField<BuckBoxEnvironment>(
initialValue: selectedEnvironment,
decoration: const InputDecoration(
labelText: "Environment",
border: OutlineInputBorder(),
),
items: const [
DropdownMenuItem(
value: BuckBoxEnvironment.production,
child: Text("Production"),
),
DropdownMenuItem(
value: BuckBoxEnvironment.staging,
child: Text("Staging"),
),
],
onChanged: isLoading
? null
: (environment) {
if (environment == null) {
return;
}
setState(() {
selectedEnvironment = environment;
});
},
),
const SizedBox(height: 16),
TextField(
controller: amountController,
enabled: !isLoading,
keyboardType: const TextInputType.numberWithOptions(
decimal: true,
),
decoration: const InputDecoration(
labelText: "Amount",
hintText: "Enter amount",
border: OutlineInputBorder(),
prefixText: "₹ ",
),
),
const SizedBox(height: 24),
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
"Checkout Status",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w700,
),
),
const SizedBox(height: 12),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 8,
),
decoration: BoxDecoration(
color: statusColor.withValues(alpha: 0.12),
borderRadius: BorderRadius.circular(20),
border: Border.all(
color: statusColor.withValues(alpha: 0.35),
),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(statusIcon, color: statusColor, size: 18),
const SizedBox(width: 8),
Text(
checkoutState,
style: TextStyle(
color: statusColor,
fontWeight: FontWeight.w700,
),
),
],
),
),
const SizedBox(height: 12),
Text(callbackText),
],
),
),
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: isLoading ? null : startCheckout,
child: Text(isLoading ? "Initializing..." : "Checkout"),
),
],
),
),
),
),
);
}
}