From eb05e46ade3ad54e74639eb08eca497ae24f203a Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 25 May 2021 13:11:25 -0500 Subject: [PATCH] Default auto-top-up to $15 and allow changing from credit card management screen --- config.ru | 22 +++++++++++++++++++++- views/activate.slim | 15 ++++++--------- views/credit_cards.slim | 16 ++++++++++++++++ 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/config.ru b/config.ru index fbb010d..1c1fcfc 100644 --- a/config.ru +++ b/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 diff --git a/views/activate.slim b/views/activate.slim index 8b74ef5..a4cb358 100644 --- a/views/activate.slim +++ b/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" diff --git a/views/credit_cards.slim b/views/credit_cards.slim index 7769e74..2b26d2a 100644 --- a/views/credit_cards.slim +++ b/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" -- 2.45.2