Class: SepayPgRuby::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:, merchant_id:, secret_key:, api_version: 'v1', checkout_version: 'v1') ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sepay_pg_ruby/client.rb', line 11

def initialize(env:, merchant_id:, secret_key:, api_version: 'v1', checkout_version: 'v1')
  @env = env
  @merchant_id = merchant_id
  @secret_key = secret_key
  @api_version = api_version
  @checkout_version = checkout_version

  validate_config!

  @order = Order.new(self)
  @checkout = Checkout.new(self)
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



9
10
11
# File 'lib/sepay_pg_ruby/client.rb', line 9

def api_version
  @api_version
end

#checkoutObject (readonly)

Returns the value of attribute checkout.



9
10
11
# File 'lib/sepay_pg_ruby/client.rb', line 9

def checkout
  @checkout
end

#checkout_versionObject (readonly)

Returns the value of attribute checkout_version.



9
10
11
# File 'lib/sepay_pg_ruby/client.rb', line 9

def checkout_version
  @checkout_version
end

#envObject (readonly)

Returns the value of attribute env.



9
10
11
# File 'lib/sepay_pg_ruby/client.rb', line 9

def env
  @env
end

#merchant_idObject (readonly)

Returns the value of attribute merchant_id.



9
10
11
# File 'lib/sepay_pg_ruby/client.rb', line 9

def merchant_id
  @merchant_id
end

#orderObject (readonly)

Returns the value of attribute order.



9
10
11
# File 'lib/sepay_pg_ruby/client.rb', line 9

def order
  @order
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



9
10
11
# File 'lib/sepay_pg_ruby/client.rb', line 9

def secret_key
  @secret_key
end

Instance Method Details

#base_api_urlObject



24
25
26
27
28
29
30
# File 'lib/sepay_pg_ruby/client.rb', line 24

def base_api_url
  if env == 'sandbox'
    "https://pgapi-sandbox.sepay.vn/#{api_version}"
  else
    "https://pgapi.sepay.vn/#{api_version}"
  end
end

#base_checkout_urlObject



32
33
34
35
36
37
38
# File 'lib/sepay_pg_ruby/client.rb', line 32

def base_checkout_url
  if env == 'sandbox'
    "https://pay-sandbox.sepay.vn/#{checkout_version}/checkout"
  else
    "https://pay.sepay.vn/#{checkout_version}/checkout"
  end
end

#request(method:, endpoint:, params: nil, body: nil) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sepay_pg_ruby/client.rb', line 40

def request(method:, endpoint:, params: nil, body: nil)
  uri = URI.parse("#{base_api_url}/#{endpoint}")
  uri.query = URI.encode_www_form(params) if params.present?

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  req = build_request(method, uri)
  req['Content-Type'] = 'application/json'
  req['Authorization'] = "Basic #{Base64.strict_encode64("#{merchant_id}:#{secret_key}")}"
  req.body = JSON.generate(body) if body

  res = http.request(req)
  parsed = parse_response_body(res.body)

  raise RequestError, "SePay request failed (#{res.code}): #{parsed.inspect}" if res.code.to_i >= 400

  { status: res.code.to_i, headers: res.to_hash, body: parsed }
end