-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthorize_helper.rb
136 lines (118 loc) · 3.96 KB
/
authorize_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def load_merchant_variables
if ENV['CLOUD'] == "Heroku"
load_merchant_from_env
else
load_merchant_from_yml
end
load_gateway
end
def load_merchant_from_yml
# LOAD the Authorize.net api credentials.
credentials = YAML.load_file(File.dirname(__FILE__) + "/config/credentials.yml")
@gateway = credentials['authorize_api_gateway']
if @merchant == "BAR"
@merchant_credentials_loaded = true
@api_login_id = credentials['authorize_api_id_bar']
@api_transaction_key = credentials['authorize_api_key_bar']
elsif @merchant == "PTD"
@merchant_credentials_loaded = true
@api_login_id = credentials['authorize_api_id_ptd']
@api_transaction_key = credentials['authorize_api_key_ptd']
else
@merchant_credentials_loaded = false
end
end
def load_merchant_from_env
@gateway = ENV['AUTHORIZE_API_GATEWAY']
if @merchant == "BAR"
@merchant_credentials_loaded = true
@api_login_id = ENV['AUTHORIZE_API_ID_BAR']
@api_transaction_key = ENV['AUTHORIZE_API_KEY_BAR']
elsif @merchant == "PTD"
@merchant_credentials_loaded = true
@api_login_id = ENV['AUTHORIZE_API_ID_PTD']
@api_transaction_key = ENV['AUTHORIZE_API_KEY_PTD']
else
@merchant_credentials_loaded = false
end
end
def load_gateway
if @gateway == "production"
@merchant_gateway_loaded = true
@gateway = {:gateway => :production}
elsif @gateway == "sandbox"
@merchant_gateway_loaded = true
@gateway = {:gateway => :sandbox, :verify_ssl => true}
else
@merchant_gateway_loaded = false
@gateway = nil
end
end
def transaction_ready
load_merchant_variables
if @merchant_credentials_loaded == true && @merchant_gateway_loaded == true && @api_login_id != nil && @api_transaction_key != nil && @gateway != nil
true
else
transaction_not_ready
false
end
end
def transaction_not_ready
@result = "ERROR"
@status_code = 99
@status_message = "[ERROR] Merchant variables are missing."
copy_status_variables_to_response_variables
@return_json_package = JSON.generate ["result"=>@result,"status_code"=>@status_code,"status_message"=>@status_message][0]
end
def transaction_ok
begin # This ensures that a response was received before proceeding.
if @authorize_response.messages.resultCode == MessageTypeEnum::Ok
@result = "OK"
true
else
transaction_error
false
end
rescue NoMethodError, Errno::ETIMEDOUT => e
transaction_failure
false
end
end
def transaction_error
@result = "ERROR"
@authorize_response_code = @authorize_response.messages.messages[0].code
@authorize_response_message = @authorize_response.messages.messages[0].text
clean_authorize_response_message
end
def transaction_failure
@result = "FAILURE"
@status_code = 98
@status_message = "[ERROR] A transactional FAILURE occurred."
copy_status_variables_to_response_variables
@return_json_package = JSON.generate ["result"=>@result,"status_code"=>@status_code,"status_message"=>@status_message,"authorize_response_code"=>@authorize_response_message,"authorize_response_message"=>@authorize_response_message][0]
end
def copy_status_variables_to_response_variables
# Because a response was NOT received, this ensures that the records are marked accordingly.
@authorize_response_code = @status_code
@authorize_response_message = @status_message
end
def transaction_payment_ok
@authorize_response_code = @authorize_response.messages.messages[0].code
@authorize_response_message = @authorize_response.messages.messages[0].text
clean_authorize_response_message
end
def transaction_payment_error
if @authorize_response.transactionResponse != nil
@authorize_response_code = @authorize_response.transactionResponse.errors.errors[0].errorCode
@authorize_response_message = @authorize_response.transactionResponse.errors.errors[0].errorText
clean_authorize_response_message
else
@result = "ERROR"
@authorize_response_kind = "TransactionError"
@authorize_response_code = "010101"
@authorize_response_message = "No response from Authorize"
end
end
def clean_authorize_response_message
@authorize_response_message.sub! "(TESTMODE) ", ""
end