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



145
146
147
# File 'lib/veritrans/cli.rb', line 145

def blue(str)
  colorize(str, 34)
end

#colorize(str, color_code) ⇒ Object



129
130
131
# File 'lib/veritrans/cli.rb', line 129

def colorize(str, color_code)
  "\e[#{color_code}m#{str}\e[0m"
end

#cyan(str) ⇒ Object



152
# File 'lib/veritrans/cli.rb', line 152

def cyan(str); colorize(str, 36) end

#get_order_info(order_id) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/veritrans/cli.rb', line 88

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



137
138
139
# File 'lib/veritrans/cli.rb', line 137

def green(str)
  colorize(str, 32)
end

#json_dataObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/veritrans/cli.rb', line 105

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.pretty_generate(data)
end

#load_local_config!Object

Raises:

  • (ArgumentError)


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
78
79
80
81
82
83
84
85
86
# File 'lib/veritrans/cli.rb', line 53

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



149
150
151
# File 'lib/veritrans/cli.rb', line 149

def pink(str)
  colorize(str, 35)
end

#red(str) ⇒ Object



133
134
135
# File 'lib/veritrans/cli.rb', line 133

def red(str)
  colorize(str, 31)
end

#test_webhook(args) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/veritrans/cli.rb', line 13

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)}"
  # Print body if it's custom
  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
#rescue Object => error
#  puts red("Failed!")
#  puts error.message
end

#yellow(str) ⇒ Object



141
142
143
# File 'lib/veritrans/cli.rb', line 141

def yellow(str)
  colorize(str, 33)
end