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

Refactor for platformPayCreatePaymentMethod error #1882

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions example/lib/screens/screens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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: [
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MobilePayCreatePaymentMethodScreen> createState() =>
_MobilePayCreatePaymentMethodScreenState();
}

class _MobilePayCreatePaymentMethodScreenState
extends State<MobilePayCreatePaymentMethodScreen> {
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<void> _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,')));
}
}
4 changes: 4 additions & 0 deletions packages/stripe_js/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.0.1

- **FIX**: The platformPayCreatePaymentMethod method results in an error on web (#1879).

## 6.0.0

- aa
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/stripe_js/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion packages/stripe_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading