Skip to content

Instantly share code, notes, and snippets.

@oelmekki
Last active Dec 10, 2015
Embed
What would you like to do?
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