Class: PayCertify::Insurance

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

Defined Under Namespace

Classes: NoCredentialsError, Response

Constant Summary collapse

API_ENDPOINT =
'https://connect.paycertify.com/'
MANDATORY_FIELDS =
[
  :firstname, :lastname, :email, :order_number, :items_ordered, :charge_amount, 
  :billing_address, :billing_address2, :billing_city, :billing_state, :billing_country, :billing_zip_code
]
OPTIONAL_FIELDS =
[
  :phone, :shipping_address, :shipping_address2, :shipping_city, 
  :shipping_state, :shipping_country, :shipping_zip_code, :shipping_carrier, :tracking_number
]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Insurance

Returns a new instance of Insurance.

Raises:



22
23
24
25
26
27
28
29
# File 'lib/paycertify/insurance.rb', line 22

def initialize(attributes)
  raise NoCredentialsError, 'No token found for api_client/secret/client_id combination.' unless token.present?

  self.attributes = HashWithIndifferentAccess.new(attributes)
  self.errors = {}

  validate!
end

Class Attribute Details

.api_public_keyObject

Returns the value of attribute api_public_key.



53
54
55
# File 'lib/paycertify/insurance.rb', line 53

def api_public_key
  @api_public_key
end

.api_secret_keyObject

Returns the value of attribute api_secret_key.



53
54
55
# File 'lib/paycertify/insurance.rb', line 53

def api_secret_key
  @api_secret_key
end

.client_idObject

Returns the value of attribute client_id.



53
54
55
# File 'lib/paycertify/insurance.rb', line 53

def client_id
  @client_id
end

.tokenObject

Returns the value of attribute token.



53
54
55
# File 'lib/paycertify/insurance.rb', line 53

def token
  @token
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



18
19
20
# File 'lib/paycertify/insurance.rb', line 18

def attributes
  @attributes
end

#errorsObject

Returns the value of attribute errors.



18
19
20
# File 'lib/paycertify/insurance.rb', line 18

def errors
  @errors
end

#responseObject

Returns the value of attribute response.



18
19
20
# File 'lib/paycertify/insurance.rb', line 18

def response
  @response
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/paycertify/insurance.rb', line 55

def configure(&block)
  yield self if block_given?

  connection = Faraday.new(url: API_ENDPOINT, ssl: {verify: false}) do |faraday|
    faraday.request :url_encoded
    faraday.response :logger
    faraday.adapter  Faraday.default_adapter
  end

  response = connection.get do |request|
    request.url 'api/v1/token'
    request.headers['api-public-key'] = api_public_key
    request.headers['api-secret-key'] = api_secret_key
    request.headers['api-client-id']  = client_id
  end

  json = JSON.parse(response.body)
  
  self.token = json['jwt']

  return {
    api_public_key: api_public_key,
    api_secret_key: api_secret_key,
    client_id: client_id,
    token: token
  }
end

Instance Method Details

#save!Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/paycertify/insurance.rb', line 35

def save!
  data = attributes.slice *[MANDATORY_FIELDS + OPTIONAL_FIELDS].flatten
  data[:ship_to_billing_addr] = true unless data[:shipping_address].present?

  api_response = connection.post do |request|
    request.url path_for('orders')
    request.headers['Content-Type'] = 'application/json'
    request.headers['Authorization'] = "JWT #{token}"
    request.body = JSON.generate(data)
  end

  self.response = Response.new(api_response)
  self.errors = errors.merge(response) unless response.success?

  response
end

#success?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/paycertify/insurance.rb', line 31

def success?
  response.success?
end