| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #import "BTPaymentMethodNonceParser.h"
- #if __has_include(<Braintree/BraintreeCore.h>)
- #import <Braintree/BTPaymentMethodNonce.h>
- #import <Braintree/BTJSON.h>
- #else
- #import <BraintreeCore/BTPaymentMethodNonce.h>
- #import <BraintreeCore/BTJSON.h>
- #endif
- @interface BTPaymentMethodNonceParser ()
- /// Dictionary of JSON parsing blocks keyed by types as strings. The blocks have the following type:
- ///
- /// `BTPaymentMethodNonce *(^)(NSDictionary *json)`
- @property (nonatomic, strong) NSMutableDictionary *JSONParsingBlocks;
- @end
- @implementation BTPaymentMethodNonceParser
- + (instancetype)sharedParser {
- static BTPaymentMethodNonceParser *sharedParser;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedParser = [[BTPaymentMethodNonceParser alloc] init];
- });
- return sharedParser;
- }
- - (NSMutableDictionary *)JSONParsingBlocks {
- if (!_JSONParsingBlocks) {
- _JSONParsingBlocks = [NSMutableDictionary dictionary];
- }
- return _JSONParsingBlocks;
- }
- - (BOOL)isTypeAvailable:(NSString *)type {
- return self.JSONParsingBlocks[type] != nil;
- }
- - (NSArray *)allTypes {
- return self.JSONParsingBlocks.allKeys;
- }
- - (void)registerType:(NSString *)type withParsingBlock:(BTPaymentMethodNonce *(^)(BTJSON *))jsonParsingBlock {
- if (jsonParsingBlock) {
- self.JSONParsingBlocks[type] = [jsonParsingBlock copy];
- }
- }
- - (BTPaymentMethodNonce *)parseJSON:(BTJSON *)json withParsingBlockForType:(NSString *)type {
- BTPaymentMethodNonce *(^block)(BTJSON *) = self.JSONParsingBlocks[type];
- if (!json) {
- return nil;
- }
- if (block) {
- 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]
- type:@"Unknown"
- isDefault:[json[@"default"] isTrue]];
- }
- @end
|