Skip to content

Commit

Permalink
Add ownershipType to entitlementInfo (#278)
Browse files Browse the repository at this point in the history
* Add ownershipType to entitlementInfo
* Add tests for ownership type
  • Loading branch information
daentech committed Dec 2, 2021
1 parent 6860cd4 commit 2c85e2b
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 4 deletions.
40 changes: 36 additions & 4 deletions lib/entitlement_info_wrapper.dart
Expand Up @@ -47,6 +47,12 @@ class EntitlementInfo {
/// Check the `isActive` property.
final String? billingIssueDetectedAt;

/// Use this property to determine whether a purchase was made by the current
/// user or shared to them by a family member. This can be useful for
/// onboarding users who have had an entitlement shared with them, but might
/// not be entirely aware of the benefits they now have.
final OwnershipType ownershipType;

/// Construct an EntitlementInfo
EntitlementInfo(
this.identifier,
Expand All @@ -60,7 +66,8 @@ class EntitlementInfo {
this.productIdentifier,
this.isSandbox,
this.unsubscribeDetectedAt,
this.billingIssueDetectedAt);
this.billingIssueDetectedAt,
this.ownershipType);

/// Constructs an EntitlementInfo from a JSON object
factory EntitlementInfo.fromJson(Map<dynamic, dynamic> json) {
Expand Down Expand Up @@ -100,6 +107,17 @@ class EntitlementInfo {
store = Store.unknownStore;
break;
}
late var ownershipType;
switch (json["ownershipType"]) {
case "PURCHASED":
ownershipType = OwnershipType.purchased;
break;
case "FAMILY_SHARED":
ownershipType = OwnershipType.familyShared;
break;
default:
ownershipType = OwnershipType.unknown;
}
return EntitlementInfo(
json["identifier"] as String,
json["isActive"] as bool,
Expand All @@ -112,12 +130,13 @@ class EntitlementInfo {
json["productIdentifier"] as String,
json["isSandbox"] as bool,
json["unsubscribeDetectedAt"] as String?,
json["billingIssueDetectedAt"] as String?);
json["billingIssueDetectedAt"] as String?,
ownershipType);
}

@override
String toString() {
return 'EntitlementInfo{identifier: $identifier, isActive: $isActive, willRenew: $willRenew, periodType: $periodType, latestPurchaseDate: $latestPurchaseDate, originalPurchaseDate: $originalPurchaseDate, expirationDate: $expirationDate, store: $store, productIdentifier: $productIdentifier, isSandbox: $isSandbox, unsubscribeDetectedAt: $unsubscribeDetectedAt, billingIssueDetectedAt: $billingIssueDetectedAt}';
return 'EntitlementInfo{identifier: $identifier, isActive: $isActive, willRenew: $willRenew, periodType: $periodType, latestPurchaseDate: $latestPurchaseDate, originalPurchaseDate: $originalPurchaseDate, expirationDate: $expirationDate, store: $store, productIdentifier: $productIdentifier, isSandbox: $isSandbox, unsubscribeDetectedAt: $unsubscribeDetectedAt, billingIssueDetectedAt: $billingIssueDetectedAt, ownershipType: $ownershipType}';
}

@override
Expand All @@ -136,7 +155,8 @@ class EntitlementInfo {
this.productIdentifier == other.productIdentifier &&
this.isSandbox == other.isSandbox &&
this.unsubscribeDetectedAt == other.unsubscribeDetectedAt &&
this.billingIssueDetectedAt == other.billingIssueDetectedAt);
this.billingIssueDetectedAt == other.billingIssueDetectedAt &&
this.ownershipType == other.ownershipType);
}
}

Expand Down Expand Up @@ -175,3 +195,15 @@ enum Store {
/// For entitlements granted via an unknown store.
unknownStore
}

/// Enum of ownership types
enum OwnershipType {
/// The purchase was made directly by this user.
purchased,

/// The purchase has been shared to this user by a family member.
familyShared,

/// The purchase has no or an unknown ownership type.
unknown
}
64 changes: 64 additions & 0 deletions test/entitlement_info_test.dart
Expand Up @@ -49,4 +49,68 @@ void main() {

expect(entitlementInfo.store, Store.unknownStore);
});

test('unknown ownership type if missing from json', () {
Map<dynamic, dynamic> entitlementInfoJson = {
"identifier": "almost_pro",
"isActive": true,
"willRenew": true,
"latestPurchaseDateMillis": 1.58759855E9,
"latestPurchaseDate": "2020-04-22T23:35:50.000Z",
"originalPurchaseDateMillis": 1.591725245E9,
"originalPurchaseDate": "2020-06-09T17:54:05.000Z",
"expirationDateMillis": null,
"expirationDate": null,
"store": "PLAY_STORE",
"productIdentifier": "consumable",
"isSandbox": true,
"unsubscribeDetectedAt": null,
"unsubscribeDetectedAtMillis": null,
"billingIssueDetectedAt": null,
"billingIssueDetectedAtMillis": null
};
final entitlementInfo = EntitlementInfo.fromJson(entitlementInfoJson);

expect(entitlementInfo.ownershipType, OwnershipType.unknown);
});

test('ownership type parsed from json', () {
Map<dynamic, dynamic> entitlementInfoJson = {
"identifier": "almost_pro",
"isActive": true,
"willRenew": true,
"latestPurchaseDateMillis": 1.58759855E9,
"latestPurchaseDate": "2020-04-22T23:35:50.000Z",
"originalPurchaseDateMillis": 1.591725245E9,
"originalPurchaseDate": "2020-06-09T17:54:05.000Z",
"expirationDateMillis": null,
"expirationDate": null,
"store": "PLAY_STORE",
"productIdentifier": "consumable",
"isSandbox": true,
"unsubscribeDetectedAt": null,
"unsubscribeDetectedAtMillis": null,
"billingIssueDetectedAt": null,
"billingIssueDetectedAtMillis": null
};
entitlementInfoJson["ownershipType"] = "PURCHASED";
var entitlementInfo = EntitlementInfo.fromJson(entitlementInfoJson);

expect(entitlementInfo.ownershipType, OwnershipType.purchased);

entitlementInfoJson["ownershipType"] = "FAMILY_SHARED";
entitlementInfo = EntitlementInfo.fromJson(entitlementInfoJson);

expect(entitlementInfo.ownershipType, OwnershipType.familyShared);

entitlementInfoJson["ownershipType"] = "UNKNOWN";
entitlementInfo = EntitlementInfo.fromJson(entitlementInfoJson);

expect(entitlementInfo.ownershipType, OwnershipType.unknown);

entitlementInfoJson["ownershipType"] = "AN_UNKNOWN_OWNERSHIP_TYPE";
entitlementInfo = EntitlementInfo.fromJson(entitlementInfoJson);

expect(entitlementInfo.ownershipType, OwnershipType.unknown);
});
}

0 comments on commit 2c85e2b

Please sign in to comment.