-
-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Remon
committed
Nov 20, 2023
1 parent
277b786
commit d074ca2
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions
90
example/lib/screens/regional_payment_methods/revolutpay_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,90 @@ | ||
import 'dart:convert'; | ||
Check warning on line 1 in example/lib/screens/regional_payment_methods/revolutpay_screen.dart GitHub Actions / Typo CIFilename: example/lib/screens/regional_payment_methods/revolutpay_screen.dart
|
||
|
||
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 RevolutPayScreen extends StatelessWidget { | ||
const RevolutPayScreen({Key? key}) : super(key: key); | ||
|
||
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({ | ||
'currency': 'eur', | ||
'payment_method_types': ['revolut_pay'], | ||
'amount': 1099 | ||
}), | ||
); | ||
|
||
return json.decode(response.body); | ||
} | ||
|
||
Future<void> _pay(BuildContext context) async { | ||
// Precondition: | ||
//Make sure to have set a custom URI scheme in your app and add it to Stripe SDK | ||
// see file main.dart in this example app. | ||
// 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. | ||
try { | ||
await Stripe.instance.confirmPayment( | ||
paymentIntentClientSecret: clientSecret, | ||
data: PaymentMethodParams.revolutPay( | ||
paymentMethodData: PaymentMethodData(), | ||
), | ||
); | ||
|
||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar( | ||
content: Text('Payment succesfully completed'), | ||
), | ||
); | ||
} on Exception catch (e, s) { | ||
throw e; | ||
if (e is StripeException) { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar( | ||
content: Text( | ||
'Error from Stripe: ${e.error.localizedMessage ?? e.error.code}'), | ||
), | ||
); | ||
} else { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar( | ||
content: Text('Unforeseen error: ${e}'), | ||
), | ||
); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ExampleScaffold( | ||
title: 'RevolutPay', | ||
tags: ['Payment method'], | ||
padding: EdgeInsets.all(16), | ||
children: [ | ||
LoadingButton( | ||
onPressed: () async { | ||
await _pay(context); | ||
}, | ||
text: 'Pay', | ||
), | ||
], | ||
); | ||
} | ||
} |
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