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 @@ ...@@ -28,6 +28,9 @@
"purchase.login.button" = "Sign in"; "purchase.login.button" = "Sign in";
"purchase.error.title" = "Purchase"; "purchase.error.title" = "Purchase";
"purchase.error.validation" = "You must enter an email address."; "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.title" = "Redeem gift card";
"redeem.subtitle" = "Type in your email address and the %lu digit PIN from your gift card or trial card below."; "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 { ...@@ -90,6 +90,13 @@ public protocol AccountProvider: class {
*/ */
func purchase(plan: Plan, _ callback: LibraryCallback<InAppTransaction>?) 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. Restores the purchase history, possibly recovering from corruption.
......
...@@ -351,4 +351,12 @@ class DefaultAccountProvider: AccountProvider, ConfigurationAccess, DatabaseAcce ...@@ -351,4 +351,12 @@ class DefaultAccountProvider: AccountProvider, ConfigurationAccess, DatabaseAcce
var webServices: WebServices { var webServices: WebServices {
return customWebServices ?? accessedWebServices 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 { ...@@ -220,4 +220,9 @@ public class MockAccountProvider: AccountProvider, WebServicesConsumer {
delegate.renew(with: request, callback) delegate.renew(with: request, callback)
} }
#endif #endif
public func isAPIEndpointAvailable(_ callback: LibraryCallback<Bool>?) {
callback?(true, nil)
}
} }
...@@ -413,4 +413,16 @@ class EphemeralAccountProvider: AccountProvider, ProvidersAccess, InAppAccess { ...@@ -413,4 +413,16 @@ class EphemeralAccountProvider: AccountProvider, ProvidersAccess, InAppAccess {
func renew(with request: RenewRequest, _ callback: ((UserAccount?, Error?) -> Void)?) { func renew(with request: RenewRequest, _ callback: ((UserAccount?, Error?) -> Void)?) {
fatalError("Not implemented") 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()
}
} }
...@@ -169,19 +169,39 @@ class PurchaseViewController: AutolayoutViewController, WelcomeChild { ...@@ -169,19 +169,39 @@ class PurchaseViewController: AutolayoutViewController, WelcomeChild {
present(alert, animated: true, completion: nil) present(alert, animated: true, completion: nil)
return 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 textEmail.text = email
log.debug("Will purchase plan: \(plan.product)") log.debug("Will purchase plan: \(plan.product)")
disableInteractions(fully: true) disableInteractions(fully: true)
preset?.accountProvider.purchase(plan: plan.plan) { (transaction, error) in preset?.accountProvider.purchase(plan: plan.plan) { (transaction, error) in
self.enableInteractions() self.enableInteractions()
guard let transaction = transaction else { guard let transaction = transaction else {
if let error = error { if let error = error {
log.error("Purchase failed (error: \(error))") log.error("Purchase failed (error: \(error))")
let alert = Macros.alert( let alert = Macros.alert(
L10n.Welcome.Iap.Error.title, L10n.Welcome.Iap.Error.title,
error.localizedDescription error.localizedDescription
...@@ -193,13 +213,14 @@ class PurchaseViewController: AutolayoutViewController, WelcomeChild { ...@@ -193,13 +213,14 @@ class PurchaseViewController: AutolayoutViewController, WelcomeChild {
} }
return return
} }
log.debug("Purchased with transaction: \(transaction)") log.debug("Purchased with transaction: \(transaction)")
self.signupEmail = email self.signupEmail = email
self.signupTransaction = transaction self.signupTransaction = transaction
self.perform(segue: StoryboardSegue.Welcome.signupViaPurchaseSegue) self.perform(segue: StoryboardSegue.Welcome.signupViaPurchaseSegue)
} }
} }
@IBAction private func logIn(_ sender: Any?) { @IBAction private func logIn(_ sender: Any?) {
......
...@@ -127,6 +127,14 @@ enum L10n { ...@@ -127,6 +127,14 @@ enum L10n {
static let title = L10n.tr("Welcome", "purchase.error.title") static let title = L10n.tr("Welcome", "purchase.error.title")
/// You must enter an email address. /// You must enter an email address.
static let validation = L10n.tr("Welcome", "purchase.error.validation") 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 { 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