subscription.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // @dart=2.9
  2. import 'dart:convert';
  3. const kFreeProductID = "free";
  4. const kStripe = "stripe";
  5. const kAppStore = "appstore";
  6. const kPlayStore = "playstore";
  7. class Subscription {
  8. final int id;
  9. final String productID;
  10. final int storage;
  11. final String originalTransactionID;
  12. final String paymentProvider;
  13. final int expiryTime;
  14. final String price;
  15. final String period;
  16. final Attributes attributes;
  17. Subscription({
  18. this.id,
  19. this.productID,
  20. this.storage,
  21. this.originalTransactionID,
  22. this.paymentProvider,
  23. this.expiryTime,
  24. this.price,
  25. this.period,
  26. this.attributes,
  27. });
  28. bool isValid() {
  29. return expiryTime > DateTime.now().microsecondsSinceEpoch;
  30. }
  31. bool isYearlyPlan() {
  32. return 'year' == period;
  33. }
  34. Subscription copyWith({
  35. int id,
  36. String productID,
  37. int storage,
  38. String originalTransactionID,
  39. String paymentProvider,
  40. int expiryTime,
  41. String price,
  42. String period,
  43. }) {
  44. return Subscription(
  45. id: id ?? this.id,
  46. productID: productID ?? this.productID,
  47. storage: storage ?? this.storage,
  48. originalTransactionID:
  49. originalTransactionID ?? this.originalTransactionID,
  50. paymentProvider: paymentProvider ?? this.paymentProvider,
  51. expiryTime: expiryTime ?? this.expiryTime,
  52. price: price ?? this.price,
  53. period: period ?? this.period,
  54. );
  55. }
  56. Map<String, dynamic> toMap() {
  57. final map = <String, dynamic>{
  58. 'id': id,
  59. 'productID': productID,
  60. 'storage': storage,
  61. 'originalTransactionID': originalTransactionID,
  62. 'paymentProvider': paymentProvider,
  63. 'expiryTime': expiryTime,
  64. 'price': price,
  65. 'period': period,
  66. 'attributes': attributes?.toJson()
  67. };
  68. return map;
  69. }
  70. factory Subscription.fromMap(Map<String, dynamic> map) {
  71. if (map == null) return null;
  72. return Subscription(
  73. id: map['id'],
  74. productID: map['productID'],
  75. storage: map['storage'],
  76. originalTransactionID: map['originalTransactionID'],
  77. paymentProvider: map['paymentProvider'],
  78. expiryTime: map['expiryTime'],
  79. price: map['price'],
  80. period: map['period'],
  81. attributes: map["attributes"] != null
  82. ? Attributes.fromJson(map["attributes"])
  83. : null,
  84. );
  85. }
  86. String toJson() => json.encode(toMap());
  87. factory Subscription.fromJson(String source) =>
  88. Subscription.fromMap(json.decode(source));
  89. @override
  90. String toString() {
  91. return 'Subscription{id: $id, productID: $productID, storage: $storage, originalTransactionID: $originalTransactionID, paymentProvider: $paymentProvider, expiryTime: $expiryTime, price: $price, period: $period, attributes: $attributes}';
  92. }
  93. @override
  94. bool operator ==(Object o) {
  95. if (identical(this, o)) return true;
  96. return o is Subscription &&
  97. o.id == id &&
  98. o.productID == productID &&
  99. o.storage == storage &&
  100. o.originalTransactionID == originalTransactionID &&
  101. o.paymentProvider == paymentProvider &&
  102. o.expiryTime == expiryTime &&
  103. o.price == price &&
  104. o.period == period;
  105. }
  106. @override
  107. int get hashCode {
  108. return id.hashCode ^
  109. productID.hashCode ^
  110. storage.hashCode ^
  111. originalTransactionID.hashCode ^
  112. paymentProvider.hashCode ^
  113. expiryTime.hashCode ^
  114. price.hashCode ^
  115. period.hashCode;
  116. }
  117. }
  118. class Attributes {
  119. bool isCancelled;
  120. String customerID;
  121. Attributes({
  122. this.isCancelled,
  123. this.customerID,
  124. });
  125. Attributes.fromJson(dynamic json) {
  126. isCancelled = json["isCancelled"];
  127. customerID = json["customerID"];
  128. }
  129. Map<String, dynamic> toJson() {
  130. final map = <String, dynamic>{};
  131. map["isCancelled"] = isCancelled;
  132. map["customerID"] = customerID;
  133. return map;
  134. }
  135. @override
  136. String toString() {
  137. return 'Attributes{isCancelled: $isCancelled, customerID: $customerID}';
  138. }
  139. }