Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfixes july #1858

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 146 additions & 9 deletions example/lib/screens/payment_sheet/payment_sheet_deffered_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PaymentSheetDefferedScreen extends StatefulWidget {

class _PaymentSheetScreenState extends State<PaymentSheetDefferedScreen> {
int step = 0;
_PaymentMode? mode = null;

@override
Widget build(BuildContext context) {
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -138,7 +273,7 @@ class _PaymentSheetScreenState extends State<PaymentSheetDefferedScreen> {
),
);
setState(() {
step = 1;
step = 2;
});
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
Expand Down Expand Up @@ -179,3 +314,5 @@ class _PaymentSheetScreenState extends State<PaymentSheetDefferedScreen> {
}
}
}

enum _PaymentMode { paymentIntent, setupIntent }
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 1.0.0
publish_to: 'none'

environment:
sdk: ">=2.17.0 <4.0.0"
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.0.0"

dependencies:
Expand Down
2 changes: 1 addition & 1 deletion packages/stripe/lib/src/widgets/card_form_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class CardFormField extends StatefulWidget {
_CardFormFieldState createState() => _CardFormFieldState();
}

abstract class CardFormFieldContext {
mixin CardFormFieldContext {
void focus();
void blur();

Expand Down
2 changes: 1 addition & 1 deletion packages/stripe/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage: https://github.com/flutter-stripe/flutter_stripe
repository: https://github.com/flutter-stripe/flutter_stripe

environment:
sdk: ">=2.17.0 <4.0.0"
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.0.0"

flutter:
Expand Down
2 changes: 1 addition & 1 deletion packages/stripe_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repository: https://github.com/flutter-stripe/flutter_stripe
homepage: https://pub.dev/packages/flutter_stripe

environment:
sdk: ">=2.12.0 <4.0.0"
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.0.0"

dependencies:
Expand Down
2 changes: 1 addition & 1 deletion packages/stripe_ios/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repository: https://github.com/flutter-stripe/flutter_stripe
homepage: https://pub.dev/packages/flutter_stripe

environment:
sdk: ">=2.12.0 <4.0.0"
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.0.0"

dependencies:
Expand Down
2 changes: 1 addition & 1 deletion packages/stripe_js/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 3.4.0
homepage: https://github.com/flutter-stripe/flutter_stripe

environment:
sdk: ">=2.17.0 <4.0.0"
sdk: ">=3.0.0 <4.0.0"

dependencies:
freezed_annotation: ^2.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const String kDebugPCIMessage =
typedef CardChangedCallback = void Function(CardFieldInputDetails? details);
typedef CardFocusCallback = void Function(CardFieldName? focusedField);

abstract class CardFieldContext {
mixin CardFieldContext {
void focus();
void blur();
void clear();
Expand Down
4 changes: 2 additions & 2 deletions packages/stripe_platform_interface/lib/src/models/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum CustomerSheetError { unknown, failed, canceled }
@freezed

/// Wrapper class that represents an error with the Stripe platform.
class StripeError<T> with _$StripeError<T>, Exception {
class StripeError<T> with _$StripeError<T> implements Exception {
@JsonSerializable(explicitToJson: true)
const factory StripeError({
@Default('Unknown error') String message,
Expand All @@ -34,7 +34,7 @@ Map<String, dynamic> _dataToJson<T>(T input) => {'code': input};
@freezed

/// Exception retrieved from the Stripe platform.
class StripeException with _$StripeException, Exception {
class StripeException with _$StripeException implements Exception {
const factory StripeException({
/// error details
required LocalizedErrorMessage error,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ class IntentConfiguration with _$IntentConfiguration {
}

@freezed
class IntentMode with _$IntentMode {
@JsonSerializable(explicitToJson: true,includeIfNull: false)
const factory IntentMode({
sealed class IntentMode with _$IntentMode {
@JsonSerializable(explicitToJson: true, includeIfNull: false)
const factory IntentMode.paymentMode({
required String currencyCode,
required int amount,

Expand All @@ -148,7 +148,15 @@ class IntentMode with _$IntentMode {

/// Capture method for the future payment intent
CaptureMethod? captureMethod,
}) = _IntentMode;
}) = _PaymentMode;

@JsonSerializable(explicitToJson: true)
const factory IntentMode.setupMode({
String? currencyCode,

/// Data related to the future payment intent
required IntentFutureUsage setupFutureUsage,
}) = _SetupMode;

factory IntentMode.fromJson(Map<String, dynamic> json) =>
_$IntentModeFromJson(json);
Expand Down
Loading
Loading