Skip to content

Commit

Permalink
create example screen for direct debit
Browse files Browse the repository at this point in the history
  • Loading branch information
Remon committed Dec 10, 2024
1 parent 02a685b commit 9edb2e9
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pluginManagement {

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "8.7.3" apply false
id "com.android.application" version "8.6.1" apply false
id "org.jetbrains.kotlin.android" version "1.8.10" apply false
}

Expand Down
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');

Check warning on line 90 in example/lib/screens/regional_payment_methods/us_bank_account_direct_debit_screen.dart

View workflow job for this annotation

GitHub Actions / Typo CI

blaat

"blaat" is a typo. Did you mean "blast"?

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);
}
}
8 changes: 7 additions & 1 deletion example/lib/screens/screens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import 'package:stripe_example/screens/regional_payment_methods/klarna_screen.da
import 'package:stripe_example/screens/regional_payment_methods/paypal_screen.dart';
import 'package:stripe_example/screens/regional_payment_methods/revolutpay_screen.dart';
import 'package:stripe_example/screens/regional_payment_methods/sofort_screen.dart';
import 'package:stripe_example/screens/regional_payment_methods/us_bank_account.dart';
import 'package:stripe_example/screens/regional_payment_methods/us_bank_account_direct_debit_screen.dart';
import 'package:stripe_example/screens/regional_payment_methods/us_bank_account_screen.dart';
import 'package:stripe_example/screens/setup_future_payments/setup_future_payments_screen.dart';
import 'package:stripe_example/screens/wallets/apple_pay_screen.dart';
import 'package:stripe_example/screens/wallets/apple_pay_screen_plugin.dart';
Expand Down Expand Up @@ -341,6 +342,11 @@ class Example extends StatelessWidget {
builder: (contex) => UsBankAccountScreen(),
platformsSupported: [DevicePlatform.android, DevicePlatform.ios],
),
Example(
title: 'Us bank accounts Direct debit(ACH)',
builder: (contex) => UsBankAccountDirectDebitScreen(),

Check warning on line 347 in example/lib/screens/screens.dart

View workflow job for this annotation

GitHub Actions / Typo CI

contex

"contex" is a typo. Did you mean "conte"?
platformsSupported: [DevicePlatform.android, DevicePlatform.ios],
),
// TODO: uncomment when we can re-enable wechat pay
// Example(
// title: 'WeChat Pay',
Expand Down
3 changes: 1 addition & 2 deletions packages/stripe/lib/src/stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,7 @@ class Stripe {

/// Collect the bankaccount details for the payment intent.
///
/// Only US bank accounts are supported. This method is only implemented for
/// iOS at the moment.
/// Only US bank accounts are supported.
Future<PaymentIntent> collectBankAccount({
/// Whether the clientsecret is associated with setup or paymentintent
required bool isPaymentIntent,
Expand Down

0 comments on commit 9edb2e9

Please sign in to comment.