Skip to content

Commit

Permalink
Update Transaction Attributes (mparticle-integrations#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonStalnaker authored Feb 26, 2020
1 parent ff23f53 commit 6932506
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cartfile.private
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github "erikdoe/ocmock" ~> 3.3.1
64 changes: 63 additions & 1 deletion mParticle-Appboy/MPKitAppboy.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ - (NSString *)stringRepresentation:(id)value {
return stringRepresentation;
}

- (Appboy *)appboyInstance {
return self->appboyInstance;
}

- (void)setAppboyInstance:(Appboy *)instance {
self->appboyInstance = instance;
}

- (NSString *)stripCharacter:(NSString *)character fromString:(NSString *)originalString {
NSRange range = [originalString rangeOfString:character];

Expand Down Expand Up @@ -379,7 +387,7 @@ - (MPKitExecStatus *)routeCommerceEvent:(MPCommerceEvent *)commerceEvent {

if (commerceEvent.action == MPCommerceEventActionPurchase) {
NSMutableDictionary *baseProductAttributes = [[NSMutableDictionary alloc] init];
NSDictionary *transactionAttributes = [commerceEvent.transactionAttributes beautifiedDictionaryRepresentation];
NSDictionary *transactionAttributes = [self simplifiedDictionary:[commerceEvent.transactionAttributes beautifiedDictionaryRepresentation]];

if (transactionAttributes) {
[baseProductAttributes addEntriesFromDictionary:transactionAttributes];
Expand Down Expand Up @@ -760,4 +768,58 @@ - (MPUserIdentity)userIdentityForString:(nullable NSString *)userIdString {
return MPUserIdentityCustomerId;
}

- (NSMutableDictionary *)simplifiedDictionary:(NSDictionary *)originalDictionary {
__block NSMutableDictionary *transformedDictionary = [[NSMutableDictionary alloc] init];

[originalDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
if ([value isKindOfClass:[NSString class]]) {
NSString *stringValue = (NSString *)value;
NSDate *dateValue = [MPDateFormatter dateFromStringRFC3339:stringValue];
if (dateValue) {
transformedDictionary[key] = dateValue;
} else if ([self isInteger:stringValue]) {
transformedDictionary[key] = [NSNumber numberWithInteger:[stringValue integerValue]];
} else if ([self isFloat:stringValue]) {
transformedDictionary[key] = [NSNumber numberWithFloat:[stringValue floatValue]];
}
else {
transformedDictionary[key] = stringValue;
}
} else if ([value isKindOfClass:[NSNumber class]]) {
transformedDictionary[key] = (NSNumber *)value;
} else if ([value isKindOfClass:[NSDate class]]) {
transformedDictionary[key] = (NSDate *)value;
}
}];

return transformedDictionary;
}

- (BOOL) isInteger:(NSString *)string {
NSCharacterSet* nonNumbers = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];

if([string hasPrefix:@"-"]) {
NSString *absoluteString = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSRange r = [absoluteString rangeOfCharacterFromSet: nonNumbers];

return r.location == NSNotFound && absoluteString.length > 0;
} else {
NSRange r = [string rangeOfCharacterFromSet: nonNumbers];

return r.location == NSNotFound && string.length > 0;
}
}

- (BOOL) isFloat:(NSString *)string {
NSArray *numList = [string componentsSeparatedByString:@"."];

if (numList.count == 2) {
if ([self isInteger:numList[0]] && [self isInteger:numList[1]]) {
return true;
}
}

return false;
}

@end
51 changes: 43 additions & 8 deletions mParticle_AppboyTests/mParticle_AppboyTests.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
//
// mParticle_AppboyTests.m
// mParticle_AppboyTests
//
// Created by Brandon Stalnaker on 9/20/18.
// Copyright © 2018 mParticle. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "MPKitAppboy.h"
#import <OCMock/OCMock.h>
#if TARGET_OS_IOS == 1
#import <Appboy_iOS_SDK/Appboy-iOS-SDK-umbrella.h>
#elif TARGET_OS_TV == 1
Expand All @@ -18,6 +11,8 @@ @interface MPKitAppboy ()

@property (nonatomic) MPUserIdentity userIdType;

- (Appboy *)appboyInstance;
- (void)setAppboyInstance:(Appboy *)instance;
- (NSMutableDictionary<NSString *, NSNumber *> *)optionsDictionary;
+ (id<ABKInAppMessageControllerDelegate>)inAppMessageControllerDelegate;

Expand Down Expand Up @@ -318,4 +313,44 @@ - (void)testUserIdMPID {
XCTAssertEqual(appBoy.userIdType, MPUserIdentityOther4);
}

- (void)testlogCommerceEvent {
MPKitAppboy *kit = [[MPKitAppboy alloc] init];

Appboy *testClient = [[Appboy alloc] init];
id mockClient = OCMPartialMock(testClient);
[kit setAppboyInstance:mockClient];

XCTAssertEqualObjects(mockClient, [kit appboyInstance]);

MPProduct *product = [[MPProduct alloc] initWithName:@"product1" sku:@"1131331343" quantity:@1 price:@13];

MPCommerceEvent *event = [[MPCommerceEvent alloc] initWithAction:MPCommerceEventActionPurchase product:product];
MPTransactionAttributes *attributes = [[MPTransactionAttributes alloc] init];
attributes.transactionId = @"foo-transaction-id";
attributes.revenue = @13.00;
attributes.tax = @3;
attributes.shipping = @-3;

event.transactionAttributes = attributes;

[[mockClient expect] logPurchase:@"1131331343"
inCurrency:@"USD"
atPrice:[[NSDecimalNumber alloc] initWithString:@"13"]
withQuantity:[[NSNumber numberWithInteger:1] longLongValue]
andProperties:@{@"Shipping Amount" : @-3,
@"Total Amount" : @13.00,
@"Total Product Amount" : @"13",
@"Tax Amount" : @3,
@"Transaction Id" : @"foo-transaction-id"
}];

MPKitExecStatus *execStatus = [kit logBaseEvent:event];

XCTAssertEqual(execStatus.returnCode, MPKitReturnCodeSuccess);

[mockClient verify];

[mockClient stopMocking];
}

@end

0 comments on commit 6932506

Please sign in to comment.