-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_money.rb
77 lines (60 loc) · 1.78 KB
/
send_money.rb
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true
require 'cabin'
require 'optparse'
require 'logger'
require 'privatbank'
require 'privatbank/p24'
# Config
MERCHANT_ID = 'XXXXX'
MERCHANT_PASSWORD = 'YYYYYYYYYYYYYYYYYYYYYYYY'
LOG_PATH = __dir__ + '/send_money.log'
# Parse arguments
options = {}
optparser = OptionParser.new do |opts|
opts.on('-p', '--payment_id PAYMENT_ID', String, 'Payment ID') do |value|
options[:payment_id] = value
end
opts.on('-c', '--card CARD', String, 'Card of the receiver') do |value|
options[:card] = value
end
opts.on('-a', '--amount AMOUNT', Float, 'Amount') do |value|
options[:amount] = value
end
opts.on('-d', '--details DETAILS', String, 'Details') do |value|
options[:details] = value
end
end
# Check arguments and raise an error if something is wrong
begin
optparser.parse!
mandatory = %i[payment_id card amount details]
missing = mandatory.select { |param| options[param].nil? }
raise OptionParser::MissingArgument, missing.join(', ') unless missing.empty?
rescue OptionParser::ParseError => e
puts e
puts optparser
exit 1
end
# Create our logger
logger = Cabin::Channel.new
logger.level = :info
logger.subscribe(Logger.new(LOG_PATH, 'daily'))
# Configure the PrivatBank library
Privatbank.configure do |config|
config.merchant_id = MERCHANT_ID
config.merchant_password = MERCHANT_PASSWORD
end
# Add some context
options[:merchant_id] = MERCHANT_ID
options[:merchant_password] = MERCHANT_PASSWORD
# Configure our variables
payment_id = options[:payment_id]
card = options[:card]
amount = options[:amount]
details = options[:details]
# Make the payment
response = Privatbank::P24.send_money_pb(card, payment_id, amount, details)
# Add result to options
options[:api_result] = response
# Log result
logger.info('Payment initialized', options)