Class: DkPaymentGateway::Authentication

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Authentication

Returns a new instance of Authentication.



10
11
12
# File 'lib/dk_payment_gateway/authentication.rb', line 10

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/dk_payment_gateway/authentication.rb', line 8

def client
  @client
end

Instance Method Details

#fetch_private_keyObject

Fetch RSA private key for signing requests Returns the private key as a string



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
# File 'lib/dk_payment_gateway/authentication.rb', line 32

def fetch_private_key
  raise AuthenticationError, 'Access token required to fetch private key' unless client.access_token

  request_id = generate_request_id
  headers = {
    'Content-Type' => 'application/json',
    'Authorization' => "Bearer #{client.access_token}"
  }
  response = client.post(
    '/v1/sign/key',
    headers: headers,
    body: {
      request_id: request_id,
      source_app: client.config.source_app
    }.to_json,
    skip_auth: false
  )

  # The response is plain text containing the private key
  if response.is_a?(String) && response.include?('BEGIN PRIVATE KEY')
    response
  elsif response.is_a?(Hash) && response['response_code'] == '3001'
    raise AuthenticationError, "Private key not found: #{response['response_detail']}"
  else
    raise AuthenticationError, 'Invalid private key response'
  end
rescue StandardError => e
  raise AuthenticationError, "Failed to fetch private key: #{e.message}"
end

#fetch_tokenObject

Fetch authorization token Returns the access token string



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dk_payment_gateway/authentication.rb', line 16

def fetch_token
  response = client.post(
    '/v1/auth/token',
    body: token_request_body,
    headers: token_request_headers,
    skip_auth: true
  )
  validate_token_response!(response)

  response['response_data']['access_token']
rescue StandardError => e
  raise AuthenticationError, "Failed to fetch token: #{e.message}"
end