Last active
December 27, 2015 02:49
-
-
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/
This file contains hidden or 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
# 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 |
Oh, wait. Just reread the comment.
Yes, this is what he was referring to.
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
Almost, but not quite what I was thinking.
Try injecting Braintree into the PaymentGateway.