Skip to content

Commit

Permalink
fix: #1912 wallet parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Remon committed Sep 18, 2024
1 parent dde7ed5 commit b714060
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ class _CustomerSheetScreenState extends State<CustomerSheetScreen> {
await Stripe.instance.initCustomerSheet(
customerSheetInitParams: CustomerSheetInitParams(
// Main params
setupIntentClientSecret: data['setupIntent'],
// setupIntentClientSecret: data['setupIntent'],
merchantDisplayName: 'Flutter Stripe Store Demo',
allowsRemovalOfLastSavedPaymentMethod: true,
// Customer params
customerId: data['customer'],
customerEphemeralKeySecret: data['ephemeralKeySecret'],
Expand Down
106 changes: 106 additions & 0 deletions example/lib/screens/others/can_add_to_wallet_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:stripe_example/utils.dart';
import 'package:stripe_example/widgets/example_scaffold.dart';
import 'package:stripe_example/widgets/loading_button.dart';
import 'package:stripe_example/widgets/response_card.dart';

class CanAddToWalletScreen extends StatefulWidget {
@override
_CanAddToWalletScreenState createState() => _CanAddToWalletScreenState();
}

class _CanAddToWalletScreenState extends State<CanAddToWalletScreen> {
late TextEditingController _controller;
CanAddCardToWalletResult? canAddCardToWallet;
IsCardInWalletResult? isCardInWallet;

@override
void initState() {
_controller = TextEditingController();
super.initState();
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return ExampleScaffold(
title: 'Can add card to wallet',
tags: ['Provisioning'],
padding: EdgeInsets.all(16),
children: [
TextField(
controller: _controller,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Last4',
),
),
SizedBox(height: 20),
LoadingButton(
onPressed: () async {
final tmp = await _handleCanAddToWallet(_controller.text);
setState(() {
canAddCardToWallet = tmp;
});
},
text: 'Can add card to Wallet',
),
SizedBox(height: 20),
LoadingButton(
onPressed: () async {
final tmp = await _isCardInToWallet(_controller.text);
setState(() {
isCardInWallet = tmp;
});
},
text: 'Is card in Wallet',
),
SizedBox(height: 20),
if (canAddCardToWallet != null || isCardInWallet != null)
ResponseCard(
response: canAddCardToWallet?.toJson().toPrettyString() ??
isCardInWallet?.toJson().toPrettyString() ??
'',
)
],
);
}

Future<CanAddCardToWalletResult> _handleCanAddToWallet(String last4) async {
try {
final result = await Stripe.instance.canAddCardToWallet(
CanAddCardToWalletParams(
cardLastFour: last4,
),
);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Can add card to wallet: ${result}')),
);
return result;
} catch (e) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Error: $e')));
rethrow;
}
}

Future<IsCardInWalletResult> _isCardInToWallet(String last4) async {
try {
final result = await Stripe.instance.isCardInWallet(last4);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Can add card to wallet: ${result}')),
);
return result;
} catch (e) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Error: $e')));
rethrow;
}
}
}
9 changes: 9 additions & 0 deletions example/lib/screens/screens.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:stripe_example/screens/customer_sheet/customer_sheet_screen.dart';
import 'package:stripe_example/screens/others/can_add_to_wallet_screen.dart';
import 'package:stripe_example/screens/payment_sheet/payment_element/payment_element.dart';
import 'package:stripe_example/screens/payment_sheet/payment_sheet_deffered_screen.dart';
import 'package:stripe_example/screens/payment_sheet/payment_sheet_screen.dart';
Expand Down Expand Up @@ -368,6 +369,14 @@ class Example extends StatelessWidget {
DevicePlatform.web,
],
),
Example(
title: 'Can add card to wallet',
builder: (context) => CanAddToWalletScreen(),
platformsSupported: [
DevicePlatform.android,
DevicePlatform.ios,
],
),
]),
];
}
2 changes: 1 addition & 1 deletion example/macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
33CC10EC2044A3C60003C045 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

Check warning on line 1 in example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

View workflow job for this annotation

GitHub Actions / Typo CI

Filename: example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

"xcscheme" in the filename is a typo. Did you mean "scheme"?

Check warning on line 1 in example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

View workflow job for this annotation

GitHub Actions / Typo CI

Filename: example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

"xcschemes" in the filename is a typo. Did you mean "schemes"?

Check warning on line 1 in example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

View workflow job for this annotation

GitHub Actions / Typo CI

Filename: example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

"xcshareddata" in the filename is a typo. Did you mean "sharecropped"?
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,24 +636,28 @@ class MethodChannelStripe extends StripePlatform {
@override
Future<CanAddCardToWalletResult> canAddCardToWallet(
CanAddCardToWalletParams params) async {
final result = await _methodChannel.invokeMethod('canAddCardToWallet', {
final result = await _methodChannel
.invokeMapMethod<String, dynamic>('canAddCardToWallet', {
'params': params.toJson(),
});
if (result!['error'] != null) {
throw StripeException.fromJson(result);
}

return ResultParser<CanAddCardToWalletResult>(
parseJson: (json) => CanAddCardToWalletResult.fromJson(json))
.parse(result: result!, successResultKey: 'canAddCardToWalletResult');
return CanAddCardToWalletResult.fromJson(result);
}

@override
Future<IsCardInWalletResult> isCardInWallet(String cardLastFour) async {
final result = await _methodChannel.invokeMethod('canAddCardToWallet', {
final result = await _methodChannel
.invokeMapMethod<String, dynamic>('isCardInWallet', {
'params': {'cardLastFour': cardLastFour},
});
if (result!['error'] != null) {
throw StripeException.fromJson(result);
}

return ResultParser<IsCardInWalletResult>(
parseJson: (json) => IsCardInWalletResult.fromJson(json))
.parse(result: result!, successResultKey: 'canAddCardToWalletResult');
return IsCardInWalletResult.fromJson(result);
}
}

Expand Down

0 comments on commit b714060

Please sign in to comment.