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

Transaction.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 1
module AuthorizeNet
2
  # The core, API agnostic transaction class. You shouldn't instantiate this one.
3
  # Instead you should use AuthorizeNet::AIM::Transaction,
4
  # AuthorizeNet::SIM::Transaction or AuthorizeNet::ARB::Transaction.
5 1
  class Transaction
6 1
    include AuthorizeNet::TypeConversions
7
8
    # Fields to convert to/from booleans.
9 1
    @@boolean_fields = []
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using a class variable like @@boolean_fields is generally not recommended; did you consider
using an class instance variable instead?
Loading history...
10
11
    # Fields to convert to/from BigDecimal.
12 1
    @@decimal_fields = []
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using a class variable like @@decimal_fields is generally not recommended; did you consider
using an class instance variable instead?
Loading history...
13
14
    # DO NOT USE. Instantiate AuthorizeNet::AIM::Transaction,
15
    # AuthorizeNet::SIM::Transaction or AuthorizeNet::ARB::Transaction instead.
16 1
    def initialize
17 13
      @fields ||= {}
18
    end
19
20
    # Sets arbitrary API fields, overwriting existing values if they exist.
21
    # Takes a hash of key/value pairs, where the keys are the field names
22
    # without the "x_" prefix. You can set a field to Nil to unset it. If the
23
    # value is an array, each value in the array will be added. For example,
24
    # set_fields({:line_item => ["item1<|>golf balls<|><|>2<|>18.95<|>Y",
25
    # "item2<|>golf bag<|>Wilson golf carry bag, red<|>1<|>39.99<|>"]})
26
    # would generate two x_line_item fields in the transaction, one for
27
    # each value in the array.
28 1
    def set_fields(fields = {})
29 13
      @fields.merge!(fields)
30 43
      @fields.reject! { |_k, v| v.nil? }
31 13
      @fields
32
    end
33
34
    # Returns the current hash of API fields.
35 1
    attr_reader :fields
36
37
    # Takes an instance of AuthorizeNet::Address and adds it to the transaction.
38 1
    def set_address(address)
39
      @fields.merge!(address.to_hash)
40
    end
41
42
    # Takes an instance of AuthorizeNet::ShippingAddress and adds it to the
43
    # transaction.
44 1
    def set_shipping_address(address)
45
      @fields.merge!(address.to_hash)
46
    end
47
48
    # Takes an instance of AuthorizeNet::Customer and adds it to the transaction.
49 1
    def set_customer(customer)
50
      @fields.merge!(customer.to_hash)
51
    end
52
53
    #:enddoc:
54 1
    protected
55
56
    # Internal method to handle multiple types of payment arguments.
57 1
    def handle_payment_argument(payment)
58 1
      case payment
59
      when AuthorizeNet::CreditCard, AuthorizeNet::ECheck
60 1
        set_fields(payment.to_hash)
61
      else
62
        set_fields(card_num: payment)
63
      end
64
    end
65
  end
66
end
67