lpe 0.0.7
lpe: ^0.0.7 copied to clipboard
Learmond Pay Element - Paysheet built in flutter. Transposable to any app framework.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:lpe/lpe.dart';
// Removed unused imports: dart:convert and http; add them when implementing server calls
void main() {
// Initialize default merchant identifiers for the example app so the
// `LearmondPayButtons` and native pay flows have default values if not
// provided explicitly by the caller.
LpeConfig.init(
appleMerchantId: 'merchant.com.example',
googleGatewayMerchantId: 'yourGatewayMerchantId',
);
runApp(const ExampleApp());
}
// Note: You may call `LpeConfig.init(...)` at app startup to set defaults
// for Apple/Google merchant ids. Example usage is shown above in `main()`.
class ExampleApp extends StatelessWidget {
const ExampleApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'LPE Example',
theme: ThemeData(primarySwatch: Colors.deepPurple),
home: const ExampleHome(),
);
}
}
class ExampleHome extends StatefulWidget {
const ExampleHome({Key? key}) : super(key: key);
@override
State<ExampleHome> createState() => _ExampleHomeState();
}
class _ExampleHomeState extends State<ExampleHome> {
final _amountController = TextEditingController(text: '10.00');
final _publishableController = TextEditingController();
final _clientSecretController = TextEditingController();
final _merchantIdController =
TextEditingController(text: 'merchant.com.example');
String _log = '';
void _appendLog(String s) => setState(() => _log = '$_log\n$s');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('LPE Example')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextField(
controller: _amountController,
decoration: const InputDecoration(labelText: 'Amount')),
const SizedBox(height: 8),
TextField(
controller: _publishableController,
decoration: const InputDecoration(
labelText: 'Publishable Key (optional)')),
const SizedBox(height: 8),
TextField(
controller: _clientSecretController,
decoration: const InputDecoration(
labelText: 'Client Secret (optional)')),
const SizedBox(height: 8),
TextField(
controller: _merchantIdController,
decoration: const InputDecoration(
labelText: 'Merchant ID (Apple Pay)')),
const SizedBox(height: 8),
LearmondPayButtons(
publishableKey: _publishableController.text.isEmpty
? null
: _publishableController.text,
clientSecret: _clientSecretController.text.isEmpty
? null
: _clientSecretController.text,
merchantId: _merchantIdController.text,
amount: _amountController.text,
currency: 'USD',
onResult: (r) {
_appendLog(
'Widget onResult: success=${r.success}, error=${r.error}');
},
),
const SizedBox(height: 8),
// Custom manual buttons demonstrating programmatic integration
Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: () async {
// If you have a client secret from your server, you can use it directly
// Otherwise this should call your server to create a PaymentIntent
final clientSecret =
_clientSecretController.text.isNotEmpty
? _clientSecretController.text
: null;
_appendLog('Manual: invoking card paysheet');
final result = await LearmondPaySheet.show(
context: context,
publishableKey: _publishableController.text.isEmpty
? 'pk_test_...'
: _publishableController.text,
clientSecret: clientSecret ?? 'pi_test_client_secret',
method: 'card',
title: 'Manual Card Pay',
amount: _amountController.text,
);
_appendLog(
'Manual card result: success=${result.success}, error=${result.error}');
},
child: const Text('Manual Card Paysheet'),
),
),
const SizedBox(width: 8),
Expanded(
child: ElevatedButton(
onPressed: () async {
final amountCents =
(double.tryParse(_amountController.text) ?? 0.0 * 100)
.round();
_appendLog('Manual: invoking native Apple Pay');
final res = await LearmondNativePay.showNativePay({
'method': 'apple_pay',
'merchantId': _merchantIdController.text,
'amountCents': amountCents,
'currency': 'USD',
});
_appendLog(
'Manual Apple Pay result: success=${res.success}, error=${res.error}, raw=${res.rawResult}');
},
child: const Text('Manual Apple Pay'),
),
),
],
),
const SizedBox(height: 12),
const Divider(),
const Text('Output', style: TextStyle(fontWeight: FontWeight.bold)),
Expanded(child: SingleChildScrollView(child: Text(_log))),
],
),
),
);
}
}