-
-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* BREAKING: min sdk is dart 3.0 * BREAKING: fix #1853 deferred payment for setupintent --------- Co-authored-by: Remon <[email protected]>
- Loading branch information
Showing
16 changed files
with
569 additions
and
107 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ class PaymentSheetDefferedScreen extends StatefulWidget { | |
|
||
class _PaymentSheetScreenState extends State<PaymentSheetDefferedScreen> { | ||
int step = 0; | ||
_PaymentMode? mode = null; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
|
@@ -27,10 +28,42 @@ class _PaymentSheetScreenState extends State<PaymentSheetDefferedScreen> { | |
currentStep: step, | ||
steps: [ | ||
Step( | ||
title: Text('Init payment'), | ||
title: Text('Select mode'), | ||
content: Column( | ||
children: [ | ||
LoadingButton( | ||
onPressed: () async { | ||
setState(() { | ||
mode = _PaymentMode.paymentIntent; | ||
step = 1; | ||
}); | ||
}, | ||
text: 'PaymentIntent', | ||
), | ||
SizedBox(width: 32), | ||
LoadingButton( | ||
onPressed: () async { | ||
setState(() { | ||
mode = _PaymentMode.setupIntent; | ||
step = 1; | ||
}); | ||
}, | ||
text: 'Setup intent', | ||
), | ||
], | ||
), | ||
), | ||
Step( | ||
title: Text('Init paymentsheet'), | ||
content: LoadingButton( | ||
onPressed: initPaymentSheet, | ||
text: 'Init payment sheet', | ||
onPressed: () async { | ||
if (mode == _PaymentMode.paymentIntent) { | ||
await initPaymentSheetPaymentMode(); | ||
} else { | ||
await initPaymentSheetSetupMode(); | ||
} | ||
}, | ||
text: 'Init payment sheet for ${mode?.name}', | ||
), | ||
), | ||
Step( | ||
|
@@ -46,7 +79,8 @@ class _PaymentSheetScreenState extends State<PaymentSheetDefferedScreen> { | |
); | ||
} | ||
|
||
Future<void> _createIntentAndConfirmToUser(String paymentMethodId) async { | ||
Future<void> _createPaymentIntentAndConfirmToUser( | ||
String paymentMethodId) async { | ||
final url = Uri.parse('$kApiUrl/payment-intent-for-payment-sheet'); | ||
final response = await http.post( | ||
url, | ||
|
@@ -62,12 +96,32 @@ class _PaymentSheetScreenState extends State<PaymentSheetDefferedScreen> { | |
throw Exception(body['error']); | ||
} | ||
|
||
await Stripe.instance.intentCreationCallback( | ||
IntentCreationCallbackParams(clientSecret: body['clientSecret'])); | ||
} | ||
|
||
Future<void> _createSetupIntentAndConfirmToUser( | ||
String paymentMethodId) async { | ||
final url = Uri.parse('$kApiUrl/create-setup-intent'); | ||
final response = await http.post( | ||
url, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: json.encode({ | ||
'paymentMethodId': paymentMethodId, | ||
}), | ||
); | ||
final body = json.decode(response.body); | ||
if (body['error'] != null) { | ||
throw Exception(body['error']); | ||
} | ||
|
||
await Stripe.instance.intentCreationCallback( | ||
IntentCreationCallbackParams(clientSecret: body['clientSecret'])); | ||
} | ||
|
||
Future<void> initPaymentSheet() async { | ||
Future<void> initPaymentSheetPaymentMode() async { | ||
try { | ||
// // 1. create payment intent on the server | ||
// final data = await _createTestPaymentSheet(); | ||
|
@@ -91,15 +145,96 @@ class _PaymentSheetScreenState extends State<PaymentSheetDefferedScreen> { | |
await Stripe.instance.initPaymentSheet( | ||
paymentSheetParameters: SetupPaymentSheetParameters( | ||
// Main params | ||
returnURL: 'flutterstripe://stripe-redirect"', | ||
returnURL: 'flutterstripe://redirect', | ||
merchantDisplayName: 'Flutter Stripe Store Demo', | ||
intentConfiguration: IntentConfiguration( | ||
mode: IntentMode( | ||
mode: IntentMode.paymentMode( | ||
currencyCode: 'USD', | ||
amount: 1500, | ||
), | ||
confirmHandler: (method, saveFuture) { | ||
_createIntentAndConfirmToUser(method.id); | ||
_createPaymentIntentAndConfirmToUser(method.id); | ||
}), | ||
|
||
// 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 = 2; | ||
}); | ||
} catch (e) { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar(content: Text('Error: $e')), | ||
); | ||
rethrow; | ||
} | ||
} | ||
|
||
Future<void> initPaymentSheetSetupMode() 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 | ||
returnURL: 'flutterstripe://flutterstripe://redirect', | ||
merchantDisplayName: 'Flutter Stripe Store Demo', | ||
intentConfiguration: IntentConfiguration( | ||
mode: IntentMode.setupMode( | ||
currencyCode: 'USD', | ||
setupFutureUsage: IntentFutureUsage.OffSession, | ||
), | ||
confirmHandler: (method, saveFuture) { | ||
_createSetupIntentAndConfirmToUser(method.id); | ||
}), | ||
|
||
// Extra params | ||
|
@@ -138,7 +273,7 @@ class _PaymentSheetScreenState extends State<PaymentSheetDefferedScreen> { | |
), | ||
); | ||
setState(() { | ||
step = 1; | ||
step = 2; | ||
}); | ||
} catch (e) { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
|
@@ -179,3 +314,5 @@ class _PaymentSheetScreenState extends State<PaymentSheetDefferedScreen> { | |
} | ||
} | ||
} | ||
|
||
enum _PaymentMode { paymentIntent, setupIntent } |
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
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
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
6 changes: 3 additions & 3 deletions
6
packages/stripe_platform_interface/lib/src/models/payment_methods.freezed.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.