Class: TbCommerce::Transaction
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- TbCommerce::Transaction
- Defined in:
- app/models/tb_commerce/transaction.rb
Defined Under Namespace
Modules: Status
Instance Attribute Summary collapse
-
#card_expiration ⇒ Object
readonly
Returns the value of attribute card_expiration.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #amount_in_cents ⇒ Object
-
#capture!(token) ⇒ Object
Capture the transaction.
- #captured? ⇒ Boolean
- #response_json ⇒ Object
Instance Attribute Details
#card_expiration ⇒ Object (readonly)
Returns the value of attribute card_expiration.
8 9 10 |
# File 'app/models/tb_commerce/transaction.rb', line 8 def card_expiration @card_expiration end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
9 10 11 |
# File 'app/models/tb_commerce/transaction.rb', line 9 def response @response end |
Instance Method Details
#amount_in_cents ⇒ Object
82 83 84 |
# File 'app/models/tb_commerce/transaction.rb', line 82 def amount_in_cents return (amount_charged * 100).to_i end |
#capture!(token) ⇒ Object
Capture the transaction
- Create a charge in stripe
- Run callbacks on the Order model
- Return true if the charge was succesful
48 49 50 51 52 53 54 55 56 57 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_commerce/transaction.rb', line 48 def capture!(token) self.with_lock(true) do if self.status == Status::CAPTURED raise StandardError, 'Transaction has already been captured' end begin charge = Stripe::Charge.create((token)) self.stripe_charge_id = charge.id self.raw_response = charge.to_json self.status = Status::CAPTURED if charge.captured self.save() rescue Stripe::StripeError => e self.status = 'fail' self.raw_response = e. self.save() self.errors.add(:base, e.) end if self.status == Status::CAPTURED begin order.run_callbacks(:capture) rescue StandardError => e logger.fatal "Error running order callbacks: #{e.}" end return true else return false end end end |
#captured? ⇒ Boolean
38 39 40 |
# File 'app/models/tb_commerce/transaction.rb', line 38 def captured? return status == Status::CAPTURED end |
#response_json ⇒ Object
86 87 88 89 90 91 92 |
# File 'app/models/tb_commerce/transaction.rb', line 86 def response_json begin return JSON.parse(raw_response) rescue JSON::ParseError => e return {} end end |