Commit 3c67fdf4 by Rune Madsen

Merge pull request #76 from Shopify/runmad.fix-apple-pay-setup-button

Fix Apple Pay "Setup" button logic
parents e477f067 07e160ad
...@@ -76,13 +76,6 @@ ...@@ -76,13 +76,6 @@
@property (nonatomic, assign, readonly) BOOL isLoading; @property (nonatomic, assign, readonly) BOOL isLoading;
/** /**
* If the merchantId is set and the device support Apple Pay but no credit card is present this allows the user to add a payment pass to the Wallet.
* The user is given the option to add a payment pass or continue with web checkout. Default is set to true. The Set Up Apple Pay button will, however
* still only show if [PKAddPaymentPassViewController canAddPaymentPass] returns true, merchantId is set and the app is running iOS 9.0 and above.
*/
@property (nonatomic, assign) BOOL allowApplePaySetup;
/**
* This is a convenience method as an alternative to presentViewController: which will force portrait orientation. This method is only * This is a convenience method as an alternative to presentViewController: which will force portrait orientation. This method is only
* required when presenting from a landscape view controller. * required when presenting from a landscape view controller.
* *
......
...@@ -128,7 +128,7 @@ CGFloat const BUYMaxProductViewHeight = 640.0; ...@@ -128,7 +128,7 @@ CGFloat const BUYMaxProductViewHeight = 640.0;
- (BUYProductView *)productView - (BUYProductView *)productView
{ {
if (_productView == nil && self.product != nil && self.shop != nil) { if (_productView == nil && self.product != nil && self.shop != nil) {
_productView = [[BUYProductView alloc] initWithFrame:CGRectMake(0, 0, self.preferredContentSize.width, self.preferredContentSize.height) product:self.product theme:self.theme shouldShowApplePaySetup:self.allowApplePaySetup]; _productView = [[BUYProductView alloc] initWithFrame:CGRectMake(0, 0, self.preferredContentSize.width, self.preferredContentSize.height) product:self.product theme:self.theme shouldShowApplePaySetup:self.shouldShowApplePaySetup];
_productView.translatesAutoresizingMaskIntoConstraints = NO; _productView.translatesAutoresizingMaskIntoConstraints = NO;
_productView.hidden = YES; _productView.hidden = YES;
[self.view addSubview:_productView]; [self.view addSubview:_productView];
...@@ -149,26 +149,6 @@ CGFloat const BUYMaxProductViewHeight = 640.0; ...@@ -149,26 +149,6 @@ CGFloat const BUYMaxProductViewHeight = 640.0;
return _productView; return _productView;
} }
- (BOOL)canShowApplePaySetup
{
PKPassLibrary *passLibrary = [[PKPassLibrary alloc] init];
if (self.allowApplePaySetup == YES &&
// Check that it's running iOS 9.0 or above
[passLibrary respondsToSelector:@selector(canAddPaymentPassWithPrimaryAccountIdentifier:)] &&
// Check if the device can add a payment pass
[PKPaymentAuthorizationViewController canMakePayments] &&
// Check that Apple Pay is enabled for the merchant
[self.merchantId length]) {
return YES;
} else {
return NO;
}
}
- (BOOL)shouldShowApplePayButton {
return self.isApplePayAvailable ? self.isApplePayAvailable : [self canShowApplePaySetup];
}
- (CGSize)preferredContentSize - (CGSize)preferredContentSize
{ {
return CGSizeMake(MIN(BUYMaxProductViewWidth, self.view.bounds.size.width), return CGSizeMake(MIN(BUYMaxProductViewWidth, self.view.bounds.size.width),
......
...@@ -171,6 +171,36 @@ ...@@ -171,6 +171,36 @@
@property (nonatomic, assign, readonly) BOOL isApplePayAvailable; @property (nonatomic, assign, readonly) BOOL isApplePayAvailable;
/** /**
* If the merchantId is set and the device support Apple Pay but no credit card is present this allows the user to add a payment pass to the Wallet.
* The user is given the option to add a payment pass or continue with web checkout. Default is set to true. The Set Up Apple Pay button will, however
* still only show if [PKAddPaymentPassViewController canAddPaymentPass] returns true, merchantId is set and the app is running iOS 9.0 and above.
*/
@property (nonatomic, assign) BOOL allowApplePaySetup;
/**
* Whether the device is setup to show the Apple Pay setup sheet.
* `allowApplePaySetup` must be set to YES, and the `merchantId` must also be set in addition to the
* device settings for this method to return YES.
*
* @return YES if the Setup Apple Pay button should be shown
*/
- (BOOL)canShowApplePaySetup;
/**
* Returns whether the Apple Pay button should be shown
*
* @return YES if `isApplePayAvailable` or `canShowApplePaySetup` returns YES
*/
- (BOOL)shouldShowApplePayButton;
/**
* Returns whether to show the Apple Pay setup button in place of the Apple Pay buy button
*
* @return YES if `isApplePayAvailable` returns NO and `canShowApplePaySetup` returns YES
*/
- (BOOL)shouldShowApplePaySetup;
/**
* The current checkout object * The current checkout object
*/ */
@property (nonatomic, strong, readonly) BUYCheckout *checkout; @property (nonatomic, strong, readonly) BUYCheckout *checkout;
......
...@@ -102,6 +102,22 @@ NSString * BUYURLKey = @"url"; ...@@ -102,6 +102,22 @@ NSString * BUYURLKey = @"url";
}]; }];
} }
- (BOOL)canShowApplePaySetup
{
PKPassLibrary *passLibrary = [[PKPassLibrary alloc] init];
if (self.allowApplePaySetup == YES &&
// Check that it's running iOS 9.0 or above
[passLibrary respondsToSelector:@selector(canAddPaymentPassWithPrimaryAccountIdentifier:)] &&
// Check if the device can add a payment pass
[PKPaymentAuthorizationViewController canMakePayments] &&
// Check that Apple Pay is enabled for the merchant
[self.merchantId length]) {
return YES;
} else {
return NO;
}
}
- (BOOL)isApplePayAvailable - (BOOL)isApplePayAvailable
{ {
// checks if the client is setup to use Apple Pay // checks if the client is setup to use Apple Pay
...@@ -112,6 +128,15 @@ NSString * BUYURLKey = @"url"; ...@@ -112,6 +128,15 @@ NSString * BUYURLKey = @"url";
[PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:self.supportedNetworks]); [PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:self.supportedNetworks]);
} }
- (BOOL)shouldShowApplePayButton {
return self.isApplePayAvailable || [self canShowApplePaySetup];
}
- (BOOL)shouldShowApplePaySetup
{
return self.isApplePayAvailable == NO && [self canShowApplePaySetup];
}
#pragma mark - Checkout Flow Methods #pragma mark - Checkout Flow Methods
#pragma mark - Step 1 - Creating or updating a Checkout #pragma mark - Step 1 - Creating or updating a Checkout
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment