Skip to content

Commit

Permalink
Add Braintree/Apple-Pay subspec for including Apple Pay
Browse files Browse the repository at this point in the history
  • Loading branch information
burnto committed Feb 2, 2015
1 parent 8d8a9f4 commit e142286
Show file tree
Hide file tree
Showing 36 changed files with 758 additions and 9 deletions.
6 changes: 5 additions & 1 deletion Braintree.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Pod::Spec.new do |s|

s.default_subspecs = %w[Drop-In API PayPal Venmo UI Payments]

s.subspec "Apple-Pay" do |s|
s.xcconfig = { "GCC_PREPROCESSOR_DEFINITIONS" => "BT_ENABLE_APPLE_PAY=1" }
end

s.subspec "Drop-In" do |s|
s.source_files = "Braintree/Drop-In/**/*.{h,m}"
s.dependency "Braintree/API"
Expand All @@ -48,7 +52,7 @@ Pod::Spec.new do |s|
s.public_header_files = "Braintree/PayPal/@Public/**/*.h", "Braintree/PayPal/mSDK/CardIO*.h"
s.frameworks = "AVFoundation", "CoreLocation", "CoreMedia", "AudioToolbox", "MessageUI", "SystemConfiguration", "MobileCoreServices"
s.vendored_library = "Braintree/PayPal/mSDK/libPayPalMobile.a"
s.xcconfig = { "GCC_TREAT_WARNINGS_AS_ERRORS" => "YES", "OTHER_LDFLAGS" => "-ObjC -lc++" }
s.xcconfig = { "OTHER_LDFLAGS" => "-ObjC -lc++" }
s.dependency "Braintree/API"
s.dependency "Braintree/UI"
end
Expand Down
452 changes: 452 additions & 0 deletions Braintree.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Braintree/API/@Public/BTApplePayPaymentMethod.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if BT_ENABLE_APPLE_PAY
@import PassKit;

#import "BTPaymentMethod.h"
Expand All @@ -10,3 +11,4 @@
@property (nonatomic, strong, readonly) PKShippingMethod *shippingMethod;

@end
#endif
6 changes: 4 additions & 2 deletions Braintree/API/@Public/BTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ typedef void (^BTClientPaymentMethodSuccessBlock)(BTPaymentMethod *paymentMethod
/// Block type that takes a `BTCardPaymentMethod`
typedef void (^BTClientCardSuccessBlock)(BTCardPaymentMethod *card);

#if BT_ENABLE_APPLE_PAY
/// Success Block type for the Save Apple Pay call
typedef void (^BTClientApplePaySuccessBlock)(BTApplePayPaymentMethod *applePayPaymentMethod);
#endif

/// Success Block type for the Save Paypal call
typedef void (^BTClientPaypalSuccessBlock)(BTPayPalPaymentMethod *paypalPaymentMethod);
Expand Down Expand Up @@ -79,8 +81,7 @@ typedef void (^BTClientFailureBlock)(NSError *error);
success:(BTClientCardSuccessBlock)successBlock
failure:(BTClientFailureBlock)failureBlock;



#if BT_ENABLE_APPLE_PAY
/// Save a payment method created via Apple Pay
///
/// @param applePayRequest A BTClientApplePayRequest
Expand All @@ -89,6 +90,7 @@ typedef void (^BTClientFailureBlock)(NSError *error);
- (void)saveApplePayPayment:(PKPayment *)payment
success:(BTClientApplePaySuccessBlock)successBlock
failure:(BTClientFailureBlock)failureBlock;
#endif

/// Save a paypal payment method to Braintree
///
Expand Down
4 changes: 4 additions & 0 deletions Braintree/API/Client/API/BTClientApplePayConfigurationAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ + (Class)resourceModelClass {
}

+ (NSDictionary *)APIFormat {
#if BT_ENABLE_APPLE_PAY
if (![PKPayment class]) {
return @{};
}
Expand All @@ -29,6 +30,9 @@ + (NSDictionary *)APIFormat {
@"mastercard": PKPaymentNetworkMasterCard,
}),
};
#else
return @{};
#endif
}

@end
2 changes: 2 additions & 0 deletions Braintree/API/Client/BTClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ - (void)saveCardWithNumber:(NSString *)creditCardNumber
failure:failureBlock];
}

#if BT_ENABLE_APPLE_PAY
- (void)saveApplePayPayment:(PKPayment *)payment
success:(BTClientApplePaySuccessBlock)successBlock
failure:(BTClientFailureBlock)failureBlock {
Expand Down Expand Up @@ -319,6 +320,7 @@ - (void)saveApplePayPayment:(PKPayment *)payment
}
}];
}
#endif

- (void)savePaypalPaymentMethodWithAuthCode:(NSString*)authCode
applicationCorrelationID:(NSString *)correlationId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ typedef NS_ENUM(NSUInteger, BTClientApplePayStatus) {
@property (nonatomic, copy) NSString *merchantIdentifier;
@property (nonatomic, copy) NSArray *supportedNetworks;

#if BT_ENABLE_APPLE_PAY
- (PKPaymentRequest *)paymentRequest;
#endif

@end
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ - (instancetype)init {
return self;
}

#if BT_ENABLE_APPLE_PAY
- (PKPaymentRequest *)paymentRequest {
PKPaymentRequest *paymentRequest = [[PKPaymentRequest alloc] init];
paymentRequest.merchantCapabilities = PKMerchantCapability3DS;
Expand All @@ -20,5 +21,6 @@ - (PKPaymentRequest *)paymentRequest {
paymentRequest.supportedNetworks = self.supportedNetworks;
return paymentRequest;
}
#endif

@end
2 changes: 2 additions & 0 deletions Braintree/API/Models/BTApplePayPaymentMethod.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if BT_ENABLE_APPLE_PAY
#import "BTApplePayPaymentMethod_Internal.h"
#import "BTMutableApplePayPaymentMethod.h"

Expand Down Expand Up @@ -49,3 +50,4 @@ - (void)dealloc {
}

@end
#endif
2 changes: 2 additions & 0 deletions Braintree/API/Models/BTApplePayPaymentMethod_Internal.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if BT_ENABLE_APPLE_PAY
#import "BTApplePayPaymentMethod.h"

@interface BTApplePayPaymentMethod ()
Expand All @@ -8,3 +9,4 @@
@property (nonatomic, strong, readwrite) PKShippingMethod *shippingMethod;

@end
#endif
2 changes: 2 additions & 0 deletions Braintree/API/Models/BTMutableApplePayPaymentMethod.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#if BT_ENABLE_APPLE_PAY
#import "BTApplePayPaymentMethod.h"
#import "BTApplePayPaymentMethod_Internal.h"

@interface BTMutableApplePayPaymentMethod : BTApplePayPaymentMethod

@end
#endif
2 changes: 2 additions & 0 deletions Braintree/API/Models/BTMutableApplePayPaymentMethod.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#if BT_ENABLE_APPLE_PAY
#import "BTMutableApplePayPaymentMethod.h"
#import "BTApplePayPaymentMethod_Internal.h"

@implementation BTMutableApplePayPaymentMethod

@end
#endif
10 changes: 9 additions & 1 deletion Braintree/API/Offline Mode/BTOfflineModeURLProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ - (void)startLoading {
responseData = nil;
}
} else if ([request.HTTPMethod isEqualToString:@"POST"] && [request.URL.path isEqualToString:@"/v1/payment_methods/apple_payment_tokens"]) {

NSDictionary *requestObject = [self queryDictionaryFromRequest:request];
NSDictionary *payment = requestObject[@"applePaymentToken"];
if (payment) {
#if BT_ENABLE_APPLE_PAY
BTMutableApplePayPaymentMethod *apple = [[BTMutableApplePayPaymentMethod alloc] init];
[[[self class] backend] addPaymentMethod:apple];

Expand All @@ -110,13 +110,21 @@ - (void)startLoading {
NSAssert(error == nil, @"Error writing offline mode JSON response: %@", error);
data;
});
#else
response = [[NSHTTPURLResponse alloc] initWithURL:self.request.URL
statusCode:501
HTTPVersion:BTOfflineModeHTTPVersionString
headerFields:@{}];
responseData = nil;
#endif
} else {
response = [[NSHTTPURLResponse alloc] initWithURL:self.request.URL
statusCode:501
HTTPVersion:BTOfflineModeHTTPVersionString
headerFields:@{}];
responseData = nil;
}

} else if ([request.HTTPMethod isEqualToString:@"POST"] && [request.URL.path isEqualToString:@"/v1/payment_methods/paypal_accounts"]) {
BTMutablePayPalPaymentMethod *payPalPaymentMethod = [BTMutablePayPalPaymentMethod new];
payPalPaymentMethod.email = @"[email protected]";
Expand Down
2 changes: 2 additions & 0 deletions Braintree/Braintree.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
- (void)tokenizeCard:(BTClientCardTokenizationRequest *)cardDetails
completion:(void (^)(NSString *nonce, NSError *error))completionBlock;

#if BT_ENABLE_APPLE_PAY
/// Creates and returns a payment method nonce for the given Apple Pay payment details
///
/// @note You should use this method if you have implemented Apple Pay directly with PassKit (PKPaymentRequest,
Expand All @@ -106,6 +107,7 @@
/// @param completionBlock Completion block that is called exactly once asynchronously, providing either a nonce upon success or an error upon failure.
- (void)tokenizeApplePayPayment:(PKPayment *)applePayPayment
completion:(void (^)(NSString *nonce, NSError *error))completionBlock;
#endif

/// Initializes a provider that can initiate various payment method creation flows.
///
Expand Down
2 changes: 2 additions & 0 deletions Braintree/Braintree.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ - (void)tokenizeCardWithNumber:(NSString *)cardNumber
completion:completionBlock];
}

#if BT_ENABLE_APPLE_PAY
- (void)tokenizeApplePayPayment:(PKPayment *)payment
completion:(void (^)(NSString *, NSError *))completionBlock {
[self.client postAnalyticsEvent:@"custom.ios.tokenize.apple-pay"];
Expand All @@ -123,6 +124,7 @@ - (void)tokenizeApplePayPayment:(PKPayment *)payment
}
}];
}
#endif

- (BTPaymentProvider *)paymentProviderWithDelegate:(id<BTPaymentMethodCreationDelegate>)delegate {
BTPaymentProvider *paymentProvider = [[BTPaymentProvider alloc] initWithClient:self.client];
Expand Down
4 changes: 4 additions & 0 deletions Braintree/Payments/@Public/BTPaymentProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ typedef NS_OPTIONS(NSInteger, BTPaymentMethodCreationOptions) {
- (BOOL)canCreatePaymentMethodWithProviderType:(BTPaymentProviderType)type;


#if BT_ENABLE_APPLE_PAY

#pragma mark Payment Request Details

/// An array of PKPaymentSummaryItems
Expand Down Expand Up @@ -132,4 +134,6 @@ typedef NS_OPTIONS(NSInteger, BTPaymentMethodCreationOptions) {
/// Currently only affects Apple Pay payments.
@property (nonatomic, copy) NSArray *supportedNetworks;

#endif

@end
2 changes: 2 additions & 0 deletions Braintree/Payments/BTPaymentApplePayProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
/// @note You must set this delegate before calling `authorizeApplePay`.
@property (nonatomic, weak) id<BTPaymentMethodCreationDelegate> delegate;

#if BT_ENABLE_APPLE_PAY
@property (nonatomic, strong) NSArray *paymentSummaryItems;
@property (nonatomic, assign) PKAddressField requiredBillingAddressFields;
@property (nonatomic, assign) ABRecordRef billingAddress;
@property (nonatomic, assign) PKAddressField requiredShippingAddressFields;
@property (nonatomic, assign) ABRecordRef shippingAddress;
@property (nonatomic, strong) NSArray *shippingMethods;
@property (nonatomic, strong) NSArray *supportedNetworks;
#endif

/// Checks whether Apple Pay is possible, considering the current environment, simulator status,
/// device, OS, etc.
Expand Down
33 changes: 33 additions & 0 deletions Braintree/Payments/BTPaymentApplePayProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
#import "BTPaymentProviderErrors.h"
#import "BTLogger_Internal.h"

#if BT_ENABLE_APPLE_PAY
@interface BTPaymentApplePayProvider () <BTMockApplePayPaymentAuthorizationViewControllerDelegate, PKPaymentAuthorizationViewControllerDelegate>
#else
@interface BTPaymentApplePayProvider ()
#endif

@property (nonatomic, strong) BTClient *client;
@property (nonatomic, strong) NSError *applePayError;
@property (nonatomic, strong) BTPaymentMethod *applePayPaymentMethod;
Expand All @@ -32,6 +37,7 @@ - (BOOL)canAuthorizeApplePayPayment {
}

- (BOOL)canAuthorizeApplePayPaymentWithoutAnalytics {
#if BT_ENABLE_APPLE_PAY
if (![PKPayment class]) {
return NO;
}
Expand All @@ -45,9 +51,13 @@ - (BOOL)canAuthorizeApplePayPaymentWithoutAnalytics {
}

return YES;
#else
return NO;
#endif
}

- (void)authorizeApplePay {
#if BT_ENABLE_APPLE_PAY
if (![PKPayment class]) {
NSError *error = [NSError errorWithDomain:BTPaymentProviderErrorDomain
code:BTPaymentProviderErrorOptionNotSupported
Expand Down Expand Up @@ -122,8 +132,20 @@ - (void)authorizeApplePay {
}

[self informDelegateRequestsPresentationOfViewController:paymentAuthorizationViewController];
#else
NSError *error = [NSError errorWithDomain:BTPaymentProviderErrorDomain
code:BTPaymentProviderErrorInitialization
userInfo:@{ NSLocalizedDescriptionKey: @"Apple Pay is not enabled in this build. Please use the Braintree/Enable-Apple-Pay CocoaPod subspec and set the BT_ENABLE_APPLE_PAY preprocessor macro." }];
[self informDelegateDidFailWithError:error];
#if DEBUG
@throw [NSException exceptionWithName:@"Apple Pay not enabled" reason:error.localizedDescription userInfo:nil];
#else
[[BTLogger sharedLogger] error:error.localizedDescription];
#endif
#endif
}

#if BT_ENABLE_APPLE_PAY
- (PKPaymentRequest *)paymentRequest {
if (![PKPaymentRequest class]) {
return nil;
Expand Down Expand Up @@ -178,6 +200,7 @@ - (void)dealloc {
CFRelease(_shippingAddress);
}
}
#endif

#pragma mark Internal Helpers - Exposed for Testing

Expand All @@ -190,6 +213,7 @@ + (BOOL)isSimulator {
}

- (BOOL)paymentAuthorizationViewControllerCanMakePayments {
#if BT_ENABLE_APPLE_PAY
if (![PKPaymentAuthorizationViewController class]) {
return NO;
}
Expand All @@ -198,10 +222,14 @@ - (BOOL)paymentAuthorizationViewControllerCanMakePayments {
} else {
return [PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:self.paymentRequest.supportedNetworks];
}
#else
return NO;
#endif
}

#pragma mark PKPaymentAuthorizationViewController Delegate

#if BT_ENABLE_APPLE_PAY
- (void)paymentAuthorizationViewController:(__unused PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus))completion {
Expand All @@ -212,9 +240,11 @@ - (void)paymentAuthorizationViewController:(__unused PKPaymentAuthorizationViewC
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
[self applePayPaymentAuthorizationViewControllerDidFinish:controller];
}
#endif

#pragma mark MockApplePayPaymentAuthorizationViewController Delegate

#if BT_ENABLE_APPLE_PAY
- (void)mockApplePayPaymentAuthorizationViewController:(__unused BTMockApplePayPaymentAuthorizationViewController *)viewController
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus))completion {
Expand All @@ -225,9 +255,11 @@ - (void)mockApplePayPaymentAuthorizationViewController:(__unused BTMockApplePayP
- (void)mockApplePayPaymentAuthorizationViewControllerDidFinish:(BTMockApplePayPaymentAuthorizationViewController *)viewController {
[self applePayPaymentAuthorizationViewControllerDidFinish:viewController];
}
#endif

#pragma mark Payment Authorization

#if BT_ENABLE_APPLE_PAY
- (void)applePayPaymentAuthorizationViewControllerDidAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion {
[self.client saveApplePayPayment:payment
success:^(BTApplePayPaymentMethod *applePayPaymentMethod) {
Expand All @@ -241,6 +273,7 @@ - (void)applePayPaymentAuthorizationViewControllerDidAuthorizePayment:(PKPayment
completion(PKPaymentAuthorizationStatusFailure);
}];
}
#endif

- (void)applePayPaymentAuthorizationViewControllerDidFinish:(UIViewController *)viewController {
if (self.applePayError) {
Expand Down
4 changes: 4 additions & 0 deletions Braintree/Payments/BTPaymentProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ - (void)paymentMethodCreator:(__unused id)sender didFailWithError:(NSError *)err
[self informDelegateDidFailWithError:error];
}

#if BT_ENABLE_APPLE_PAY

#pragma mark Payment Request Details

- (void)setPaymentSummaryItems:(NSArray *)paymentSummaryItems {
Expand Down Expand Up @@ -349,4 +351,6 @@ - (void)dealloc {
}
}

#endif

@end
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if BT_ENABLE_APPLE_PAY
@import UIKit;

@protocol BTMockApplePayPaymentAuthorizationViewDelegate;
Expand All @@ -14,3 +15,4 @@
- (void)mockApplePayPaymentAuthorizationViewDidCancel:(BTMockApplePayPaymentAuthorizationView *)view;

@end
#endif
Loading

0 comments on commit e142286

Please sign in to comment.