Skip to content

Commit

Permalink
fix collect bank account
Browse files Browse the repository at this point in the history
  • Loading branch information
Remon committed Dec 15, 2024
1 parent 23d79cf commit 2c8f90c
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class RevolutPayScreen extends StatelessWidget {
// 1. on the backend create a payment intent for payment method and save the
// client secret.
final result = await _createPaymentIntent();
print('blaat $result');
final clientSecret = await result['clientSecret'];

// 2. use the client secret to confirm the payment and handle the result.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ class _UsBankAccountDirectDebitScreenState
extends State<UsBankAccountDirectDebitScreen> {
late TextEditingController _nameController;
late TextEditingController _emailController;
bool canConfirm = false;
String clientSecretForConfirm = '';

@override
void initState() {
super.initState();

_nameController = TextEditingController();
_emailController = TextEditingController();
}
Expand Down Expand Up @@ -76,7 +79,13 @@ class _UsBankAccountDirectDebitScreenState
SizedBox(height: 10),
LoadingButton(
onPressed: _handlePayPress,
text: 'Pay',
text: 'Collect account',
),
SizedBox(height: 10),
LoadingButton(
onPressed:
canConfirm ? () => confirmPayment(clientSecretForConfirm) : null,
text: 'Confirm',
),
],
);
Expand All @@ -87,8 +96,6 @@ class _UsBankAccountDirectDebitScreenState
// 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(
Expand All @@ -97,29 +104,42 @@ class _UsBankAccountDirectDebitScreenState
}

// collect the bankaccount
final clientSecret = paymentIntentResult['clientSecret'];

if (paymentIntentResult['clientSecret'] != null) {
final result = await Stripe.instance.collectBankAccount(
clientSecret: paymentIntentResult['clientSecret'],
clientSecret: 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',
paymentMethodData: CollectBankAccountPaymentMethodData(
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');
if (result.status == PaymentIntentsStatus.RequiresConfirmation) {
setState(() {
canConfirm = true;
clientSecretForConfirm = result.clientSecret;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('You can now confirm the payment'),
),
);
}
}
} catch (e) {
ScaffoldMessenger.of(context)
Expand All @@ -128,6 +148,24 @@ class _UsBankAccountDirectDebitScreenState
}
}

Future<void> confirmPayment(String clientSecret) async {
try {
await Stripe.instance.confirmPayment(
paymentIntentClientSecret: clientSecret,
);

ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Payment succesfully completed'),

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

View workflow job for this annotation

GitHub Actions / Typo CI

succesfully

"succesfully" is a typo. Did you mean "successfully"?
),
);
} 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ class MethodChannelStripe extends StripePlatform {
}) async {
final result = await _methodChannel
.invokeMapMethod<String, dynamic>('collectBankAccount', {
'isPaymentIntent': isPaymentIntent,
'intentType': isPaymentIntent,
'params': params.toJson(),
'clientSecret': clientSecret,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CollectBankAccountParams with _$CollectBankAccountParams {
/// Billingdetails of the account holder
///
/// It is required to fill in the name in the billing details
required BillingDetails billingDetails,
required CollectBankAccountPaymentMethodData paymentMethodData,

/// The paymentmethod type. At this point only method [PaymentMethodType.USBankAccount]
/// is supported.
Expand All @@ -26,6 +26,21 @@ class CollectBankAccountParams with _$CollectBankAccountParams {
_$CollectBankAccountParamsFromJson(json);
}

/// Specific payment method data needed for collecting the bank account of
/// an payment method
@freezed

Check warning on line 31 in packages/stripe_platform_interface/lib/src/models/ach_params.dart

View workflow job for this annotation

GitHub Actions / Typo CI

freezed

"freezed" is a typo. Did you mean "freezes"?
class CollectBankAccountPaymentMethodData
with _$CollectBankAccountPaymentMethodData {
@JsonSerializable(explicitToJson: true)
const factory CollectBankAccountPaymentMethodData({
required BillingDetails billingDetails,
}) = _CollectBankAccountPaymentMethodData;

factory CollectBankAccountPaymentMethodData.fromJson(
Map<String, dynamic> json) =>
_$CollectBankAccountPaymentMethodDataFromJson(json);
}

/// The type of payment intent used for collecting bank accoutn
@freezed
Expand Down
Loading

0 comments on commit 2c8f90c

Please sign in to comment.