~singpolyma/jmp-pay

02f0825808f085523ae543845bcc5d7d3927d9fc — Stephen Paul Weber 6 months ago 7ee5681 + 6f4cc93
Merge branch 'activate-with-balance'

* activate-with-balance:
  Do not activate directly in cronjob
1 files changed, 12 insertions(+), 40 deletions(-)

M bin/process_pending_btc_transactions
M bin/process_pending_btc_transactions => bin/process_pending_btc_transactions +12 -40
@@ 67,17 67,15 @@ btc_sell_price[:USD] = btc_sell_price[:CAD] * cad_to_usd
class Plan
	def self.for_customer(customer)
		row = DB.exec_params(<<-SQL, [customer.id]).first
			SELECT plan_name FROM customer_plans WHERE customer_id=$1 LIMIT 1
			SELECT customer_plans.plan_name, UPPER(date_range) - LOWER(date_range) < '2 seconds' AS pd
			FROM customer_plans LEFT JOIN plan_log
				ON customer_plans.customer_id = plan_log.customer_id
				AND plan_log.date_range -|- tsrange(expires_at, expires_at, '[]')
			WHERE customer_plans.customer_id=$1 LIMIT 1
		SQL
		from_name(customer, row&.[]("plan_name"))
	end
		return nil unless row

	def self.pending_for_customer(customer)
		from_name(
			customer,
			REDIS.get("pending_plan_for-#{customer.id}"),
			klass: Pending
		)
		from_name(customer, row["plan_name"], klass: row["pd"] ? Pending : Plan)
	end

	def self.from_name(customer, plan_name, klass: Plan)


@@ 117,17 115,7 @@ class Plan
		BigDecimal(@plan[:monthly_price].to_i) * 0.0001
	end

	def insert(start:, expire:)
		params = [@customer.id, name, start, expire]
		DB.exec_params(<<-SQL, params)
			INSERT INTO plan_log
				(customer_id, plan_name, date_range)
			VALUES
				($1, $2, tsrange($3, $4))
		SQL
	end

	def activate_any_pending_plan!; end
	def notify_any_pending_plan!; end

	class Pending < Plan
		def initialize(customer, plan)


@@ 140,7 128,7 @@ class Plan
			[camnt, price].max
		end

		def activate_any_pending_plan!
		def notify_any_pending_plan!
			if @customer.balance < activation_amount
				@customer.notify(
					"Your account could not be activated because your " \


@@ 149,24 137,12 @@ class Plan
					"Please buy more credit to have your account activated."
				)
			else
				charge_insert_notify
				notify_approved
			end
		end

	protected

		def charge_insert_notify
			return unless @customer.add_transaction(
				"activate_#{@customer.id}_#{name}_until_#{@go_until}",
				-price,
				"Activate pending plan"
			)

			insert(start: Date.today, expire: @go_until)
			REDIS.del("pending_plan_for-#{@customer.id}")
			notify_approved
		end

		def notify_approved
			@customer.notify(
				"Your JMP account has been approved. To complete " \


@@ 197,11 173,7 @@ class Customer
	end

	def plan
		Plan.for_customer(self) || pending_plan
	end

	def pending_plan
		Plan.pending_for_customer(self)
		Plan.for_customer(self)
	end

	def balance


@@ 258,7 230,7 @@ done = REDIS.hgetall("pending_btc_transactions").map { |(txid, customer_id)|
		if (plan = customer.plan)
			amount = btc * btc_sell_price.fetch(plan.currency).round(4, :floor)
			customer.add_btc_credit(txid, btc, amount)
			customer.plan.activate_any_pending_plan!
			plan.notify_any_pending_plan!
			REDIS.hdel("pending_btc_transactions", txid)
			txid
		else