-
-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create example screen for direct debit
- Loading branch information
Remon
committed
Dec 10, 2024
1 parent
02a685b
commit 9edb2e9
Showing
5 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
example/lib/screens/regional_payment_methods/us_bank_account_direct_debit_screen.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_stripe/flutter_stripe.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:stripe_example/widgets/example_scaffold.dart'; | ||
import 'package:stripe_example/widgets/loading_button.dart'; | ||
|
||
import '../../config.dart'; | ||
|
||
class UsBankAccountDirectDebitScreen extends StatefulWidget { | ||
@override | ||
_UsBankAccountDirectDebitScreenState createState() => | ||
_UsBankAccountDirectDebitScreenState(); | ||
} | ||
|
||
class _UsBankAccountDirectDebitScreenState | ||
extends State<UsBankAccountDirectDebitScreen> { | ||
late TextEditingController _nameController; | ||
late TextEditingController _emailController; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_nameController = TextEditingController(); | ||
_emailController = TextEditingController(); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_nameController.dispose(); | ||
_emailController.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ExampleScaffold( | ||
title: 'ACH payment Direct debit', | ||
tags: ['Payments'], | ||
padding: EdgeInsets.all(16), | ||
children: [ | ||
SizedBox(height: 20), | ||
TextFormField( | ||
decoration: InputDecoration( | ||
border: OutlineInputBorder(), | ||
labelText: 'Email', | ||
), | ||
validator: (value) { | ||
if (value == null || value.isEmpty) { | ||
return 'Please enter an email'; | ||
} | ||
// Simple regex for validating email | ||
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$'); | ||
if (!emailRegex.hasMatch(value)) { | ||
return 'Please enter a valid email'; | ||
} | ||
return null; | ||
}, | ||
controller: _emailController, | ||
), | ||
SizedBox(height: 10), | ||
TextFormField( | ||
decoration: InputDecoration( | ||
border: OutlineInputBorder(), | ||
labelText: 'Name', | ||
), | ||
controller: _nameController, | ||
validator: (value) { | ||
if (value == null || value.isEmpty || value.length < 3) { | ||
return 'Enter a valid name'; | ||
} | ||
return null; | ||
}, | ||
), | ||
SizedBox(height: 10), | ||
LoadingButton( | ||
onPressed: _handlePayPress, | ||
text: 'Pay', | ||
), | ||
], | ||
); | ||
} | ||
|
||
Future<void> _handlePayPress() async { | ||
try { | ||
// 1. call API to create PaymentIntent | ||
final paymentIntentResult = await _createPaymentIntent(); | ||
|
||
print('blaat $json'); | ||
|
||
if (paymentIntentResult['error'] != null) { | ||
// Error during creating or confirming Intent | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar(content: Text('Error: ${paymentIntentResult['error']}'))); | ||
return; | ||
} | ||
|
||
// collect the bankaccount | ||
|
||
if (paymentIntentResult['clientSecret'] != null) { | ||
final result = await Stripe.instance.collectBankAccount( | ||
clientSecret: paymentIntentResult['clientSecret'], | ||
isPaymentIntent: true, | ||
params: CollectBankAccountParams( | ||
billingDetails: BillingDetails( | ||
email: _emailController.text, | ||
name: _nameController.text, | ||
phone: '+48888000888', | ||
address: Address( | ||
city: 'Houston', | ||
country: 'US', | ||
line1: '1459 Circle Drive', | ||
line2: '', | ||
state: 'Texas', | ||
postalCode: '77063', | ||
), | ||
), | ||
), | ||
); | ||
|
||
print('result'); | ||
} | ||
} catch (e) { | ||
ScaffoldMessenger.of(context) | ||
.showSnackBar(SnackBar(content: Text('Error: $e'))); | ||
rethrow; | ||
} | ||
} | ||
|
||
Future<Map<String, dynamic>> _createPaymentIntent() async { | ||
final url = Uri.parse('$kApiUrl/create-payment-intent'); | ||
final response = await http.post( | ||
url, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: json.encode({ | ||
'email': '[email protected]', | ||
'items': ['id-1'], | ||
'currency': 'usd', | ||
'payment_method_types': ['us_bank_account'], | ||
}), | ||
); | ||
|
||
return json.decode(response.body); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters