subscription.dart 3.8 KB

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