Class: GMO::Payment::API

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

Direct Known Subclasses

RemittanceAPI, ShopAPI, ShopAndSiteAPI, SiteAPI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



21
22
23
# File 'lib/gmo.rb', line 21

def initialize(options = {})
  @host = options[:host]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



24
25
26
# File 'lib/gmo.rb', line 24

def host
  @host
end

Instance Method Details

#api(path, args = {}, verb = "post", options = {}) {|body| ... } ⇒ Object

Yields:

  • (body)


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
# File 'lib/gmo.rb', line 26

def api(path, args = {}, verb = "post", options = {}, &error_checking_block)
  # Setup args for make_request
  path = "/payment/#{path}" unless path =~ /^\//
  options.merge!({ :host => @host })
  # Make request via the provided service
  result = GMO.make_request path, args, verb, options
  # Check for any 500 server errors before parsing the body
  if result.status >= 500
    error_detail = {
      :http_status => result.status.to_i,
      :body        => result.body,
    }
    raise GMO::Payment::ServerError.new(result.body, error_detail)
  end
  # Transform the body to Hash
  if /\.json\Z/ =~ path
    # Parse the body as JSON
    parsed_result = ::JSON.parse(result.body)
    response = parsed_result.is_a?(Array) ? parsed_result[0] : parsed_result
  else
    # Parse the body as Query string
    # "ACS=1&ACSUrl=url" => { "ACS" => "1", ACSUrl => "url" }
    key_values = result.body.to_s.split('&').map { |str| str.split('=', 2) }.flatten
    response = Hash[*key_values]
  end
  # Transform the redirect_url
  # "ACS=2&RedirectUrl=https://manage.tds2gw.gmopg.jp/api/v2/brw/callback?transId=6e48e31f-2940-48e1-a702-ebba2f3373ee&t=dccc8a7ed85372c9accff576bff59b3a"
  # => { "ACS" => "2", RedirectUrl => "https://manage.tds2gw.gmopg.jp/api/v2/brw/callback?transId=6e48e31f-2940-48e1-a702- ebba2f3373ee&t=dccc8a7ed85372c9accff576bff59b3a" }
  if response['RedirectUrl'] && response['RedirectUrl'] != '' && response['t'] && response['t'] != '' && response.keys.index('RedirectUrl') + 1 == response.keys.index('t')
    response['RedirectUrl'] = response['RedirectUrl'] + '&t=' + response['t']
    response.delete('t')
  end
  # converting to UTF-8
  body = response = Hash[response.map { |k,v| [k, NKF.nkf('-S -w',v)] }]
  # Check for errors if provided a error_checking_block
  yield(body) if error_checking_block
  # Return result
  if options[:http_component]
    result.send options[:http_component]
  else
    body
  end
end

#get_request(name, args = {}, options = {}) ⇒ Object Also known as: get!

gmo.get_request(“EntryTran.idPass”, => “bar”) GET /EntryTran.idPass with params foo=bar



72
73
74
# File 'lib/gmo.rb', line 72

def get_request(name, args = {}, options = {})
  api_call(name, args, "get", options)
end

#post_request(name, args = {}, options = {}) ⇒ Object Also known as: post!

gmo.post_request(“EntryTran.idPass”, => “bar”) POST /EntryTran.idPass with params foo=bar



79
80
81
82
# File 'lib/gmo.rb', line 79

def post_request(name, args = {}, options = {})
  args = associate_options_to_gmo_params(name, args)
  api_call(name, args, "post", options)
end