Skip to content

Commit

Permalink
Update BTJSON objectForKeyedSubscript signature
Browse files Browse the repository at this point in the history
Fixes issue #195, which is caused by the Swift compiler doing something
weird with the objectForKeyedSubscript implementation in BTJSON.
  • Loading branch information
dmerino and richardshin committed Dec 9, 2015
1 parent bea8a15 commit c1a15ba
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
BTJSON *authBody = [[BTJSON alloc] initWithValue:[NSJSONSerialization JSONObjectWithData:[jsonAuthResponse dataUsingEncoding:NSUTF8StringEncoding] options:0 error:NULL]];

BTThreeDSecureResponse *authResponse = [[BTThreeDSecureResponse alloc] init];
authResponse.success = authBody[@"success"].isTrue;
authResponse.threeDSecureInfo = authBody[@"threeDSecureInfo"].asDictionary;
authResponse.success = [authBody[@"success"] isTrue];
authResponse.threeDSecureInfo = [authBody[@"threeDSecureInfo"] asDictionary];
authResponse.tokenizedCard = [BTThreeDSecureCardNonce cardNonceWithJSON:authBody[@"paymentMethod"]];
authResponse.errorMessage = authBody[@"error"][@"message"].asString;
authResponse.errorMessage = [authBody[@"error"][@"message"] asString];

[self didCompleteAuthentication:authResponse];

Expand Down
4 changes: 2 additions & 2 deletions Braintree3DSecure/BTThreeDSecureCardNonce.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ + (instancetype)cardNonceWithJSON:(BTJSON *)cardJSON {
}

- (BOOL)liabilityShifted {
return self.threeDSecureJSON[@"liabilityShifted"].isTrue;
return [self.threeDSecureJSON[@"liabilityShifted"] isTrue];
}

- (BOOL)liabilityShiftPossible {
return self.threeDSecureJSON[@"liabilityShiftPossible"].isTrue;
return [self.threeDSecureJSON[@"liabilityShiftPossible"] isTrue];
}

@end
22 changes: 11 additions & 11 deletions Braintree3DSecure/BTThreeDSecureDriver.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ - (void)lookupThreeDSecureForNonce:(NSString *)nonce
NSMutableDictionary *requestParameters = [@{ @"amount": amount } mutableCopy];

if (configuration.json[@"merchantAccountId"]) {
requestParameters[@"merchant_account_id"] = configuration.json[@"merchantAccountId"].asString;
requestParameters[@"merchant_account_id"] = [configuration.json[@"merchantAccountId"] asString];
}
NSString *urlSafeNonce = [nonce stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[self.apiClient POST:[NSString stringWithFormat:@"v1/payment_methods/%@/three_d_secure/lookup", urlSafeNonce]
Expand All @@ -113,14 +113,14 @@ - (void)lookupThreeDSecureForNonce:(NSString *)nonce
NSMutableDictionary *userInfo = [error.userInfo mutableCopy];
BTJSON *errorBody = error.userInfo[BTHTTPJSONResponseBodyKey];

if (errorBody[@"error"][@"message"].isString) {
userInfo[NSLocalizedDescriptionKey] = errorBody[@"error"][@"message"].asString;
if ([errorBody[@"error"][@"message"] isString]) {
userInfo[NSLocalizedDescriptionKey] = [errorBody[@"error"][@"message"] asString];
}
if (errorBody[@"threeDSecureInfo"].isObject) {
userInfo[BTThreeDSecureInfoKey] = errorBody[@"threeDSecureInfo"].asDictionary;
if ([errorBody[@"threeDSecureInfo"] isObject]) {
userInfo[BTThreeDSecureInfoKey] = [errorBody[@"threeDSecureInfo"] asDictionary];
}
if (errorBody[@"error"].isObject) {
userInfo[BTThreeDSecureValidationErrorsKey] = errorBody[@"error"].asDictionary;
if ([errorBody[@"error"] isObject]) {
userInfo[BTThreeDSecureValidationErrorsKey] = [errorBody[@"error"] asDictionary];
}

error = [NSError errorWithDomain:BTThreeDSecureErrorDomain
Expand All @@ -135,10 +135,10 @@ - (void)lookupThreeDSecureForNonce:(NSString *)nonce
BTJSON *lookupJSON = body[@"lookup"];

BTThreeDSecureLookupResult *lookup = [[BTThreeDSecureLookupResult alloc] init];
lookup.acsURL = lookupJSON[@"acsUrl"].asURL;
lookup.PAReq = lookupJSON[@"pareq"].asString;
lookup.MD = lookupJSON[@"md"].asString;
lookup.termURL = lookupJSON[@"termUrl"].asURL;
lookup.acsURL = [lookupJSON[@"acsUrl"] asURL];
lookup.PAReq = [lookupJSON[@"pareq"] asString];
lookup.MD = [lookupJSON[@"md"] asString];
lookup.termURL = [lookupJSON[@"termUrl"] asURL];
lookup.tokenizedCard = [BTThreeDSecureCardNonce cardNonceWithJSON:body[@"paymentMethod"]];

completionBlock(lookup, nil);
Expand Down
12 changes: 6 additions & 6 deletions BraintreeApplePay/BTApplePayClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ @implementation BTApplePayClient
+ (void)load {
if (self == [BTApplePayClient class]) {
[[BTPaymentMethodNonceParser sharedParser] registerType:@"ApplePayCard" withParsingBlock:^BTPaymentMethodNonce * _Nullable(BTJSON * _Nonnull applePayCard) {
NSString *cardType = applePayCard[@"details"][@"cardType"] ? applePayCard[@"details"][@"cardType"].asString : @"ApplePayCard";
return [[BTApplePayCardNonce alloc] initWithNonce:applePayCard[@"nonce"].asString localizedDescription:applePayCard[@"description"].asString type:cardType];
NSString *cardType = applePayCard[@"details"][@"cardType"] ? [applePayCard[@"details"][@"cardType"] asString] : @"ApplePayCard";
return [[BTApplePayCardNonce alloc] initWithNonce:[applePayCard[@"nonce"] asString] localizedDescription:[applePayCard[@"description"] asString] type:cardType];
}];
}
}
Expand Down Expand Up @@ -62,8 +62,8 @@ - (void)tokenizeApplePayPayment:(PKPayment *)payment completion:(void (^)(BTAppl
return;
}

if (!configuration.json[@"applePay"][@"status"].isString ||
[configuration.json[@"applePay"][@"status"].asString isEqualToString:@"off"]) {
if (![configuration.json[@"applePay"][@"status"] isString] ||
[[configuration.json[@"applePay"][@"status"] asString] isEqualToString:@"off"]) {
NSError *error = [NSError errorWithDomain:BTApplePayErrorDomain
code:BTApplePayErrorTypeUnsupported
userInfo:@{ NSLocalizedDescriptionKey: @"Apple Pay is not enabled for this merchant. Please ensure that Apple Pay is enabled in the control panel and then try saving an Apple Pay payment method again." }];
Expand All @@ -90,8 +90,8 @@ - (void)tokenizeApplePayPayment:(PKPayment *)payment completion:(void (^)(BTAppl
}

BTJSON *applePayCard = body[@"applePayCards"][0];
NSString *cardType = applePayCard[@"details"][@"cardType"] ? applePayCard[@"details"][@"cardType"].asString : @"ApplePayCard";
BTApplePayCardNonce *tokenized = [[BTApplePayCardNonce alloc] initWithNonce:applePayCard[@"nonce"].asString localizedDescription:applePayCard[@"description"].asString type:cardType];
NSString *cardType = applePayCard[@"details"][@"cardType"] ? [applePayCard[@"details"][@"cardType"] asString] : @"ApplePayCard";
BTApplePayCardNonce *tokenized = [[BTApplePayCardNonce alloc] initWithNonce:[applePayCard[@"nonce"] asString] localizedDescription:[applePayCard[@"description"] asString] type:cardType];

completionBlock(tokenized, nil);
[self.apiClient sendAnalyticsEvent:@"ios.apple-pay.success"];
Expand Down
2 changes: 1 addition & 1 deletion BraintreeApplePay/BTConfiguration+ApplePay.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ @implementation BTConfiguration (ApplePay)

- (BOOL)isApplePayEnabled {
BTJSON *applePayConfiguration = self.json[@"applePay"];
return applePayConfiguration[@"status"].isString && ![applePayConfiguration[@"status"].asString isEqualToString:@"off"];
return [applePayConfiguration[@"status"] isString] && ![[applePayConfiguration[@"status"] asString] isEqualToString:@"off"];
}

@end
8 changes: 4 additions & 4 deletions BraintreeCard/BTCardNonce.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ + (NSString *)stringFromCardNetwork:(BTCardNetwork)cardNetwork {

+ (instancetype)cardNonceWithJSON:(BTJSON *)cardJSON {
// Normalize the card network string in cardJSON to be lowercase so that our enum mapping is case insensitive
BTJSON *cardType = [[BTJSON alloc] initWithValue:cardJSON[@"details"][@"cardType"].asString.lowercaseString];
return [[[self class] alloc] initWithNonce:cardJSON[@"nonce"].asString
description:cardJSON[@"description"].asString
BTJSON *cardType = [[BTJSON alloc] initWithValue:[cardJSON[@"details"][@"cardType"] asString].lowercaseString];
return [[[self class] alloc] initWithNonce:[cardJSON[@"nonce"] asString]
description:[cardJSON[@"description"] asString]
cardNetwork:[cardType asEnum:@{
@"american express": @(BTCardNetworkAMEX),
@"diners club": @(BTCardNetworkDinersClub),
Expand All @@ -66,7 +66,7 @@ + (instancetype)cardNonceWithJSON:(BTJSON *)cardJSON {
@"uk maestro": @(BTCardNetworkUKMaestro),
@"visa": @(BTCardNetworkVisa),}
orDefault:BTCardNetworkUnknown]
lastTwo:cardJSON[@"details"][@"lastTwo"].asString];
lastTwo:[cardJSON[@"details"][@"lastTwo"] asString]];
}

@end
4 changes: 2 additions & 2 deletions BraintreeCore/BTAPIClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ - (nullable instancetype)initWithAuthorization:(NSString *)authorization {
return nil;
}

NSURL *baseURL = self.clientToken.json[@"clientApiUrl"].asURL;
NSURL *baseURL = [self.clientToken.json[@"clientApiUrl"] asURL];
_http = [[BTHTTP alloc] initWithBaseURL:baseURL authorizationFingerprint:self.clientToken.authorizationFingerprint];

[self sendAnalyticsEvent:@"ios.started.client-token"];
Expand Down Expand Up @@ -210,7 +210,7 @@ - (void)sendAnalyticsEvent:(NSString *)eventKind completion:(void(^)(NSError *er
return;
}

NSURL *analyticsURL = configuration.json[@"analytics"][@"url"].asURL;
NSURL *analyticsURL = [configuration.json[@"analytics"][@"url"] asURL];
if (analyticsURL) {
if (!self.analyticsHttp) {
if (self.clientToken) {
Expand Down
4 changes: 2 additions & 2 deletions BraintreeCore/BTClientToken.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ - (instancetype)initWithClientToken:(NSString *)clientToken error:(NSError * __a
if (self = [super init]) {
// Client token must be decoded first because the other values are retrieved from it
_json = [self decodeClientToken:clientToken error:error];
_authorizationFingerprint = _json[BTClientTokenKeyAuthorizationFingerprint].asString;
_configURL = _json[BTClientTokenKeyConfigURL].asURL;
_authorizationFingerprint = [_json[BTClientTokenKeyAuthorizationFingerprint] asString];
_configURL = [_json[BTClientTokenKeyConfigURL] asURL];
_originalValue = clientToken;

if (![self validateClientToken:error]) {
Expand Down
2 changes: 1 addition & 1 deletion BraintreeCore/BTHTTP.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ - (instancetype)initWithBaseURL:(nonnull NSURL *)URL tokenizationKey:(nonnull NS
}

- (instancetype)initWithClientToken:(BTClientToken *)clientToken {
return [self initWithBaseURL:clientToken.json[@"clientApiUrl"].asURL authorizationFingerprint:clientToken.authorizationFingerprint];
return [self initWithBaseURL:[clientToken.json[@"clientApiUrl"] asURL] authorizationFingerprint:clientToken.authorizationFingerprint];
}

- (instancetype)copyWithZone:(NSZone *)zone {
Expand Down
4 changes: 2 additions & 2 deletions BraintreeCore/BTPaymentMethodNonceParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ - (BTPaymentMethodNonce *)parseJSON:(BTJSON *)json withParsingBlockForType:(NSSt
return block(json);
}
// Unregistered types should fall back to parsing basic nonce and description from JSON
if (!json[@"nonce"].isString) return nil;
return [[BTPaymentMethodNonce alloc] initWithNonce:json[@"nonce"].asString localizedDescription:json[@"description"].asString];
if (![json[@"nonce"] isString]) return nil;
return [[BTPaymentMethodNonce alloc] initWithNonce:[json[@"nonce"] asString] localizedDescription:[json[@"description"] asString]];
}

@end
2 changes: 1 addition & 1 deletion BraintreeCore/Public/BTJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ typedef NS_ENUM(NSInteger, BTJSONErrorCode) {
/// Indexes into the JSON as if the current value is an object
///
/// Notably, this method will always return successfully; however, if the value is not an object, the JSON will wrap an error.
- (BTJSON *)objectForKeyedSubscript:(NSString *)key;
- (id)objectForKeyedSubscript:(NSString *)key;

/// Indexes into the JSON as if the current value is an array
///
Expand Down
4 changes: 2 additions & 2 deletions BraintreePayPal/BTConfiguration+PayPal.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
@implementation BTConfiguration (PayPal)

- (BOOL)isPayPalEnabled {
return self.json[@"paypalEnabled"].isTrue;
return [self.json[@"paypalEnabled"] isTrue];
}

- (BOOL)isBillingAgreementsEnabled {
return self.json[@"paypal"][@"billingAgreementsEnabled"].isTrue;
return [self.json[@"paypal"][@"billingAgreementsEnabled"] isTrue];
}

@end
Loading

0 comments on commit c1a15ba

Please sign in to comment.