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

Flutter stripe platform api #2

Merged
merged 7 commits into from
Apr 2, 2021
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
4 changes: 3 additions & 1 deletion stripe/example/lib/screens/no_webhook_payment_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class NoWebhookPaymentScreen extends StatelessWidget {
appBar: AppBar(),
body: Column(
children: [
CardField(),
CardField(
onCardChanged: (_) {},
),
ElevatedButton(
onPressed: _handlePayPress,
child: Text('Pay'),
Expand Down
28 changes: 17 additions & 11 deletions stripe/example/lib/screens/setup_future_payment_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SetupFuturePaymentScreen extends StatefulWidget {

class _SetupFuturePaymentScreenState extends State<SetupFuturePaymentScreen> {
PaymentIntent retrievedPaymentIntent;
CardFieldInputDetails card;
CardFieldInputDetails _card;

@override
Widget build(BuildContext context) {
Expand All @@ -18,7 +18,13 @@ class _SetupFuturePaymentScreenState extends State<SetupFuturePaymentScreen> {
body: Column(
children: [
TextField(),
CardField(),
CardField(
onCardChanged: (card) {
setState(() {
_card = card;
});
},
),
ElevatedButton(
onPressed: _handlePayPress,
child: Text('Save'),
Expand All @@ -36,7 +42,7 @@ class _SetupFuturePaymentScreenState extends State<SetupFuturePaymentScreen> {
}

Future<void> _handlePayPress() async {
if (card == null) {
if (_card == null) {
return;
}

Expand All @@ -47,19 +53,19 @@ class _SetupFuturePaymentScreenState extends State<SetupFuturePaymentScreen> {
final BillingDetails billingDetails = BillingDetails(
email: 'email',
phone: '+48888000888',
/*addressCity: 'Houston',
addressCountry: 'US',
addressCity: 'Houston',
addressCountry: 'US', // TODO country
addressLine1: '1459 Circle Drive',
addressLine2: 'Texas',
addressPostalCode: '77063',*/
addressLine2: 'test',
adressPostalCode: '77063',
); // mocked data for tests

// 3. Confirm setup intent
await Stripe.instance
.confirmSetupIntent(
clientSecret,
CardParams(
details: card,
PaymentMethodParams.card(
details: _card,
//billingDetails,
))
.then((setupIntentResult) {
Expand All @@ -68,8 +74,8 @@ class _SetupFuturePaymentScreenState extends State<SetupFuturePaymentScreen> {
'Success: Setup intent created. Intent status: ${setupIntentResult.status}')));
//setSetupIntent(setupIntentResult);
}).catchError((error) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Error code: ${error.code} - ${error.message}')));
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Error code: ${error}')));
});
}

Expand Down
46 changes: 30 additions & 16 deletions stripe/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ packages:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.2"
version: "2.0.13"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.0"
async:
dependency: transitive
description:
Expand Down Expand Up @@ -50,27 +57,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
version: "2.1.5"
cupertino_icons:
dependency: "direct main"
description:
name: cupertino_icons
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
equatable:
dependency: transitive
description:
name: equatable
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
fake_async:
dependency: transitive
description:
Expand All @@ -84,7 +91,7 @@ packages:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.0"
version: "6.0.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -100,6 +107,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
freezed_annotation:
dependency: transitive
description:
name: freezed_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.1"
fuchsia_remote_debug_protocol:
dependency: transitive
description: flutter
Expand All @@ -123,7 +137,7 @@ packages:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
version: "0.9.2+2"
json_annotation:
dependency: transitive
description:
Expand Down Expand Up @@ -179,7 +193,7 @@ packages:
name: process
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.0"
version: "4.0.0"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -191,7 +205,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.0"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -247,7 +261,7 @@ packages:
name: sync_http
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.2.0"
term_glyph:
dependency: transitive
description:
Expand Down Expand Up @@ -282,14 +296,14 @@ packages:
name: vm_service
url: "https://pub.dartlang.org"
source: hosted
version: "6.0.1-nullsafety.1"
version: "5.5.0"
webdriver:
dependency: transitive
description:
name: webdriver
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
version: "2.1.2"
sdks:
dart: ">=2.12.0-224.0.dev <3.0.0"
flutter: ">=1.20.0"
33 changes: 19 additions & 14 deletions stripe/lib/src/stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:stripe_platform_interface/src/method_channel_stripe.dart';
import 'package:stripe_platform_interface/src/models/apple_pay.dart';
import 'package:stripe_platform_interface/src/models/app_info.dart';
import 'package:stripe_platform_interface/src/models/errors.dart';
import 'package:stripe_platform_interface/src/models/payment_intents.dart';
import 'package:stripe_platform_interface/src/models/payment_methods.dart';
Expand Down Expand Up @@ -46,7 +47,6 @@ class Stripe {

Future<void> initialise({
required String publishableKey,
// AppInfo? appInfo,
String? stripeAccountId,
ThreeDSecureConfigurationParams? threeDSecureParams,
String? merchantIdentifier,
Expand Down Expand Up @@ -75,7 +75,7 @@ class Stripe {
final paymentMethod = await _platform.createPaymentMethod(data, options);
return paymentMethod;
} on StripeError catch (error) {
throw StripeError<CreatePaymentMethodError>(error.code, error.message);
throw StripeError.generic(message: error.message, code: error.message);
}
}

Expand All @@ -84,7 +84,7 @@ class Stripe {
final paymentMethod = await _platform.retrievePaymentIntent(clientSecret);
return paymentMethod;
} on StripeError catch (error) {
throw StripeError<RetrievePaymentIntentError>(error.code, error.message);
throw StripeError.generic(message: error.message, code: error.message);
}
}

Expand All @@ -106,8 +106,7 @@ class Stripe {
ApplePayPresentParams params,
) async {
if (!isApplePaySupported.value) {
throw StripeError<ApplePayError>(
ApplePayError.canceled, 'APPLE_PAY_NOT_SUPPORTED_MESSAGE');
//throw StripeError<ApplePayError>(ApplePayError.canceled, 'APPLE_PAY_NOT_SUPPORTED_MESSAGE');
}
try {
await _platform.presentApplePay(params);
Expand All @@ -120,8 +119,7 @@ class Stripe {
String clientSecret,
) async {
if (!isApplePaySupported.value) {
throw StripeError<ApplePayError>(
ApplePayError.canceled, 'APPLE_PAY_NOT_SUPPORTED_MESSAGE');
//throw StripeError<ApplePayError>(ApplePayError.canceled, 'APPLE_PAY_NOT_SUPPORTED_MESSAGE');
}
try {
await _platform.confirmApplePayPayment(clientSecret);
Expand All @@ -138,7 +136,8 @@ class Stripe {
await _platform.handleCardAction(paymentIntentClientSecret);
return paymentIntent;
} on StripeError catch (error) {
throw StripeError<CardActionError>(error.code, error.message);
//throw StripeError<CardActionError>(error.code, error.message);
rethrow;
}
}

Expand All @@ -152,7 +151,8 @@ class Stripe {
paymentIntentClientSecret, data, options);
return setupIntent;
} on StripeError catch (error) {
throw StripeError<CardActionError>(error.code, error.message);
//throw StripeError<CardActionError>(error.code, error.message);
rethrow;
}
}

Expand All @@ -165,7 +165,8 @@ class Stripe {
);
return tokenId;
} on StripeError catch (error) {
throw StripeError<CardActionError>(error.code, error.message);
//throw StripeError<CardActionError>(error.code, error.message);
rethrow;
}
}

Expand All @@ -177,7 +178,8 @@ class Stripe {
);
return option;
} on StripeError catch (error) {
throw StripeError<CardActionError>(error.code, error.message);
//throw StripeError<CardActionError>(error.code, error.message);
rethrow;
}
}

Expand All @@ -187,7 +189,8 @@ class Stripe {
final option = await _platform.presentPaymentSheet(clientSecret);
return option;
} on StripeError catch (error) {
throw StripeError<CardActionError>(error.code, error.message);
//throw StripeError<CardActionError>(error.code, error.message);
rethrow;
} finally {
_isPaymentSheetDisplayed.value = false;
}
Expand All @@ -198,7 +201,8 @@ class Stripe {
final option = await _platform.paymentSheetConfirmPayment();
return option;
} on StripeError catch (error) {
throw StripeError<CardActionError>(error.code, error.message);
//throw StripeError<CardActionError>(error.code, error.message);
rethrow;
}
}

Expand All @@ -207,7 +211,8 @@ class Stripe {
final option = await _platform.presentPaymentOptions();
return option;
} on StripeError catch (error) {
throw StripeError<CardActionError>(error.code, error.message);
//throw StripeError<CardActionError>(error.code, error.message);
rethrow;
}
}
}
Expand Down
30 changes: 10 additions & 20 deletions stripe/lib/src/widgets/card_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ typedef CardFocusCallback = void Function(CardFieldName? focusedField);

// TODO refactor this for parameters
class CardField extends StatelessWidget {
final CardChangedCallback onCardChanged;

const CardField({Key? key, required this.onCardChanged}) : super(key: key);

@override
Widget build(BuildContext context) {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return AndroidCardField();
return AndroidCardField(
onChange: onCardChanged,
);
case TargetPlatform.iOS:
return UiKitCardField();
return UiKitCardField(
onChange: onCardChanged,
);
default:
throw UnsupportedError("Unsupported platform view");
}
Expand Down Expand Up @@ -266,21 +274,3 @@ class _UiKitCardFieldState extends State<UiKitCardField> {
super.didUpdateWidget(oldWidget);
}
}

class NativeCardDetails extends CardFieldInputDetails {
NativeCardDetails({
required String last4,
required int expiryMonth,
required int expiryYear,
String? postalCode,
required CardBrand brand,
required bool complete,
}) : super(
last4: last4,
expiryMonth: expiryMonth,
expiryYear: expiryYear,
postalCode: postalCode,
brand: brand,
complete: complete,
);
}
Loading