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

paymentItems is not reactive #235

Closed
dickermoshe opened this issue Dec 26, 2023 · 3 comments
Closed

paymentItems is not reactive #235

dickermoshe opened this issue Dec 26, 2023 · 3 comments

Comments

@dickermoshe
Copy link
Contributor

BUG

If you change the value of paymentItems on a PayButton it doesn't update it internally.

Example

This Widget returns a ApplePayButton that will charge priceInCents.
However, If the parent widget changes the price (E.G. The customer selects a different tip amount) the payment will still show the original amount.

class PaymentButton extends HookConsumerWidget {
  const PaymentButton(
      {super.key,
      required this.priceInCents,
      this.onPressed,
      this.onSuccess,
      this.onError});
  final int priceInCents;
  final VoidCallback? onPressed;
  final VoidCallback? onSuccess;
  final void Function(Object?)? onError;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final paymentItems = [
      p.PaymentItem(
          label: "Donation",
          amount: (priceInCents / 100).toStringAsFixed(2),
          type: p.PaymentItemType.total,
          status: p.PaymentItemStatus.final_price)
    ];

    return p.ApplePayButton(
      onPressed: onPressed,
      paymentConfiguration:
          p.PaymentConfiguration.fromJsonString(defaultApplePay),
      type: p.ApplePayButtonType.donate,
      onPaymentResult: (result) {
        runApplePay(result, amountInCents: priceInCents);
        if (onSuccess != null) onSuccess!();
      },
      paymentItems: paymentItems,
    );
  }
}
Simulator.Screen.Recording.-.iPhone.15.Pro.-.2023-12-26.at.01.47.53.1.mp4

Temp Workaround

Assign the button a UniqueKey and change it when the priceInCents changes.
This will force Flutter to rebuild the entire ApplePayButton
(This is using flutter_hooks, but you could use something else to do this.)

 final buttonKey = useMemoized(() => UniqueKey(), [priceInCents]);
@dickermoshe dickermoshe changed the title paymentItems are not reactive paymentItems is not reactive Dec 26, 2023
@dickermoshe
Copy link
Contributor Author

dickermoshe commented Dec 26, 2023

Cause

This is because onMethodCallHandler is only called once.

return UiKitView(
viewType: buttonId,
creationParamsCodec: const StandardMessageCodec(),
creationParams: {'style': style.enumString, 'type': type.enumString},
onPlatformViewCreated: (viewId) {
methodChannel = MethodChannel('$buttonId/$viewId');
methodChannel?.setMethodCallHandler((call) async {
if (call.method == 'onPressed') onPressed?.call();
return;
});

If we made _UiKitApplePayButton stateful, we could save the viewId to state and update the function if it changes.

@dickermoshe
Copy link
Contributor Author

PR #236 fixes this.
Please upvote

@JlUgia
Copy link
Member

JlUgia commented Jan 21, 2024

Published under pay_ios:1.0.11 and pay:1.2.0-beta02.

@JlUgia JlUgia closed this as completed Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants