Skip to content

Commit

Permalink
Merge pull request #550 from mysteriumnetwork/bugfix/payment_balance_…
Browse files Browse the repository at this point in the history
…limit_message

Added payment balance limit message
  • Loading branch information
IrynaTsymbaliukGeniusee authored Mar 29, 2022
2 parents f157793 + 9e492a2 commit e75eed2
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import com.beust.klaxon.Klaxon
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import mysterium.*
import updated.mysterium.vpn.exceptions.ConnectAlreadyExistsException
import updated.mysterium.vpn.exceptions.ConnectInsufficientBalanceException
import updated.mysterium.vpn.exceptions.ConnectInvalidProposalException
import updated.mysterium.vpn.exceptions.ConnectUnknownException
import updated.mysterium.vpn.exceptions.*
import updated.mysterium.vpn.model.connection.Status
import updated.mysterium.vpn.model.connection.StatusResponse
import updated.mysterium.vpn.model.manual.connect.ConnectionState
Expand All @@ -30,6 +27,7 @@ class NodeRepository(var deferredNode: DeferredNode) {

private companion object {
const val TAG = "NodeRepository"
const val MAX_BALANCE_LIMIT = 5
}

// Get available proposals for mobile. Internally on Go side
Expand Down Expand Up @@ -139,9 +137,18 @@ class NodeRepository(var deferredNode: DeferredNode) {

suspend fun createPaymentGatewayOrder(req: CreatePaymentGatewayOrderReq) =
withContext(Dispatchers.IO) {
val order = deferredNode.await().createPaymentGatewayOrder(req)
Log.d(TAG, "createPaymentGatewayOrder response: ${String(order)}")
CardOrder.fromJSON(order.decodeToString()) ?: error("Could not parse JSON: $order")
try {
val order = deferredNode.await().createPaymentGatewayOrder(req)
CardOrder.fromJSON(order.decodeToString()) ?: error("Could not parse JSON: $order")
} catch (e: Exception) {
if (isBalanceLimitExceeded()) {
throw TopupPreconditionFailedException(
e.message ?: "You can only top-up if you have less than 5 MYST in balance"
)
} else {
error(e)
}
}
}

suspend fun listOrders(req: ListOrdersRequest) = withContext(Dispatchers.IO) {
Expand Down Expand Up @@ -279,4 +286,13 @@ class NodeRepository(var deferredNode: DeferredNode) {
}
}

private suspend fun isBalanceLimitExceeded() = withContext(Dispatchers.IO) {
val identityAddress = getIdentity().address
val balanceRequest = GetBalanceRequest().apply {
this.identityAddress = identityAddress
}
val balance = balance(balanceRequest)
balance > MAX_BALANCE_LIMIT
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package updated.mysterium.vpn.exceptions

class TopupPreconditionFailedException(message: String): Exception(message)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import network.mysterium.vpn.R
import network.mysterium.vpn.databinding.ActivityCardSummaryBinding
import network.mysterium.vpn.databinding.PopUpCardPaymentBinding
import org.koin.android.ext.android.inject
import updated.mysterium.vpn.exceptions.TopupPreconditionFailedException
import updated.mysterium.vpn.model.payment.CardOrder
import updated.mysterium.vpn.model.payment.PaymentStatus
import updated.mysterium.vpn.model.pushy.PushyTopic
Expand Down Expand Up @@ -66,6 +67,9 @@ class CardSummaryActivity : BaseActivity() {
binding.confirmButton.setOnClickListener {
launchCardinityPayment()
}
binding.cancelButton.setOnClickListener {
navigateToHome()
}
binding.closeButton.setOnClickListener {
binding.closeButton.visibility = View.GONE
binding.webView.visibility = View.GONE
Expand All @@ -77,29 +81,8 @@ class CardSummaryActivity : BaseActivity() {
binding.paymentProcessingLayout.closeBannerButton.setOnClickListener {
binding.paymentProcessingLayout.root.visibility = View.GONE
}
}

private fun showPaymentPopUp() {
val bindingPopUp = PopUpCardPaymentBinding.inflate(layoutInflater)
val dialog = createPopUp(bindingPopUp.root, false)
bindingPopUp.okayButton.setOnClickListener {
dialog.dismiss()
showPaymentProcessingBanner()
}
dialog.show()
}

private fun showPaymentProcessingBanner() {
binding.paymentProcessingLayout.root.visibility = View.VISIBLE
val animationX =
(binding.titleTextView.x + binding.titleTextView.height + resources.getDimension(R.dimen.margin_padding_size_medium))
ObjectAnimator.ofFloat(
binding.paymentProcessingLayout.root,
"translationY",
animationX
).apply {
duration = 2000
start()
binding.paymentBalanceLimitLayout.closeBannerButton.setOnClickListener {
binding.paymentBalanceLimitLayout.root.visibility = View.GONE
}
}

Expand All @@ -123,7 +106,10 @@ class CardSummaryActivity : BaseActivity() {
paymentHtml = order.pageHtml.htmlSecureData.toString()
}
it.onFailure { error ->
Log.e(TAG, error.localizedMessage ?: error.toString())
Log.e(TAG, error.message ?: error.toString())
if (error is TopupPreconditionFailedException) {
showPaymentBalanceLimitError()
}
}
}
}
Expand All @@ -137,6 +123,8 @@ class CardSummaryActivity : BaseActivity() {
binding.vatTextView.text = getString(
R.string.card_payment_vat_value, taxesPercent
)
binding.confirmContainer.visibility = View.VISIBLE
binding.cancelContainer.visibility = View.INVISIBLE
}

@SuppressLint("SetJavaScriptEnabled")
Expand Down Expand Up @@ -184,6 +172,36 @@ class CardSummaryActivity : BaseActivity() {
}
}

private fun showPaymentBalanceLimitError() {
showBanner(binding.paymentBalanceLimitLayout.root)
binding.confirmContainer.visibility = View.INVISIBLE
binding.cancelContainer.visibility = View.VISIBLE
}

private fun showPaymentPopUp() {
val bindingPopUp = PopUpCardPaymentBinding.inflate(layoutInflater)
val dialog = createPopUp(bindingPopUp.root, false)
bindingPopUp.okayButton.setOnClickListener {
dialog.dismiss()
showBanner(binding.paymentProcessingLayout.root)
}
dialog.show()
}

private fun showBanner(view: View) {
view.visibility = View.VISIBLE
val animationX =
(binding.titleTextView.x + binding.titleTextView.height + resources.getDimension(R.dimen.margin_padding_size_medium))
ObjectAnimator.ofFloat(
view,
"translationY",
animationX
).apply {
duration = 2000
start()
}
}

private fun navigateToHome() {
viewModel.accountFlowShown()
val intent = Intent(this, HomeSelectionActivity::class.java).apply {
Expand Down
27 changes: 27 additions & 0 deletions android/app/src/main/res/drawable/icon_payment_balance_limit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="46dp"
android:height="46dp"
android:viewportWidth="46"
android:viewportHeight="46">
<group>
<clip-path
android:pathData="M2,6L44,6A2,2 0,0 1,46 8L46,38A2,2 0,0 1,44 40L2,40A2,2 0,0 1,0 38L0,8A2,2 0,0 1,2 6z"/>
<path
android:pathData="M2,6L44,6A2,2 0,0 1,46 8L46,38A2,2 0,0 1,44 40L2,40A2,2 0,0 1,0 38L0,8A2,2 0,0 1,2 6z"
android:strokeWidth="8"
android:fillColor="#00000000"
android:strokeColor="@color/payment_balance_limit_icon_color"/>
</group>
<path
android:pathData="M10.2344,29.5386H10.2458"
android:strokeWidth="4"
android:fillColor="#00000000"
android:strokeColor="@color/payment_balance_limit_icon_color"
android:strokeLineCap="round"/>
<path
android:pathData="M2.5352,19.0786H43.5"
android:strokeWidth="4"
android:fillColor="#00000000"
android:strokeColor="@color/payment_balance_limit_icon_color"
android:strokeLineCap="round"/>
</vector>
104 changes: 70 additions & 34 deletions android/app/src/main/res/layout/activity_card_summary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_padding_size_medium_large"
android:layout_marginTop="@dimen/margin_padding_size_large"
android:contentDescription="@string/back"
android:src="@drawable/icon_back"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/titleTextView"
Expand All @@ -57,6 +57,11 @@
layout="@layout/item_payment_processing_banner"
app:layout_constraintBottom_toTopOf="parent" />

<include
android:id="@+id/paymentBalanceLimitLayout"
layout="@layout/item_payment_balance_limit_banner"
app:layout_constraintBottom_toTopOf="parent" />

<com.airbnb.lottie.LottieAnimationView
android:layout_width="wrap_content"
android:layout_height="0dp"
Expand All @@ -77,7 +82,7 @@
android:layout_marginHorizontal="@dimen/margin_padding_size_xlarge"
android:layout_marginBottom="@dimen/margin_padding_size_medium"
android:paddingHorizontal="@dimen/margin_padding_size_biggest"
android:text="@string/card_payment_confirm"
android:text="@string/payment_details"
android:textAlignment="center"
android:textColor="@android:color/white"
app:layout_constraintBottom_toTopOf="@+id/cardView"
Expand Down Expand Up @@ -199,47 +204,78 @@
android:layout_marginHorizontal="@dimen/margin_padding_size_xlarge"
android:layout_marginBottom="@dimen/margin_padding_size_large"
android:background="@drawable/divider_country_group"
app:layout_constraintBottom_toTopOf="@+id/cardinityPaymentDescription" />
app:layout_constraintBottom_toTopOf="@+id/confirmContainer" />

<TextView
android:id="@+id/cardinityPaymentDescription"
style="@style/TextAppearance.Caption"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/margin_padding_size_xlarge"
android:layout_marginBottom="@dimen/margin_padding_size_medium_large"
android:paddingHorizontal="@dimen/margin_padding_size_medium"
android:text="@string/card_payment_cardinity"
android:textAlignment="center"
android:textColor="@color/card_payment_description"
app:layout_constraintBottom_toTopOf="@+id/confirmButton"
app:layout_constraintEnd_toEndOf="@+id/confirmButton"
app:layout_constraintStart_toStartOf="@+id/confirmButton" />

<com.google.android.material.button.MaterialButton
android:id="@+id/confirmButton"
style="@style/Widget.PrimaryButton"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/confirmContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginHorizontal="@dimen/margin_padding_size_xlarge"
android:layout_marginBottom="@dimen/margin_padding_size_biggest"
android:text="@string/card_payment_pay"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
app:layout_constraintStart_toStartOf="parent">

<TextView
android:id="@+id/cardinityPaymentDescription"
style="@style/TextAppearance.Caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/margin_padding_size_xlarge"
android:layout_marginBottom="@dimen/margin_padding_size_medium_large"
android:paddingHorizontal="@dimen/margin_padding_size_medium"
android:text="@string/card_payment_cardinity"
android:textAlignment="center"
android:textColor="@color/card_payment_description"
app:layout_constraintBottom_toTopOf="@+id/confirmButton"
app:layout_constraintEnd_toEndOf="@+id/confirmButton"
app:layout_constraintStart_toStartOf="@+id/confirmButton" />

<com.google.android.material.button.MaterialButton
android:id="@+id/confirmButton"
style="@style/Widget.PrimaryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/margin_padding_size_xlarge"
android:layout_marginBottom="@dimen/margin_padding_size_biggest"
android:text="@string/card_payment_pay"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<ImageView
android:id="@+id/confirmButtonShadow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="4dp"
android:src="@drawable/shadow_primary_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="ContentDescription" />

<ImageView
android:id="@+id/confirmButtonShadow"
</androidx.constraintlayout.widget.ConstraintLayout>

<FrameLayout
android:id="@+id/cancelContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="4dp"
android:src="@drawable/shadow_primary_button"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="ContentDescription" />
app:layout_constraintStart_toStartOf="parent">

<com.google.android.material.button.MaterialButton
android:id="@+id/cancelButton"
style="@style/Widget.NegativeButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:layout_marginHorizontal="@dimen/margin_padding_size_xlarge"
android:layout_marginBottom="@dimen/margin_padding_size_biggest"
android:text="@string/back" />

</FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="parent">

<com.google.android.material.card.MaterialCardView
android:id="@+id/paymentBalanceLimitCard"
style="@style/Widget.PaymentBalanceLimitCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_padding_size_medium"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="@dimen/margin_padding_size_medium"
tools:ignore="UseCompoundDrawables">

<ImageView
android:id="@+id/clockImageView"
android:layout_width="46dp"
android:layout_height="46dp"
android:src="@drawable/icon_payment_balance_limit"
tools:ignore="ContentDescription" />

<TextView
android:id="@+id/paymentProcessingTextView"
style="@style/TextAppearance.BodyMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingStart="@dimen/margin_padding_size_medium_large"
android:text="@string/payment_balance_limit_text"
android:textColor="@android:color/white"
tools:ignore="RtlSymmetry" />

</LinearLayout>

</com.google.android.material.card.MaterialCardView>

<ImageView
android:id="@+id/closeBannerButton"
android:layout_width="18dp"
android:layout_height="18dp"
android:contentDescription="@string/close"
android:src="@drawable/icon_close_payment_banner"
app:layout_constraintBottom_toTopOf="@+id/paymentBalanceLimitCard"
app:layout_constraintEnd_toEndOf="@+id/paymentBalanceLimitCard"
app:layout_constraintStart_toEndOf="@+id/paymentBalanceLimitCard"
app:layout_constraintTop_toTopOf="@+id/paymentBalanceLimitCard" />

</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit e75eed2

Please sign in to comment.