Skip to content

Instantly share code, notes, and snippets.

@oelmekki
Last active December 10, 2015 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oelmekki/4501186 to your computer and use it in GitHub Desktop.
Save oelmekki/4501186 to your computer and use it in GitHub Desktop.
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