flutter_braintree_native 0.1.1
flutter_braintree_native: ^0.1.1 copied to clipboard
Flutter plugin that wraps the native Braintree SDKs. Enables payments with credit cards, PayPal, Google Pay, Apple Pay and more.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_braintree_native/flutter_braintree_native.dart';
void main() => runApp(
const MaterialApp(
home: MyApp(),
debugShowCheckedModeBanner: false,
),
);
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const String tokenizationKey = "sandbox_tokenization_key"; // Your sandbox tokenization key
static const String clientToken = "123456..."; // Your customer client token generated by your backend
static const String appLinkUrl = "https://your-domain.com/braintree-payments/venmo"; // Your website domain name or app link url if you've setup deep links
static const String returnUrl = "https://your-domain.com/mobile/paypal"; // return url must match Android app links
void showResultDialog(dynamic result) {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: const Text("Gateway Response"),
content: SingleChildScrollView(
child: Text(result != null ? result.toString() : "NULL (Cancelled)"),
),
actions: [
TextButton(
child: const Text("OK"),
onPressed: () => Navigator.pop(context),
)
],
),
);
}
Divider get sep => const Divider(height: 32);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('flutter_braintree_native Example')),
body: Padding(
padding: const EdgeInsets.all(24),
child: ListView(
children: [
const Text("👀 Device Data", style: TextStyle(fontSize: 18)),
ElevatedButton(
onPressed: () async {
try {
final data = await Braintree.collectDeviceData(tokenizationKey);
showResultDialog(data);
} catch (e) {
showResultDialog("ERROR: $e");
}
},
child: const Text("Collect Device Data"),
),
sep,
const Text("💳 Credit Card (3DS)", style: TextStyle(fontSize: 18)),
ElevatedButton(
onPressed: () async {
try {
// if you need to pass postal code and billing address then us startCardPaymentWithBilling
final result = await Braintree.startCardPayment(
authorization: clientToken,
cardNumber: "4111111111111111",
expirationMonth: "12",
expirationYear: "2030",
cvv: "100",
amount: "6.20", // in US Dollars
);
showResultDialog(result);
} catch (e) {
showResultDialog("ERROR: $e");
}
},
child: const Text("Start Card Payment"),
),
sep,
Platform.isIOS ? const Text(" Apple Pay", style: TextStyle(fontSize: 18)) : const Text("💲 Google Pay", style: TextStyle(fontSize: 18)),
Platform.isIOS
? ElevatedButton(
onPressed: () async {
try {
final result = await Braintree.startApplePay(
tokenizationKey: tokenizationKey,
amount: "20.10",
displayName: "Whatever Name you wanna show on ApplePay sheet",
companyName: "Company Name",
countryCode: "US", // optional
currencyCode: "USD", // optional
merchantIdentifier: "merchant.your-domain.com",
);
showResultDialog(result);
} catch (e) {
showResultDialog("ERROR: $e");
}
},
child: const Text("Start Card Payment"),
)
: ElevatedButton(
onPressed: () async {
try {
final result = await Braintree.startGooglePay(
tokenizationKey: tokenizationKey,
amount: "19.99",
currencyCode: "USD",
merchantName: "",
environment: "TEST", // or "PRODUCTION"
);
showResultDialog(result);
} catch (e) {
showResultDialog("ERROR: $e");
}
},
child: const Text("Start Google Pay"),
),
sep,
const Text("🅿️ PayPal Checkout", style: TextStyle(fontSize: 18)),
ElevatedButton(
onPressed: () async {
try {
final result = await Braintree.startPayPal(
authorization: tokenizationKey,
amount: "12.97",
currencyCode: "USD",
returnUrl: returnUrl,
hasUserLocationConsent: false,
);
showResultDialog(result);
} catch (e) {
showResultDialog("ERROR: $e");
}
},
child: const Text("Start PayPal Checkout"),
),
sep,
const Text("🟣 Venmo", style: TextStyle(fontSize: 18)),
ElevatedButton(
onPressed: () async {
try {
final result = await Braintree.startVenmo(
tokenizationKey: tokenizationKey,
appLinkUrl: appLinkUrl,
amount: "11.22",
usage: "SINGLE_USE",
);
showResultDialog(result);
} catch (e) {
showResultDialog("ERROR: $e");
}
},
child: const Text("Start Venmo"),
),
],
),
),
);
}
}