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

Add payment canceled exception #82

Merged

Conversation

stsc3000
Copy link
Contributor

👋 Hi,

as outlined in #61, it would be nice to have consistent feedback when the user cancels payment authorization by closing the native payment selector.

The proposed changes end up throwing a PlatformException(code: 'paymentCanceled').
The Android implementation is straightforward, as Activity.RESULT_CANCELED makes it obvious when the user canceled.

The iOS implementation is more involved, as it requires a more detailed tracking via delegate methods to make sure that the user canceled voluntarily, i.e. user canceled if:

  • native payment selector was shown (paymentAuthorizationControllerWillAuthorizePayment)
  • user does not try to authorize a payment (didAuthorizePayment)
  • native payment selector is closed (paymentAuthorizationControllerDidFinish)

The implementation also fixes #77.

@google-cla
Copy link

google-cla bot commented Aug 16, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@stsc3000
Copy link
Contributor Author

@googlebot I signed it!

@tomarra
Copy link

tomarra commented Aug 24, 2021

@stsc3000 @JlUgia what other steps need to be followed to get this reviewed and merged? This fix would really help me out with a client project :)

@stsc3000
Copy link
Contributor Author

@tomarra @JlUgia I'm unsure whether there is something left for me to do to proceed on this issue. I'll be afk for about a week, but feel free to proceed in the meantime.

Copy link
Collaborator

@jamesblasco jamesblasco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JlUgia I think this PR is a very useful and demanded feature. Could you take a quick look into it?

@jamesblasco jamesblasco requested a review from JlUgia August 30, 2021 11:48
Co-authored-by: Jaime Blasco <jaimeblasco97@gmail.com>
@stsc3000
Copy link
Contributor Author

stsc3000 commented Sep 7, 2021

@jamesblasco thx! I fixed the error message.

@xmany
Copy link

xmany commented Sep 15, 2021

Huge thanks for the commits! however I just tested the update on Android, it seems it won't throw the exception when payment sheet is dragged down and dismissed, the function did not return either, any idea?

@stsc3000
Copy link
Contributor Author

@xmany Thanks for trying it out!
I just tried to drag down the payment sheet on my device (Pixel 4a) and it appears that this is not an option. The only ways to cancel appear the X button and pressing the back button / gesture. I'm not sure what determines the Google Pay UI and which variants there are. Does it work when cancelling in a different way? Do you mind sharing your device and android version? I'll look into the native code anyway.

@xmany
Copy link

xmany commented Sep 16, 2021

@xmany Thanks for trying it out!
I just tried to drag down the payment sheet on my device (Pixel 4a) and it appears that this is not an option. The only ways to cancel appear the X button and pressing the back button / gesture. I'm not sure what determines the Google Pay UI and which variants there are. Does it work when cancelling in a different way? Do you mind sharing your device and android version? I'll look into the native code anyway.

Thanks! My device is Samsung S20 plus, Android 11

@xmany
Copy link

xmany commented Sep 16, 2021

It might have something to do with debug/release mode (Flutter), on iOS, when I was in debug mode, the fix was working, in release mode it was not.
iPhone 11, iOS 14.2

Haven't test the debug/release difference on Android, will test soon. However the above issue about not working on Android happened in release mode.

@stsc3000
Copy link
Contributor Author

@xmany I think the Android GooglePayHandler handles all possible return cases from the native call (PaymentHandler conforms to AutoResolveHelper results), so I don't know why or how it should handle differently on your device.

Please share the results of your tests, I am not aware of issues with release mode.

@xmany
Copy link

xmany commented Sep 19, 2021

@xmany I think the Android GooglePayHandler handles all possible return cases from the native call (PaymentHandler conforms to AutoResolveHelper results), so I don't know why or how it should handle differently on your device.

Please share the results of your tests, I am not aware of issues with release mode.

You are right, After cleaning up and referencing the repo locally, the commits you had worked correctly, it's my environment a bit messed up,

Sorry

@tomarra
Copy link

tomarra commented Sep 20, 2021

@jamesblasco @JlUgia still looking to get this in as it's blocking the release of a project for me. This has been opened for over a month now. Can this be merged and released?

@JlUgia
Copy link
Member

JlUgia commented Sep 20, 2021

Thank you everyone.
Let's aim to get this merged this week at the latest, and add it to the next minor release.

@@ -33,7 +38,7 @@ typealias PaymentCompletionHandler = (Bool) -> Void
class PaymentHandler: NSObject {

/// Holds the current status of the payment process.
var paymentStatus = PKPaymentAuthorizationStatus.failure
var paymentHandlerStatus: PaymentHandlerStatus = .started
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to use implicit unwrapping (PaymentHandlerStatus!) instead of initializing to an arbitrary value. Now that we are using startPayment to set the status to .started we can treat the declaration as a late initialization instead.

paymentStatus = .success
completion(PKPaymentAuthorizationResult(status: paymentStatus, errors: nil))
paymentHandlerStatus = .authorized

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think removing this line would be useful to conceptually connect calling the completion handler and the status update.

@JlUgia
Copy link
Member

JlUgia commented Sep 24, 2021

Left two comments.
The other changes LGTM

@stsc3000
Copy link
Contributor Author

@JlUgia I updated the PR with your recommended changes (I hope).

@stsc3000 stsc3000 requested a review from JlUgia September 28, 2021 11:06
@@ -194,8 +194,6 @@ extension PaymentHandler: PKPaymentAuthorizationControllerDelegate {
// Return the result back to the channel
self.paymentResult(String(decoding: paymentResultData, as: UTF8.self))

paymentHandlerStatus = .authorized

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually meant to only remove this white line to group lines of code according to their role in the class.
I think that the .authorized state has its place there and helps with clarity.
In other words, I'd suggest we keep lines 197 and 26 as they were prior to this commit.
Apologies for the confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, alright. Will fix ASAP.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JlUgia Thanks for the feedback, I "reverted" the changes.

@JlUgia
Copy link
Member

JlUgia commented Sep 28, 2021

Hi @stsc3000! Thank you for the commit.
I left one comment inline.

@stsc3000 stsc3000 requested a review from JlUgia September 28, 2021 13:11
@JlUgia JlUgia merged commit 2a8f056 into google-pay:main Oct 4, 2021
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

Successfully merging this pull request may close these issues.

iOS PaymentHandler retaining paymentStatus breaks cancelling payment request
5 participants