@@ -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