lightrail_client 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +300 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/lightrail_client.rb +29 -0
- data/lib/lightrail_client/account.rb +93 -0
- data/lib/lightrail_client/card.rb +40 -0
- data/lib/lightrail_client/code.rb +30 -0
- data/lib/lightrail_client/connection.rb +50 -0
- data/lib/lightrail_client/constants.rb +23 -0
- data/lib/lightrail_client/contact.rb +93 -0
- data/lib/lightrail_client/errors.rb +34 -0
- data/lib/lightrail_client/lightrail_object.rb +10 -0
- data/lib/lightrail_client/ping.rb +11 -0
- data/lib/lightrail_client/shopper_token_factory.rb +41 -0
- data/lib/lightrail_client/transaction.rb +77 -0
- data/lib/lightrail_client/validator.rb +317 -0
- data/lib/lightrail_client/version.rb +3 -0
- data/lightrail_client.gemspec +42 -0
- metadata +183 -0
@@ -0,0 +1,40 @@
|
|
1
|
+
module Lightrail
|
2
|
+
class Card < Lightrail::LightrailObject
|
3
|
+
|
4
|
+
def self.create(create_params)
|
5
|
+
params_for_create = Lightrail::Validator.set_params_for_card_create!(create_params)
|
6
|
+
response = Lightrail::Connection.send :make_post_request_and_parse_response, "cards", params_for_create
|
7
|
+
response['card']
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.charge(charge_params)
|
11
|
+
Lightrail::Transaction.charge_card(charge_params, false)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.simulate_charge(charge_params)
|
15
|
+
params_for_simulation = Lightrail::Validator.set_nsf_for_simulate!(charge_params)
|
16
|
+
Lightrail::Transaction.charge_card(params_for_simulation, true)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.fund(fund_params)
|
20
|
+
Lightrail::Transaction.fund_card(fund_params)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.get_maximum_value(card_id)
|
24
|
+
card_details = self.get_details(card_id)
|
25
|
+
maximum_value = 0
|
26
|
+
card_details['valueStores'].each do |valueStore|
|
27
|
+
if valueStore['state'] == 'ACTIVE'
|
28
|
+
maximum_value += valueStore['value']
|
29
|
+
end
|
30
|
+
end
|
31
|
+
maximum_value
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.get_details(card_id)
|
35
|
+
response = Lightrail::Connection.make_get_request_and_parse_response("cards/#{CGI::escape(card_id)}/details")
|
36
|
+
response['details']
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Lightrail
|
2
|
+
class Code
|
3
|
+
|
4
|
+
def self.charge(charge_params)
|
5
|
+
Lightrail::Transaction.charge_code(charge_params, false)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.simulate_charge(charge_params)
|
9
|
+
params_for_simulation = Lightrail::Validator.set_nsf_for_simulate!(charge_params)
|
10
|
+
Lightrail::Transaction.charge_code(params_for_simulation, true)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.get_maximum_value(code)
|
14
|
+
code_details = self.get_details(code)
|
15
|
+
maximum_value = 0
|
16
|
+
code_details['valueStores'].each do |valueStore|
|
17
|
+
if valueStore['state'] == 'ACTIVE'
|
18
|
+
maximum_value += valueStore['value']
|
19
|
+
end
|
20
|
+
end
|
21
|
+
maximum_value
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.get_details(code)
|
25
|
+
response = Lightrail::Connection.make_get_request_and_parse_response("codes/#{CGI::escape(code)}/details")
|
26
|
+
response['details']
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Lightrail
|
2
|
+
class Connection
|
3
|
+
def self.connection
|
4
|
+
conn = Faraday.new Lightrail.api_base, ssl: {version: :TLSv1_2}
|
5
|
+
conn.headers['Content-Type'] = 'application/json; charset=utf-8'
|
6
|
+
conn.headers['Authorization'] = "Bearer #{Lightrail.api_key}"
|
7
|
+
conn
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.ping
|
11
|
+
self.make_get_request_and_parse_response('ping')
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.make_post_request_and_parse_response (url, body)
|
15
|
+
resp = Lightrail::Connection.connection.post do |req|
|
16
|
+
req.url url
|
17
|
+
req.body = JSON.generate(body)
|
18
|
+
end
|
19
|
+
self.handle_response(resp)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.make_get_request_and_parse_response (url)
|
23
|
+
resp = Lightrail::Connection.connection.get {|req| req.url url}
|
24
|
+
self.handle_response(resp)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.handle_response(response)
|
28
|
+
body = JSON.parse(response.body) || {}
|
29
|
+
message = body['message'] || ''
|
30
|
+
case response.status
|
31
|
+
when 200...300
|
32
|
+
JSON.parse(response.body)
|
33
|
+
when 400
|
34
|
+
if (message =~ /insufficient value/i)
|
35
|
+
raise Lightrail::InsufficientValueError.new(message, response)
|
36
|
+
else
|
37
|
+
raise Lightrail::BadParameterError.new(message, response)
|
38
|
+
end
|
39
|
+
when 401, 403
|
40
|
+
raise Lightrail::AuthorizationError.new(message, response)
|
41
|
+
when 404
|
42
|
+
raise Lightrail::CouldNotFindObjectError.new(message, response)
|
43
|
+
when 409
|
44
|
+
raise Lightrail::BadParameterError.new(message, response)
|
45
|
+
else
|
46
|
+
raise LightrailError.new("Server responded with: (#{response.status}) #{message}", response)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Lightrail
|
2
|
+
class Constants
|
3
|
+
|
4
|
+
code_keys_sym = [:code, :lightrail_code]
|
5
|
+
card_id_keys_sym = [:cardId, :card_id, :lightrail_card_id]
|
6
|
+
contact_id_keys_sym = [:contactId, :contact_id, :lightrail_contact_id]
|
7
|
+
shopper_id_keys_sym = [:shopperId, :shopper_id, :lightrail_shopper_id]
|
8
|
+
user_supplied_id_keys_sym = [:userSuppliedId, :user_supplied_id, :lightrail_user_supplied_id, :idempotency_key]
|
9
|
+
transaction_id_keys_sym = [:transactionId, :transaction_id, :lightrail_transaction_id]
|
10
|
+
|
11
|
+
LIGHTRAIL_CODE_KEYS = code_keys_sym + code_keys_sym.map {|code_key| code_key.to_s}
|
12
|
+
LIGHTRAIL_CARD_ID_KEYS = card_id_keys_sym + card_id_keys_sym.map {|card_id_key| card_id_key.to_s}
|
13
|
+
LIGHTRAIL_CONTACT_ID_KEYS = contact_id_keys_sym + contact_id_keys_sym.map {|contact_id_key| contact_id_key.to_s}
|
14
|
+
LIGHTRAIL_SHOPPER_ID_KEYS = shopper_id_keys_sym + shopper_id_keys_sym.map {|shopper_id_key| shopper_id_key.to_s}
|
15
|
+
LIGHTRAIL_USER_SUPPLIED_ID_KEYS = user_supplied_id_keys_sym + user_supplied_id_keys_sym.map {|user_supplied_id_key| user_supplied_id_key.to_s}
|
16
|
+
LIGHTRAIL_TRANSACTION_ID_KEYS = transaction_id_keys_sym + transaction_id_keys_sym.map {|transaction_id_key| transaction_id_key.to_s}
|
17
|
+
|
18
|
+
LIGHTRAIL_PAYMENT_METHODS = self::LIGHTRAIL_CODE_KEYS + self::LIGHTRAIL_CARD_ID_KEYS + self::LIGHTRAIL_CONTACT_ID_KEYS + self::LIGHTRAIL_SHOPPER_ID_KEYS
|
19
|
+
|
20
|
+
LIGHTRAIL_TRANSACTION_TYPES = [:code_drawdown, :card_id_drawdown, :code_pending, :card_id_pending, :fund_card, :refund, :capture, :void]
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module Lightrail
|
2
|
+
class Contact < Lightrail::LightrailObject
|
3
|
+
|
4
|
+
def self.create(create_params)
|
5
|
+
params_with_user_supplied_id = self.set_user_supplied_id_for_contact_create(create_params)
|
6
|
+
params_with_name_if_present = self.set_name_if_present(params_with_user_supplied_id)
|
7
|
+
response = Lightrail::Connection.send :make_post_request_and_parse_response, "contacts", params_with_name_if_present
|
8
|
+
response['contact']
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.retrieve_by_shopper_id(shopper_id)
|
12
|
+
response = Lightrail::Connection.send :make_get_request_and_parse_response, "contacts?userSuppliedId=#{CGI::escape(shopper_id)}"
|
13
|
+
response['contacts'][0]
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.retrieve_by_contact_id(contact_id)
|
17
|
+
response = Lightrail::Connection.send :make_get_request_and_parse_response, "contacts/#{CGI::escape(contact_id)}"
|
18
|
+
response['contact']
|
19
|
+
end
|
20
|
+
|
21
|
+
# Utility methods
|
22
|
+
|
23
|
+
def self.retrieve_or_create_by_shopper_id(shopper_id)
|
24
|
+
contact = self.retrieve_by_shopper_id(shopper_id)
|
25
|
+
if !contact
|
26
|
+
contact = self.create({shopper_id: shopper_id})
|
27
|
+
end
|
28
|
+
contact
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.get_contact_id_from_id_or_shopper_id(charge_params)
|
32
|
+
if Lightrail::Validator.has_valid_contact_id?(charge_params)
|
33
|
+
return Lightrail::Validator.get_contact_id(charge_params)
|
34
|
+
end
|
35
|
+
|
36
|
+
if Lightrail::Validator.has_valid_shopper_id?(charge_params)
|
37
|
+
shopper_id = Lightrail::Validator.get_shopper_id(charge_params)
|
38
|
+
contact = self.get_by_shopper_id(shopper_id)
|
39
|
+
if (!contact.nil? && !contact.empty? && contact['contactId'])
|
40
|
+
return contact['contactId']
|
41
|
+
else
|
42
|
+
return nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
return nil
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def self.get_contact_id_from_shopper_id(shopper_id)
|
52
|
+
contact = self.retrieve_by_shopper_id(shopper_id)
|
53
|
+
contact['contactId']
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.set_user_supplied_id_for_contact_create(create_params)
|
57
|
+
params_with_user_supplied_id = create_params.clone
|
58
|
+
shopper_id = Lightrail::Validator.get_shopper_id(create_params) || nil
|
59
|
+
user_supplied_id = Lightrail::Validator.get_user_supplied_id(create_params) || nil
|
60
|
+
|
61
|
+
if !(shopper_id || user_supplied_id)
|
62
|
+
raise Lightrail::LightrailArgumentError.new("Must provide one of shopper_id or user_supplied_id to create new Contact")
|
63
|
+
elsif (shopper_id && user_supplied_id)
|
64
|
+
raise Lightrail::LightrailArgumentError.new("Must provide only one of shopper_id or user_supplied_id to create new Contact")
|
65
|
+
end
|
66
|
+
|
67
|
+
if shopper_id
|
68
|
+
params_with_user_supplied_id[:userSuppliedId] ||= shopper_id
|
69
|
+
elsif user_supplied_id
|
70
|
+
params_with_user_supplied_id[:userSuppliedId] ||= user_supplied_id
|
71
|
+
end
|
72
|
+
|
73
|
+
params_with_user_supplied_id
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.set_name_if_present(create_params)
|
77
|
+
params_with_name = create_params.clone
|
78
|
+
params_with_name[:firstName] ||= params_with_name[:first_name]
|
79
|
+
params_with_name[:lastName] ||= params_with_name[:last_name]
|
80
|
+
params_with_name
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.get_by_id(contact_id)
|
84
|
+
response = Lightrail::Connection.make_get_request_and_parse_response("contacts/#{CGI::escape(contact_id)}")
|
85
|
+
response['contact']
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.get_by_shopper_id(shopper_id)
|
89
|
+
response = Lightrail::Connection.make_get_request_and_parse_response("contacts?userSuppliedId=#{CGI::escape(shopper_id)}")
|
90
|
+
response['contacts'][0]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Lightrail
|
2
|
+
class LightrailError < StandardError
|
3
|
+
attr_reader :message
|
4
|
+
attr_accessor :response
|
5
|
+
|
6
|
+
def initialize (message='', response)
|
7
|
+
@message = message
|
8
|
+
@response = response
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class AuthorizationError < LightrailError
|
13
|
+
end
|
14
|
+
|
15
|
+
class InsufficientValueError < LightrailError
|
16
|
+
end
|
17
|
+
|
18
|
+
class BadParameterError < LightrailError
|
19
|
+
end
|
20
|
+
|
21
|
+
class CouldNotFindObjectError < LightrailError
|
22
|
+
end
|
23
|
+
|
24
|
+
class IdempotencyError < LightrailError
|
25
|
+
end
|
26
|
+
|
27
|
+
class ThirdPartyPaymentError < LightrailError
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
class LightrailArgumentError < ArgumentError
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Lightrail
|
2
|
+
class Ping < Lightrail::LightrailObject
|
3
|
+
attr_accessor :username, :mode, :scopes, :roles, :effectiveScopes
|
4
|
+
|
5
|
+
def self.ping
|
6
|
+
response = Lightrail::Connection.ping
|
7
|
+
Lightrail::Validator.validate_ping_response!(response)
|
8
|
+
self.new(response['user'])
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Lightrail
|
2
|
+
class ShopperTokenFactory
|
3
|
+
def self.generate (contact, validity_in_seconds=nil)
|
4
|
+
raise Lightrail::BadParameterError.new("Lightrail::api_key is not set") unless Lightrail::api_key
|
5
|
+
raise Lightrail::BadParameterError.new("Lightrail::shared_secret is not set") unless Lightrail::shared_secret
|
6
|
+
|
7
|
+
raise Lightrail::BadParameterError.new("Must provide a contact with one of shopper_id, contact_id or user_supplied_id to generate a shopper token") unless (Lightrail::Validator.has_valid_or_empty_shopper_id?(contact) ||
|
8
|
+
Lightrail::Validator.has_valid_contact_id?(contact) ||
|
9
|
+
Lightrail::Validator.has_valid_user_supplied_id?(contact))
|
10
|
+
|
11
|
+
g = {}
|
12
|
+
if (Lightrail::Validator.has_valid_or_empty_shopper_id?(contact))
|
13
|
+
g['shi'] = Lightrail::Validator.get_shopper_id(contact)
|
14
|
+
elsif (Lightrail::Validator.has_valid_contact_id?(contact))
|
15
|
+
g['coi'] = Lightrail::Validator.get_contact_id(contact)
|
16
|
+
elsif (Lightrail::Validator.has_valid_user_supplied_id?(contact))
|
17
|
+
g['cui'] = Lightrail::Validator.get_user_supplied_id(contact)
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
payload = Lightrail::api_key.split('.')
|
22
|
+
payload = JSON.parse(Base64.decode64(payload[1]))
|
23
|
+
|
24
|
+
g['gui'] = payload['g']['gui']
|
25
|
+
g['gmi'] = payload['g']['gmi']
|
26
|
+
|
27
|
+
iat = Time.now.to_i
|
28
|
+
payload = {
|
29
|
+
g: g,
|
30
|
+
iat: iat,
|
31
|
+
iss: "MERCHANT"
|
32
|
+
}
|
33
|
+
|
34
|
+
if validity_in_seconds
|
35
|
+
payload['exp'] = iat + validity_in_seconds
|
36
|
+
end
|
37
|
+
|
38
|
+
JWT.encode(payload, Lightrail::shared_secret, 'HS256')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module Lightrail
|
2
|
+
class Transaction < Lightrail::LightrailObject
|
3
|
+
attr_accessor :transactionId, :value, :userSuppliedId, :dateCreated, :transactionType, :transactionAccessMethod, :giftbitUserId, :cardId, :currency, :codeLastFour, :metadata, :parentTransactionId
|
4
|
+
|
5
|
+
def self.charge_code(transaction_params, simulate)
|
6
|
+
transaction_type = transaction_params[:pending] ? :code_pending : :code_drawdown
|
7
|
+
self.create(transaction_params, transaction_type, simulate)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.charge_card(transaction_params, simulate)
|
11
|
+
transaction_type = transaction_params[:pending] ? :card_id_pending : :card_id_drawdown
|
12
|
+
self.create(transaction_params, transaction_type, simulate)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.fund_card(transaction_params)
|
16
|
+
self.create(transaction_params, :card_id_fund, false)
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def self.refund (original_transaction_info, new_request_body={})
|
21
|
+
handle_transaction(original_transaction_info, 'refund', new_request_body)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.void (original_transaction_info, new_request_body={})
|
25
|
+
handle_transaction(original_transaction_info, 'void', new_request_body)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.capture (original_transaction_info, new_request_body={})
|
29
|
+
handle_transaction(original_transaction_info, 'capture', new_request_body)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
# def self.simulate(transaction_params, transaction_type)
|
35
|
+
# transaction_params_for_lightrail = Lightrail::Validator.send("set_params_for_#{transaction_type}!", transaction_params)
|
36
|
+
# response = self.post_transaction(transaction_params_for_lightrail, true)
|
37
|
+
# end
|
38
|
+
|
39
|
+
def self.create(transaction_params, transaction_type, simulate)
|
40
|
+
transaction_params_for_lightrail = Lightrail::Validator.send("set_params_for_#{transaction_type}!", transaction_params)
|
41
|
+
response = self.post_transaction(transaction_params_for_lightrail, simulate)
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.handle_transaction (original_transaction_info, action, new_request_body={})
|
45
|
+
transaction_params_for_lightrail = Lightrail::Validator.set_params_for_acting_on_existing_transaction!(original_transaction_info, new_request_body)
|
46
|
+
response = self.act_on_transaction(transaction_params_for_lightrail, action)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.post_transaction(transaction_params, simulate)
|
50
|
+
dry_run = simulate ? '/dryRun' : ''
|
51
|
+
response = {}
|
52
|
+
if (transaction_params[:code])
|
53
|
+
code = transaction_params.delete(:code)
|
54
|
+
response = Lightrail::Connection.send :make_post_request_and_parse_response, "codes/#{CGI::escape(code)}/transactions#{dry_run}", transaction_params
|
55
|
+
elsif (transaction_params[:cardId])
|
56
|
+
card_id = transaction_params.delete(:cardId)
|
57
|
+
response = Lightrail::Connection.send :make_post_request_and_parse_response, "cards/#{CGI::escape(card_id)}/transactions#{dry_run}", transaction_params
|
58
|
+
else
|
59
|
+
raise Lightrail::LightrailArgumentError.new("Lightrail code or cardId required to post a transaction: #{transaction_params.inspect}")
|
60
|
+
end
|
61
|
+
response['transaction']
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.act_on_transaction(transaction_params, action)
|
65
|
+
response = {}
|
66
|
+
if (transaction_params[:cardId])
|
67
|
+
card_id = transaction_params.delete(:cardId)
|
68
|
+
transaction_id = transaction_params.delete(:transactionId)
|
69
|
+
response = Lightrail::Connection.send :make_post_request_and_parse_response, "cards/#{CGI::escape(card_id)}/transactions/#{CGI::escape(transaction_id)}/#{action}", transaction_params
|
70
|
+
else
|
71
|
+
raise Lightrail::LightrailArgumentError.new("Lightrail cardId required to act on an existing transaction: #{transaction_params.inspect}")
|
72
|
+
end
|
73
|
+
response['transaction']
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,317 @@
|
|
1
|
+
module Lightrail
|
2
|
+
class Validator
|
3
|
+
def self.set_params_for_code_drawdown!(charge_params)
|
4
|
+
validated_params = charge_params.clone
|
5
|
+
begin
|
6
|
+
return validated_params if ((validated_params.is_a? Hash) &&
|
7
|
+
self.set_code!(validated_params, validated_params) &&
|
8
|
+
self.validate_amount!(validated_params[:amount] || validated_params[:value]) &&
|
9
|
+
self.validate_currency!(validated_params[:currency]) &&
|
10
|
+
self.get_or_set_userSuppliedId!(validated_params))
|
11
|
+
rescue Lightrail::LightrailArgumentError
|
12
|
+
end
|
13
|
+
raise Lightrail::LightrailArgumentError.new("Invalid charge_params for set_params_for_code_drawdown!: #{charge_params.inspect}")
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.set_params_for_code_pending!(charge_params)
|
17
|
+
begin
|
18
|
+
validated_params = self.set_params_for_code_drawdown!(charge_params)
|
19
|
+
validated_params[:pending] = true
|
20
|
+
return validated_params
|
21
|
+
rescue Lightrail::LightrailError
|
22
|
+
end
|
23
|
+
raise Lightrail::LightrailArgumentError.new("Invalid charge_params for set_params_for_code_pending!: #{charge_params.inspect}")
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.set_params_for_card_id_drawdown!(charge_params)
|
27
|
+
validated_params = charge_params.clone
|
28
|
+
begin
|
29
|
+
return validated_params if ((validated_params.is_a? Hash) &&
|
30
|
+
(self.set_cardId!(validated_params, validated_params) ||
|
31
|
+
Lightrail::Account.replace_contact_id_or_shopper_id_with_card_id(validated_params)) &&
|
32
|
+
self.validate_amount!(validated_params[:amount] || validated_params[:value]) &&
|
33
|
+
self.validate_currency!(validated_params[:currency]) &&
|
34
|
+
self.get_or_set_userSuppliedId!(validated_params))
|
35
|
+
rescue Lightrail::LightrailArgumentError
|
36
|
+
end
|
37
|
+
raise Lightrail::LightrailArgumentError.new("Invalid charge_params for set_params_for_card_id_drawdown!: #{charge_params.inspect}")
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.set_params_for_card_id_fund!(fund_params)
|
41
|
+
validated_params = fund_params.clone
|
42
|
+
begin
|
43
|
+
return validated_params if ((validated_params.is_a? Hash) &&
|
44
|
+
(self.set_cardId!(validated_params, validated_params) ||
|
45
|
+
Lightrail::Account.replace_contact_id_or_shopper_id_with_card_id(validated_params)) &&
|
46
|
+
self.validate_amount!(validated_params[:amount] || validated_params[:value]) &&
|
47
|
+
self.validate_currency!(validated_params[:currency]) &&
|
48
|
+
self.get_or_set_userSuppliedId!(validated_params))
|
49
|
+
rescue Lightrail::LightrailArgumentError
|
50
|
+
end
|
51
|
+
raise Lightrail::LightrailArgumentError.new("Invalid fund_params for set_params_for_card_id_fund!: #{fund_params.inspect}")
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.set_params_for_card_create!(create_params)
|
55
|
+
validated_params = create_params.clone
|
56
|
+
begin
|
57
|
+
return validated_params if ((validated_params.is_a? Hash) &&
|
58
|
+
self.validate_currency!(validated_params[:currency]) &&
|
59
|
+
self.has_valid_user_supplied_id?(validated_params) &&
|
60
|
+
validated_params[:userSuppliedId] ||= self.get_user_supplied_id(create_params))
|
61
|
+
rescue Lightrail::LightrailArgumentError
|
62
|
+
end
|
63
|
+
raise Lightrail::LightrailArgumentError.new("Invalid create_params for set_params_for_card_create!!: #{create_params.inspect}")
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.set_params_for_account_create!(create_account_params)
|
67
|
+
validated_params = create_account_params.clone
|
68
|
+
|
69
|
+
begin
|
70
|
+
return validated_params if ((validated_params.is_a? Hash) &&
|
71
|
+
self.set_userSuppliedId_from_existing!(validated_params, validated_params) &&
|
72
|
+
(self.has_valid_shopper_id?(validated_params) || self.has_valid_contact_id?(validated_params)) &&
|
73
|
+
self.validate_currency!(validated_params[:currency]) &&
|
74
|
+
Lightrail::Account.set_account_card_type(validated_params))
|
75
|
+
rescue Lightrail::LightrailArgumentError
|
76
|
+
end
|
77
|
+
raise Lightrail::LightrailArgumentError.new("Invalid create_account_params for set_params_for_account_create!: #{create_account_params.inspect}")
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.set_nsf_for_simulate!(charge_params)
|
81
|
+
params_for_simulate = charge_params.clone
|
82
|
+
if (!params_for_simulate.key?([:nsf]) && !params_for_simulate.key?(['nsf']))
|
83
|
+
params_for_simulate[:nsf] = false
|
84
|
+
end
|
85
|
+
params_for_simulate
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.set_params_for_card_id_pending!(charge_params)
|
89
|
+
begin
|
90
|
+
validated_params = self.set_params_for_card_id_drawdown!(charge_params)
|
91
|
+
validated_params[:pending] = true
|
92
|
+
return validated_params
|
93
|
+
rescue Lightrail::LightrailError
|
94
|
+
end
|
95
|
+
raise Lightrail::LightrailArgumentError.new("Invalid charge_params for set_params_for_card_id_pending!!: #{charge_params.inspect}")
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.set_params_for_acting_on_existing_transaction!(original_transaction, new_request_body={})
|
99
|
+
validated_params = new_request_body.clone
|
100
|
+
validated_params[:original_transaction] = original_transaction
|
101
|
+
begin
|
102
|
+
return validated_params if ((validated_params.is_a? Hash) &&
|
103
|
+
(validated_params[:original_transaction].is_a? Hash) &&
|
104
|
+
self.set_cardId!(validated_params, validated_params[:original_transaction]) &&
|
105
|
+
self.set_transactionId!(validated_params, validated_params[:original_transaction]) &&
|
106
|
+
self.get_or_set_userSuppliedId!(validated_params))
|
107
|
+
rescue Lightrail::LightrailArgumentError
|
108
|
+
end
|
109
|
+
raise Lightrail::LightrailArgumentError.new("Invalid params for set_params_for_acting_on_existing_transaction!: original_transaction: #{original_transaction.inspect}; new_request_body: #{new_request_body.inspect}")
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
def self.validate_charge_object! (charge_params)
|
114
|
+
begin
|
115
|
+
return true if (self.set_params_for_code_drawdown!(charge_params) if self.has_valid_code?(charge_params)) ||
|
116
|
+
(self.set_params_for_card_id_drawdown!(charge_params) if (self.has_valid_card_id?(charge_params) || self.has_valid_contact_id?(charge_params) || self.has_valid_shopper_id?(charge_params)))
|
117
|
+
rescue Lightrail::LightrailArgumentError
|
118
|
+
end
|
119
|
+
raise Lightrail::LightrailArgumentError.new("Invalid charge_params: #{charge_params.inspect}")
|
120
|
+
end
|
121
|
+
|
122
|
+
def self.validate_transaction_response! (transaction_response)
|
123
|
+
begin
|
124
|
+
return true if (transaction_response.is_a? Hash) &&
|
125
|
+
self.has_valid_transaction_id?(transaction_response) &&
|
126
|
+
self.has_valid_card_id?(transaction_response)
|
127
|
+
rescue Lightrail::LightrailArgumentError
|
128
|
+
end
|
129
|
+
raise Lightrail::LightrailArgumentError.new("Invalid transaction_response: #{transaction_response.inspect}")
|
130
|
+
end
|
131
|
+
|
132
|
+
def self.validate_fund_object! (fund_params)
|
133
|
+
begin
|
134
|
+
return true if (self.set_params_for_card_id_fund!(fund_params) if (self.has_valid_card_id?(fund_params) ||
|
135
|
+
self.has_valid_contact_id?(fund_params) || self.has_valid_shopper_id?(fund_params)))
|
136
|
+
rescue Lightrail::LightrailArgumentError
|
137
|
+
end
|
138
|
+
raise Lightrail::LightrailArgumentError.new("Invalid fund_params: #{fund_params.inspect}")
|
139
|
+
end
|
140
|
+
|
141
|
+
def self.validate_ping_response! (ping_response)
|
142
|
+
begin
|
143
|
+
return true if ((ping_response.is_a? Hash) &&
|
144
|
+
(ping_response['user'].is_a? Hash) &&
|
145
|
+
!ping_response['user'].empty? &&
|
146
|
+
self.validate_username!(ping_response['user']['username']))
|
147
|
+
rescue Lightrail::LightrailArgumentError
|
148
|
+
end
|
149
|
+
raise Lightrail::LightrailArgumentError.new("Invalid ping_response: #{ping_response.inspect}")
|
150
|
+
end
|
151
|
+
|
152
|
+
|
153
|
+
def self.validate_card_id! (card_id)
|
154
|
+
return true if ((card_id.is_a? String) && ((/\A[A-Z0-9\-]+\z/i =~ card_id).is_a? Integer))
|
155
|
+
raise Lightrail::LightrailArgumentError.new("Invalid card_id: #{card_id.inspect}")
|
156
|
+
end
|
157
|
+
|
158
|
+
def self.validate_code! (code)
|
159
|
+
return true if ((code.is_a? String) && ((/\A[A-Z0-9\-]+\z/i =~ code).is_a? Integer))
|
160
|
+
raise Lightrail::LightrailArgumentError.new("Invalid code: #{code.inspect}")
|
161
|
+
end
|
162
|
+
|
163
|
+
def self.validate_contact_id! (contact_id)
|
164
|
+
return true if ((contact_id.is_a? String) && ((/\A[A-Z0-9\-]+\z/i =~ contact_id).is_a? Integer))
|
165
|
+
raise Lightrail::LightrailArgumentError.new("Invalid contact_id: #{contact_id.inspect}")
|
166
|
+
end
|
167
|
+
|
168
|
+
def self.validate_shopper_id! (shopper_id)
|
169
|
+
return true if ((shopper_id.is_a? String) && ((/\A[A-Z0-9\-]+\z/i =~ shopper_id).is_a? Integer))
|
170
|
+
raise Lightrail::LightrailArgumentError.new("Invalid shopper_id: #{shopper_id.inspect}")
|
171
|
+
end
|
172
|
+
|
173
|
+
def self.validate_user_supplied_id! (user_supplied_id)
|
174
|
+
return true if (user_supplied_id.is_a? String)
|
175
|
+
raise Lightrail::LightrailArgumentError.new("Invalid user_supplied_id (must be a String): #{user_supplied_id.inspect} ")
|
176
|
+
end
|
177
|
+
|
178
|
+
def self.validate_transaction_id! (transaction_id)
|
179
|
+
return true if ((transaction_id.is_a? String) && !transaction_id.empty?)
|
180
|
+
raise Lightrail::LightrailArgumentError.new("Invalid transaction_id: #{transaction_id.inspect}")
|
181
|
+
end
|
182
|
+
|
183
|
+
def self.validate_amount! (amount)
|
184
|
+
return true if (amount.is_a? Integer)
|
185
|
+
raise Lightrail::LightrailArgumentError.new("Invalid amount: #{amount.inspect}")
|
186
|
+
end
|
187
|
+
|
188
|
+
def self.validate_currency! (currency)
|
189
|
+
return true if (/\A[A-Z]{3}\z/ === currency)
|
190
|
+
raise Lightrail::LightrailArgumentError.new("Invalid currency: #{currency.inspect}")
|
191
|
+
end
|
192
|
+
|
193
|
+
def self.validate_username!(username)
|
194
|
+
return true if ((username.is_a? String) && !username.empty?)
|
195
|
+
raise Lightrail::LightrailArgumentError.new("Invalid username: #{username.inspect}")
|
196
|
+
end
|
197
|
+
|
198
|
+
private
|
199
|
+
|
200
|
+
def self.set_code!(destination_params, source_params)
|
201
|
+
destination_params[:code] ||= self.has_valid_code?(source_params) ? self.get_code(source_params) : nil
|
202
|
+
end
|
203
|
+
|
204
|
+
def self.set_cardId!(destination_params, source_params)
|
205
|
+
destination_params[:cardId] ||= self.has_valid_card_id?(source_params) ? self.get_card_id(source_params) : nil
|
206
|
+
end
|
207
|
+
|
208
|
+
def self.set_transactionId!(destination_params, source_params)
|
209
|
+
destination_params[:transactionId] = self.has_valid_transaction_id?(source_params) ? self.get_transaction_id(source_params) : nil
|
210
|
+
end
|
211
|
+
|
212
|
+
def self.set_contactId_from_contact_or_shopper_id!(destination_params, source_params)
|
213
|
+
contact_id = Lightrail::Validator.has_valid_contact_id?(source_params) ? Lightrail::Validator.get_contact_id(source_params) : nil
|
214
|
+
shopper_id = Lightrail::Validator.has_valid_shopper_id?(source_params) ? Lightrail::Validator.get_shopper_id(source_params) : nil
|
215
|
+
|
216
|
+
raise Lightrail::LightrailArgumentError.new("Must set one of shopper_id or contact_id in #{source_params.inspect}.") unless shopper_id || contact_id
|
217
|
+
|
218
|
+
contact_id_from_shopper_id = shopper_id ? Lightrail::Contact.get_contact_id_from_shopper_id(shopper_id) : nil
|
219
|
+
|
220
|
+
if contact_id && contact_id_from_shopper_id && (contact_id != contact_id_from_shopper_id)
|
221
|
+
raise Lightrail::LightrailArgumentError.new("Error from set_contactId_from_contact_or_shopper_id!: received shopper_id #{shopper_id} and contact_id #{contact_id} which do not belong to the same contact.")
|
222
|
+
end
|
223
|
+
|
224
|
+
destination_params[:contactId] = contact_id || contact_id_from_shopper_id
|
225
|
+
end
|
226
|
+
|
227
|
+
def self.set_userSuppliedId_from_existing!(destination_params, source_params)
|
228
|
+
destination_params[:userSuppliedId] ||= self.has_valid_user_supplied_id?(source_params) ? self.get_user_supplied_id(source_params) : nil
|
229
|
+
end
|
230
|
+
|
231
|
+
def self.get_or_set_userSuppliedId!(charge_params)
|
232
|
+
charge_params[:userSuppliedId] ||= self.get_or_create_user_supplied_id(charge_params)
|
233
|
+
end
|
234
|
+
|
235
|
+
|
236
|
+
def self.has_valid_code?(charge_params)
|
237
|
+
code = (charge_params.respond_to? :keys) ? self.get_code(charge_params) : false
|
238
|
+
code && self.validate_code!(code)
|
239
|
+
end
|
240
|
+
|
241
|
+
def self.has_valid_card_id?(charge_params)
|
242
|
+
cardId = (charge_params.respond_to? :keys) ? self.get_card_id(charge_params) : false
|
243
|
+
cardId && self.validate_card_id!(cardId)
|
244
|
+
end
|
245
|
+
|
246
|
+
def self.has_valid_contact_id?(charge_params)
|
247
|
+
contactId = (charge_params.respond_to? :keys) ? self.get_contact_id(charge_params) : false
|
248
|
+
contactId && self.validate_contact_id!(contactId)
|
249
|
+
end
|
250
|
+
|
251
|
+
def self.has_valid_shopper_id?(charge_params)
|
252
|
+
shopperId = (charge_params.respond_to? :keys) ? self.get_shopper_id(charge_params) : false
|
253
|
+
shopperId && self.validate_shopper_id!(shopperId)
|
254
|
+
end
|
255
|
+
|
256
|
+
def self.has_valid_or_empty_shopper_id?(charge_params)
|
257
|
+
shopperId = (charge_params.respond_to? :keys) ? self.get_shopper_id(charge_params) : false
|
258
|
+
shopperId && (shopperId === '' || self.validate_shopper_id!(shopperId))
|
259
|
+
end
|
260
|
+
|
261
|
+
def self.has_valid_user_supplied_id?(params)
|
262
|
+
userSuppliedId = (params.respond_to? :keys) ? self.get_user_supplied_id(params) : false
|
263
|
+
userSuppliedId && self.validate_user_supplied_id!(userSuppliedId)
|
264
|
+
end
|
265
|
+
|
266
|
+
def self.has_valid_transaction_id?(charge_params)
|
267
|
+
transactionId = (charge_params.respond_to? :keys) ? self.get_transaction_id(charge_params) : false
|
268
|
+
transactionId && self.validate_transaction_id!(transactionId)
|
269
|
+
end
|
270
|
+
|
271
|
+
|
272
|
+
def self.get_card_id(charge_params)
|
273
|
+
card_id_key = (charge_params.keys & Lightrail::Constants::LIGHTRAIL_CARD_ID_KEYS).first
|
274
|
+
charge_params[card_id_key]
|
275
|
+
end
|
276
|
+
|
277
|
+
def self.get_code(charge_params)
|
278
|
+
code_key = (charge_params.keys & Lightrail::Constants::LIGHTRAIL_CODE_KEYS).first
|
279
|
+
charge_params[code_key]
|
280
|
+
end
|
281
|
+
|
282
|
+
def self.get_contact_id(charge_params)
|
283
|
+
contact_id_key = (charge_params.keys & Lightrail::Constants::LIGHTRAIL_CONTACT_ID_KEYS).first
|
284
|
+
charge_params[contact_id_key]
|
285
|
+
end
|
286
|
+
|
287
|
+
def self.get_shopper_id(charge_params)
|
288
|
+
shopper_id_key = (charge_params.keys & Lightrail::Constants::LIGHTRAIL_SHOPPER_ID_KEYS).first
|
289
|
+
charge_params[shopper_id_key]
|
290
|
+
end
|
291
|
+
|
292
|
+
def self.get_transaction_id(charge_params)
|
293
|
+
transaction_id_key = (charge_params.keys & Lightrail::Constants::LIGHTRAIL_TRANSACTION_ID_KEYS).first
|
294
|
+
charge_params[transaction_id_key]
|
295
|
+
end
|
296
|
+
|
297
|
+
def self.get_code_or_card_id_key(charge_params)
|
298
|
+
(charge_params.keys & Lightrail::Constants::LIGHTRAIL_PAYMENT_METHODS).first
|
299
|
+
end
|
300
|
+
|
301
|
+
def self.get_user_supplied_id(params)
|
302
|
+
user_supplied_id_key = (params.keys & Lightrail::Constants::LIGHTRAIL_USER_SUPPLIED_ID_KEYS).first
|
303
|
+
params[user_supplied_id_key]
|
304
|
+
end
|
305
|
+
|
306
|
+
def self.get_or_create_user_supplied_id(charge_params)
|
307
|
+
user_supplied_id_key = (charge_params.keys & Lightrail::Constants::LIGHTRAIL_USER_SUPPLIED_ID_KEYS).first
|
308
|
+
charge_params[user_supplied_id_key] || SecureRandom::uuid
|
309
|
+
end
|
310
|
+
|
311
|
+
def self.get_or_create_user_supplied_id_with_action_suffix(charge_params, new_user_supplied_id_base, action_suffix)
|
312
|
+
user_supplied_id_key = (charge_params.keys & Lightrail::Constants::LIGHTRAIL_USER_SUPPLIED_ID_KEYS).first
|
313
|
+
charge_params[user_supplied_id_key] || "#{new_user_supplied_id_base}-#{action_suffix}"
|
314
|
+
end
|
315
|
+
|
316
|
+
end
|
317
|
+
end
|