-
-
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.
implement affirm example screen (#1816)
Co-authored-by: Remon <[email protected]>
- Loading branch information
Showing
3 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
example/lib/screens/regional_payment_methods/affirm_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,87 @@ | ||
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 AffirmScreen extends StatelessWidget { | ||
const AffirmScreen({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': 'usd', | ||
'payment_method_types': ['affirm'], | ||
}), | ||
); | ||
|
||
print('blaat ${json.decode(response.body)}'); | ||
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(); | ||
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.affirm(paymentMethodData: PaymentMethodData()), | ||
); | ||
|
||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar( | ||
content: Text('Payment succesfully completed'), | ||
), | ||
); | ||
} on Exception catch (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: 'Affirm', | ||
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