Class: Veritrans::TestingLib

Inherits:
Object
  • Object
show all
Defined in:
lib/veritrans/testing.rb

Overview

Client for Midtrans Testing API and some helper methods for SNAP API

Status: still in development

Instance Method Summary collapse

Constructor Details

#initialize(server_url = "https://api.sandbox.midtrans.com:7676") ⇒ TestingLib

Returns a new instance of TestingLib.



11
12
13
# File 'lib/veritrans/testing.rb', line 11

def initialize(server_url = "https://api.sandbox.midtrans.com:7676")
  @server_url = server_url
end

Instance Method Details

#extract_form(html) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/veritrans/testing.rb', line 124

def extract_form(html)
  form_html = html.match(/(<form.+<\/form>)/m)[1]
  form_url = form_html.match(/action="(.+?)"/)[1]
  form_method = form_html.match(/method="(.+?)"/)[1]
  fields = {}
  form_html.gsub(/<input(.+?)>/) do |token|
    key = (token.match(/name="(.+?)"/) || [])[1]
    value = (token.match(/value="(.+?)"/) || [])[1]
    fields[key] = value
  end

  return {
    method: form_method,
    url: form_url,
    fields: fields
  }
end

#GET(url, options = {}) ⇒ Object



23
24
25
26
27
28
# File 'lib/veritrans/testing.rb', line 23

def GET(url, options = {})
  logger.info("Veritrans::Testing GET #{url}")
  result = Excon.get(url)
  logger.info("Veritrans::Testing -> #{result.status}\n#{result.body}")
  options[:return_raw] ? result : result_json(result)
end

#get_bca_va(va_number) ⇒ Object



45
46
47
# File 'lib/veritrans/testing.rb', line 45

def get_bca_va(va_number)
  GET(@server_url + "/api/bca_va/#{va_number}")
end

#get_cimb_clicks(redirect_url) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/veritrans/testing.rb', line 77

def get_cimb_clicks(redirect_url)
  gateway_result = GET(redirect_url, return_raw: true)

  form_result = extract_form(gateway_result.body)

  POST(form_result[:url].sub("cimb/clicks/index", "api/cimb/inquiry"),
    body: URI.encode_www_form(form_result[:fields].to_a),
    headers: {'Content-Type' => 'application/x-www-form-urlencoded'}
  )
end

#get_indomaret(payment_code) ⇒ Object



69
70
71
# File 'lib/veritrans/testing.rb', line 69

def get_indomaret(payment_code)
  GET(@server_url + "/api/indomaret/#{payment_code}")
end

#get_klik_bca(merchant_code, user_id) ⇒ Object



53
54
55
# File 'lib/veritrans/testing.rb', line 53

def get_klik_bca(merchant_code, user_id)
  GET(@server_url + "/api/klikbca/#{merchant_code}/#{user_id}")
end

#get_mandiri_bill(bill_key) ⇒ Object



61
62
63
# File 'lib/veritrans/testing.rb', line 61

def get_mandiri_bill(bill_key)
  GET(@server_url + "/api/mandiri_bill/#{bill_key}")
end

#get_permata_va(va_number) ⇒ Object



37
38
39
# File 'lib/veritrans/testing.rb', line 37

def get_permata_va(va_number)
  GET(@server_url + "/api/permata_va/#{va_number}")
end

#json_headersObject

:nodoc:



146
147
148
149
150
151
# File 'lib/veritrans/testing.rb', line 146

def json_headers # :nodoc:
  {
    'Content-Type' => 'application/json;charset=UTF-8',
    'Accept' => 'application/json, text/plain, */*'
  }
end

#loggerObject



15
16
17
# File 'lib/veritrans/testing.rb', line 15

def logger
  @logger || Veritrans.logger
end

#logger=(value) ⇒ Object



19
20
21
# File 'lib/veritrans/testing.rb', line 19

def logger=(value)
  @logger = value
end

#pay_bca_va(va_number) ⇒ Object



49
50
51
# File 'lib/veritrans/testing.rb', line 49

def pay_bca_va(va_number)
  POST(@server_url + "/api/bca_va/#{va_number}/pay")
end

#pay_cimb_clicks(redirect_url, user_id = "testuser00") ⇒ Object



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

def pay_cimb_clicks(redirect_url, user_id = "testuser00")
  gateway_result = GET(redirect_url, return_raw: true)

  form_result = extract_form(gateway_result.body)

  form_result[:fields]["AccountId"] = user_id

  result = POST(form_result[:url].sub("cimb/clicks/index", "api/cimb/pay"),
    body: URI.encode_www_form(form_result[:fields].to_a),
    headers: {'Content-Type' => 'application/x-www-form-urlencoded'}
  )

  result2 = POST(result['redirect_url'],
    body: URI.encode_www_form(result['redirect_params'].to_a),
    headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
    return_raw: true
  )

  result
end

#pay_indomaret(payment_code) ⇒ Object



73
74
75
# File 'lib/veritrans/testing.rb', line 73

def pay_indomaret(payment_code)
  POST(@server_url + "/api/indomaret/#{payment_code}/pay")
end

#pay_klik_bca(merchant_code, user_id, order_id = nil) ⇒ Object



57
58
59
# File 'lib/veritrans/testing.rb', line 57

def pay_klik_bca(merchant_code, user_id, order_id = nil)
  POST(@server_url + "/api/klikbca/#{merchant_code}/#{user_id}/pay" + (order_id ? "?order_id=#{order_id}" : ''))
end

#pay_mandiri_bill(bill_key) ⇒ Object



65
66
67
# File 'lib/veritrans/testing.rb', line 65

def pay_mandiri_bill(bill_key)
  POST(@server_url + "/api/mandiri_bill/#{bill_key}/pay")
end

#pay_permata_va(va_number) ⇒ Object



41
42
43
# File 'lib/veritrans/testing.rb', line 41

def pay_permata_va(va_number)
  POST(@server_url + "/api/permata_va/#{va_number}/pay")
end

#pay_snap(snap_token, payment_type, transaction_params = {}) ⇒ Object

experimental



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/veritrans/testing.rb', line 110

def pay_snap(snap_token, payment_type, transaction_params = {})
  #snap_data = GET("https://app.sandbox.veritrans.co.id/snap/v1/payment_pages/#{snap_token}")
  #snap_data = GET("https://app.sandbox.midtrans.com/snap/v1/transactions/#{snap_token}")

  result = POST("https://app.sandbox.midtrans.com/snap/v1/transactions/#{snap_token}/pay",
    body: JSON.pretty_generate({
      payment_type: payment_type
    }.merge(transaction_params)),
    headers: json_headers
  )

  result
end

#POST(url, options = {}) ⇒ Object



30
31
32
33
34
35
# File 'lib/veritrans/testing.rb', line 30

def POST(url, options = {})
  logger.info("Veritrans::Testing POST #{url} #{options[:body]}")
  result = Excon.post(url, options)
  logger.info("Veritrans::Testing -> #{result.status}\n#{result.body}")
  options[:return_raw] ? result : result_json(result)
end

#result_json(result) ⇒ Object

:nodoc:



142
143
144
# File 'lib/veritrans/testing.rb', line 142

def result_json(result) # :nodoc:
  JSON.parse(result.body)
end