Class: SepayPgRuby::Client
- Inherits:
-
Object
- Object
- SepayPgRuby::Client
- Defined in:
- lib/sepay_pg_ruby/client.rb
Instance Attribute Summary collapse
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
-
#checkout ⇒ Object
readonly
Returns the value of attribute checkout.
-
#checkout_version ⇒ Object
readonly
Returns the value of attribute checkout_version.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#merchant_id ⇒ Object
readonly
Returns the value of attribute merchant_id.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
Instance Method Summary collapse
- #base_api_url ⇒ Object
- #base_checkout_url ⇒ Object
-
#initialize(env:, merchant_id:, secret_key:, api_version: 'v1', checkout_version: 'v1') ⇒ Client
constructor
A new instance of Client.
- #request(method:, endpoint:, params: nil, body: nil) ⇒ Object
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_version ⇒ Object (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 |
#checkout ⇒ Object (readonly)
Returns the value of attribute checkout.
9 10 11 |
# File 'lib/sepay_pg_ruby/client.rb', line 9 def checkout @checkout end |
#checkout_version ⇒ Object (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 |
#env ⇒ Object (readonly)
Returns the value of attribute env.
9 10 11 |
# File 'lib/sepay_pg_ruby/client.rb', line 9 def env @env end |
#merchant_id ⇒ Object (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 |
#order ⇒ Object (readonly)
Returns the value of attribute order.
9 10 11 |
# File 'lib/sepay_pg_ruby/client.rb', line 9 def order @order end |
#secret_key ⇒ Object (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_url ⇒ Object
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_url ⇒ Object
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
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 |