Merge branch 'auto-top-up'

Stephen Paul Weber created

* auto-top-up:
  Default auto-top-up to $15 and allow changing from credit card management screen

Change summary

config.ru               | 22 +++++++++++++++++++++-
views/activate.slim     | 15 ++++++---------
views/credit_cards.slim | 16 ++++++++++++++++
3 files changed, 43 insertions(+), 10 deletions(-)

Detailed changes

config.ru 🔗

@@ -117,6 +117,10 @@ class CreditCardGateway
 		@gateway.client_token.generate(customer_id: customer_id)
 	end
 
+	def payment_methods?
+		!@gateway.customer.find(customer_id).payment_methods.empty?
+	end
+
 	def default_payment_method=(nonce)
 		@gateway.payment_method.create(
 			customer_id: customer_id,
@@ -211,6 +215,8 @@ class UnknownTransactions
 	end
 end
 
+# This class must contain all of the routes because of how the DSL works
+# rubocop:disable Metrics/ClassLength
 class JmpPay < Roda
 	SENTRY_DSN = ENV["SENTRY_DSN"] && URI(ENV["SENTRY_DSN"])
 	plugin :render, engine: "slim"
@@ -311,18 +317,32 @@ class JmpPay < Roda
 						"credit_cards",
 						locals: {
 							token: gateway.client_token,
-							customer_id: gateway.customer_id
+							customer_id: gateway.customer_id,
+							auto_top_up: REDIS.get(
+								"jmp_customer_auto_top_up_amount-#{gateway.customer_id}"
+							) || (gateway.payment_methods? ? "" : "15")
 						}
 					)
 				end
 
 				r.post do
 					gateway.default_payment_method = request.params["braintree_nonce"]
+					if request.params["auto_top_up_amount"].to_i >= 15
+						REDIS.set(
+							"jmp_customer_auto_top_up_amount-#{gateway.customer_id}",
+							request.params["auto_top_up_amount"].to_i
+						)
+					elsif request.params["auto_top_up_amount"].to_i == 0
+						REDIS.del(
+							"jmp_customer_auto_top_up_amount-#{gateway.customer_id}"
+						)
+					end
 					"OK"
 				end
 			end
 		end
 	end
 end
+# rubocop:enable Metrics/ClassLength
 
 run JmpPay.freeze.app

views/activate.slim 🔗

@@ -9,16 +9,12 @@ scss:
 		max-width: 40em;
 
 		fieldset {
-			max-width: 20em;
+			max-width: 25em;
 			margin: 2em auto;
 			label {
 				display: block;
 			}
-		}
-
-		details {
-			margin: 2em auto;
-			input { max-width: 3em; }
+			input[type=number] { max-width: 3em; }
 		}
 
 		button {
@@ -61,11 +57,12 @@ form method="post" action=""
 			' $17.95 CAD
 			input type="radio" name="plan_name" value="cad_beta_unlimited-v20210223" required="required"
 
-	details
-		summary Auto top-up when account balance is low?
+	fieldset
+		legend Auto top-up when account balance is low?
 		label
 			| When balance drops below $5, add $
-			input type="number" name="auto_top_up_amount" min="15" value=""
+			input type="number" name="auto_top_up_amount" min="15" value="15"
+			small Leave blank for no auto top-up.
 
 	input type="hidden" name="customer_id" value=customer_id
 	input type="hidden" name="braintree_nonce"

views/credit_cards.slim 🔗

@@ -4,6 +4,15 @@ scss:
 		max-width: 40em;
 		text-align: center;
 
+		fieldset {
+			max-width: 25em;
+			margin: 2em auto;
+			label {
+				display: block;
+			}
+			input[type=number] { max-width: 3em; }
+		}
+
 		button {
 			display: block;
 			width: 10em;
@@ -16,6 +25,13 @@ form method="post" action=""
 	#braintree
 		| Unfortunately, our credit card processor requires JavaScript.
 
+	fieldset
+		legend Auto top-up when account balance is low?
+		label
+			| When balance drops below $5, add $
+			input type="number" name="auto_top_up_amount" min="15" value=auto_top_up
+			small Leave blank for no auto top-up.
+
 	input type="hidden" name="customer_id" value=customer_id
 	input type="hidden" name="braintree_nonce"