From 41f96b45fee0464fde4e8e5ed757e3166eb67a15 Mon Sep 17 00:00:00 2001 From: Eduard Dumitrescu Date: Mon, 5 Aug 2024 14:31:08 +0300 Subject: [PATCH] fix platformPayCreatePaymentMethod error --- example/lib/screens/screens.dart | 6 ++ ...bile_pay_create_payment_method_screen.dart | 84 +++++++++++++++++++ packages/stripe_js/CHANGELOG.md | 4 + .../js/payment_requests/payment_request.dart | 8 +- packages/stripe_js/pubspec.yaml | 2 +- packages/stripe_web/pubspec.yaml | 2 +- 6 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 example/lib/screens/wallets/mobile_pay_create_payment_method_screen.dart diff --git a/example/lib/screens/screens.dart b/example/lib/screens/screens.dart index 6fdf97ece..de470f7cd 100644 --- a/example/lib/screens/screens.dart +++ b/example/lib/screens/screens.dart @@ -19,6 +19,7 @@ import 'package:stripe_example/screens/wallets/apple_pay_screen.dart'; import 'package:stripe_example/screens/wallets/apple_pay_screen_plugin.dart'; import 'package:stripe_example/screens/wallets/google_pay_screen.dart'; import 'package:stripe_example/screens/wallets/google_pay_stripe_screen.dart'; +import 'package:stripe_example/screens/wallets/mobile_pay_create_payment_method_screen.dart'; import 'package:stripe_example/screens/wallets/open_apple_pay_setup_screen.dart'; import 'package:stripe_example/widgets/platform_icons.dart'; @@ -225,6 +226,11 @@ class Example extends StatelessWidget { builder: (c) => GooglePayScreen(), platformsSupported: [DevicePlatform.android], ), + Example( + title: 'Google/Apple Pay - Create payment method', + builder: (c) => MobilePayCreatePaymentMethodScreen(), + platformsSupported: [DevicePlatform.web], + ), ], ), ExampleSection(title: 'Regional Payment Methods', children: [ diff --git a/example/lib/screens/wallets/mobile_pay_create_payment_method_screen.dart b/example/lib/screens/wallets/mobile_pay_create_payment_method_screen.dart new file mode 100644 index 000000000..6217e2cfa --- /dev/null +++ b/example/lib/screens/wallets/mobile_pay_create_payment_method_screen.dart @@ -0,0 +1,84 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_stripe/flutter_stripe.dart'; +import 'package:stripe_example/widgets/example_scaffold.dart'; +import 'package:stripe_example/widgets/response_card.dart'; + +class MobilePayCreatePaymentMethodScreen extends StatefulWidget { + const MobilePayCreatePaymentMethodScreen({super.key}); + + @override + State createState() => + _MobilePayCreatePaymentMethodScreenState(); +} + +class _MobilePayCreatePaymentMethodScreenState + extends State { + PlatformPayPaymentMethod? response; + + @override + void initState() { + Stripe.instance.isPlatformPaySupportedListenable.addListener(update); + super.initState(); + } + + @override + void dispose() { + Stripe.instance.isPlatformPaySupportedListenable.removeListener(update); + super.dispose(); + } + + void update() { + setState(() {}); + } + + @override + Widget build(BuildContext context) { + return ExampleScaffold( + title: 'Google/Apple Pay', + tags: ['Web'], + padding: EdgeInsets.all(16), + children: [ + if (Stripe.instance.isPlatformPaySupportedListenable.value) + SizedBox( + height: 77, + width: 30, + child: PlatformPayButton( + type: PlatformButtonType.googlePayMark, + onPressed: _handlePayPress, + ), + ) + else + Text('Google/Apple Pay is not available in this device'), + SizedBox( + height: 50, + ), + ResponseCard(response: response?.toString() ?? ''), + ], + ); + } + + Future _handlePayPress() async { + // 1. create payment method + + final paymentMethod = await Stripe.instance.createPlatformPayPaymentMethod( + params: PlatformPayPaymentMethodParams.web( + options: PlatformPayWebPaymentRequestCreateOptions( + country: 'DE', + currency: 'eur', + total: PlatformPayWebPaymentItem( + amount: 1521, + label: '', + ), + ), + ), + ); + + setState(() { + response = paymentMethod; + }); + + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text( + 'Success!: The payment method with id: ${paymentMethod.paymentMethod.id} was created successfully,'))); + } +} diff --git a/packages/stripe_js/CHANGELOG.md b/packages/stripe_js/CHANGELOG.md index 20425e63e..c7a40c3bb 100644 --- a/packages/stripe_js/CHANGELOG.md +++ b/packages/stripe_js/CHANGELOG.md @@ -1,3 +1,7 @@ +## 6.0.1 + +- **FIX**: The platformPayCreatePaymentMethod method results in an error on web (#1879). + ## 6.0.0 - aa diff --git a/packages/stripe_js/lib/src/js/payment_requests/payment_request.dart b/packages/stripe_js/lib/src/js/payment_requests/payment_request.dart index eb0190acd..1048417b2 100644 --- a/packages/stripe_js/lib/src/js/payment_requests/payment_request.dart +++ b/packages/stripe_js/lib/src/js/payment_requests/payment_request.dart @@ -47,7 +47,7 @@ class PaymentResponse { PaymentMethod get paymentMethod => PaymentMethod.fromJson(_js.paymentMethod.toDart); String get walletName => _js.walletName; - Function(String complete) get complete => _js.complete; + void complete(final String complete) => _js.complete(complete); } extension type _JS._(JSObject o) { @@ -60,10 +60,8 @@ extension type JsPaymentResponse._(JSObject o) { external JSMap get paymentMethod; external String get walletName; @JS('complete') - external JSFunction get _complete; - void Function(String) get complete { - return (String val) => _complete.callAsFunction(val.toJS); - } + external void _complete(final String complete); + void complete(final String complete) => _complete(complete); } extension type JsPaymentRequest._(JSObject o) { diff --git a/packages/stripe_js/pubspec.yaml b/packages/stripe_js/pubspec.yaml index 40357307e..8a0e35e8c 100644 --- a/packages/stripe_js/pubspec.yaml +++ b/packages/stripe_js/pubspec.yaml @@ -1,6 +1,6 @@ name: stripe_js description: Stripe.js bindings for dart. This package is used by Stripe web so that the Stripe js sdk can be invoked directly. -version: 6.0.0 +version: 6.0.1 homepage: https://github.com/flutter-stripe/flutter_stripe environment: diff --git a/packages/stripe_web/pubspec.yaml b/packages/stripe_web/pubspec.yaml index 52600f693..f191b707a 100644 --- a/packages/stripe_web/pubspec.yaml +++ b/packages/stripe_web/pubspec.yaml @@ -14,7 +14,7 @@ dependencies: sdk: flutter freezed_annotation: ^2.0.3 stripe_platform_interface: ^11.0.0 - stripe_js: ^6.0.0 + stripe_js: ^6.0.1 web: ^0.5.1 dev_dependencies: