/Pods/Stripe/Stripe/STPToken.m

https://bitbucket.org/urjasheth/stripereusable · Objective C · 105 lines · 72 code · 25 blank · 8 comment · 19 complexity · e88a86bf828fe124c5290b37f2bf70ed MD5 · raw file

  1. //
  2. // STPToken.m
  3. // Stripe
  4. //
  5. // Created by Saikat Chakrabarti on 11/5/12.
  6. //
  7. //
  8. #import "STPToken.h"
  9. #import "NSDictionary+Stripe.h"
  10. #import "STPBankAccount.h"
  11. #import "STPCard.h"
  12. @interface STPToken()
  13. @property (nonatomic, nonnull) NSString *tokenId;
  14. @property (nonatomic) BOOL livemode;
  15. @property (nonatomic, nullable) STPCard *card;
  16. @property (nonatomic, nullable) STPBankAccount *bankAccount;
  17. @property (nonatomic, nullable) NSDate *created;
  18. @property (nonatomic, readwrite, nonnull, copy) NSDictionary *allResponseFields;
  19. @end
  20. @implementation STPToken
  21. #pragma mark - Description
  22. - (NSString *)description {
  23. return self.tokenId ?: @"Unknown token";
  24. }
  25. - (NSString *)debugDescription {
  26. NSString *token = self.tokenId ?: @"Unknown token";
  27. NSString *livemode = self.livemode ? @"live mode" : @"test mode";
  28. return [NSString stringWithFormat:@"%@ (%@)", token, livemode];
  29. }
  30. #pragma mark - Equality
  31. - (BOOL)isEqual:(id)object {
  32. return [self isEqualToToken:object];
  33. }
  34. - (NSUInteger)hash {
  35. return [self.tokenId hash];
  36. }
  37. - (BOOL)isEqualToToken:(STPToken *)object {
  38. if (self == object) {
  39. return YES;
  40. }
  41. if (!object || ![object isKindOfClass:self.class]) {
  42. return NO;
  43. }
  44. if ((self.card || object.card) && (![self.card isEqual:object.card])) {
  45. return NO;
  46. }
  47. if ((self.bankAccount || object.bankAccount) && (![self.bankAccount isEqual:object.bankAccount])) {
  48. return NO;
  49. }
  50. return self.livemode == object.livemode && [self.tokenId isEqualToString:object.tokenId] && [self.created isEqualToDate:object.created] &&
  51. [self.card isEqual:object.card] && [self.tokenId isEqualToString:object.tokenId] && [self.created isEqualToDate:object.created];
  52. }
  53. #pragma mark - STPSourceProtocol
  54. - (NSString *)stripeID {
  55. return self.tokenId;
  56. }
  57. #pragma mark - STPAPIResponseDecodable
  58. + (instancetype)decodedObjectFromAPIResponse:(NSDictionary *)response {
  59. NSDictionary *dict = [response stp_dictionaryByRemovingNulls];
  60. if (!dict) {
  61. return nil;
  62. }
  63. // required fields
  64. NSString *stripeId = [dict stp_stringForKey:@"id"];
  65. NSDate *created = [dict stp_dateForKey:@"created"];
  66. if (!stripeId || !created || !dict[@"livemode"]) {
  67. return nil;
  68. }
  69. STPToken *token = [self new];
  70. token.tokenId = stripeId;
  71. token.livemode = [dict stp_boolForKey:@"livemode" or:YES];
  72. token.created = created;
  73. NSDictionary *rawCard = [dict stp_dictionaryForKey:@"card"];
  74. token.card = [STPCard decodedObjectFromAPIResponse:rawCard];
  75. NSDictionary *rawBankAccount = [dict stp_dictionaryForKey:@"bank_account"];
  76. token.bankAccount = [STPBankAccount decodedObjectFromAPIResponse:rawBankAccount];
  77. token.allResponseFields = dict;
  78. return token;
  79. }
  80. @end