-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add possibility to create deffered payment for payment sheet
- Loading branch information
Remon
committed
Aug 12, 2023
1 parent
80449d9
commit 9906a5c
Showing
17 changed files
with
1,114 additions
and
25 deletions.
There are no files selected for viewing
178 changes: 178 additions & 0 deletions
178
example/lib/screens/payment_sheet/payment_sheet_deffered_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,178 @@ | ||
import 'dart:convert'; | ||
Check warning on line 1 in example/lib/screens/payment_sheet/payment_sheet_deffered_screen.dart GitHub Actions / Typo CIFilename: example/lib/screens/payment_sheet/payment_sheet_deffered_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/config.dart'; | ||
import 'package:stripe_example/screens/payment_sheet/payment_sheet_screen_custom_flow.dart'; | ||
import 'package:stripe_example/widgets/example_scaffold.dart'; | ||
import 'package:stripe_example/widgets/loading_button.dart'; | ||
|
||
class PaymentSheetDefferedScreen extends StatefulWidget { | ||
@override | ||
_PaymentSheetScreenState createState() => _PaymentSheetScreenState(); | ||
} | ||
|
||
class _PaymentSheetScreenState extends State<PaymentSheetDefferedScreen> { | ||
int step = 0; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ExampleScaffold( | ||
title: 'Payment Sheet', | ||
tags: ['Single Step'], | ||
children: [ | ||
Stepper( | ||
controlsBuilder: emptyControlBuilder, | ||
currentStep: step, | ||
steps: [ | ||
Step( | ||
title: Text('Init payment'), | ||
content: LoadingButton( | ||
onPressed: initPaymentSheet, | ||
text: 'Init payment sheet', | ||
), | ||
), | ||
Step( | ||
title: Text('Confirm payment'), | ||
content: LoadingButton( | ||
onPressed: confirmPayment, | ||
text: 'Pay now', | ||
), | ||
), | ||
], | ||
), | ||
], | ||
); | ||
} | ||
|
||
Future<void> _createIntentAndConfirmToUser() async { | ||
final url = Uri.parse('$kApiUrl/payment-sheet'); | ||
final response = await http.post( | ||
url, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: json.encode({ | ||
'a': 'a', | ||
}), | ||
); | ||
final body = json.decode(response.body); | ||
if (body['error'] != null) { | ||
throw Exception(body['error']); | ||
} | ||
|
||
await Stripe.instance.intentCreationCallback( | ||
IntentCreationCallbackParams(clientSecret: body['paymentIntent'])); | ||
} | ||
|
||
Future<void> initPaymentSheet() async { | ||
try { | ||
// // 1. create payment intent on the server | ||
// final data = await _createTestPaymentSheet(); | ||
|
||
// create some billingdetails | ||
final billingDetails = BillingDetails( | ||
name: 'Flutter Stripe', | ||
email: '[email protected]', | ||
phone: '+48888000888', | ||
address: Address( | ||
city: 'Houston', | ||
country: 'US', | ||
line1: '1459 Circle Drive', | ||
line2: '', | ||
state: 'Texas', | ||
postalCode: '77063', | ||
), | ||
); // mocked data for tests | ||
|
||
// 2. initialize the payment sheet | ||
await Stripe.instance.initPaymentSheet( | ||
paymentSheetParameters: SetupPaymentSheetParameters( | ||
// Main params | ||
merchantDisplayName: 'Flutter Stripe Store Demo', | ||
intentConfiguration: IntentConfiguration( | ||
mode: IntentMode( | ||
currencyCode: 'EUR', | ||
amount: 1500, | ||
), | ||
intentCreationCallback: (method, saveFuture) { | ||
_createIntentAndConfirmToUser(); | ||
}), | ||
|
||
// Extra params | ||
primaryButtonLabel: 'Pay now', | ||
applePay: PaymentSheetApplePay( | ||
merchantCountryCode: 'DE', | ||
), | ||
googlePay: PaymentSheetGooglePay( | ||
merchantCountryCode: 'DE', | ||
testEnv: true, | ||
), | ||
style: ThemeMode.dark, | ||
appearance: PaymentSheetAppearance( | ||
colors: PaymentSheetAppearanceColors( | ||
background: Colors.lightBlue, | ||
primary: Colors.blue, | ||
componentBorder: Colors.red, | ||
), | ||
shapes: PaymentSheetShape( | ||
borderWidth: 4, | ||
shadow: PaymentSheetShadowParams(color: Colors.red), | ||
), | ||
primaryButton: PaymentSheetPrimaryButtonAppearance( | ||
shapes: PaymentSheetPrimaryButtonShape(blurRadius: 8), | ||
colors: PaymentSheetPrimaryButtonTheme( | ||
light: PaymentSheetPrimaryButtonThemeColors( | ||
background: Color.fromARGB(255, 231, 235, 30), | ||
text: Color.fromARGB(255, 235, 92, 30), | ||
border: Color.fromARGB(255, 235, 92, 30), | ||
), | ||
), | ||
), | ||
), | ||
billingDetails: billingDetails, | ||
), | ||
); | ||
setState(() { | ||
step = 1; | ||
}); | ||
} catch (e) { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar(content: Text('Error: $e')), | ||
); | ||
rethrow; | ||
} | ||
} | ||
|
||
Future<void> confirmPayment() async { | ||
try { | ||
// 3. display the payment sheet. | ||
await Stripe.instance.presentPaymentSheet(); | ||
|
||
setState(() { | ||
step = 0; | ||
}); | ||
|
||
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}'), | ||
), | ||
); | ||
} else { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar( | ||
content: Text('Unforeseen error: ${e}'), | ||
), | ||
); | ||
} | ||
} | ||
} | ||
} |
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
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
13 changes: 13 additions & 0 deletions
13
packages/stripe_platform_interface/lib/src/models/capture_method.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,13 @@ | ||
/// Defines how the money will be captured from the customer's account. | ||
// ignore_for_file: constant_identifier_names | ||
|
||
enum CaptureMethod { | ||
/// Reserve the funds but the customer has to authorize the payment. | ||
Manual, | ||
|
||
/// Funds are automatically captured by stripe | ||
Automatic, | ||
|
||
/// The payment was collected outside of Stripe. | ||
AutomaticAsync, | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/stripe_platform_interface/lib/src/models/intent_creation_callback_params.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,20 @@ | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:stripe_platform_interface/src/models/errors.dart'; | ||
|
||
part 'intent_creation_callback_params.freezed.dart'; | ||
part 'intent_creation_callback_params.g.dart'; | ||
|
||
@freezed | ||
class IntentCreationCallbackParams with _$IntentCreationCallbackParams { | ||
@JsonSerializable(explicitToJson: true) | ||
const factory IntentCreationCallbackParams({ | ||
/// Client secret of the payment intent or setup intent. | ||
String? clientSecret, | ||
|
||
/// Error that occurred during the creation of the payment intent or setup intent. | ||
StripeException? error, | ||
}) = _IntentCreationCallbackParams; | ||
|
||
factory IntentCreationCallbackParams.fromJson(Map<String, dynamic> json) => | ||
_$IntentCreationCallbackParamsFromJson(json); | ||
} |
Oops, something went wrong.