Skip to content

Commit

Permalink
Non-subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto committed Sep 20, 2023
1 parent 4c20917 commit 9d92e7d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 45 deletions.
41 changes: 1 addition & 40 deletions RevenueCatUI/Data/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,45 +467,6 @@ internal enum TestData {
)
}()

static let customerInfoWithSubscriptions: CustomerInfo = {
return .decode(
"""
{
"schema_version": "4",
"request_date": "2022-03-08T17:42:58Z",
"request_date_ms": 1646761378845,
"subscriber": {
"first_seen": "2022-03-08T17:42:58Z",
"last_seen": "2022-03-08T17:42:58Z",
"management_url": "https://apps.apple.com/account/subscriptions",
"non_subscriptions": {
},
"original_app_user_id": "$RCAnonymousID:5b6fdbac3a0c4f879e43d269ecdf9ba1",
"original_application_version": "1.0",
"original_purchase_date": "2022-04-12T00:03:24Z",
"other_purchases": {
},
"subscriptions": {
"com.revenuecat.product": {
"billing_issues_detected_at": null,
"expires_date": "2062-04-12T00:03:35Z",
"grace_period_expires_date": null,
"is_sandbox": true,
"original_purchase_date": "2022-04-12T00:03:28Z",
"period_type": "intro",
"purchase_date": "2022-04-12T00:03:28Z",
"store": "app_store",
"unsubscribe_detected_at": null
},
},
"entitlements": {
}
}
}
"""
)
}()

static let localization1: PaywallData.LocalizedConfiguration = .init(
title: "Ignite your child's *curiosity*",
subtitle: "Get access to all our educational content trusted by **thousands** of parents.",
Expand Down Expand Up @@ -574,7 +535,7 @@ extension PackageType {

}

private extension CustomerInfo {
extension CustomerInfo {

static func decode(_ json: String) -> Self {
let decoder = JSONDecoder()
Expand Down
12 changes: 10 additions & 2 deletions RevenueCatUI/Purchasing/PurchaseHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ extension PurchaseHandler {
self.restoredCustomerInfo = customerInfo
}

return (customerInfo,
success: !customerInfo.activeSubscriptions.isEmpty)
return (info: customerInfo,
success: customerInfo.hasActiveSubscriptionsOrNonSubscriptions)
}

func trackPaywallImpression(_ eventData: PaywallEvent.Data) {
Expand Down Expand Up @@ -223,3 +223,11 @@ private extension PaywallEvent.Data {
}

}

private extension CustomerInfo {

var hasActiveSubscriptionsOrNonSubscriptions: Bool {
return !self.activeSubscriptions.isEmpty || !self.nonSubscriptions.isEmpty
}

}
96 changes: 93 additions & 3 deletions Tests/RevenueCatUITests/Purchasing/PurchaseHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,104 @@ class PurchaseHandlerTests: TestCase {
expect(handler.actionInProgress) == false
}

func testRestorePurchasesWithNoTransactions() async throws {
let handler: PurchaseHandler = .mock(customerInfo: TestData.customerInfoWithSubscriptions)
func testRestorePurchasesWithActiveSubscriptions() async throws {
let handler: PurchaseHandler = .mock(customerInfo: Self.customerInfoWithSubscriptions)

let result = try await handler.restorePurchases()
expect(result.info) === TestData.customerInfoWithSubscriptions
expect(result.info) === Self.customerInfoWithSubscriptions
expect(result.success) == true
}

func testRestorePurchasesWithNonSubscriptions() async throws {
let handler: PurchaseHandler = .mock(customerInfo: Self.customerInfoWithNonSubscriptions)

let result = try await handler.restorePurchases()
expect(result.info) === Self.customerInfoWithNonSubscriptions
expect(result.success) == true
}

}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
private extension PurchaseHandlerTests {

static let customerInfoWithSubscriptions: CustomerInfo = {
return .decode(
"""
{
"schema_version": "4",
"request_date": "2022-03-08T17:42:58Z",
"request_date_ms": 1646761378845,
"subscriber": {
"first_seen": "2022-03-08T17:42:58Z",
"last_seen": "2022-03-08T17:42:58Z",
"management_url": "https://apps.apple.com/account/subscriptions",
"non_subscriptions": {
},
"original_app_user_id": "$RCAnonymousID:5b6fdbac3a0c4f879e43d269ecdf9ba1",
"original_application_version": "1.0",
"original_purchase_date": "2022-04-12T00:03:24Z",
"other_purchases": {
},
"subscriptions": {
"com.revenuecat.product": {
"billing_issues_detected_at": null,
"expires_date": "2062-04-12T00:03:35Z",
"grace_period_expires_date": null,
"is_sandbox": true,
"original_purchase_date": "2022-04-12T00:03:28Z",
"period_type": "intro",
"purchase_date": "2022-04-12T00:03:28Z",
"store": "app_store",
"unsubscribe_detected_at": null
},
},
"entitlements": {
}
}
}
"""
)
}()

st