Commit 339c56c8 authored by Jose Blaya's avatar Jose Blaya
Browse files

Check connectivity with the API before start the transaction to buy a new plan

parent 4aecfa61
......@@ -28,6 +28,9 @@
"purchase.login.button" = "Sign in";
"purchase.error.title" = "Purchase";
"purchase.error.validation" = "You must enter an email address.";
"purchase.error.connectivity.title" = "Connection Failure";
"purchase.error.connectivity.description" = "We are unable to reach Private Internet Access. This may due to poor internet or our service is blocked in your country.";
"redeem.title" = "Redeem gift card";
"redeem.subtitle" = "Type in your email address and the %lu digit PIN from your gift card or trial card below.";
......
......@@ -90,6 +90,13 @@ public protocol AccountProvider: class {
*/
func purchase(plan: Plan, _ callback: LibraryCallback<InAppTransaction>?)
/**
Check if the user has access to our servers from the country where is based.
- Parameter callback: Returns a boolean indicating if the operation was successfully or not.
*/
func isAPIEndpointAvailable(_ callback: LibraryCallback<Bool>?)
/**
Restores the purchase history, possibly recovering from corruption.
......
......@@ -351,4 +351,12 @@ class DefaultAccountProvider: AccountProvider, ConfigurationAccess, DatabaseAcce
var webServices: WebServices {
return customWebServices ?? accessedWebServices
}
func isAPIEndpointAvailable(_ callback: LibraryCallback<Bool>?) {
let task = webServices.taskForConnectivityCheck { (_, error) in
callback?(error == nil, error)
}
task.resume()
}
}
......@@ -220,4 +220,9 @@ public class MockAccountProvider: AccountProvider, WebServicesConsumer {
delegate.renew(with: request, callback)
}
#endif
public func isAPIEndpointAvailable(_ callback: LibraryCallback<Bool>?) {
callback?(true, nil)
}
}
......@@ -413,4 +413,16 @@ class EphemeralAccountProvider: AccountProvider, ProvidersAccess, InAppAccess {
func renew(with request: RenewRequest, _ callback: ((UserAccount?, Error?) -> Void)?) {
fatalError("Not implemented")
}
func isAPIEndpointAvailable(_ callback: LibraryCallback<Bool>?) {
guard let webServices = webServices else {
callback?(false, nil)
return
}
let task = webServices.taskForConnectivityCheck { (_, error) in
callback?(error == nil, error)
}
task.resume()
}
}
......@@ -170,6 +170,26 @@ class PurchaseViewController: AutolayoutViewController, WelcomeChild {
return
}
preset?.accountProvider.isAPIEndpointAvailable({ [weak self] (isAvailable, error) in
guard let isAvailable = isAvailable,
isAvailable else {
let alert = Macros.alert(
L10n.Welcome.Purchase.Error.Connectivity.title,
L10n.Welcome.Purchase.Error.Connectivity.description
)
alert.addCancelAction(L10n.Ui.Global.close)
self?.present(alert, animated: true, completion: nil)
return
}
self?.startPurchaseProcessWithEmail(email,
andPlan: plan)
})
}
private func startPurchaseProcessWithEmail(_ email: String,
andPlan plan: PurchasePlan) {
textEmail.text = email
log.debug("Will purchase plan: \(plan.product)")
......@@ -200,6 +220,7 @@ class PurchaseViewController: AutolayoutViewController, WelcomeChild {
self.signupTransaction = transaction
self.perform(segue: StoryboardSegue.Welcome.signupViaPurchaseSegue)
}
}
@IBAction private func logIn(_ sender: Any?) {
......
......@@ -127,6 +127,14 @@ enum L10n {
static let title = L10n.tr("Welcome", "purchase.error.title")
/// You must enter an email address.
static let validation = L10n.tr("Welcome", "purchase.error.validation")
enum Connectivity {
/// Connection Failure
static let title = L10n.tr("Welcome", "purchase.error.connectivity.title")
/// We are unable to reach Private Internet Access. This may due to poor internet or our service is blocked in your country.
static let description = L10n.tr("Welcome", "purchase.error.connectivity.description")
}
}
enum Login {
......
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