Last active
December 10, 2015 22:18
-
-
Save oelmekki/4501186 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Purchasable | |
def purchase( attrs = nil ) | |
Purchase.new( attrs || purchase_attributes ).process | |
end | |
class Purchase | |
attr_reader :total, :card | |
def initialize( options = {} ) | |
@total, @card = options[ :total ] || 0, options[ :card ] || test_card | |
end | |
def process | |
transaction.success? | |
end | |
private | |
def transaction | |
@transaction ||= BrainTree::Transaction.sale( amount: total, credit_card: card ) | |
end | |
def test_card | |
'00000000000000' | |
end | |
end | |
end | |
class Order < ActiveRecord::Base | |
include Purchasable | |
belongs_to :user | |
# mean to retrieve value can change any time | |
def purchase_attributes | |
{ total: compute_total, card: user.credit_card } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment