~singpolyma/sgx-jmp

ref: 4be555de103e992ee7e03feb48b45c1eca917c45 sgx-jmp/lib/transaction.rb -rw-r--r-- 901 bytes
4be555deStephen Paul Weber Split logic out into testable objects 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true

class Transaction
	def self.sale(merchant_account, payment_method, amount)
		BRAINTREE.transaction.sale(
			amount: amount,
			payment_method_token: payment_method.token,
			merchant_account_id: merchant_account,
			options: { submit_for_settlement: true }
		).then do |response|
			raise response.message unless response.success?
			new(response.transaction)
		end
	end

	attr_reader :amount

	def initialize(braintree_transaction)
		@customer_id = braintree_transaction.customer_details.id
		@transaction_id = braintree_transaction.id
		@created_at = braintree_transaction.created_at
		@amount = braintree_transaction.amount
	end

	def insert
		params = [@customer_id, @transaction_id, @created_at, @amount]
		DB.exec_defer(<<~SQL, params)
			INSERT INTO transactions
				(customer_id, transaction_id, created_at, amount)
			VALUES
				($1, $2, $3, $4)
		SQL
	end
end