-
Notifications
You must be signed in to change notification settings - Fork 147
➕ Add the success/failure handling from Dart side for Apple Pay #200
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
a3b1b81
78dfe7b
fcab163
0d8c189
23b63f8
cddc099
1323120
4a48cc8
a06e1ec
cd07e7c
87268b8
8e8e194
8d236bb
122d7ba
b09fd8c
73fa61c
747ff50
b39bddd
15fb1aa
7b64e53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| /// See the License for the specific language governing permissions and | ||
| /// limitations under the License. | ||
|
|
||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_localizations/flutter_localizations.dart'; | ||
| import 'package:pay/pay.dart'; | ||
|
|
@@ -44,6 +45,8 @@ class PayMaterialApp extends StatelessWidget { | |
| const Locale('es', ''), | ||
| const Locale('de', ''), | ||
| ], | ||
| theme: ThemeData.light(), | ||
| darkTheme: ThemeData.dark(), | ||
YazeedAlKhalaf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| home: PaySampleApp(), | ||
| ); | ||
| } | ||
|
|
@@ -57,21 +60,32 @@ class PaySampleApp extends StatefulWidget { | |
| } | ||
|
|
||
| class _PaySampleAppState extends State<PaySampleApp> { | ||
| final _applePayConfig = PaymentConfiguration.fromJsonString( | ||
| payment_configurations.defaultApplePay, | ||
| ); | ||
| late final Future<PaymentConfiguration> _googlePayConfigFuture; | ||
| late Pay _pay; | ||
|
||
|
|
||
| bool shouldPaymentSucceedOnIos = true; | ||
|
|
||
| @override | ||
| void initState() { | ||
| super.initState(); | ||
| _googlePayConfigFuture = | ||
| PaymentConfiguration.fromAsset('default_google_pay_config.json'); | ||
| _googlePayConfigFuture = PaymentConfiguration.fromAsset( | ||
| 'default_google_pay_config.json', | ||
| ); | ||
| _pay = Pay({ | ||
| PayProvider.apple_pay: _applePayConfig, | ||
| }); | ||
| } | ||
|
|
||
| void onGooglePayResult(paymentResult) { | ||
| debugPrint(paymentResult.toString()); | ||
| } | ||
|
|
||
| void onApplePayResult(paymentResult) { | ||
| void onApplePayResult(paymentResult) async { | ||
| debugPrint(paymentResult.toString()); | ||
| await _pay.updatePaymentStatus(shouldPaymentSucceedOnIos); | ||
| } | ||
|
|
||
| @override | ||
|
|
@@ -126,23 +140,25 @@ class _PaySampleAppState extends State<PaySampleApp> { | |
| ), | ||
| // Example pay button configured using an asset | ||
| FutureBuilder<PaymentConfiguration>( | ||
| future: _googlePayConfigFuture, | ||
| builder: (context, snapshot) => snapshot.hasData | ||
| ? GooglePayButton( | ||
| paymentConfiguration: snapshot.data!, | ||
| paymentItems: _paymentItems, | ||
| type: GooglePayButtonType.buy, | ||
| margin: const EdgeInsets.only(top: 15.0), | ||
| onPaymentResult: onGooglePayResult, | ||
| loadingIndicator: const Center( | ||
| child: CircularProgressIndicator(), | ||
| ), | ||
| ) | ||
| : const SizedBox.shrink()), | ||
| future: _googlePayConfigFuture, | ||
| builder: (context, snapshot) => snapshot.hasData | ||
| ? GooglePayButton( | ||
| paymentConfiguration: snapshot.data!, | ||
| paymentItems: _paymentItems, | ||
| type: GooglePayButtonType.buy, | ||
| margin: const EdgeInsets.only(top: 15.0), | ||
| onPaymentResult: onGooglePayResult, | ||
| loadingIndicator: const Center( | ||
| child: CircularProgressIndicator(), | ||
| ), | ||
| ) | ||
| : const SizedBox.shrink(), | ||
| ), | ||
| // Example pay button configured using a string | ||
| ApplePayButton( | ||
| paymentConfiguration: PaymentConfiguration.fromJsonString( | ||
| payment_configurations.defaultApplePay), | ||
| payment_configurations.defaultApplePay, | ||
| ), | ||
| paymentItems: _paymentItems, | ||
| style: ApplePayButtonStyle.black, | ||
| type: ApplePayButtonType.buy, | ||
|
|
@@ -152,6 +168,25 @@ class _PaySampleAppState extends State<PaySampleApp> { | |
| child: CircularProgressIndicator(), | ||
| ), | ||
| ), | ||
| // This allows you to control whether the payment shall succeed or not. | ||
| // This is only available on iOS. | ||
| // Usually you would want to wait for your backend to return a value, | ||
| // before you update the payment status. | ||
| if (defaultTargetPlatform == TargetPlatform.iOS) | ||
YazeedAlKhalaf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Row( | ||
| mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||
| children: [ | ||
| Text('Should the payment succeed?'), | ||
| Switch.adaptive( | ||
| value: shouldPaymentSucceedOnIos, | ||
| onChanged: (value) { | ||
| setState(() { | ||
| shouldPaymentSucceedOnIos = value; | ||
| }); | ||
| }, | ||
| ), | ||
| ], | ||
| ), | ||
| const SizedBox(height: 15) | ||
| ], | ||
| ), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,14 +31,19 @@ class Pay { | |
|
|
||
| /// Creates an instance with a dictionary of [_configurations] and | ||
| /// instantiates the [_payPlatform] to communicate with the native platforms. | ||
| Pay(this._configurations) : _payPlatform = PayMethodChannel(); | ||
| Pay(this._configurations) | ||
| : _payPlatform = defaultTargetPlatform == TargetPlatform.iOS | ||
| ? IOSPayMethodChannel() | ||
| : PayMethodChannel(); | ||
|
|
||
| /// Alternative constructor to create a [Pay] object with a list of | ||
| /// configurations in [String] format. | ||
| @Deprecated( | ||
| 'Prefer to use [Pay({ [PayProvider]: [PaymentConfiguration] })]. Take a look at the readme to see examples') | ||
| Pay.withAssets(List<String> configAssets) | ||
| : _payPlatform = PayMethodChannel() { | ||
| : _payPlatform = defaultTargetPlatform == TargetPlatform.iOS | ||
| ? IOSPayMethodChannel() | ||
| : PayMethodChannel() { | ||
| _assetInitializationFuture = _loadConfigAssets(configAssets); | ||
| } | ||
|
|
||
|
|
@@ -76,6 +81,16 @@ class Pay { | |
| _configurations![provider]!, paymentItems); | ||
| } | ||
|
|
||
| /// Update the payment status with the native platform. | ||
| /// Works only on iOS. | ||
| Future<void> updatePaymentStatus(bool isSuccess) async { | ||
| if (_payPlatform is IOSPayMethodChannel) { | ||
|
||
| await _assetInitializationFuture; | ||
| final iosPayPlatform = _payPlatform as IOSPayMethodChannel; | ||
| return iosPayPlatform.updatePaymentStatus(isSuccess); | ||
| } | ||
| } | ||
|
|
||
| /// Verifies that the selected provider has been previously configured or | ||
| /// throws otherwise. | ||
| Future throwIfProviderIsNotDefined(PayProvider provider) async { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /// Copyright 2023 Google LLC. | ||
| /// SPDX-License-Identifier: Apache-2.0 | ||
| import 'package:pay_platform_interface/pay_channel.dart'; | ||
|
|
||
| /// This implements the iOS specific functionality of the Pay plugin. | ||
| class IOSPayMethodChannel extends PayMethodChannel { | ||
YazeedAlKhalaf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// Update the payment status with the native platform. | ||
| Future<void> updatePaymentStatus(bool isSuccess) async { | ||
| return channel.invokeMethod('updatePaymentStatus', isSuccess); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /// Copyright 2023 Google LLC | ||
| /// | ||
| /// Licensed under the Apache License, Version 2.0 (the "License"); | ||
| /// you may not use this file except in compliance with the License. | ||
| /// You may obtain a copy of the License at | ||
| /// | ||
| /// https://www.apache.org/licenses/LICENSE-2.0 | ||
| /// | ||
| /// Unless required by applicable law or agreed to in writing, software | ||
| /// distributed under the License is distributed on an "AS IS" BASIS, | ||
| /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| /// See the License for the specific language governing permissions and | ||
| /// limitations under the License. | ||
|
|
||
| import 'package:flutter/services.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:pay_ios/pay_ios.dart'; | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| late final IOSPayMethodChannel _payChannel; | ||
|
|
||
| setUpAll(() async { | ||
| _payChannel = IOSPayMethodChannel(); | ||
| }); | ||
|
|
||
| group('Verify channel I/O for', () { | ||
| final log = <MethodCall>[]; | ||
| const testResponses = <String, Object?>{ | ||
| 'updatePaymentStatus': null, | ||
| }; | ||
|
|
||
| setUp(() { | ||
| TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger | ||
| .setMockMethodCallHandler( | ||
| _payChannel.channel, | ||
| (MethodCall methodCall) async { | ||
| log.add(methodCall); | ||
| final response = testResponses[methodCall.method]; | ||
| if (response is Exception) { | ||
| return Future<Object?>.error(response); | ||
| } | ||
| return Future<Object?>.value(response); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| test('updatePaymentStatus', () async { | ||
| await _payChannel.updatePaymentStatus(true); | ||
| expect( | ||
| log, | ||
| <Matcher>[isMethodCall('updatePaymentStatus', arguments: true)], | ||
| ); | ||
| }); | ||
| }); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.