Skip to content

Commit

Permalink
fix platformPayCreatePaymentMethod error
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Dumitrescu committed Aug 5, 2024
1 parent 716dbdf commit 41f96b4
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 7 deletions.
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

0 comments on commit 41f96b4

Please sign in to comment.