Completed
Push — master ( 330dad...b4ceb2 )
by
unknown
12s queued 10s
created

Transaction.custom_fields()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
module AuthorizeNet::CIM
2
  # The CIM transaction class.
3
  class Transaction < AuthorizeNet::XmlTransaction
4
    include AuthorizeNet::CIM::Fields
5
6
    # The class to wrap our response in.
7
    @response_class = AuthorizeNet::CIM::Response
8
9
    # The default options for the constructor.
10
    @@option_defaults = {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using a class variable like @@option_defaults is generally not recommended; did you consider
using an class instance variable instead?
Loading history...
11
      gateway: :production,
12
      verify_ssl: true,
13
      reference_id: nil
14
    }
15
16
    # Constructs a CIM transaction. You can use the new CIM transaction object
17
    # to issue a request to the payment gateway and parse the response into a new
18
    # AuthorizeNet::CIM::Response object.
19
    #
20
    # +api_login_id+:: Your API login ID, as a string.
21
    # +api_transaction_key+:: Your API transaction key, as a string.
22
    # +options+:: A hash of options. See below for values.
23
    #
24
    # Options
25
    # +gateway+:: The gateway to submit the transaction to. Can be a URL string, an AuthorizeNet::CIM::Transaction::Gateway constant, or one of the convenience symbols :sandbox, :test, :production, or :live (:test is an alias for :sandbox, and :live is an alias for :production).
26
    # +verify_ssl+:: A boolean indicating if the SSL certificate of the +gateway+ should be verified. Defaults to true.
27
    # +reference_id+:: A string that can be used to identify a particular transaction with its response. Will be echo'd in the response, only if it was provided in the transaction. Defaults to nil.
28
    #
29
    def initialize(api_login_id, api_transaction_key, options = {})
30
      ActiveSupport::Deprecation.warn "use AuthorizeNet::API::Transaction"
31
      super
32
      @delim_char = ','
33
      @encap_char = nil
34
      @custom_fields = {}
35
    end
36
37
    # The default options for create_profile.
38
    @@create_profile_option_defaults = {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using a class variable like @@create_profile_option_defaults is generally not recommended; did you consider
using an class instance variable instead?
Loading history...
39
      validation_mode: :none
40
    }
41
42
    # The default options for get_hosted_profile_token.
43
    @@get_hosted_profile_token_option_defaults = {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using a class variable like @@get_hosted_profile_token_option_defaults is generally not recommended; did you consider
using an class instance variable instead?
Loading history...
44
      border_visible: true,
45
      validation_mode: :none
46
    }
47
48
    # The default options for create_payment_profile.
49
    @@create_payment_profile_option_defaults = {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using a class variable like @@create_payment_profile_option_defaults is generally not recommended; did you consider
using an class instance variable instead?
Loading history...
50
      validation_mode: :none
51
    }
52
53
    # The default options for update_payment_profile.
54
    @@update_payment_profile_option_defaults = {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using a class variable like @@update_payment_profile_option_defaults is generally not recommended; did you consider
using an class instance variable instead?
Loading history...
55
      validation_mode: :none
56
    }
57
58
    # The default options for create_transaction and the various type specific create transaction methods.
59
    @@create_transaction_option_defaults = {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using a class variable like @@create_transaction_option_defaults is generally not recommended; did you consider
using an class instance variable instead?
Loading history...
60
      address_id: nil,
61
      split_tender_id: nil,
62
      aim_options: nil,
63
      custom_fields: nil
64
    }
65
66
    # The default options for validate_payment_profile.
67
    @@validate_payment_profile_option_defaults = {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using a class variable like @@validate_payment_profile_option_defaults is generally not recommended; did you consider
using an class instance variable instead?
Loading history...
68
      address_id: nil,
69
      card_code: nil,
70
      validation_mode: :testMode
71
    }
72
73
    # A list of keys that should be stored if passed as aim_options.
74
    @@aim_response_options = %i[delim_char encap_char]
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using a class variable like @@aim_response_options is generally not recommended; did you consider
using an class instance variable instead?
Loading history...
75
76
    # Sets up and submits a createCustomerProfileRequest transaction. If this transaction has already been
77
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object. The
78
    # response object will have a profile_id if the request was successful. If any PaymentProfiles or Addresses
79
    # were included, you can find their IDs in the response objects payment_profile_ids and address_ids methods.
80
    #
81
    # +profile+:: An AuthorizeNet::CIM::CustomerProfile object describing the profile to create.
82
    # +options+:: An optional hash of options.
83
    #
84
    # Options:
85
    # +validation_mode+:: Set to :testMode, :liveMode or :none (the default) to indicate what sort of PaymentProfile validation to do (assuming a PaymentProfile is included with the CustomerProfile)
86
    #
87
    # Typical usage:
88
    #
89
    #   profile = AuthorizeNet::CIM::CustomerProfile.new(
90
    #     :email => '[email protected]',
91
    #     :id => 'userassignedid'
92
    #   )
93
    #   response = transaction.create_profile(profile)
94
    #   puts response.profile_id if response.success?
95
    #
96 View Code Duplication
    def create_profile(profile, options = {})
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
97
      options = @@create_profile_option_defaults.merge(options)
98
      @type = Type::CIM_CREATE_PROFILE
99
      @fields.merge!(profile.to_hash)
100
      set_fields(validation_mode: options[:validation_mode])
101
      make_request
102
    end
103
104
    # Sets up and submits a getCustomerProfileRequest transaction. If this transaction has already been
105
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object. The
106
    # response object will have the CustomerProfile object requested available via its profile method if
107
    # the request was successful.
108
    #
109
    #
110
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile you want to retrieve, or a CustomerProfile object with the ID populated.
111
    # Typical usage:
112
    #
113
    #   response = transaction.get_profile('123456')
114
    #   profile = response.profile if response.success?
115
    #
116
    def get_profile(profile_id)
117
      @type = Type::CIM_GET_PROFILE
118
      handle_profile_id(profile_id)
119
      make_request
120
    end
121
122
    # Sets up and submits a updateCustomerProfileRequest transaction. If this transaction has already been
123
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object. Note that
124
    # any PaymentProfiles will NOT be updated using this method. This is a limitation of the CIM API. See
125
    # update_payment_profile if you need to update a PaymentProfile.
126
    #
127
    #
128
    # +profile+:: An AuthorizeNet::CIM::CustomerProfile object describing the profile to update. It must contain the customer_profile_id of the record to update.
129
    # Typical usage:
130
    #
131
    #   profile.fax = '5555551234'
132
    #   response = transaction.update_profile(profile)
133
    #   puts response.success?
134
    #
135
    def update_profile(profile)
136
      @type = Type::CIM_UPDATE_PROFILE
137
      @fields.merge!(profile.to_hash)
138
      make_request
139
    end
140
141
    # Sets up and submits a deleteCustomerProfileRequest transaction. If this transaction has already been
142
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object.
143
    #
144
    #
145
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile you want to delete, or a CustomerProfile object with the ID populated.
146
    # Typical usage:
147
    #
148
    #   response = transaction.delete_profile('123456')
149
    #   puts response.success?
150
    #
151
    def delete_profile(profile_id)
152
      @type = Type::CIM_DELETE_PROFILE
153
      handle_profile_id(profile_id)
154
      make_request
155
    end
156
157
    # Sets up and submits a getHostedProfilePageRequest transaction. If this transaction has already been
158
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object.
159
    #
160
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile you want to delete, or a CustomerProfile object with the ID populated.
161
    #
162
    # Options:
163
    # +return_url+::  Enter the URL for the page that the customer returns to when the hosted session ends. Do not pass this setting for iframes or popups.
164
    # +return_url_text+:: Enter the text to display on the button that returns the customer to your web site. The value can be any text up to 200 characters.
165
    #                     If you do not pass this parameter, the default button text is Continue. Do not pass this setting for iframes or popups.
166
    # +heading_color+:: Enter a hex color string such as #e0e0e0. The background color of the section headers changes from gray to a custom color.
167
    # +border_visible+:: Enter true or false. Must be false for iframes or popups, and must be true for redirects.
168
    # +iframe_communicator_url+:: Enter the URL to a page that can communicate with the merchant’s main page using javascript.
169
    #                             This parameter enables you to dynamically change the size of the popup so that there are no scroll bars.
170
    #                             This parameter is required only for iframe or lightbox applications.
171
    # +validation_mode+:: Set to :testMode, :liveMode or :none (the default) to set hostedProfileValidationMode mode
172
173
    def get_hosted_profile_token(profile_id, options = {})
174
      options = @@get_hosted_profile_token_option_defaults.merge(options)
175
      @type = Type::CIM_GET_HOSTED_PROFILE
176
      handle_profile_id(profile_id)
177
      handle_hosted_profile_settings(options)
178
      make_request
179
    end
180
181
    # Sets up and submits a createCustomerPaymentProfileRequest transaction. If this transaction has already been
182
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object. The
183
    # response object will have a payment_profile_id if the request was successful.
184
    #
185
    # +profile+:: An AuthorizeNet::CIM::PaymentProfile object describing the profile to create.
186
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who will own the PaymentProfile, or a CustomerProfile object with the ID populated.
187
    # +options+:: An optional hash of options.
188
    #
189
    # Options:
190
    # +validation_mode+:: Set to :testMode, :liveMode or :none (the default) to indicate what sort of PaymentProfile validation to do (assuming a PaymentProfile is included with the CustomerProfile)
191
    #
192
    # Typical usage:
193
    #
194
    #   credit_card = AuthorizeNet::CreditCard.new('4111111111111111', '0120')
195
    #   payment_profile = AuthorizeNet::CIM::PaymentProfile.new(:payment_method => credit_card)
196
    #   response = transaction.create_payment_profile(payment_profile, '123456')
197
    #   puts response.payment_profile_id if response.success?
198
    #
199 View Code Duplication
    def create_payment_profile(payment_profile, profile_id, options = {})
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
200
      options = @@create_payment_profile_option_defaults.merge(options)
201
      @type = Type::CIM_CREATE_PAYMENT
202
      @fields.merge!(payment_profile.to_hash)
203
      set_fields(validation_mode: options[:validation_mode])
204
      handle_profile_id(profile_id)
205
      make_request
206
    end
207
208
    # Sets up and submits a getCustomerPaymentProfileRequest transaction. If this transaction has already been
209
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object. The
210
    # response object will have the PaymentProfile object requested available via its payment_profile method if
211
    # the request was successful.
212
    #
213
    # +payment_profile_id+:: Takes either a String containing the ID of the PaymentProfile you want to retrieve, or a PaymentProfile object with the ID populated.
214
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who owns this PaymentProfile, or a CustomerProfile object with the ID populated.
215
    #
216
    # Typical usage:
217
    #
218
    #   response = transaction.get_payment_profile('654321', '123456')
219
    #   payment_profile = response.payment_profile if response.success?
220
    #
221
    def get_payment_profile(payment_profile_id, profile_id)
222
      @type = Type::CIM_GET_PAYMENT
223
      handle_payment_profile_id(payment_profile_id)
224
      handle_profile_id(profile_id)
225
      make_request
226
    end
227
228
    # Sets up and submits a updateCustomerPaymentProfileRequest transaction. If this transaction has already been
229
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object.
230
    #
231
    #
232
    # +payment_profile+:: An AuthorizeNet::CIM::PaymentProfile object describing the profile to update.
233
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who owns this PaymentProfile, or a CustomerProfile object with the ID populated.
234
    #
235
    # Typical usage:
236
    #
237
    #   payment_profile.cust_type = :business
238
    #   response = transaction.update_payment_profile(payment_profile, '123456')
239
    #   puts response.success?
240
    #
241
    # Options:
242
    # +validation_mode+:: Set to :testMode, :liveMode or :none (the default) to indicate what sort of PaymentProfile validation to do.
243
    #
244 View Code Duplication
    def update_payment_profile(payment_profile, profile_id, options = {})
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
245
      options = @@create_payment_profile_option_defaults.merge(options)
246
      @type = Type::CIM_UPDATE_PAYMENT
247
      @fields.merge!(payment_profile.to_hash)
248
      set_fields(validation_mode: options[:validation_mode])
249
      handle_profile_id(profile_id)
250
      make_request
251
    end
252
253
    # Sets up and submits a deleteCustomerPaymentProfileRequest transaction. If this transaction has already been
254
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object.
255
    #
256
    #
257
    # +payment_profile_id+:: Takes either a String containing the ID of the PaymentProfile you want to retrieve, or a PaymentProfile object with the ID populated.
258
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who owns this PaymentProfile, or a CustomerProfile object with the ID populated.
259
    #
260
    # Typical usage:
261
    #
262
    #   response = transaction.delete_profile('123456')
263
    #   puts response.success?
264
    #
265
    def delete_payment_profile(payment_profile_id, profile_id)
266
      @type = Type::CIM_DELETE_PAYMENT
267
      handle_payment_profile_id(payment_profile_id)
268
      handle_profile_id(profile_id)
269
      make_request
270
    end
271
272
    # Sets up and submits a validateCustomerPaymentProfileRequest transaction. If this transaction has already been
273
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object. The results
274
    # of the validation can be obtained via the direct_response method of the response object.
275
    #
276
    #
277
    # +payment_profile_id+::  Takes either a String containing the ID of the PaymentProfile you want to validate, or a PaymentProfile object with the ID populated.
278
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who owns this PaymentProfile, or a CustomerProfile object with the ID populated.
279
    #
280
    # Typical usage:
281
    #
282
    #   response = transaction.validate_payment_profile('654321', '123456')
283
    #   puts response.direct_response.success? if response.success?
284
    #
285
    # Options:
286
    # +validation_mode+:: Set to :testMode (the default) or :liveMode to indicate what sort of PaymentProfile validation to do.
287
    # +address_id+:: Set a shipping Address to be part of the validation transaction.
288
    # +card_code+:: Set a CCV code if one is needed for validation. Defaults to nil.
289
    #
290
    def validate_payment_profile(payment_profile_id, profile_id, options = {})
291
      @type = Type::CIM_VALIDATE_PAYMENT
292
      options = @@validate_payment_profile_option_defaults.merge(options)
293
      handle_payment_profile_id(payment_profile_id)
294
      handle_profile_id(profile_id)
295
      handle_address_id(options[:address_id]) unless options[:address_id].nil?
296
      set_fields(validation_mode: options[:validation_mode])
297
      set_fields(card_code: options[:card_code]) unless options[:card_code].nil?
298
      make_request
299
    end
300
301
    # Sets up and submits a createCustomerShippingAddressRequest transaction. If this transaction has already been
302
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object. The
303
    # response object will have a address_id if the request was successful.
304
    #
305
    # +address+:: An AuthorizeNet::Address object describing the profile to create.
306
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who will own this Address, or a CustomerProfile object with the ID populated.
307
    #
308
    # Typical usage:
309
    #
310
    #   address = AuthorizeNet::Address.new(:first_name => 'Jane', :last_name => 'Doe', :address => '123 Fake St', :city => 'Raccoon Junction', :state => 'WY', :zip => '99999')
311
    #   response = transaction.create_address(address, '123456')
312
    #   puts response.address_id if response.success?
313
    #
314
    def create_address(address, profile_id)
315
      @type = Type::CIM_CREATE_ADDRESS
316
      @fields.merge!(address.to_hash)
317
      handle_profile_id(profile_id)
318
      make_request
319
    end
320
321
    # Sets up and submits a getCustomerShippingAddressRequest transaction. If this transaction has already been
322
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object. The
323
    # response object will have the Address object requested available via its address method if
324
    # the request was successful.
325
    #
326
    #
327
    # +address_id+:: Takes either a String containing the ID of the Address you want to retrieve, or an Address object with the ID populated.
328
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who owns this Address, or a CustomerProfile object with the ID populated.
329
    #
330
    # Typical usage:
331
    #
332
    #   response = transaction.get_address('654321', '123456')
333
    #   address = response.address if response.success?
334
    #
335
    def get_address(address_id, profile_id)
336
      @type = Type::CIM_GET_ADDRESS
337
      handle_address_id(address_id)
338
      handle_profile_id(profile_id)
339
      make_request
340
    end
341
342
    # Sets up and submits a updateCustomerShippingAddressRequest transaction. If this transaction has already been
343
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object.
344
    #
345
    #
346
    # +address+:: An Address object describing the address to update.
347
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who owns this Address, or a CustomerProfile object with the ID populated.
348
    #
349
    # Typical usage:
350
    #
351
    #   address.city = 'Somewhere Else'
352
    #   response = transaction.update_address(address, '123456')
353
    #   puts response.success?
354
    #
355
    def update_address(address, profile_id)
356
      @type = Type::CIM_UPDATE_ADDRESS
357
      @fields.merge!(address.to_hash)
358
      handle_profile_id(profile_id)
359
      make_request
360
    end
361
362
    # Sets up and submits a deleteCustomerShippingAddressRequest transaction. If this transaction has already been
363
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object.
364
    #
365
    #
366
    # +address_id+:: Takes either a String containing the ID of the Address you want to delete, or an Address object with the ID populated.
367
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who owns this Address, or a CustomerProfile object with the ID populated.
368
    #
369
    # Typical usage:
370
    #
371
    #   response = transaction.delete_address('654321', '123456')
372
    #   puts response.success?
373
    #
374
    def delete_address(address_id, profile_id)
375
      @type = Type::CIM_DELETE_ADDRESS
376
      handle_address_id(address_id)
377
      handle_profile_id(profile_id)
378
      make_request
379
    end
380
381
    # Sets up and submits a createCustomerProfileTransactionRequest transaction. If this transaction has already been
382
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object. It is recommend
383
    # to use the connivence methods for each type of payment transaction rather than this method.
384
    #
385
    # +type+:: The type of payment transaction to run. Can be :auth_capture, :auth_only, :prior_auth_capture, :void, or :refund.
386
    # +amount+:: The amount of the transaction. This is required for every transaction type except :void.
387
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who owns the PaymentProfile to be charged, or a CustomerProfile object with the ID populated.
388
    # +payment_profile_id+:: Takes either a String containing the ID of the PaymentProfile you want to charge, or a PaymentProfile object with the ID populated.
389
    # +order+:: An Order object describing the order that this payment transaction is for. Pass nil for no order.
390
    # +options+:: An optional hash of options.
391
    #
392
    # Options:
393
    # +address_id+:: Takes either a String containing the ID of an Address, or an Address object with the ID populated. Used as the shipping address for the payment transaction. Defaults to nil.
394
    # +split_tender_id+:: A split tender transaction ID as a string. If the transaction is to be part of a split tender batch, this must be included. Defaults to nil.
395
    # +aim_options+:: A hash of AIM options to be included with the payment transaction. Since the payment transactions in CIM are just encapsulated AIM transactions, any field used by AIM can be set here. Defaults to nil.
396
    # +custom_fields+:: A hash of custom fields to pass along with the payment transaction. These fields will be stored with the AIM transaction generated by the CIM transaction. Defaults to nil.
397
    #
398
    # Typical usage:
399
    #
400
    #   response = transaction.create_transaction(:auth_capture, 10.00, '123456', '654321', nil)
401
    #   if response.success?
402
    #     puts response.direct_response if direct_response.success?
403
    #
404
    #
405
    def create_transaction(type, amount, profile_id, payment_profile_id, order, options = {})
406
      @type = Type::CIM_CREATE_TRANSACTION
407
      options = @@create_transaction_option_defaults.merge(options)
408
      handle_profile_id(profile_id)
409
      handle_payment_profile_id(payment_profile_id)
410
      handle_address_id(options[:address_id]) unless options[:address_id].nil?
411
      set_fields(split_tender_id: options[:split_tender_id])
412
      @transaction_type = type
413
      @fields.merge!(order.to_hash) unless order.nil?
414
      set_fields(amount: amount)
415
      handle_aim_options(options[:aim_options])
416
      handle_custom_fields(options[:custom_fields])
417
      make_request
418
    end
419
420
    # Sets up and submits an AUTHORIZE_AND_CAPTURE transaction using stored payment information. If this
421
    # transaction has already been run, this method will return nil. Otherwise it will return an
422
    # AuthorizeNet::CIM::Response object.
423
    #
424
    # +amount+:: The amount of the transaction.
425
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who owns the PaymentProfile to be charged, or a CustomerProfile object with the ID populated.
426
    # +payment_profile_id+:: Takes either a String containing the ID of the PaymentProfile you want to charge, or a PaymentProfile object with the ID populated.
427
    # +order+:: An Order object describing the order that this payment transaction is for. Pass nil for no order.
428
    # +options+:: An optional hash of options.
429
    #
430
    # Options:
431
    # +address_id+:: Takes either a String containing the ID of an Address, or an Address object with the ID populated. Used as the shipping address for the payment transaction. Defaults to nil.
432
    # +split_tender_id+:: A split tender transaction ID as a string. If the transaction is to be part of a split tender batch, this must be included. Defaults to nil.
433
    # +aim_options+:: A hash of AIM options to be included with the payment transaction. Since the payment transactions in CIM are just encapsulated AIM transactions, any field used by AIM can be set here. Defaults to nil.
434
    # +custom_fields+:: A hash of custom fields to pass along with the payment transaction. These fields will be stored with the AIM transaction generated by the CIM transaction. Defaults to nil.
435
    #
436
    # Typical usage:
437
    #
438
    #   response = transaction.create_transaction_auth_capture(10.00, '123456', '654321', nil)
439
    #   if response.success?
440
    #     puts response.direct_response if direct_response.success?
441
    #
442
    def create_transaction_auth_capture(amount, profile_id, payment_profile_id, order = nil, options = {})
443
      create_transaction(:auth_capture, amount, profile_id, payment_profile_id, order, options)
444
    end
445
446
    # Sets up and submits an AUTH_ONLY transaction using stored payment information. If this
447
    # transaction has already been run, this method will return nil. Otherwise it will return an
448
    # AuthorizeNet::CIM::Response object.
449
    #
450
    # +amount+:: The amount of the transaction.
451
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who owns the PaymentProfile to be charged, or a CustomerProfile object with the ID populated.
452
    # +payment_profile_id+:: Takes either a String containing the ID of the PaymentProfile you want to charge, or a PaymentProfile object with the ID populated.
453
    # +order+:: An Order object describing the order that this payment transaction is for. Pass nil for no order.
454
    # +options+:: An optional hash of options.
455
    #
456
    # Options:
457
    # +address_id+:: Takes either a String containing the ID of an Address, or an Address object with the ID populated. Used as the shipping address for the payment transaction. Defaults to nil.
458
    # +split_tender_id+:: A split tender transaction ID as a string. If the transaction is to be part of a split tender batch, this must be included. Defaults to nil.
459
    # +aim_options+:: A hash of AIM options to be included with the payment transaction. Since the payment transactions in CIM are just encapsulated AIM transactions, any field used by AIM can be set here. Defaults to nil.
460
    # +custom_fields+:: A hash of custom fields to pass along with the payment transaction. These fields will be stored with the AIM transaction generated by the CIM transaction. Defaults to nil.
461
    #
462
    # Typical usage:
463
    #
464
    #   response = transaction.create_transaction_auth_only(10.00, '123456', '654321', nil)
465
    #   if response.success?
466
    #     puts response.direct_response if direct_response.success?
467
    #
468
    def create_transaction_auth_only(amount, profile_id, payment_profile_id, order = nil, options = {})
469
      create_transaction(:auth_only, amount, profile_id, payment_profile_id, order, options)
470
    end
471
472
    # Sets up and submits a PRIOR_AUTHORIZATION_AND_CAPTURE transaction using stored payment information. If this
473
    # transaction has already been run, this method will return nil. Otherwise it will return an
474
    # AuthorizeNet::CIM::Response object.
475
    #
476
    # +transaction_id+:: A string containing a transaction ID that was generated via an AUTH_ONLY transaction.
477
    # +amount+:: The amount to capture. Must be <= the amount authorized via the AUTH_ONLY transaction.
478
    # +order+:: An Order object describing the order that this payment transaction is for. Pass nil for no order.
479
    # +options+:: An optional hash of options.
480
    #
481
    # Options:
482
    # +aim_options+:: A hash of AIM options to be included with the payment transaction. Since the payment transactions in CIM are just encapsulated AIM transactions, any field used by AIM can be set here. Defaults to nil.
483
    # +custom_fields+:: A hash of custom fields to pass along with the payment transaction. These fields will be stored with the AIM transaction generated by the CIM transaction. Defaults to nil.
484
    #
485
    # Typical usage:
486
    #
487
    #   response = transaction.create_transaction_prior_auth_capture('111222', 10.00)
488
    #   if response.success?
489
    #     puts response.direct_response if direct_response.success?
490
    #
491 View Code Duplication
    def create_transaction_prior_auth_capture(transaction_id, amount, order = nil, options = {})
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
492
      handle_transaction_id(transaction_id)
493
      create_transaction(:prior_auth_capture, amount, nil, nil, order, options)
494
    end
495
496
    # Sets up and submits a VOID transaction using stored payment information. If this
497
    # transaction has already been run, this method will return nil. Otherwise it will return an
498
    # AuthorizeNet::CIM::Response object.
499
    #
500
    # +transaction_id+:: A string containing a transaction ID to void.
501
    # +options+:: An optional hash of options.
502
    #
503
    # Options:
504
    # +aim_options+:: A hash of AIM options to be included with the payment transaction. Since the payment transactions in CIM are just encapsulated AIM transactions, any field used by AIM can be set here. Defaults to nil.
505
    # +custom_fields+:: A hash of custom fields to pass along with the payment transaction. These fields will be stored with the AIM transaction generated by the CIM transaction. Defaults to nil.
506
    #
507
    # Typical usage:
508
    #
509
    #   response = transaction.create_transaction_void('111222')
510
    #   if response.success?
511
    #     puts response.direct_response if direct_response.success?
512
    #
513
    def create_transaction_void(transaction_id, options = {})
514
      handle_transaction_id(transaction_id)
515
      create_transaction(:void, nil, nil, nil, nil, options)
516
    end
517
518
    # Sets up and submits a REFUND transaction using stored payment information. If this
519
    # transaction has already been run, this method will return nil. Otherwise it will return an
520
    # AuthorizeNet::CIM::Response object.
521
    #
522
    # +transaction_id+:: A string containing a transaction ID to refund. Pass nil for an unlinked refund.
523
    # +amount+:: The amount to refund.
524
    # +profile_id+:: Takes either a String containing the ID of the CustomerProfile who owns the PaymentProfile to be credited, or a CustomerProfile object with the ID populated. Pass nil for an unlinked refund.
525
    # +payment_profile_id+:: Takes either a String containing the ID of the PaymentProfile you want to credit, or a PaymentProfile object with the ID populated. Pass nil for an unlinked refund.
526
    # +order+:: An Order object describing the order that is being refunded. Pass nil for no order.
527
    # +options+:: An optional hash of options.
528
    #
529
    # Options:
530
    # +aim_options+:: A hash of AIM options to be included with the payment transaction. Since the payment transactions in CIM are just encapsulated AIM transactions, any field used by AIM can be set here. Defaults to nil.
531
    # +custom_fields+:: A hash of custom fields to pass along with the payment transaction. These fields will be stored with the AIM transaction generated by the CIM transaction. Defaults to nil.
532
    #
533
    # Typical usage:
534
    #
535
    #   response = transaction.create_transaction_refund('111222', 10.00, '123456', '654321')
536
    #   if response.success?
537
    #     puts response.direct_response if direct_response.success?
538
    #
539 View Code Duplication
    def create_transaction_refund(transaction_id, amount, profile_id, payment_profile_id, order = nil, options = {})
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
540
      handle_transaction_id(transaction_id)
541
      create_transaction(:refund, amount, profile_id, payment_profile_id, order, options)
542
    end
543
544
    # Sets up and submits a updateSplitTenderGroupRequest transaction. Use this to end or void a split
545
    # tender batch. If this transaction has already been run, this method will return nil. Otherwise
546
    # it will return an AuthorizeNet::CIM::Response object.
547
    #
548
    # +split_tender_id+:: The split tender batch ID you want to change the status of.
549
    # +status+:: The new status to set for the batch. Options are :voided or :completed.
550
    #
551
    # Typical usage:
552
    #
553
    #   response = transaction.update_split_tender('1111111', :voided)
554
    #   puts response if response.success?
555
    #
556
    def update_split_tender(split_tender_id, status)
557
      @type = Type::CIM_UPDATE_SPLIT
558
      set_fields(split_tender_id: split_tender_id, split_tender_status: status.to_s)
559
      make_request
560
    end
561
562
    # Sets up and submits a getCustomerProfileIdsRequest transaction. If this transaction has already been
563
    # run, this method will return nil. Otherwise it will return an AuthorizeNet::CIM::Response object. The
564
    # response object will have a list of all CustomerProfile IDs available via its profile_ids method on success.
565
    #
566
    # Typical usage:
567
    #
568
    #   response = transaction.get_profile_ids()
569
    #   puts response.profile_ids if response.success?
570
    #
571
    def get_profile_ids
572
      @type = Type::CIM_GET_PROFILE_IDS
573
      make_request
574
    end
575
576
    # :stopdoc:
577
    # Duck-type as an AIM transaction so we can build AIM responses easily
578
579
    # No encapsulation_character.
580
    def encapsulation_character
581
      @encap_char
582
    end
583
584
    # Comma delimited.
585
    def delimiter
586
      @delim_char
587
    end
588
589
    # Custom fields accessor.
590
    attr_reader :custom_fields
591
592
    # Always version 3.1.
593
    def version
594
      3.1
595
    end
596
597
    # Always nil.
598
    def cp_version
599
      nil
600
    end
601
    # :startdoc:
602
603
    #:enddoc:
604
    protected
605
606
    # Handles profile id type massaging.
607
    def handle_profile_id(id)
608
      case id
609
      when CustomerProfile
610
        set_fields(customer_profile_id: id.customer_profile_id.to_s)
611
      when nil
612
        nil
613
      else
614
        set_fields(customer_profile_id: id.to_s)
615
      end
616
    end
617
618
    # Handles payment profile id type massaging.
619
    def handle_payment_profile_id(id)
620
      case id
621
      when PaymentProfile
622
        set_fields(customer_payment_profile_id: id.customer_payment_profile_id.to_s)
623
      when nil
624
        nil
625
      else
626
        set_fields(customer_payment_profile_id: id.to_s)
627
      end
628
    end
629
630
    # Handles address id type massaging.
631
    def handle_address_id(id)
632
      case id
633
      when AuthorizeNet::Address
634
        set_fields(customer_address_id: id.customer_address_id.to_s)
635
      when nil
636
        nil
637
      else
638
        set_fields(customer_address_id: id.to_s)
639
      end
640
    end
641
642
    # Handles tranasction id type massaging.
643
    def handle_transaction_id(id)
644
      case id
645
      when AuthorizeNet::AIM::Response
646
        set_fields(trans_id: id.transaction_id.to_s)
647
      else
648
        set_fields(trans_id: id.to_s)
649
      end
650
    end
651
652
    # Handles packing up aim options.
653
    def handle_aim_options(options)
654
      encoded_options = []
655
      case options
656
      when Hash
657
        options.each_pair do |k, v|
658
          if @@aim_response_options.include?(k)
659
            instance_variable_set(('@' + k.to_s).to_sym, v)
660
          end
661
        end
662
      when nil
663
        return
664
      else
665
        return handle_aim_options(options.to_hash)
666
      end
667
668
      @fields[:extra_options] ||= {}
669
      @fields[:extra_options].merge!(options)
670
    end
671
672
    def handle_hosted_profile_settings(options)
673
      options_mapping = {
674
        return_url: :hostedProfileReturnUrl,
675
        return_url_text: :hostedProfileReturnUrlText,
676
        heading_color: :hostedProfileHeadingBgColor,
677
        border_visible: :hostedProfilePageBorderVisible,
678
        iframe_communicator_url: :hostedProfileIFrameCommunicatorUrl,
679
        validation_mode: :hostedProfileValidationMode
680
      }
681
      set_fields(
682
        hosted_settings:             options.select do |k, _|
683
                                       options_mapping.keys.include? k
684
                                     end.map do |k, v|
685
                                       { setting_name: options_mapping[k], setting_value: v }
686
                                     end
687
      )
688
    end
689
690
    # Handles packing up custom fields.
691
    def handle_custom_fields(options)
692
      encoded_options = []
693
      case options
694
      when Hash
695
        @custom_fields.merge!(options)
696
      when nil
697
        return
698
      else
699
        return handle_custom_fields(options.to_hash)
700
      end
701
    end
702
703
    # Callback for creating the right node structure for a given transaction type. `node` is ignored for now.
704
    def select_transaction_type_fields(_node)
705
      case @transaction_type
706
      when :auth_only
707
        TRANSACTION_AUTH_FIELDS
708
      when :auth_capture
709
        TRANSACTION_AUTH_CAPTURE_FIELDS
710
      when :void
711
        TRANSACTION_VOID_FIELDS
712
      when :refund
713
        TRANSACTION_REFUND_FIELDS
714
      when :capture_only
715
        TRASNACTION_CAPTURE_FIELDS
716
      when :prior_auth_capture
717
        TRANSACTION_PRIOR_AUTH_CAPTURE_FIELDS
718
      end
719
    end
720
  end
721
end
722