Class: ActiveMerchant::Billing::PriorityGateway

Inherits:
Gateway
  • Object
show all
Defined in:
lib/active_merchant/billing/gateways/priority.rb

Constant Summary

Constants inherited from Gateway

Gateway::CREDIT_DEPRECATION_MESSAGE, Gateway::RECURRING_DEPRECATION_MESSAGE, Gateway::STANDARD_ERROR_CODE

Instance Attribute Summary

Attributes inherited from Gateway

#options

Instance Method Summary collapse

Methods inherited from Gateway

#add_field_to_post_if_present, #add_fields_to_post_if_present, card_brand, #card_brand, #generate_unique_id, inherited, supported_countries, #supported_countries, supported_countries=, supports?, #supports_network_tokenization?, #test?

Methods included from CreditCardFormatting

#expdate, #format

Methods included from PostsData

included, #raw_ssl_request, #ssl_get, #ssl_post, #ssl_request

Constructor Details

#initialize(options = {}) ⇒ PriorityGateway

Returns a new instance of PriorityGateway.



29
30
31
32
# File 'lib/active_merchant/billing/gateways/priority.rb', line 29

def initialize(options = {})
  requires!(options, :merchant_id, :key, :secret)
  super
end

Instance Method Details

#add_credit_card(params, credit_card, action, options) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/active_merchant/billing/gateways/priority.rb', line 125

def add_credit_card(params, credit_card, action, options)
  return unless credit_card&.is_a?(CreditCard)

  card_details = {}

  card_details['expiryMonth'] = format(credit_card.month, :two_digits).to_s
  card_details['expiryYear'] = format(credit_card.year, :two_digits).to_s
  card_details['expiryDate'] = exp_date(credit_card)
  card_details['cardType'] = credit_card.brand
  card_details['last4'] = credit_card.last_digits
  card_details['cvv'] = credit_card.verification_value
  card_details['number'] = credit_card.number

  card_details['entryMode'] = options['entryMode'].blank? ? 'Keyed' : options['entryMode']

  case action
  when 'purchase'
    card_details['avsStreet'] = options[:billing_address][:address1] if options[:billing_address]
    card_details['avsZip'] =  options[:billing_address][:zip] if options[:billing_address]
  when 'refund'
    card_details['cardId'] = options[:card_id]
    card_details['cardPresent'] = options[:card_present]
    card_details['hasContract'] = options[:has_contract]
    card_details['isCorp'] = options[:is_corp]
    card_details['isDebit'] = options[:is_debit]
    card_details['token'] = options[:token]
  else
    card_details
  end

  params['cardAccount'] = card_details
end

#add_pos_data(options) ⇒ Object



298
299
300
301
302
303
# File 'lib/active_merchant/billing/gateways/priority.rb', line 298

def add_pos_data(options)
  pos_options = {}
  pos_options['panCaptureMethod'] = options[:pan_capture_method]

  pos_options
end

#add_purchases_data(options) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/active_merchant/billing/gateways/priority.rb', line 305

def add_purchases_data(options)
  purchases = {}

  purchases['dateCreated'] = options[:date_created]
  purchases['iId'] = options[:i_id]
  purchases['transactionIId'] = options[:transaction_i_id]
  purchases['transactionId'] = options[:transaction_id]
  purchases['name'] = options[:name]
  purchases['description'] = options[:description]
  purchases['code'] = options[:code]
  purchases['unitOfMeasure'] = options[:unit_of_measure]
  purchases['unitPrice'] = options[:unit_price]
  purchases['quantity'] = options[:quantity]
  purchases['taxRate'] = options[:tax_rate]
  purchases['taxAmount'] = options[:tax_amount]
  purchases['discountRate'] = options[:discount_rate]
  purchases['discountAmount'] = options[:discount_amt]
  purchases['extendedAmount'] = options[:extended_amt]
  purchases['lineItemId'] = options[:line_item_id]

  purchase_arr = []
  purchase_arr[0] = purchases
  purchase_arr
end

#add_risk_data(options) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/active_merchant/billing/gateways/priority.rb', line 330

def add_risk_data(options)
  risk = {}
  risk['cvvResponseCode'] = options[:cvv_response_code]
  risk['cvvResponse'] = options[:cvv_response]
  risk['cvvMatch'] = options[:cvv_match]
  risk['avsResponse'] = options[:avs_response]
  risk['avsAddressMatch'] = options[:avs_address_match]
  risk['avsZipMatch'] = options[:avs_zip_match]

  risk
end

#add_type_merchant_purchase(params, merchant, is_settle_funds, options) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/active_merchant/billing/gateways/priority.rb', line 166

def add_type_merchant_purchase(params, merchant, is_settle_funds, options)
  params['cardPresent'] = false
  params['cardPresentType'] = 'CardNotPresent'
  params['isAuth'] = true
  params['isSettleFunds'] = is_settle_funds
  params['isTicket'] = false

  params['merchantId'] = merchant
  params['mxAdvantageEnabled'] = false
  params['paymentType'] = 'Sale'

  params['purchases'] = purchases

  params['shouldGetCreditCardLevel'] = true
  params['shouldVaultCard'] = true
  params['source'] = options[:source]
  params['sourceZip'] = options[:billing_address][:zip] if options[:billing_address]
  params['taxExempt'] = false
  params['tenderType'] = 'Card'
end

#authorization_from(response) ⇒ Object



283
284
285
286
287
288
# File 'lib/active_merchant/billing/gateways/priority.rb', line 283

def authorization_from(response)
  {
    'payment_token' => response['paymentToken'],
    'id' => response['id']
  }
end

#authorize(amount, credit_card, options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/active_merchant/billing/gateways/priority.rb', line 61

def authorize(amount, credit_card, options = {})
  params = {}
  params['amount'] = localized_amount(amount.to_f, options[:currency])
  params['authOnly'] = true

  add_credit_card(params, credit_card, 'purchase', options)
  add_type_merchant_purchase(params, @options[:merchant_id], false, options)
  commit('purchase', params: params, jwt: options)
end

#base_urlObject



242
243
244
# File 'lib/active_merchant/billing/gateways/priority.rb', line 242

def base_url
  test? ? test_url : live_url
end

#basic_authObject



34
35
36
# File 'lib/active_merchant/billing/gateways/priority.rb', line 34

def basic_auth
  Base64.strict_encode64("#{@options[:key]}:#{@options[:secret]}")
end

#batch_urlObject



254
255
256
# File 'lib/active_merchant/billing/gateways/priority.rb', line 254

def batch_url
  test? ? self.test_url_batch : self.live_url_batch
end

#capture(amount, authorization, options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/active_merchant/billing/gateways/priority.rb', line 80

def capture(amount, authorization, options = {})
  params = {}
  params['amount'] = localized_amount(amount.to_f, options[:currency])
  params['authCode'] = options[:authCode]
  params['merchantId'] = @options[:merchant_id]
  params['paymentToken'] = get_hash(authorization)['payment_token']
  params['shouldGetCreditCardLevel'] = true
  params['source'] = options[:source]
  params['tenderType'] = 'Card'

  commit('capture', params: params, jwt: options)
end

#close_batch(batch_id, options) ⇒ Object



110
111
112
# File 'lib/active_merchant/billing/gateways/priority.rb', line 110

def close_batch(batch_id, options)
  commit('close_batch', params: batch_id, jwt: options)
end

#commit(action, params: '', iid: '', card_number: nil, jwt: '') ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/active_merchant/billing/gateways/priority.rb', line 187

def commit(action, params: '', iid: '', card_number: nil, jwt: '')
  response =
    begin
      case action
      when 'void'
        parse(ssl_request(:delete, url(action, params, ref_number: iid), nil, request_headers))
      when 'verify'
        parse(ssl_get(url(action, params, credit_card_number: card_number), request_verify_headers(jwt)))
      when 'get_payment_status', 'create_jwt'
        parse(ssl_get(url(action, params, ref_number: iid), request_headers))
      when 'close_batch'
        parse(ssl_request(:put, url(action, params, ref_number: iid), nil, request_headers))
      else
        parse(ssl_post(url(action, params), post_data(params), request_headers))
      end
    rescue ResponseError => e
      parse(e.response.body)
    end
  success = success_from(response, action)
  response = { 'code' => '204' } if response == ''
  Response.new(
    success,
    message_from(response),
    response,
    authorization: success ? authorization_from(response) : nil,
    error_code: success || response == '' ? nil : error_from(response),
    test: test?
  )
end

#create_jwt(options) ⇒ Object



114
115
116
# File 'lib/active_merchant/billing/gateways/priority.rb', line 114

def create_jwt(options)
  commit('create_jwt', params: @options[:merchant_id], jwt: options)
end

#error_from(response) ⇒ Object



290
291
292
# File 'lib/active_merchant/billing/gateways/priority.rb', line 290

def error_from(response)
  response['errorCode'] || response['status']
end

#exp_date(credit_card) ⇒ Object



158
159
160
# File 'lib/active_merchant/billing/gateways/priority.rb', line 158

def exp_date(credit_card)
  "#{format(credit_card.month, :two_digits)}/#{format(credit_card.year, :two_digits)}"
end

#get_hash(string) ⇒ Object



342
343
344
# File 'lib/active_merchant/billing/gateways/priority.rb', line 342

def get_hash(string)
  JSON.parse(string.gsub('=>', ':'))
end

#get_payment_status(batch_id, options) ⇒ Object



106
107
108
# File 'lib/active_merchant/billing/gateways/priority.rb', line 106

def get_payment_status(batch_id, options)
  commit('get_payment_status', params: batch_id, jwt: options)
end

#handle_response(response) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/active_merchant/billing/gateways/priority.rb', line 217

def handle_response(response)
  if response.code != '204' && (200...300).cover?(response.code.to_i)
    response.body
  elsif response.code == '204' || response == ''
    response.body = { 'code' => '204' }
  else
    raise ResponseError.new(response)
  end
end

#jwt_urlObject



250
251
252
# File 'lib/active_merchant/billing/gateways/priority.rb', line 250

def jwt_url
  test? ? self.test_url_jwt : self.live_url_jwt
end

#message_from(response) ⇒ Object



277
278
279
280
281
# File 'lib/active_merchant/billing/gateways/priority.rb', line 277

def message_from(response)
  return response['details'][0] if response['details'] && response['details'][0]

  response['authMessage'] || response['message'] || response['status']
end

#parse(body) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/active_merchant/billing/gateways/priority.rb', line 258

def parse(body)
  return body if body['code'] == '204'

  JSON.parse(body)
rescue JSON::ParserError
  message = 'Invalid JSON response received from Priority Gateway. Please contact Priority Gateway if you continue to receive this message.'
  message += " (The raw response returned by the API was #{body.inspect})"
  {
    'message' => message
  }
end

#post_data(params) ⇒ Object



294
295
296
# File 'lib/active_merchant/billing/gateways/priority.rb', line 294

def post_data(params)
  params.to_json
end

#purchase(amount, credit_card, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/active_merchant/billing/gateways/priority.rb', line 51

def purchase(amount, credit_card, options = {})
  params = {}
  params['amount'] = localized_amount(amount.to_f, options[:currency])
  params['authOnly'] = false

  add_credit_card(params, credit_card, 'purchase', options)
  add_type_merchant_purchase(params, @options[:merchant_id], true, options)
  commit('purchase', params: params, jwt: options)
end

#purchasesObject



162
163
164
# File 'lib/active_merchant/billing/gateways/priority.rb', line 162

def purchases
  [{ taxRate: '0.0000', additionalTaxRate: nil, discountRate: nil }]
end

#refund(amount, authorization, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/active_merchant/billing/gateways/priority.rb', line 71

def refund(amount, authorization, options = {})
  params = {}
  params['merchantId'] = @options[:merchant_id]
  params['paymentToken'] = get_hash(authorization)['payment_token'] || options[:payment_token]
  # refund amounts must be negative
  params['amount'] = ('-' + localized_amount(amount.to_f, options[:currency])).to_f
  commit('refund', params: params, jwt: options)
end

#request_headersObject



38
39
40
41
42
43
# File 'lib/active_merchant/billing/gateways/priority.rb', line 38

def request_headers
  {
    'Content-Type' => 'application/json',
    'Authorization' => "Basic #{basic_auth}"
  }
end

#request_verify_headers(jwt) ⇒ Object



45
46
47
48
49
# File 'lib/active_merchant/billing/gateways/priority.rb', line 45

def request_verify_headers(jwt)
  {
    'Authorization' => "Bearer #{jwt}"
  }
end

#scrub(transcript) ⇒ Object



118
119
120
121
122
123
# File 'lib/active_merchant/billing/gateways/priority.rb', line 118

def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
    gsub(%r((number\\?"\s*:\s*\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r((cvv\\?"\s*:\s*\\?")[^"]*)i, '\1[FILTERED]')
end

#success_from(response, action) ⇒ Object



270
271
272
273
274
275
# File 'lib/active_merchant/billing/gateways/priority.rb', line 270

def success_from(response, action)
  success = response['status'] == 'Approved' || response['status'] == 'Open' if response['status']
  success = response['code'] == '204' if action == 'void'
  success = !response['bank'].empty? if action == 'verify' && response['bank']
  success
end

#supports_scrubbing?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/active_merchant/billing/gateways/priority.rb', line 102

def supports_scrubbing?
  true
end

#url(action, params, ref_number: '', credit_card_number: nil) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/active_merchant/billing/gateways/priority.rb', line 227

def url(action, params, ref_number: '', credit_card_number: nil)
  case action
  when 'void'
    base_url + "/#{ref_number}?force=true"
  when 'verify'
    (verify_url + '?search=') + credit_card_number.to_s[0..6]
  when 'get_payment_status', 'close_batch'
    batch_url + "/#{params}"
  when 'create_jwt'
    jwt_url + "/#{params}/token"
  else
    base_url + '?includeCustomerMatches=false&echo=true'
  end
end

#verify(credit_card, options) ⇒ Object



97
98
99
100
# File 'lib/active_merchant/billing/gateways/priority.rb', line 97

def verify(credit_card, options)
  jwt = options[:jwt_token]
  commit('verify', card_number: credit_card.number, jwt: jwt)
end

#verify_urlObject



246
247
248
# File 'lib/active_merchant/billing/gateways/priority.rb', line 246

def verify_url
  test? ? self.test_url_verify : self.live_url_verify
end

#void(authorization, options = {}) ⇒ Object



93
94
95
# File 'lib/active_merchant/billing/gateways/priority.rb', line 93

def void(authorization, options = {})
  commit('void', iid: get_hash(authorization)['id'], jwt: options)
end