|
| 1 | +// |
| 2 | +// BNCCommerceEvent.h |
| 3 | +// BranchSDK-iOS |
| 4 | +// |
| 5 | +// Created by Edward Smith on 12/14/16. |
| 6 | +// Copyright (c) 2016 Branch Metrics. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | + |
| 10 | +#import "BNCCommerceEvent.h" |
| 11 | +#import "BranchConstants.h" |
| 12 | + |
| 13 | + |
| 14 | +@implementation BNCProduct |
| 15 | + |
| 16 | +- (NSMutableDictionary*) dictionary { |
| 17 | + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; |
| 18 | + |
| 19 | + #define assign(x) \ |
| 20 | + do { if (self.x) { dictionary[@#x] = self.x; } } while (0) |
| 21 | + |
| 22 | + assign(sku); |
| 23 | + assign(name); |
| 24 | + assign(price); |
| 25 | + assign(quantity); |
| 26 | + assign(brand); |
| 27 | + assign(category); |
| 28 | + assign(variant); |
| 29 | + |
| 30 | + #undef assign |
| 31 | + |
| 32 | + return dictionary; |
| 33 | +} |
| 34 | + |
| 35 | +@end |
| 36 | + |
| 37 | + |
| 38 | +@implementation BNCCommerceEvent : NSObject |
| 39 | + |
| 40 | +- (NSDictionary*) dictionary { |
| 41 | + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; |
| 42 | + |
| 43 | + #define assign(x) \ |
| 44 | + do { if (self.x) { dictionary[@#x] = self.x; } } while (0) |
| 45 | + |
| 46 | + assign(revenue); |
| 47 | + assign(currency); |
| 48 | + assign(transactionID); |
| 49 | + assign(shipping); |
| 50 | + assign(tax); |
| 51 | + assign(coupon); |
| 52 | + assign(affiliation); |
| 53 | + |
| 54 | + NSMutableArray *products = [NSMutableArray arrayWithCapacity:self.products.count]; |
| 55 | + for (BNCProduct *product in self.products) { |
| 56 | + NSDictionary * d = [product dictionary]; |
| 57 | + if (d) [products addObject:d]; |
| 58 | + } |
| 59 | + dictionary[@"products"] = products; |
| 60 | + |
| 61 | + #undef assign |
| 62 | + |
| 63 | + return dictionary; |
| 64 | +} |
| 65 | + |
| 66 | +@end |
| 67 | + |
| 68 | + |
| 69 | +#pragma mark - BranchCommerceEventRequest |
| 70 | + |
| 71 | + |
| 72 | +@interface BranchCommerceEventRequest () |
| 73 | +@property (strong) NSDictionary *commerceDictionary; |
| 74 | +@property (strong) NSDictionary *metadata; |
| 75 | +@property (copy) void (^completion)(NSDictionary* response, NSError* error); |
| 76 | +@end |
| 77 | + |
| 78 | + |
| 79 | +@implementation BranchCommerceEventRequest |
| 80 | + |
| 81 | +- (instancetype) initWithCommerceEvent:(BNCCommerceEvent*)commerceEvent |
| 82 | + metadata:(NSDictionary*)metadata |
| 83 | + completion:(void (^)(NSDictionary* response, NSError* error))completion { |
| 84 | + self = [super init]; |
| 85 | + if (!self) return self; |
| 86 | + |
| 87 | + if ([commerceEvent.revenue isEqualToNumber:[NSDecimalNumber numberWithDouble:0.0]]) { |
| 88 | + NSLog(@"[Branch] Warning: Sending a commerce event with zero value!!"); |
| 89 | + } |
| 90 | + |
| 91 | + self.commerceDictionary = [commerceEvent dictionary]; |
| 92 | + self.metadata = metadata; |
| 93 | + self.completion = completion; |
| 94 | + return self; |
| 95 | +} |
| 96 | + |
| 97 | +- (void)makeRequest:(BNCServerInterface *)serverInterface |
| 98 | + key:(NSString *)key callback:(BNCServerCallback)callback { |
| 99 | + |
| 100 | + BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper preferenceHelper]; |
| 101 | + |
| 102 | + NSMutableDictionary *params = [NSMutableDictionary dictionary]; |
| 103 | + params[BRANCH_REQUEST_KEY_ACTION] = @"purchase"; |
| 104 | + params[BRANCH_REQUEST_KEY_DEVICE_FINGERPRINT_ID] = preferenceHelper.deviceFingerprintID; |
| 105 | + params[BRANCH_REQUEST_KEY_BRANCH_IDENTITY] = preferenceHelper.identityID; |
| 106 | + params[BRANCH_REQUEST_KEY_SESSION_ID] = preferenceHelper.sessionID; |
| 107 | + |
| 108 | + if (self.metadata) |
| 109 | + params[@"metadata"] = self.metadata; |
| 110 | + if (self.commerceDictionary) |
| 111 | + params[@"commerce_data"] = self.commerceDictionary; |
| 112 | + |
| 113 | + NSString *URL = [preferenceHelper getAPIURL:BRANCH_REQUEST_ENDPOINT_USER_COMPLETED_ACTION]; |
| 114 | + [serverInterface postRequest:params |
| 115 | + url:URL |
| 116 | + key:key |
| 117 | + callback:callback]; |
| 118 | +} |
| 119 | + |
| 120 | +- (void)processResponse:(BNCServerResponse*)response |
| 121 | + error:(NSError*)error { |
| 122 | + |
| 123 | + NSDictionary *dictionary = |
| 124 | + ([response.data isKindOfClass:[NSDictionary class]]) |
| 125 | + ? (NSDictionary*) response.data |
| 126 | + : nil; |
| 127 | + |
| 128 | + if (self.completion) |
| 129 | + self.completion(dictionary, error); |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +#pragma mark - NSCoding |
| 134 | + |
| 135 | + |
| 136 | +- (instancetype)initWithCoder:(NSCoder *)decoder { |
| 137 | + self = [super initWithCoder:decoder]; |
| 138 | + if (!self) return self; |
| 139 | + |
| 140 | + self.commerceDictionary = [decoder decodeObjectForKey:@"commerceDictionary"]; |
| 141 | + self.metadata = [decoder decodeObjectForKey:@"metaData"]; |
| 142 | + return self; |
| 143 | +} |
| 144 | + |
| 145 | +- (void)encodeWithCoder:(NSCoder *)coder { |
| 146 | + [super encodeWithCoder:coder]; |
| 147 | + [coder encodeObject:self.commerceDictionary forKey:@"commerceDictionary"]; |
| 148 | + [coder encodeObject:self.metadata forKey:@"metadata"]; |
| 149 | +} |
| 150 | + |
| 151 | +@end |
0 commit comments