transaction.rb

 1# frozen_string_literal: true
 2
 3require "bigdecimal"
 4
 5class Transaction
 6	def self.sale(customer, amount:, payment_method: nil)
 7		REDIS.get("jmp_pay_decline-#{customer.customer_id}").then do |declines|
 8			raise "too many declines" if declines.to_i >= 2
 9
10			BRAINTREE.transaction.sale(
11				amount: amount,
12				**sale_args_for(customer, payment_method)
13			).then do |response|
14				decline_guard(customer, response)
15				new(response.transaction)
16			end
17		end
18	end
19
20	def self.decline_guard(customer, response)
21		return if response.success?
22
23		REDIS.incr("jmp_pay_decline-#{customer.customer_id}").then do
24			REDIS.expire("jmp_pay_decline-#{customer.customer_id}", 60 * 60 * 24)
25		end
26		raise response.message
27	end
28
29	def self.sale_args_for(customer, payment_method=nil)
30		{
31			merchant_account_id: customer.merchant_account,
32			options: { submit_for_settlement: true }
33		}.merge(
34			if payment_method
35				{ payment_method_token: payment_method.token }
36			else
37				{ customer_id: customer.id }
38			end
39		)
40	end
41
42	attr_reader :amount
43
44	def initialize(braintree_transaction)
45		@customer_id = braintree_transaction.customer_details.id
46		@transaction_id = braintree_transaction.id
47		@created_at = braintree_transaction.created_at
48		@amount = BigDecimal.new(braintree_transaction.amount, 4)
49	end
50
51	def insert
52		EM.promise_fiber do
53			DB.transaction do
54				insert_tx
55				insert_bonus
56			end
57		end
58	end
59
60	def bonus
61		return BigDecimal.new(0) if amount <= 15
62		amount *
63			case amount
64			when (15..29.99)
65				0.01
66			when (30..139.99)
67				0.03
68			else
69				0.05
70			end
71	end
72
73protected
74
75	def insert_tx
76		params = [@customer_id, @transaction_id, @created_at, @amount]
77		DB.exec(<<~SQL, params)
78			INSERT INTO transactions
79				(customer_id, transaction_id, created_at, amount, note)
80			VALUES
81				($1, $2, $3, $4, 'Credit card payment')
82		SQL
83	end
84
85	def insert_bonus
86		return if bonus <= 0
87		params = [@customer_id, "bonus_for_#{@transaction_id}", @created_at, bonus]
88		DB.exec(<<~SQL, params)
89			INSERT INTO transactions
90				(customer_id, transaction_id, created_at, amount, note)
91			VALUES
92				($1, $2, $3, $4, 'Credit card payment bonus')
93		SQL
94	end
95end