Commit 7cd787b4 authored by Jose Blaya's avatar Jose Blaya
Browse files

Merge branch '146-flag-to-disable-trials-from-backend' into 'release/2.1.0'

Resolve "Flag to disable trials from backend"

See merge request ios/client-library-apple!203
parents a0713643 a7e8a713
......@@ -15,5 +15,7 @@ public struct AppStoreInformation {
public let isInIntroOfferPeriod: Bool
public let isTrialPeriod: Bool
public let trialsEnabled: Bool
}
......@@ -237,21 +237,32 @@ class PIAWebServices: WebServices, ConfigurationAccess {
}
if let availableJSONProducts = json["available_products"] as? [JSON] {
guard let products = [Product].from(jsonArray: availableJSONProducts) else {
callback?(nil, error)
return
}
let isIntroOffer = json["is_in_intro_offer_period"] as? Bool ?? false
let isTrialPeriod = json["is_trial_period"] as? Bool ?? false
let trialsEnabled = json["trial_enabled"] as? Bool ?? false
let receipt = json["receipt"] as? [String: Any] ?? [:]
let isIntroOffer = receipt["is_in_intro_offer_period"] as? Bool ?? false
let isTrialPeriod = receipt["is_trial_period"] as? Bool ?? false
let info = AppStoreInformation(products: products,
isInIntroOfferPeriod: isIntroOffer,
isTrialPeriod: isTrialPeriod)
isTrialPeriod: isTrialPeriod,
trialsEnabled: trialsEnabled)
//If either of these fields are true for a given subscription, the user is not eligible for an introductory offer on that subscription product or any other products within the same subscription group.
if info.isInIntroOfferPeriod || info.isTrialPeriod {
Client.configuration.eligibleForTrial = false
} else {
Client.configuration.eligibleForTrial = true
}
//Backend can disable the trials and override the previous value
Client.configuration.eligibleForTrial = info.trialsEnabled
callback?(info, nil)
} else {
callback?(nil, error)
......
......@@ -57,5 +57,9 @@ extension Client {
public static func useMockInAppProviderWithoutReceipt() {
store = MockInAppProvider(with: nil)
}
public static func useMockInAppProviderWithReceipt() {
store = MockInAppProvider(with: "abcdefg".data(using: .utf8))
}
#endif
}
......@@ -109,7 +109,17 @@ public class MockAccountProvider: AccountProvider, WebServicesConsumer {
price: "3.99",
legacy: false)],
isInIntroOfferPeriod: false,
isTrialPeriod: false)
isTrialPeriod: false,
trialsEnabled: true)
}
webServices.appstoreInformationEligibleButDisabledFromBackend = {
return AppStoreInformation(products: [Product(identifier: "com.product.monthly",
plan: .monthly,
price: "3.99",
legacy: false)],
isInIntroOfferPeriod: false,
isTrialPeriod: false,
trialsEnabled: false)
}
webServices.appstoreInformationNotEligible = {
return AppStoreInformation(products: [Product(identifier: "com.product.monthly",
......@@ -117,7 +127,8 @@ public class MockAccountProvider: AccountProvider, WebServicesConsumer {
price: "3.99",
legacy: false)],
isInIntroOfferPeriod: true,
isTrialPeriod: false)
isTrialPeriod: false,
trialsEnabled: true)
}
}
......
......@@ -19,6 +19,8 @@ class MockWebServices: WebServices {
var appstoreInformationNotEligible: (() -> AppStoreInformation)?
var appstoreInformationEligibleButDisabledFromBackend: (() -> AppStoreInformation)?
var serversBundle: (() -> ServersBundle)?
func token(credentials: Credentials, _ callback: ((String?, Error?) -> Void)?) {
......@@ -73,8 +75,12 @@ class MockWebServices: WebServices {
func subscriptionInformation(with receipt: Data?, _ callback: LibraryCallback<AppStoreInformation>?) {
let result = { () -> AppStoreInformation? in
if let _ = receipt {
return self.appstoreInformationNotEligible?()
if let receipt = receipt {
if receipt.count == 0 {
return self.appstoreInformationNotEligible?()
} else {
return self.appstoreInformationEligibleButDisabledFromBackend?()
}
} else {
return self.appstoreInformationEligible?()
}
......@@ -85,6 +91,8 @@ class MockWebServices: WebServices {
} else {
Client.configuration.eligibleForTrial = true
}
Client.configuration.eligibleForTrial = result()!.trialsEnabled
callback?(result(), nil)
......
......@@ -95,6 +95,31 @@ class ProductTests: XCTestCase {
}
func testMockTrialsUserEligibleButTrialsDisabledOnBackend() {
Client.useMockInAppProviderWithReceipt()
let expUpdate = expectation(description: "trials_disabled_from_backend")
mock.accountProvider.subscriptionInformation { subscriptionInfo, error in
if let _ = error {
print("error found: \(error!)")
expUpdate.fulfill()
XCTAssert(false)
return
}
guard let _ = subscriptionInfo else {
print("testMockTrials: \(error!)")
expUpdate.fulfill()
XCTAssert(false)
return
}
XCTAssertFalse(Client.configuration.eligibleForTrial)
expUpdate.fulfill()
}
waitForExpectations(timeout: 5.0, handler: nil)
}
private func __testRetrieveSubscriptionPlans(webServices: PIAWebServices) {
let exp = expectation(description: "subscription.plans")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment