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

[DO NOT MERGE] Extra confirm callback proposal for Apple Pay #238

Closed
Show file tree
Hide file tree
Changes from all commits
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: 4 additions & 2 deletions pay/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ class _PaySampleAppState extends State<PaySampleApp> {
debugPrint(paymentResult.toString());
}

void onApplePayResult(paymentResult) {
onApplePayResultWithConfirm(
Map<String, dynamic> paymentResult, PaymentConfirmation handler) {
debugPrint(paymentResult.toString());
handler.confirmPayment(true);
}

@override
Expand Down Expand Up @@ -149,7 +151,7 @@ class _PaySampleAppState extends State<PaySampleApp> {
style: ApplePayButtonStyle.black,
type: ApplePayButtonType.buy,
margin: const EdgeInsets.only(top: 15.0),
onPaymentResult: onApplePayResult,
onPaymentResultWithConfirm: onApplePayResultWithConfirm,
loadingIndicator: const Center(
child: CircularProgressIndicator(),
),
Expand Down
21 changes: 20 additions & 1 deletion pay/lib/src/widgets/apple_pay_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> result, PaymentConfirmation handler);

/// A widget to show the Apple Pay button according to the rules and constraints
/// specified in [PayButton].
///
Expand All @@ -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<String, dynamic> result) onPaymentResult,
PaymentResultCallback? onPaymentResult,
PaymentConfirmCallback? onPaymentResultWithConfirm,
required List<PaymentItem> paymentItems,
ApplePayButtonStyle style = ApplePayButtonStyle.black,
ApplePayButtonType type = ApplePayButtonType.plain,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pay/lib/src/widgets/google_pay_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> result) onPaymentResult,
PaymentResultCallback? onPaymentResult,
required List<PaymentItem> paymentItems,
GooglePayButtonType type = GooglePayButtonType.pay,
double width = RawGooglePayButton.minimumButtonWidth,
Expand Down
10 changes: 6 additions & 4 deletions pay/lib/src/widgets/pay_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

part of '../../pay.dart';

typedef PaymentResultCallback = Function(Map<String, dynamic> result);

/// A widget that handles the API logic to facilitate the integration.
///
/// This widget provides an alternative UI-based integration path that wraps
Expand All @@ -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<String, dynamic> result) onPaymentResult;
late final PaymentResultCallback? paymentCallback;

final double width;
final double height;
Expand All @@ -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,
Expand All @@ -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);
}
Expand Down