Skip to content

Instantly share code, notes, and snippets.

@srt32
Last active December 27, 2015 02:49
Show Gist options
  • Save srt32/7254914 to your computer and use it in GitHub Desktop.
Save srt32/7254914 to your computer and use it in GitHub Desktop.
responding to the first commenter's idea from http://codeulate.com/2012/07/depend-upon-abstractions/
# app/models/user.rb
class User
attr_reader :payment_gateway
def initialize
@payment_gateway = PaymentGateway::Braintree.new
end
def charge_for_subscription
payment_gateway.charge_for_subscription
end
def create_as_customer
payment_gateway.create_customer(user.name)
end
end
# app/models/refund.rb
class Refund
def process!
PaymentGateway::Braintree.new.refund(self)
end
end
# New file: lib/payment_gateway.rb
class PaymentGateway
SUBSCRIPTION_AMOUNT = 10.to_money
class Braintree
def charge_for_subscription
Braintree.charge(SUBSCRIPTION_AMOUNT)
end
def create_customer(customer_name)
Braintree.create_customer(customer_name)
end
def refund(refund)
Braintree.refund(refund.order_amount, refund.user_braintree_id)
end
end
end
@srt32
Copy link
Author

srt32 commented Nov 1, 2013

Ok, nice. I could see decoupling the payment vendors (Braintree and others) from the PaymentGateway if we did go with injecting Braintree into the PaymentGateway as you suggested in your comment. Would you take it that far in practice? Thanks for explaining!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment