Skip to content

Commit

Permalink
Add price checks for purchase flow (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
majd authored Mar 22, 2022
1 parent f0c7e02 commit 3040955
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Sources/CLI/Commands/Download.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ extension Download {


private mutating func purchase(app: iTunesResponse.Result, account: Account) async {
guard app.price == 0 else {
logger.log("It is only possible to obtain a license for free apps. Purchase the app manually and run the \"download\" command again.", level: .error)
_exit(1)
}

logger.log("Creating HTTP client...", level: .debug)
let httpClient = HTTPClient(session: URLSession.shared)

Expand All @@ -93,6 +98,8 @@ extension Download {
logger.log("Purchase failed.", level: .error)
case StoreClient.Error.duplicateLicense:
logger.log("A license already exists for this item.", level: .error)
case StoreResponse.Error.priceMismatch:
logger.log("Pirce mismatch. It is only possible to obtain a license for free apps.", level: .error)
case StoreResponse.Error.invalidCountry:
logger.log("The country provided does not match with the account you are using. Supply a valid country using the \"--country\" flag.", level: .error)
case StoreResponse.Error.passwordTokenExpired:
Expand Down
11 changes: 10 additions & 1 deletion Sources/CLI/Commands/Purchase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,18 @@ extension Purchase {

do {
logger.log("Querying the iTunes Store for '\(bundleIdentifier)' in country '\(countryCode)'...", level: .info)
return try await itunesClient.lookup(
let app = try await itunesClient.lookup(
bundleIdentifier: bundleIdentifier,
countryCode: countryCode,
deviceFamily: deviceFamily
)

guard app.price == 0 else {
logger.log("Only free apps are supported.", level: .error)
_exit(1)
}

return app
} catch {
logger.log("\(error)", level: .debug)

Expand Down Expand Up @@ -86,6 +93,8 @@ extension Purchase {
logger.log("Purchase failed.", level: .error)
case StoreClient.Error.duplicateLicense:
logger.log("A license already exists for this item.", level: .error)
case StoreResponse.Error.priceMismatch:
logger.log("Pirce mismatch. It is only possible to obtain a license for free apps.", level: .error)
case StoreResponse.Error.invalidCountry:
logger.log("The country provided does not match with the account you are using. Supply a valid country using the \"--country\" flag.", level: .error)
case StoreResponse.Error.passwordTokenExpired:
Expand Down
1 change: 1 addition & 0 deletions Sources/StoreAPI/Store/StoreResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum StoreResponse {
case lockedAccount = -10001
case invalidCountry = -128
case passwordTokenExpired = 2034
case priceMismatch = 2019
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/StoreAPI/iTunes/iTunesResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extension iTunesResponse {
public let version: String
public let identifier: Int
public let name: String
public let price: Float
}
}

Expand All @@ -34,5 +35,6 @@ extension iTunesResponse.Result: Codable {
case name = "trackName"
case bundleIdentifier = "bundleId"
case version
case price
}
}

0 comments on commit 3040955

Please sign in to comment.