Shopify / mobile-buy-sdk-ios

Shopify’s Mobile Buy SDK makes it simple to sell physical products inside your mobile app. With a few lines of code, you can connect your app with the Shopify platform and let your users buy your products using Apple Pay or their credit card.
MIT License
452 stars 199 forks source link

Apple Pay Custom Errors Implementation #1128

Open tareksabry1337 opened 3 years ago

tareksabry1337 commented 3 years ago

What this does

Since the SDK supports iOS 12 as a minimum right now, it was time to move away from Apple Pay iOS 10 delegate methods to iOS 11 delegate methods, this allows other developers to validate and provide custom errors through Apple Pay error objects such as

PKPaymentRequest.paymentContactInvalidError
PKPaymentRequest.paymentShippingAddressInvalidError
PKPaymentRequest.paymentBillingAddressInvalidError
PKPaymentRequest.paymentShippingAddressUnserviceableError

Instead of just showing "Shipping address is invalid", developers will be able to show specific error message which ultimately allows for a better customization when presenting Shopify's Apple Pay Modal.

This PR introduces breaking changes to PaySessionDelegate, all delegate methods now include extra error array in provide closures, and the final call which has completeTransaction closure now has associated type on its failure case to include array of errors, in order to display them on the UI.

PaySessionDelegate Changes

func paySession(_ paySession: PaySession, didRequestShippingRatesFor address: PayPostalAddress, checkout: PayCheckout, provide: @escaping (PayCheckout?, [PayShippingRate], [Error]?) -> Void)
func paySession(_ paySession: PaySession, didUpdateShippingAddress address: PayPostalAddress, checkout: PayCheckout, provide: @escaping (PayCheckout?, [Error]?) -> Void)
func paySession(_ paySession: PaySession, didSelectShippingRate shippingRate: PayShippingRate, checkout: PayCheckout, provide: @escaping (PayCheckout?, [Error]?) -> Void)
func paySession(_ paySession: PaySession, didAuthorizePayment authorization: PayAuthorization, checkout: PayCheckout, completeTransaction: @escaping (PaySession.TransactionStatus) -> Void)

Incentive

While implementing Shopify's Apple Pay modal, a requirement was defined that I need to validate all the user inputs and display custom messages on the UI. There was no way except for forking this and rolling my own implementation. I suppose this will be helpful for other developers that would face the same challenge.

Code Design / Convention / Tests

The PR ensures that the new implementation does not break the design of the code, naming conventions and all tests are passing.

xanderbuck commented 3 years ago

@tareksabry1337 was this ever integrated in the SDK, this would be awesome to have!

tareksabry1337 commented 3 years ago

@xanderbuck Unfortunately, the PR is stale, I am just rolling my own version and keeping it up to date with master branch