Module: MuckCommerce::Models::MuckOrderTransaction::ClassMethods

Defined in:
lib/muck-commerce/models/order_transaction.rb

Instance Method Summary collapse

Instance Method Details

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

Make an authorization using the billing_information object provided. The billing_information object must have a valid credit card



22
23
24
25
26
27
# File 'lib/muck-commerce/models/order_transaction.rb', line 22

def authorize(amount, billing, options = {})
  validate_card(billing)
  process(I18n.translate('muck.commerce.authorization'), amount) do |gw|
    gw.authorize(amount, billing.credit_card, options)
  end
end

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

Capture funds from the given authorization



30
31
32
33
34
# File 'lib/muck-commerce/models/order_transaction.rb', line 30

def capture(amount, authorization, options = {})
  process(I18n.translate('muck.commerce.capture'), amount) do |gw|
    gw.capture(amount, authorization, options)
  end
end

#cim_authorize(amount, billing_information, options = {}) ⇒ Object

Make an authorization using the CIM. This assumes that the billing_information object has a valid entry in the CIM



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/muck-commerce/models/order_transaction.rb', line 47

def cim_authorize(amount, billing_information, options = {})
  transaction = {
    :type => :auth_only,
    :amount => sprintf("%.2f", amount.to_f / 100) # Convert into decimal amount.  HACK/TODO the rest of the library uses cents.  One day this value might need to be cents instead of dollars
  }
  transaction[:customer_profile_id] = billing_information.billable.customer_profile_id
  transaction[:customer_payment_profile_id] = billing_information.customer_payment_profile_id
  process(I18n.translate('muck.commerce.authorization_cim'), amount, true) do |gw|
    gw.create_customer_profile_transaction(:transaction => transaction)
  end
end

#cim_create(billable, billing_information) ⇒ Object

Creates an entry in the CIM system for the given billing information



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/muck-commerce/models/order_transaction.rb', line 74

def cim_create(billable, billing_information)
  process(I18n.translate('muck.commerce.cim_create'), 0, true) do |gw|
    response = gw.create_customer_profile(:profile => get_cim_profile(billing_information.billable, billing_information))
    if response.success?
      # these are protected attributes so mass updates are not allowed
      billable.customer_profile_id = response.authorization
      billable.save!
      billing_information.customer_payment_profile_id = response.params["customer_payment_profile_id_list"]["numeric_string"]
      billing_information.save!
      billing_information.reload # associated billable object has changed and needs to be reloaded
    end
    response
  end
end

#cim_create_update(billable, billing_information, validate = true) ⇒ Object

Creates or updates billing information in the CIM based on whether or not it already exists.



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/muck-commerce/models/order_transaction.rb', line 108

def cim_create_update(billable, billing_information, validate = true)
  if billing_information.has_billing_profile?
    response = self.cim_update(billable, validate)
    if response.success?
      self.cim_update_payment_profile(billing_information, validate)
    else
      response
    end
  else
    self.cim_create(billable, billing_information)
  end
end

#cim_delete(customer_profile_id) ⇒ Object

Remove billing information from the CIM system.



122
123
124
# File 'lib/muck-commerce/models/order_transaction.rb', line 122

def cim_delete(customer_profile_id)
  cim_delete_by_customer_profile_id(customer_profile_id)
end

#cim_delete_by_customer_profile_id(customer_profile_id) ⇒ Object



126
127
128
129
130
# File 'lib/muck-commerce/models/order_transaction.rb', line 126

def cim_delete_by_customer_profile_id(customer_profile_id)
  process(I18n.translate('muck.commerce.cim_delete_user'), 0, true) do |gw|
    gw.delete_customer_profile(:customer_profile_id => customer_profile_id) 
  end
end

#cim_get_customer_payment_profile(customer_profile_id, customer_payment_profile_id) ⇒ Object

Gets an existing customer payment profile



151
152
153
154
155
156
157
# File 'lib/muck-commerce/models/order_transaction.rb', line 151

def cim_get_customer_payment_profile(customer_profile_id, customer_payment_profile_id)
  process(I18n.translate('muck.commerce.cim_get_payment_customer_profile'), 0, true) do |gw|
    gw.get_customer_payment_profile(
    :customer_profile_id => customer_profile_id,
    :customer_payment_profile_id => customer_payment_profile_id)
  end
end

#cim_get_customer_profile(customer_profile_id) ⇒ Object

Gets an existing profile



143
144
145
146
147
148
# File 'lib/muck-commerce/models/order_transaction.rb', line 143

def cim_get_customer_profile(customer_profile_id)
  process(I18n.translate('muck.commerce.cim_get_customer_profile'), 0, true) do |gw|
    gw.get_customer_profile(
    :customer_profile_id => customer_profile_id)
  end
end

#cim_purchase(amount, billing_information, options = {}) ⇒ Object

Make a purchase using the CIM. This assumes that the billing_information object has a valid entry in the CIM



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/muck-commerce/models/order_transaction.rb', line 61

def cim_purchase(amount, billing_information, options = {})
  transaction = {
    :type => :auth_capture,
    :amount => sprintf("%.2f", amount.to_f / 100) # Convert into decimal amount.  HACK/TODO the rest of the library uses cents.  One day this value might need to be cents instead of dollars
  }
  transaction[:customer_profile_id] = billing_information.billable.customer_profile_id
  transaction[:customer_payment_profile_id] = billing_information.customer_payment_profile_id
  process(I18n.translate('muck.commerce.purchase_cim'), amount, true) do |gw|
    gw.create_customer_profile_transaction(:transaction => transaction)
  end
end

#cim_update(billable, validate = true) ⇒ Object

Updates user information in the CIM system



90
91
92
93
94
95
96
# File 'lib/muck-commerce/models/order_transaction.rb', line 90

def cim_update(billable, validate = true)
  profile = get_cim_profile(billable, nil, validate)
  profile[:customer_profile_id] = billable.customer_profile_id
  process(I18n.translate('muck.commerce.cim_update_user'), 0, true) do |gw|
    gw.update_customer_profile(:profile => profile)
  end
end

#cim_update_payment_profile(billing_information, validate = true) ⇒ Object

Updates billing information in the CIM system



99
100
101
102
103
104
105
# File 'lib/muck-commerce/models/order_transaction.rb', line 99

def cim_update_payment_profile(billing_information, validate = true)
  profile = get_cim_payment_profile(billing_information, validate)
  process(I18n.translate('muck.commerce.cim_update_payment_profile'), 0, true) do |gw|
    gw.update_customer_payment_profile( :customer_profile_id => billing_information.billable.customer_profile_id,
                                        :payment_profile => profile)
  end
end

#cim_validate_customer_payment_profile(billing_information) ⇒ Object

validates an existing profile



133
134
135
136
137
138
139
140
# File 'lib/muck-commerce/models/order_transaction.rb', line 133

def cim_validate_customer_payment_profile(billing_information)
  process(I18n.translate('muck.commerce.cim_validate_profile'), 0, true) do |gw|
    gw.validate_customer_payment_profile(
      :customer_profile_id => billing_information.billable.customer_profile_id,
      :customer_payment_profile_id => billing_information.customer_payment_profile_id,
      :validation_mode => :live)
  end
end

#get_cim_payment_profile(billing_information, validate = true, customer_type = 'individual') ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/muck-commerce/models/order_transaction.rb', line 182

def get_cim_payment_profile(billing_information, validate = true, customer_type = 'individual')
  validate_card(billing_information) if validate
  payment = { :credit_card => billing_information.credit_card_for_cim }
  address = { :first_name => billing_information.get_first_name,
              :last_name => billing_information.get_last_name }
  address[:company] = billing_information.company unless billing_information.company.blank?
  address[:address] = "#{billing_information.address1} #{billing_information.address2}" unless billing_information.address1.blank?
  address[:city] = billing_information.city unless billing_information.city.blank?
  address[:state] = billing_information.state.name unless billing_information.state.blank?
  address[:country] = billing_information.country.name unless billing_information.country.blank?
  address[:zip] = billing_information.postal_code unless billing_information.postal_code.blank?
  payment_profile = { :customer_type => customer_type,
                      :bill_to => address,
                      :payment => payment }
  payment_profile[:customer_payment_profile_id] = billing_information.customer_payment_profile_id if billing_information.customer_payment_profile_id
  payment_profile
end

#get_cim_profile(billable, billing_information = nil, validate = true, customer_type = 'individual') ⇒ Object

Get a profile object compatible with the CIM system.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/muck-commerce/models/order_transaction.rb', line 167

def get_cim_profile(billable, billing_information = nil, validate = true, customer_type = 'individual')
  profile = {
    :merchant_customer_id => billable.id,
    :description => billable.cim_description,
    :email => billable.email }
  if billing_information
    if billing_information.has_billing_profile?
      profile[:payment_profile] = get_cim_payment_profile(billing_information, validate, customer_type)
    else
      profile[:payment_profiles] = get_cim_payment_profile(billing_information, validate, customer_type)
    end
  end
  profile
end

#process(action, amount = nil, cim_transaction = false) ⇒ Object



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/muck-commerce/models/order_transaction.rb', line 204

def process(action, amount = nil, cim_transaction = false)
  result = OrderTransaction.new
  result.amount = amount
  result.action = action
  begin
    if cim_transaction
      response = yield cim_gateway
    else
      response = yield gateway
    end
    result.success   = response.success?
    result.reference = response.authorization
    result.message   = response.message
    result.params    = response.params
    result.test      = response.test?
  rescue ActiveMerchant::ActiveMerchantError => e
    result.success   = false
    result.reference = nil
    result.message   = e.message
    result.params    = {}
    result.test      = gateway.test?
  end
    
  result
end

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

Make a purchase using the billing_information object provided. The billing_information object must have a valid credit card



38
39
40
41
42
43
# File 'lib/muck-commerce/models/order_transaction.rb', line 38

def purchase(amount, billing_information, options = {})
  validate_card(billing_information)
  process(I18n.translate('muck.commerce.first_time_purchase'), amount) do |gw|
    gw.purchase(amount, billing_information.credit_card, options)
  end
end

#send_money_via_paypal(amount, email, options = {}) ⇒ Object

Transfer money to an email account using paypal



160
161
162
163
164
# File 'lib/muck-commerce/models/order_transaction.rb', line 160

def send_money_via_paypal(amount, email, options = {})
  process(I18n.translate('muck.commerce.send_money_via_paypal'), amount) do |gw|
    gw.transfer(amount, email, options)
  end
end

#validate_card(billing) ⇒ Object



200
201
202
# File 'lib/muck-commerce/models/order_transaction.rb', line 200

def validate_card(billing)
  raise MuckCommerce::Exceptions::PaymentGatewayError, I18n.translate('muck.commerce.invalid_credit_card_information') if !billing.credit_card.valid?
end