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_bank(params, auth_code) ⇒ Object



126
127
128
129
130
# File 'lib/active_merchant/billing/gateways/priority.rb', line 126

def add_bank(params, auth_code)
  params['authCode'] = auth_code
  params['authOnly'] = false
  params['availableAuthAmount'] = 0
end

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



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
157
158
159
160
161
162
163
# File 'lib/active_merchant/billing/gateways/priority.rb', line 132

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]
    card_details['avsZip'] =  options[:billing_address][:zip]
  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



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

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

  pos_options
end

#add_purchases_data(options) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/active_merchant/billing/gateways/priority.rb', line 348

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



373
374
375
376
377
378
379
380
381
382
383
# File 'lib/active_merchant/billing/gateways/priority.rb', line 373

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/active_merchant/billing/gateways/priority.rb', line 173

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]
  params['taxExempt'] = false
  params['tenderType'] = options[:tender_type]
end

#add_type_merchant_refund(params, options) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/active_merchant/billing/gateways/priority.rb', line 194

def add_type_merchant_refund(params, options)
  params['cardPresent'] = options[:card_present]
  params['clientReference'] = options[:client_ref]
  params['created'] = options[:created]
  params['creatorName'] = options[:creator_name]
  params['currency'] = options[:currency]
  params['customerCode'] = options[:customer_code]
  params['enteredAmount'] = options[:amount]
  params['id'] = 0
  params['invoice'] = options[:invoice]
  params['isDuplicate'] = false
  params['merchantId'] = @options[:merchant_id]
  params['paymentToken'] = options[:payment_token]

  params['posData'] = add_pos_data(options[:pos_data]) if options[:pos_data]

  params['purchases'] = add_purchases_data(options[:purchases][0]) if options[:purchases]

  params['reference'] = options[:reference]
  params['requireSignature'] = false

  params['risk'] = add_risk_data(options[:risk]) if options[:risk]

  params['settledAmount'] = options[:settled_amt]
  params['settledCurrency'] = options[:settled_currency]
  params['settledDate'] = options[:created]
  params['shipToCountry'] = options[:ship_to_country]
  params['shouldGetCreditCardLevel'] = options[:should_get_credit_card_level]
  params['source'] = options[:source]
  params['status'] = options[:status]
  params['tax'] = options[:tax]
  params['taxExempt'] = options[:tax_exempt]
  params['tenderType'] = options[:tender_type]
  params['type'] = options[:type]
end

#authorization_from(response) ⇒ Object



326
327
328
329
330
331
# File 'lib/active_merchant/billing/gateways/priority.rb', line 326

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



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

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



297
298
299
# File 'lib/active_merchant/billing/gateways/priority.rb', line 297

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

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



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

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'] = options[:tender_type]

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

#close_batch(batch_id, options) ⇒ Object



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

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

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



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/active_merchant/billing/gateways/priority.rb', line 230

def commit(action, params: '', iid: '', card_number: nil, jwt: '')
  response =
    begin
      case action
      when 'void'
        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'
        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(success, response),
    response,
    authorization: success && response['code'] != '204' ? authorization_from(response) : nil,
    error_code: success || response['code'] == '204' || response == '' ? nil : error_from(response),
    test: test?
  )
end

#create_jwt(options) ⇒ Object



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

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

#error_from(response) ⇒ Object



333
334
335
# File 'lib/active_merchant/billing/gateways/priority.rb', line 333

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

#exp_date(credit_card) ⇒ Object



165
166
167
# File 'lib/active_merchant/billing/gateways/priority.rb', line 165

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

#get_hash(string) ⇒ Object



385
386
387
# File 'lib/active_merchant/billing/gateways/priority.rb', line 385

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

#get_payment_status(batch_id, options) ⇒ Object



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

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

#handle_response(response) ⇒ Object



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

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



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

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

#message_from(succeeded, response) ⇒ Object



318
319
320
321
322
323
324
# File 'lib/active_merchant/billing/gateways/priority.rb', line 318

def message_from(succeeded, response)
  if succeeded
    response['status']
  else
    response['authMessage']
  end
end

#parse(body) ⇒ Object



301
302
303
304
305
306
307
308
309
# File 'lib/active_merchant/billing/gateways/priority.rb', line 301

def parse(body)
  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



337
338
339
# File 'lib/active_merchant/billing/gateways/priority.rb', line 337

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



169
170
171
# File 'lib/active_merchant/billing/gateways/priority.rb', line 169

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

#refund(amount, credit_card, options) ⇒ Object



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

def refund(amount, credit_card, options)
  params = {}
  # refund amounts must be negative
  params['amount'] = ('-' + localized_amount(amount.to_f, options[:currency])).to_f
  add_bank(params, options[:auth_code])
  add_credit_card(params, credit_card, 'refund', options) unless options[:auth_code]
  add_type_merchant_refund(params, options)
  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



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

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



311
312
313
314
315
316
# File 'lib/active_merchant/billing/gateways/priority.rb', line 311

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)


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

def supports_scrubbing?
  true
end

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



270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/active_merchant/billing/gateways/priority.rb', line 270

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



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

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

#verify_urlObject



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

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

#void(authorization, options) ⇒ Object



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

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