ADJSubscription.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // ADJSubscription.m
  3. // Adjust
  4. //
  5. // Created by Uglješa Erceg on 16.04.20.
  6. // Copyright © 2020 adjust GmbH. All rights reserved.
  7. //
  8. #import "ADJUtil.h"
  9. #import "ADJSubscription.h"
  10. #import "ADJAdjustFactory.h"
  11. @interface ADJSubscription()
  12. @property (nonatomic, weak) id<ADJLogger> logger;
  13. @property (nonatomic, strong) NSMutableDictionary *mutableCallbackParameters;
  14. @property (nonatomic, strong) NSMutableDictionary *mutablePartnerParameters;
  15. @end
  16. @implementation ADJSubscription
  17. - (nullable id)initWithPrice:(nonnull NSDecimalNumber *)price
  18. currency:(nonnull NSString *)currency
  19. transactionId:(nonnull NSString *)transactionId
  20. andReceipt:(nonnull NSData *)receipt {
  21. self = [super init];
  22. if (self == nil) {
  23. return nil;
  24. }
  25. _price = [price copy];
  26. _currency = [currency copy];
  27. _transactionId = [transactionId copy];
  28. _receipt = [receipt copy];
  29. _billingStore = @"iOS";
  30. _logger = ADJAdjustFactory.logger;
  31. return self;
  32. }
  33. - (void)setTransactionDate:(NSDate *)transactionDate {
  34. @synchronized (self) {
  35. _transactionDate = [transactionDate copy];
  36. }
  37. }
  38. - (void)setSalesRegion:(NSString *)salesRegion {
  39. @synchronized (self) {
  40. _salesRegion = [salesRegion copy];
  41. }
  42. }
  43. - (void)addCallbackParameter:(nonnull NSString *)key
  44. value:(nonnull NSString *)value
  45. {
  46. @synchronized (self) {
  47. NSString *immutableKey = [key copy];
  48. NSString *immutableValue = [value copy];
  49. if (![ADJUtil isValidParameter:immutableKey
  50. attributeType:@"key"
  51. parameterName:@"Callback"]) {
  52. return;
  53. }
  54. if (![ADJUtil isValidParameter:immutableValue
  55. attributeType:@"value"
  56. parameterName:@"Callback"]) {
  57. return;
  58. }
  59. if (self.mutableCallbackParameters == nil) {
  60. self.mutableCallbackParameters = [[NSMutableDictionary alloc] init];
  61. }
  62. if ([self.mutableCallbackParameters objectForKey:immutableKey]) {
  63. [self.logger warn:@"key %@ was overwritten", immutableKey];
  64. }
  65. [self.mutableCallbackParameters setObject:immutableValue forKey:immutableKey];
  66. }
  67. }
  68. - (void)addPartnerParameter:(nonnull NSString *)key
  69. value:(nonnull NSString *)value
  70. {
  71. @synchronized (self) {
  72. NSString *immutableKey = [key copy];
  73. NSString *immutableValue = [value copy];
  74. if (![ADJUtil isValidParameter:immutableKey
  75. attributeType:@"key"
  76. parameterName:@"Partner"]) {
  77. return;
  78. }
  79. if (![ADJUtil isValidParameter:immutableValue
  80. attributeType:@"value"
  81. parameterName:@"Partner"]) {
  82. return;
  83. }
  84. if (self.mutablePartnerParameters == nil) {
  85. self.mutablePartnerParameters = [[NSMutableDictionary alloc] init];
  86. }
  87. if ([self.mutablePartnerParameters objectForKey:immutableKey]) {
  88. [self.logger warn:@"key %@ was overwritten", immutableKey];
  89. }
  90. [self.mutablePartnerParameters setObject:immutableValue forKey:immutableKey];
  91. }
  92. }
  93. - (nonnull NSDictionary *)callbackParameters {
  94. return [self.mutableCallbackParameters copy];
  95. }
  96. - (nonnull NSDictionary *)partnerParameters {
  97. return [self.mutablePartnerParameters copy];
  98. }
  99. - (id)copyWithZone:(NSZone *)zone {
  100. ADJSubscription *copy = [[[self class] allocWithZone:zone] init];
  101. if (copy) {
  102. copy->_price = [self.price copyWithZone:zone];
  103. copy->_currency = [self.currency copyWithZone:zone];
  104. copy->_transactionId = [self.transactionId copyWithZone:zone];
  105. copy->_receipt = [self.receipt copyWithZone:zone];
  106. copy->_billingStore = [self.billingStore copyWithZone:zone];
  107. copy->_transactionDate = [self.transactionDate copyWithZone:zone];
  108. copy->_salesRegion = [self.salesRegion copyWithZone:zone];
  109. copy.mutableCallbackParameters = [self.mutableCallbackParameters copyWithZone:zone];
  110. copy.mutablePartnerParameters = [self.mutablePartnerParameters copyWithZone:zone];
  111. }
  112. return copy;
  113. }
  114. @end