From abc6060f36ff4f99e6b34e5196baf9611f418688 Mon Sep 17 00:00:00 2001 From: Jose Ugia Date: Fri, 5 Jan 2024 15:10:15 +0100 Subject: [PATCH] Extra confirm callback proposal for Apple Pay --- pay/example/lib/main.dart | 6 ++++-- pay/lib/src/widgets/apple_pay_button.dart | 21 ++++++++++++++++++++- pay/lib/src/widgets/google_pay_button.dart | 2 +- pay/lib/src/widgets/pay_button.dart | 10 ++++++---- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/pay/example/lib/main.dart b/pay/example/lib/main.dart index d6a92bcb..df35c047 100644 --- a/pay/example/lib/main.dart +++ b/pay/example/lib/main.dart @@ -72,8 +72,10 @@ class _PaySampleAppState extends State { debugPrint(paymentResult.toString()); } - void onApplePayResult(paymentResult) { + onApplePayResultWithConfirm( + Map paymentResult, PaymentConfirmation handler) { debugPrint(paymentResult.toString()); + handler.confirmPayment(true); } @override @@ -149,7 +151,7 @@ class _PaySampleAppState extends State { style: ApplePayButtonStyle.black, type: ApplePayButtonType.buy, margin: const EdgeInsets.only(top: 15.0), - onPaymentResult: onApplePayResult, + onPaymentResultWithConfirm: onApplePayResultWithConfirm, loadingIndicator: const Center( child: CircularProgressIndicator(), ), diff --git a/pay/lib/src/widgets/apple_pay_button.dart b/pay/lib/src/widgets/apple_pay_button.dart index 0c293d1a..6a00304b 100644 --- a/pay/lib/src/widgets/apple_pay_button.dart +++ b/pay/lib/src/widgets/apple_pay_button.dart @@ -3,6 +3,18 @@ part of '../../pay.dart'; +class PaymentConfirmation { + final Pay _payClient; + PaymentConfirmation(Pay payClient) : _payClient = payClient; + + void confirmPayment(bool completedSuccessfully) { + _payClient.userCanPay(PayProvider.google_pay); + } +} + +typedef PaymentConfirmCallback = Function( + Map result, PaymentConfirmation handler); + /// A widget to show the Apple Pay button according to the rules and constraints /// specified in [PayButton]. /// @@ -29,7 +41,8 @@ class ApplePayButton extends PayButton { 'Prefer to use [paymentConfiguration]. Take a look at the readme to see examples') String? paymentConfigurationAsset, PaymentConfiguration? paymentConfiguration, - required void Function(Map result) onPaymentResult, + PaymentResultCallback? onPaymentResult, + PaymentConfirmCallback? onPaymentResultWithConfirm, required List paymentItems, ApplePayButtonStyle style = ApplePayButtonStyle.black, ApplePayButtonType type = ApplePayButtonType.plain, @@ -59,6 +72,12 @@ class ApplePayButton extends PayButton { style: style, type: type, onPressed: _defaultOnPressed(onPressed, paymentItems)); + + paymentCallback = (result) => { + onPaymentResult?.call(result), + onPaymentResultWithConfirm?.call( + result, PaymentConfirmation(_payClient)) + }; } @override diff --git a/pay/lib/src/widgets/google_pay_button.dart b/pay/lib/src/widgets/google_pay_button.dart index aa6dc794..4813779a 100644 --- a/pay/lib/src/widgets/google_pay_button.dart +++ b/pay/lib/src/widgets/google_pay_button.dart @@ -28,7 +28,7 @@ class GooglePayButton extends PayButton { 'Prefer to use [paymentConfiguration]. Take a look at the readme to see examples') String? paymentConfigurationAsset, PaymentConfiguration? paymentConfiguration, - required void Function(Map result) onPaymentResult, + PaymentResultCallback? onPaymentResult, required List paymentItems, GooglePayButtonType type = GooglePayButtonType.pay, double width = RawGooglePayButton.minimumButtonWidth, diff --git a/pay/lib/src/widgets/pay_button.dart b/pay/lib/src/widgets/pay_button.dart index 955b191d..89a9946c 100644 --- a/pay/lib/src/widgets/pay_button.dart +++ b/pay/lib/src/widgets/pay_button.dart @@ -14,6 +14,8 @@ part of '../../pay.dart'; +typedef PaymentResultCallback = Function(Map result); + /// A widget that handles the API logic to facilitate the integration. /// /// This widget provides an alternative UI-based integration path that wraps @@ -27,13 +29,13 @@ part of '../../pay.dart'; /// method which starts the payment process. abstract class PayButton extends StatefulWidget { /// A resident client to issue requests against the APIs. - late final Pay _payClient; + final Pay _payClient; /// Specifies the payment provider supported by the button final PayProvider buttonProvider; /// A function called when the payment process yields a result. - final void Function(Map result) onPaymentResult; + late final PaymentResultCallback? paymentCallback; final double width; final double height; @@ -59,7 +61,7 @@ abstract class PayButton extends StatefulWidget { 'Prefer to use [paymentConfiguration]. Take a look at the readme to see examples') final String? paymentConfigurationAsset, final PaymentConfiguration? paymentConfiguration, - this.onPaymentResult, + this.paymentCallback, this.width, this.height, this.margin, @@ -83,7 +85,7 @@ abstract class PayButton extends StatefulWidget { try { final result = await _payClient.showPaymentSelector(buttonProvider, paymentItems); - onPaymentResult(result); + paymentCallback?.call(result); } catch (error) { onError?.call(error); }