Class: Hps::HpsChargeService

Inherits:
HpsService show all
Defined in:
lib/hps/services/hps_charge_service.rb

Instance Attribute Summary

Attributes inherited from HpsService

#exception_mapper

Instance Method Summary collapse

Methods inherited from HpsService

#doTransaction, #gateway_url_for_key, #hydrate_transaction_header, #initialize

Constructor Details

This class inherits a constructor from Hps::HpsService

Instance Method Details

#authorize(amount, currency, card, card_holder = nil, request_multi_use_token = false, details = nil, txn_descriptor = nil) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/hps/services/hps_charge_service.rb', line 251

def authorize(amount, currency, card, card_holder = nil, request_multi_use_token = false, details = nil, txn_descriptor = nil)

  check_amount(amount)
  check_currency(currency)

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :CreditAuth do
      xml.hps :Block1 do
        xml.hps :AllowDup, "Y"
        xml.hps :Amt, amount
        xml << hydrate_cardholder_data(card_holder) if card_holder
        xml << hydrate_additional_txn_fields(details) if details
        xml.hps :TxnDescriptor, txn_descriptor if txn_descriptor
        xml.hps :CardData do

          # NOTE: Process as Manual Entry if they gave us a Credit Card
          if card.is_a? HpsCreditCard
            xml << hydrate_manual_entry(card)
          # Note: Otherwise, consider it a token
          else
            xml.hps :TokenData do
              xml.hps :TokenValue, card
            end
          end

          xml.hps :TokenRequest, request_multi_use_token ? "Y" : "N"

        end
      end
    end
  end

  submit_authorize(xml.target!, amount, currency)
end

#authorize_swipe(amount, currency, track_data, encryption_data = nil, gratuity = 0, allow_partial_auth = false, txn_descriptor = nil) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/hps/services/hps_charge_service.rb', line 287

def authorize_swipe(amount, currency, track_data, encryption_data = nil, gratuity = 0, allow_partial_auth = false, txn_descriptor = nil)
  check_amount(amount)
  check_currency(currency)

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :CreditAuth do
      xml.hps :Block1 do
        xml.hps :AllowDup, "Y"
        xml.hps :Amt, amount
        xml.hps :TxnDescriptor, txn_descriptor if txn_descriptor
        xml.hps :AllowPartialAuth, allow_partial_auth ? "Y" : "N"
        xml.hps :CardData do
          xml << hydrate_card_track_data(track_data)
          xml << hydrate_encryption_data(encryption_data) if encryption_data
        end
      end
    end
  end

  submit_authorize(xml.target!, amount, currency)
end

#capture(transaction_id, amount = nil) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/hps/services/hps_charge_service.rb', line 310

def capture(transaction_id, amount = nil)

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :CreditAddToBatch do
      xml.hps :GatewayTxnId, transaction_id
      xml.hps :Amt, amount if amount
    end
  end

  response = doTransaction(xml.target!)
  header = response["Header"]

  raise  @exception_mapper.map_gateway_exception(transaction_id, header["GatewayRspCode"], header["GatewayRspMsg"]) unless header["GatewayRspCode"].eql? "0"

  get(transaction_id)
end

#charge(amount, currency, card, card_holder = nil, request_multi_use_token = false, details = nil, txn_descriptor = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/hps/services/hps_charge_service.rb', line 141

def charge(amount, currency, card, card_holder = nil, request_multi_use_token = false, details = nil, txn_descriptor = nil)
  check_amount(amount)
  check_currency(currency)

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :CreditSale do
      xml.hps :Block1 do
        xml.hps :AllowDup, "Y"
        xml.hps :Amt, amount
        xml << hydrate_cardholder_data(card_holder) if card_holder
        xml << hydrate_additional_txn_fields(details) if details
        xml.hps :TxnDescriptor, txn_descriptor if txn_descriptor
        xml.hps :CardData do

          # NOTE: Process as Manual Entry if they gave us a Credit Card
          if card.is_a? HpsCreditCard
            xml << hydrate_manual_entry(card)
          # Note: Otherwise, consider it a token
          else
            xml.hps :TokenData do
              xml.hps :TokenValue, card
            end
          end

          xml.hps :TokenRequest, request_multi_use_token ? "Y" : "N"

        end
      end
    end
  end

  submit_charge(xml.target!, amount, currency)
end

#charge_swipe(amount, currency, track_data, encryption_data = nil, gratuity = 0, allow_partial_auth = false, txn_descriptor = nil, request_multi_use_token = false, direct_market_data = nil) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/hps/services/hps_charge_service.rb', line 176

def charge_swipe(amount, currency, track_data, encryption_data = nil, gratuity = 0, allow_partial_auth = false, txn_descriptor = nil, request_multi_use_token = false, direct_market_data = nil)
  check_amount(amount)
  check_currency(currency)

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :CreditSale do
      xml.hps :Block1 do
        xml.hps :AllowDup, "Y"
        xml.hps :Amt, amount
        xml.hps :GratuityAmtInfo, gratuity if gratuity != 0
        xml.hps :TxnDescriptor, txn_descriptor if txn_descriptor
        xml.hps :AllowPartialAuth, allow_partial_auth ? "Y" : "N"
        xml.hps :CardData do
          xml << hydrate_card_track_data(track_data)
          xml << hydrate_encryption_data(encryption_data) if encryption_data
          xml.hps :TokenRequest, request_multi_use_token ? "Y" : "N"
        end
        xml << hydrate_direct_market_data(direct_market_data) if direct_market_data
      end
    end
  end

  submit_charge(xml.target!, amount, currency)
end

#get(transaction_id) ⇒ Object



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
# File 'lib/hps/services/hps_charge_service.rb', line 4

def get(transaction_id)

  if transaction_id.nil? or transaction_id == 0
    raise @exception_mapper.map_sdk_exception(SdkCodes.invalid_transaction_id)
  end

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :ReportTxnDetail do
      xml.hps :TxnId, transaction_id
    end
  end

  response = doTransaction(xml.target!)
  detail = response["Transaction"]["ReportTxnDetail"]

  header = hydrate_transaction_header(response["Header"])
  result = HpsReportTransactionDetails.new(header)
  result.transaction_id = detail["GatewayTxnId"]
  result.original_transaction_id = detail["OriginalGatewayTxnId"]
  result.authorized_amount = detail["Data"]["AuthAmt"]
  result.authorization_code = detail["Data"]["AuthCode"]
  result.avs_result_code = detail["Data"]["AVSRsltCode"]
  result.avs_result_text = detail["Data"]["AVSRsltText"]
  result.card_type = detail["Data"]["CardType"]
  result.masked_card_number = detail["Data"]["MaskedCardNbr"]
  result.transaction_type = Hps.service_name_to_transaction_type(detail["ServiceName"])
  result.transaction_date = detail["RspUtcDT"]
  result.cpc_indicator = detail["Data"]["CPCInd"]
  result.cvv_result_code = detail["Data"]["CVVRsltCode"]
  result.cvv_result_text = detail["Data"]["CVVRsltText"]
  result.reference_number = detail["Data"]["RefNbr"]
  result.response_code = detail["Data"]["RspCode"]
  result.response_text = detail["Data"]["RspText"]

  tokenization_message = detail["Data"]["TokenizationMsg"]

  unless tokenization_message.nil?
    result.token_data = HpsTokenData.new(tokenization_message)
  end

  header_response_code = response["Header"]["GatewayRspCode"]
  data_response_code = detail["Data"]["RspCode"]

  if header_response_code != "0" or data_response_code != "0" or data_response_code != "00"

    exceptions = HpsChargeExceptions.new()

    if header_response_code != "0"
      message = response["Header"]["GatewayRspMsg"]
      exceptions.hps_exception = @exception_mapper.map_gateway_exception(result.transaction_id, header_response_code, message)
    end

    if data_response_code != "0" || data_response_code != "00"
      message = detail["Data"]["RspText"]
      exceptions.card_exception = @exception_mapper.map_issuer_exception(transaction_id, data_response_code, message)
    end

    result.exceptions = exceptions

  end

  result

end

#list(start_date, end_date, filter_by = nil) ⇒ Object



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
137
138
139
# File 'lib/hps/services/hps_charge_service.rb', line 70

def list(start_date, end_date, filter_by = nil)

  if start_date > DateTime.now
    raise @exception_mapper.map_sdk_exception(SdkCodes.invalid_start_date)
  elsif end_date > DateTime.now
    raise @exception_mapper.map_sdk_exception(SdkCodes.invalid_end_date)
  end

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :ReportActivity do
      xml.hps :RptStartUtcDT, start_date.utc.iso8601
      xml.hps :RptEndUtcDT, end_date.utc.iso8601
    end
  end

  response = doTransaction(xml.target!)

  # Gateway exception
  if response["Header"]["GatewayRspCode"] != "0"
    transaction_id = response["Header"]["GatewayTxnId"]
    response_code = response["Header"]["GatewayRspCode"]
    response_message = response["Header"]["GatewayRspMsg"]
    raise @exception_mapper.map_gateway_exception(transaction_id, response_code, response_message)
  end

  result = Array.new

  if response["Transaction"]["ReportActivity"]["Header"]["TxnCnt"] == "0"
    return result
  end

  response["Transaction"]["ReportActivity"]["Details"].each { |charge|

    next if !filter_by.nil? and charge.serviceName != Hps.transaction_type_to_service_name(filter_by)

    summary = HpsReportTransactionSummary.new()
    summary.transaction_id = charge["GatewayTxnId"]
    summary.original_transaction_id = charge["OriginalGatewayTxnId"]
    summary.masked_card_number = charge["MaskedCardNbr"]
    summary.response_code = charge["IssuerRspCode"]
    summary.response_text = charge["IssuerRspText"]
    summary.transaction_type = Hps.transaction_type_to_service_name(charge["ServiceName"]) if filter_by.nil? == false

    gw_response_code = charge["GatewayRspCode"]
    issuer_response_code = charge["IssuerRspCode"]

    if gw_response_code != "0" or issuer_response_code != "0" or issuer_response_code != "00"

      exceptions = HpsChargeExceptions.new()

      if gw_response_code != "0"
        message = charge["GatewayRspMsg"]
        exceptions.hps_exception = @exception_mapper.map_gateway_exception(charge["GatewayTxnId"], gw_response_code, message)
      end

      if issuer_response_code != "0" || issuer_response_code != "00"
        message = charge["IssuerRspText"]
        exceptions.card_exception = @exception_mapper.map_issuer_exception(charge["GatewayTxnId"], issuer_response_code, message)
      end

      summary.exceptions = exceptions

    end

    result << summary
  }

  result
end

#refund(amount, currency, card, card_holder = nil, details = nil) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/hps/services/hps_charge_service.rb', line 376

def refund(amount, currency, card, card_holder = nil, details = nil)
  check_amount(amount)
  check_currency(currency)

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :CreditReturn do
      xml.hps :Block1 do
        xml.hps :AllowDup, "Y"
        xml.hps :Amt, amount
        xml << hydrate_cardholder_data(card_holder) if card_holder
        xml << hydrate_additional_txn_fields(details) if details
        xml.hps :CardData do

          # NOTE: Process as Manual Entry if they gave us a Credit Card
          if card.is_a? HpsCreditCard
            xml << hydrate_manual_entry(card)
          # Note: Otherwise, consider it a token
          else
            xml.hps :TokenData do
              xml.hps :TokenValue, card
            end
          end

        end
      end
    end
  end

  submit_refund(xml.target!)
end

#refund_transaction(amount, currency, transaction_id, card_holder = nil, details = nil) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/hps/services/hps_charge_service.rb', line 408

def refund_transaction(amount, currency, transaction_id, card_holder = nil, details = nil)
  check_amount(amount)
  check_currency(currency)

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :CreditReturn do
      xml.hps :Block1 do
        xml.hps :AllowDup, "Y"
        xml.hps :Amt, amount
        xml.hps :GatewayTxnId, transaction_id
        xml << hydrate_cardholder_data(card_holder) if card_holder
        xml << hydrate_additional_txn_fields(details) if details
      end
    end
  end

  submit_refund(xml.target!)
end

#reverse(card, amount, currency, details = nil) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/hps/services/hps_charge_service.rb', line 328

def reverse(card, amount, currency, details = nil)
  check_amount(amount)
  check_currency(currency)

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :CreditReversal do
      xml.hps :Block1 do
        xml.hps :Amt, amount
        xml << hydrate_additional_txn_fields(details) if details
        xml.hps :CardData do

          # NOTE: Process as Manual Entry if they gave us a Credit Card
          if card.is_a? HpsCreditCard
            xml << hydrate_manual_entry(card)
          # Note: Otherwise, consider it a token
          else
            xml.hps :TokenData do
              xml.hps :TokenValue, card
            end
          end

        end
      end
    end
  end

  submit_reverse(xml.target!)
end

#reverse_transaction(transaction_id, amount, currency, details = nil) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/hps/services/hps_charge_service.rb', line 358

def reverse_transaction(transaction_id, amount, currency, details = nil)
  check_amount(amount)
  check_currency(currency)

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :CreditReversal do
      xml.hps :Block1 do
        xml.hps :Amt, amount
        xml.hps :GatewayTxnId, transaction_id
        xml << hydrate_additional_txn_fields(details) if details
      end
    end
  end

  submit_reverse(xml.target!)
end

#update_token_expiration(token_value, exp_month, exp_year) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/hps/services/hps_charge_service.rb', line 439

def update_token_expiration(token_value, exp_month, exp_year)
  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :ManageTokens do
      xml.hps :TokenValue, token_value
      xml.hps :TokenActions do
        xml.hps :Set do
          xml.hps :Attribute do
            xml.hps :Name, "ExpMonth"
            xml.hps :Value, format('%02d', exp_month)
          end
          xml.hps :Attribute do
            xml.hps :Name, "ExpYear"
            xml.hps :Value, exp_year
          end
        end
      end
    end
  end

  submit_manage_tokens(xml.target!)
end

#verify(card, card_holder = nil, request_multi_use_token = false, client_txn_id = nil) ⇒ Object



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
229
# File 'lib/hps/services/hps_charge_service.rb', line 202

def verify(card, card_holder = nil, request_multi_use_token = false, client_txn_id = nil)

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :CreditAccountVerify do
      xml.hps :Block1 do
        xml << hydrate_cardholder_data(card_holder) if card_holder
        xml.hps :CardData do

          # NOTE: Process as Manual Entry if they gave us a Credit Card
          if card.is_a? HpsCreditCard
            xml << hydrate_manual_entry(card)
          # Note: Otherwise, consider it a token
          else
            xml.hps :TokenData do
              xml.hps :TokenValue, card
            end
          end

          xml.hps :TokenRequest, request_multi_use_token ? "Y" : "N"

        end
      end
    end
  end

  submit_verify(xml.target!)
end

#verify_swipe(track_data, card_holder = nil, encryption_data = nil, request_multi_use_token = false, client_txn_id = nil) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/hps/services/hps_charge_service.rb', line 231

def verify_swipe(track_data, card_holder = nil, encryption_data = nil, request_multi_use_token = false, client_txn_id = nil)

  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :CreditAccountVerify do
      xml.hps :Block1 do
        xml << hydrate_cardholder_data(card_holder) if card_holder
        xml.hps :CardData do
          xml << hydrate_card_track_data(track_data)
          xml << hydrate_encryption_data(encryption_data) if encryption_data

          xml.hps :TokenRequest, request_multi_use_token ? "Y" : "N"
        end
      end
    end
  end

  submit_verify(xml.target!)
end

#void(transaction_id) ⇒ Object



428
429
430
431
432
433
434
435
436
437
# File 'lib/hps/services/hps_charge_service.rb', line 428

def void(transaction_id)
  xml = Builder::XmlMarkup.new
  xml.hps :Transaction do
    xml.hps :CreditVoid do
      xml.hps :GatewayTxnId, transaction_id
    end
  end

  submit_void(xml.target!)
end