Skip to content

Commit

Permalink
fix platformPayCreatePaymentMethod error (#1882)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduard Dumitrescu <[email protected]>
  • Loading branch information
EduardDumitrescu and Eduard Dumitrescu authored Oct 4, 2024
1 parent 4fbd54f commit dac2181
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 8 deletions.
6 changes: 6 additions & 0 deletions example/lib/screens/screens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -226,6 +227,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.1.1

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

## 6.1.0
- Sync with Stripe [0.38.6](https://github.com/stripe/stripe-react-native/releases/tag/v0.38.6).
- Minor fixes and improvements.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'dart:js_interop';

import 'package:stripe_js/src/js/utils/parse_intent_response.dart';
import 'package:stripe_js/stripe_api.dart';
import 'package:stripe_js/stripe_js.dart';
import 'dart:js_interop';

extension PaymentRequestExtension on Stripe {
_JS get _js => this as _JS;
Expand Down Expand Up @@ -47,7 +48,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 +61,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 _complete.dartify() as void Function(String);
}
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.1.0
version: 6.1.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.1.1
stripe_js: ^6.1.0
stripe_js: ^6.1.1
web: ^1.0.0

dev_dependencies:
Expand Down

0 comments on commit dac2181

Please sign in to comment.