subscription.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import 'dart:convert';
  2. const freeProductID = "free";
  3. const stripe = "stripe";
  4. const appStore = "appstore";
  5. const playStore = "playstore";
  6. class Subscription {
  7. final String productID;
  8. final int storage;
  9. final String originalTransactionID;
  10. final String paymentProvider;
  11. final int expiryTime;
  12. final String price;
  13. final String period;
  14. final Attributes? attributes;
  15. Subscription({
  16. required this.productID,
  17. required this.storage,
  18. required this.originalTransactionID,
  19. required this.paymentProvider,
  20. required this.expiryTime,
  21. required this.price,
  22. required this.period,
  23. this.attributes,
  24. });
  25. bool isValid() {
  26. return expiryTime > DateTime.now().microsecondsSinceEpoch;
  27. }
  28. bool isYearlyPlan() {
  29. return 'year' == period;
  30. }
  31. static fromMap(Map<String, dynamic>? map) {
  32. if (map == null) return null;
  33. return Subscription(
  34. productID: map['productID'],
  35. storage: map['storage'],
  36. originalTransactionID: map['originalTransactionID'],
  37. paymentProvider: map['paymentProvider'],
  38. expiryTime: map['expiryTime'],
  39. price: map['price'],
  40. period: map['period'],
  41. attributes: map["attributes"] != null
  42. ? Attributes.fromMap(map["attributes"])
  43. : null,
  44. );
  45. }
  46. Map<String, dynamic> toMap() {
  47. return {
  48. 'productID': productID,
  49. 'storage': storage,
  50. 'originalTransactionID': originalTransactionID,
  51. 'paymentProvider': paymentProvider,
  52. 'expiryTime': expiryTime,
  53. 'price': price,
  54. 'period': period,
  55. 'attributes': attributes?.toMap(),
  56. };
  57. }
  58. String toJson() => json.encode(toMap());
  59. factory Subscription.fromJson(String source) =>
  60. Subscription.fromMap(json.decode(source));
  61. }
  62. class Attributes {
  63. bool? isCancelled;
  64. String? customerID;
  65. Attributes({
  66. this.isCancelled,
  67. this.customerID,
  68. });
  69. Map<String, dynamic> toMap() {
  70. return {
  71. 'isCancelled': isCancelled,
  72. 'customerID': customerID,
  73. };
  74. }
  75. factory Attributes.fromMap(Map<String, dynamic> map) {
  76. return Attributes(
  77. isCancelled: map['isCancelled'],
  78. customerID: map['customerID'],
  79. );
  80. }
  81. String toJson() => json.encode(toMap());
  82. factory Attributes.fromJson(String source) =>
  83. Attributes.fromMap(json.decode(source));
  84. }