diff --git a/config.ru b/config.ru index d1ba6edff32d23f4702139c6ea7723c946052a37..c04f33f6e457aa8da6f08a323836a454223dd471 100644 --- a/config.ru +++ b/config.ru @@ -53,6 +53,13 @@ class Plan BRAINTREE_CONFIG[:merchant_accounts][currency] end + def self.active?(customer_id) + DB.exec_params(<<~SQL, [customer_id]).first&.[]("count").to_i > 0 + SELECT count(1) AS count FROM customer_plans + WHERE customer_id=$1 AND expires_at > NOW() + SQL + end + def activate(customer_id, months) DB.exec_params( "INSERT INTO plan_log VALUES ($1, $2, $3, $4)", @@ -232,15 +239,21 @@ class JmpPay < Roda end r.get do - render.call + if Plan.active?(gateway.customer_id) + r.redirect request.params["return_to"], 303 + else + render.call + end end r.post do - result = gateway.buy_plan( - request.params["plan_name"], - 5, - request.params["braintree_nonce"] - ) + result = DB.transaction do + Plan.active?(gateway.customer_id) || gateway.buy_plan( + request.params["plan_name"], + 5, + request.params["braintree_nonce"] + ) + end if result r.redirect request.params["return_to"], 303 else