Class: TbCheckout::Transaction

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
BelongsToUserSession
Defined in:
app/models/tb_checkout/transaction.rb

Defined Under Namespace

Modules: Status

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BelongsToUserSession

#belongs_to?

Instance Attribute Details

#card_ccvObject

Returns the value of attribute card_ccv.



7
8
9
# File 'app/models/tb_checkout/transaction.rb', line 7

def card_ccv
  @card_ccv
end

#card_expirationObject

Returns the value of attribute card_expiration.



8
9
10
# File 'app/models/tb_checkout/transaction.rb', line 8

def card_expiration
  @card_expiration
end

#card_numberObject

Returns the value of attribute card_number.



6
7
8
# File 'app/models/tb_checkout/transaction.rb', line 6

def card_number
  @card_number
end

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'app/models/tb_checkout/transaction.rb', line 9

def response
  @response
end

Instance Method Details

#amount_in_centsObject



95
96
97
# File 'app/models/tb_checkout/transaction.rb', line 95

def amount_in_cents
  return amount_charged * 100
end

#authorize!Object

Authorize the transaction against the payment gateway

  • The transaction must be in PENDING state in order ot take this action



50
51
52
53
# File 'app/models/tb_checkout/transaction.rb', line 50

def authorize!
  ActiveSupport::Deprecation.warn 'TbCheckout::Transaction#authorize! is deprecated and will be removed. Call capture! instead.', caller
  return true
end

#billing_full_addressObject



103
104
105
106
# File 'app/models/tb_checkout/transaction.rb', line 103

def billing_full_address
  street = [billing_address_1, billing_address_2].reject(&:blank?).join(', ')
  return "#{street}, #{billing_city} #{billing_state}, #{billing_postal}"
end

#billing_full_nameObject



99
100
101
# File 'app/models/tb_checkout/transaction.rb', line 99

def billing_full_name
  return "#{billing_first_name} #{billing_last_name}"
end

#capture!Object

Capture the funds from an authorized transaction through the payment gateway

  • The transaction must be in AUTHORIZED state in order ot take this action



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/tb_checkout/transaction.rb', line 58

def capture!
  self.with_lock(true) do
    if self.status != Status::PENDING
      raise StandardError, 'Payment must be in Pending state before capture'
    end
    @response = TbCheckout.gateway.purchase(amount_in_cents, build_credit_card(), build_purchase_options())
    if @response.success?
      self.update_columns(:status => Status::CAPTURED, :response_text => @response.to_json)
    else
      self.update_columns(:status => Status::FAIL, :response_text => @response.to_json)
      logger.fatal @response.inspect
    end
  end
  if self.status == Status::CAPTURED
    self.cart.update_attributes(:is_completed => true)
    self.cart.cart_items.each do |cart_item|
      cart_item.item.run_callbacks(:capture)
    end
    return true
  else
    return false
  end
end

#response_jsonObject



127
128
129
130
131
132
133
# File 'app/models/tb_checkout/transaction.rb', line 127

def response_json
  begin
    return JSON.parse(response_text)
  rescue JSON::ParseError
    return {}
  end
end

#void_or_refund!Object

Voids or Refunds the transaction

  • An unsettled trasanction cannot be refunded, and a settled transaction cannot be voided

  • Transactions are settled nightly

  • If the transaction is less than 24 hours old, attempt to void it and if that fails attempt a refund



87
88
89
90
91
92
93
# File 'app/models/tb_checkout/transaction.rb', line 87

def void_or_refund!
  if (1.days.ago < self.created_at && void!) || refund!
    return true
  else
    return false
  end
end