UnityJsonAdditions.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #import "UnityJsonAdditions.h"
  2. NSString *const NSUnityPurchasingTransactionDetailErrorDomain = @"NSUPURTransactionDetailErrorDomain";
  3. NSString *const NSUnityPurchasingTransactionErrorDetailErrorDomain = @"NSUPURTransactionErrorDetailErrorDomain";
  4. UPURTransactionError UPURTransactionErrorFromNSString(NSString *error) {
  5. if (error) {
  6. if ([error isEqualToString:@"NotSupported"]) {
  7. return kUPURTransactionErrorNotSupported;
  8. } else if ([error isEqualToString:@"Item_Unavailable"]) {
  9. return kUPURTransactionErrorItemUnavailable;
  10. } else if ([error isEqualToString:@"UserCancelled"]) {
  11. return kUPURTransactionErrorUserCancelled;
  12. } else if ([error isEqualToString:@"NetworkError"]) {
  13. return kUPURTransactionErrorNetworkError;
  14. } else if ([error isEqualToString:@"ServerError"]) {
  15. return kUPURTransactionErrorServerError;
  16. } else if ([error isEqualToString:@"UnknownError"]) {
  17. return kUPURTransactionErrorUnknownError;
  18. } else {
  19. return kUPURTransactionErrorUnknownError;
  20. }
  21. } else {
  22. return kUPURTransactionErrorUnknownError;
  23. }
  24. }
  25. UPURStore UPURStoreFromNSString(NSString *store) {
  26. if (store) {
  27. if ([store isEqualToString:@"GooglePlay"]) {
  28. return kUPURStoreGooglePlay;
  29. } else if ([store isEqualToString:@"AmazonAppStore"]) {
  30. return kUPURStoreAmazonAppStore;
  31. } else if ([store isEqualToString:@"CloudMoolah"]) {
  32. return kUPURStoreCloudMoolah;
  33. } else if ([store isEqualToString:@"SamsungApps"]) {
  34. return kUPURStoreSamsungApps;
  35. } else if ([store isEqualToString:@"XiaomiMiPay"]) {
  36. return kUPURStoreXiaomiMiPay;
  37. } else if ([store isEqualToString:@"MacAppStore"]) {
  38. return kUPURStoreMacAppStore;
  39. } else if ([store isEqualToString:@"AppleAppStore"]) {
  40. return kUPURStoreAppleAppStore;
  41. } else if ([store isEqualToString:@"WinRT"]) {
  42. return kUPURStoreWinRT;
  43. } else if ([store isEqualToString:@"TizenStore"]) {
  44. return kUPURStoreTizenStore;
  45. } else if ([store isEqualToString:@"FacebookStore"]) {
  46. return kUPURStoreFacebookStore;
  47. } else if ([store isEqualToString:@"NotSpecified"]) {
  48. return kUPURStoreNotSpecified;
  49. } else {
  50. return kUPURStoreNotSpecified;
  51. }
  52. } else {
  53. return kUPURStoreNotSpecified;
  54. }
  55. }
  56. @implementation UPURTransactionDetails (UnityJsonAdditions)
  57. // must check error before using object
  58. +(instancetype)buildWithJson:(NSString *)json error:(NSError **)error {
  59. id object = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:error];
  60. if (*error) {
  61. NSLog(@"UPURTransactionDetails Unable to serialize from json: %@", [*error description]);
  62. return nil;
  63. } else if ([object isKindOfClass:[NSDictionary class]]) {
  64. return [UPURTransactionDetails build:^(UPURTransactionDetailsBuilder *builder) {
  65. NSDictionary *dictionary = (NSDictionary *) object;
  66. builder.productId = [dictionary valueForKey:@"productId"];
  67. builder.transactionId = [dictionary valueForKey:@"transactionId"];
  68. builder.receipt = [dictionary valueForKey:@"receipt"];
  69. builder.price = [dictionary valueForKey:@"price"];
  70. builder.currency = [dictionary valueForKey:@"currency"];
  71. id extras = [dictionary valueForKey:@"extras"];
  72. if (![extras isKindOfClass:[NSNull class]]) {
  73. builder.extras = extras;
  74. }
  75. }];
  76. } else {
  77. NSMutableDictionary *info = [NSMutableDictionary dictionary];
  78. [info setValue:@"UPURTransactionDetails Expected json object to be a NSDictionary but it was not" forKey:@"Reason"];
  79. *error = [NSError errorWithDomain:NSUnityPurchasingTransactionDetailErrorDomain code:1 userInfo:info];
  80. return nil;
  81. }
  82. }
  83. @end
  84. @implementation UPURTransactionErrorDetails (UnityJsonAdditions)
  85. // must check error before using object
  86. +(instancetype)buildWithJson:(NSString *)json error:(NSError **)error {
  87. id object = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingAllowFragments error:error];
  88. if (*error) {
  89. NSLog(@"UPURTransactionErrorDetails Unable to serialize from json: %@", [*error description]);
  90. return nil;
  91. } else if ([object isKindOfClass:[NSDictionary class]]) {
  92. return [UPURTransactionErrorDetails build:^(UPURTransactionErrorDetailsBuilder *builder) {
  93. NSDictionary *dictionary = (NSDictionary *) object;
  94. builder.transactionError = UPURTransactionErrorFromNSString([dictionary valueForKey:@"transactionError"]);
  95. builder.exceptionMessage = [dictionary valueForKey:@"exceptionMessage"];
  96. builder.store = UPURStoreFromNSString([dictionary valueForKey:@"store"]);
  97. builder.storeSpecificErrorCode = [dictionary valueForKey:@"storeSpecificErrorCode"];
  98. id extras = [dictionary valueForKey:@"extras"];
  99. if (![extras isKindOfClass:[NSNull class]]) {
  100. builder.extras = extras;
  101. }
  102. }];
  103. } else {
  104. NSMutableDictionary *info = [NSMutableDictionary dictionary];
  105. [info setValue:@"UPURTransactionErrorDetails Expected json object to be a NSDictionary but it was not" forKey:@"Reason"];
  106. *error = [NSError errorWithDomain:NSUnityPurchasingTransactionErrorDetailErrorDomain code:1 userInfo:info];
  107. return nil;
  108. }
  109. }
  110. @end
  111. @implementation UMONCustomEvent (UnityJsonAdditions)
  112. // must check error before using object
  113. +(instancetype)buildWithJson:(NSString *)json error: (NSError **)error {
  114. id object = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:error];
  115. if (*error) {
  116. NSLog(@"UMONCustomEvent Unable to serialize from json: %@", [*error description]);
  117. return nil;
  118. } else if ([object isKindOfClass:[NSDictionary class]]) {
  119. return [UMONCustomEvent build:^(UMONCustomEventBuilder *builder) {
  120. NSDictionary *dictionary = (NSDictionary *) object;
  121. builder.category = [dictionary valueForKey:@"category"];
  122. builder.type = [dictionary valueForKey:@"type"];
  123. builder.userInfo = [dictionary valueForKey:@"userInfo"];
  124. }];
  125. } else {
  126. NSMutableDictionary *info = [NSMutableDictionary dictionary];
  127. [info setValue:@"UMONCustomEvent Expected json object to be a NSDictionary but it was not" forKey:@"Reason"];
  128. *error = [NSError errorWithDomain:NSUnityPurchasingTransactionDetailErrorDomain code:1 userInfo:info];
  129. return nil;
  130. }
  131. }
  132. @end