Module: Veritrans::CLI
- Extended by:
- CLI
- Included in:
- CLI
- Defined in:
- lib/veritrans/cli.rb
Defined Under Namespace
Classes: AuthenticationError, OrderNotFound
Instance Method Summary
collapse
Instance Method Details
#blue(str) ⇒ Object
156
157
158
|
# File 'lib/veritrans/cli.rb', line 156
def blue(str)
colorize(str, 34)
end
|
#colorize(str, color_code) ⇒ Object
140
141
142
|
# File 'lib/veritrans/cli.rb', line 140
def colorize(str, color_code)
"\e[#{color_code}m#{str}\e[0m"
end
|
#cyan(str) ⇒ Object
163
|
# File 'lib/veritrans/cli.rb', line 163
def cyan(str); colorize(str, 36) end
|
#get_order_info(order_id) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/veritrans/cli.rb', line 99
def get_order_info(order_id)
puts "#{green('*')} Getting order #{order_id}"
Veritrans.logger = Logger.new("/dev/null")
response = Veritrans.status(order_id)
if response.success?
return response
else
puts red("Error")
if response.status_code == 401
raise AuthenticationError, "Can not find order with id=#{order_id} (#{response.status_message})"
else
raise OrderNotFound, "Can not find order with id=#{order_id} (#{response.status_message})"
end
end
end
|
#green(str) ⇒ Object
148
149
150
|
# File 'lib/veritrans/cli.rb', line 148
def green(str)
colorize(str, 32)
end
|
#json_data ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/veritrans/cli.rb', line 116
def json_data
data = {
status_code: "200",
status_message: "Veritrans payment notification",
transaction_id: SecureRandom.uuid,
order_id: "cli-testin-#{rand}",
payment_type: "credit_card",
transaction_time: Time.now.strftime("%Y-%m-%d %H:%M:%S"),
transaction_status: "capture",
fraud_status: "accept",
masked_card: "411111-1111",
gross_amount: "50000.0"
}
if CONFIG[:order]
load_local_config!
order_info = get_order_info(CONFIG[:order])
order_data = order_info.data.except(:status_message, :signature_key)
data = data.except(:fraud_status, :masked_card).merge(order_data)
end
JSON.dump(JSON.pretty_generate(data))
end
|
#load_local_config! ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/veritrans/cli.rb', line 64
def load_local_config!
if CONFIG[:config_path]
if File.exists?(CONFIG[:config_path])
config_file = CONFIG[:config_path]
else
raise ArgumentError, "Can not find config at #{CONFIG[:config_path]}" unless config_file
end
end
if File.exists?("./veritrans.yml")
config_file = "./veritrans.yml"
end
if File.exists?("./config/veritrans.yml")
config_file = "./config/veritrans.yml"
end
raise ArgumentError, "Can not find config at ./config/veritrans.yml or ./veritrans.yml" unless config_file
puts "#{green('*')} Load config #{config_file}"
ENV['RAILS_ENV'] ||= 'development'
Veritrans.setup.load_yml("#{config_file}##{ENV['RAILS_ENV']}")
if !Veritrans.config.client_key || Veritrans.config.client_key == ""
puts red("Error")
raise ArgumentError, "Can not find client_key in #{config_file}"
end
if !Veritrans.config.server_key || Veritrans.config.server_key == ""
puts red("Error")
raise ArgumentError, "Can not find server_key in #{config_file}"
end
end
|
#pink(str) ⇒ Object
160
161
162
|
# File 'lib/veritrans/cli.rb', line 160
def pink(str)
colorize(str, 35)
end
|
#red(str) ⇒ Object
144
145
146
|
# File 'lib/veritrans/cli.rb', line 144
def red(str)
colorize(str, 31)
end
|
#test_webhook(args) ⇒ Object
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
|
# File 'lib/veritrans/cli.rb', line 24
def test_webhook(args)
url = args.shift
raise ArgumentError, "missing required parameter URL" unless url && url != ""
options = {
body: json_data,
headers: {
:Accept => "application/json",
:"Content-Type" => "application/json",
:"User-Agent" => "Veritrans gem #{Veritrans::VERSION} - webhook tester"
},
read_timeout: 10,
write_timeout: 10,
connect_timeout: 10
}
puts "Sending #{options[:body].length} bytes to:"
puts " => #{cyan(url)}"
puts options[:body] + "\n\n" if CONFIG[:order]
s_time = Time.now
response = Excon.post(url, options)
response.body = response.body.to_s.encode('UTF-8', {:invalid => :replace, :undef => :replace, :replace => '?'})
puts "Got response: (#{((Time.now - s_time) * 1000).round}ms)"
puts " status: #{response.status}"
puts " body: #{response.body}"
if response.status >= 200 && response.status < 300
puts green("Success!")
else
puts red("Failed!")
puts "Response status is #{response.status} not 200"
end
end
|
#yellow(str) ⇒ Object
152
153
154
|
# File 'lib/veritrans/cli.rb', line 152
def yellow(str)
colorize(str, 33)
end
|