This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
VenmoTransaction.m
75 lines (66 loc) · 2.57 KB
/
VenmoTransaction.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#import <Foundation/Foundation.h>
#import "VenmoDefines_Internal.h"
#import "NSDictionary+Venmo.h"
#import "VenmoTransaction.h"
#import "VenmoTransaction_Internal.h"
@implementation VenmoTransaction {
NSNumberFormatter *amountNumberFormatter;
}
@synthesize transactionID;
@synthesize type;
@synthesize fromUserID;
@synthesize toUserID;
@synthesize toUserHandle;
@synthesize amount;
@synthesize note;
@synthesize success;
+ (VenmoTransactionType)typeWithString:(NSString *)string {
return [[string lowercaseString] isEqualToString:@"charge"] ?
VenmoTransactionTypeCharge : VenmoTransactionTypePay;
}
- (NSString *)typeString {
return type == VenmoTransactionTypeCharge ? @"charge" : @"pay";
}
- (NSString *)typeStringPast {
return type == VenmoTransactionTypeCharge ? @"charged" : @"paid";
}
- (NSString *)amountString {
// TODO: Consider making amountNumberFormatter a static variable and adding another static
// variable called transactionCount, incremented in init & decremented in dealloc.
// Then, in dealloc, if transactionCount == 0, set amountNumberFormatter = nil.
if (!amountNumberFormatter) {
amountNumberFormatter = [[NSNumberFormatter alloc] init];
[amountNumberFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[amountNumberFormatter setMinimumFractionDigits:2];
}
return [amountNumberFormatter stringFromNumber:amount];
}
#pragma mark - Internal
// {
// "payments": [
// {
// "payment_id": "1234",
// "verb": "pay",
// "actor_user_id": "1",
// "target_user_id": "2",
// "amount": "1.00",
// "note": "Have a drink on me!",
// "success": 1
// }
// ]
// }
+ (id)transactionWithDictionary:(NSDictionary *)dictionary {
DLog(@"transaction Dictionary: %@", dictionary);
if (!dictionary) return nil;
VenmoTransaction *transaction = [[VenmoTransaction alloc] init];
transaction.transactionID = [dictionary stringForKey:@"payment_id"];
transaction.type = [VenmoTransaction typeWithString:[dictionary objectForKey:@"verb"]];
transaction.fromUserID = [dictionary stringForKey:@"actor_user_id"];
transaction.toUserID = [dictionary stringForKey:@"target_user_id"];
transaction.amount = [NSDecimalNumber decimalNumberWithString:
[dictionary stringForKey:@"amount"]];
transaction.note = [dictionary stringForKey:@"note"];
transaction.success = [dictionary boolForKey:@"success"];
return transaction;
}
@end