Created
November 29, 2012 19:14
-
-
Save christopherhein/4171205 to your computer and use it in GitHub Desktop.
Abstraction Of payments layer
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
| # config/environments/{test,development,production}.rb | |
| config.stripe_key = "foobar" | |
| Rails.configuration.stripe_key | |
| # Controller | |
| Purchase.charge("tok_jOq0M8vJprCUUU") | |
| # Model | |
| class Purchase < AR::Base | |
| def self.charge(gateway, card) | |
| gateway = PaymentGateway.new(amount, currency, token, description) | |
| gateway.charge! | |
| rescue PaymentGateway::PaymentFailed => e | |
| self.errors[:base] << "Payment failed" | |
| end | |
| end | |
| # Payment Gateway | |
| class PaymentGateway < Struct.new(amount, currency, card, description) | |
| class PaymentFailed < StandardError; end | |
| def charge! | |
| Stripe::Charge.create( | |
| :amount => price, | |
| :currency => "usd", | |
| :card => card, # obtained with Stripe.js | |
| :description => "Charge for awesome shit" | |
| ) | |
| end | |
| end | |
| # Test | |
| describe PaymentGateway do | |
| describe "when charging cc" do | |
| describe "with valid params" do | |
| ... | |
| end | |
| describe "with invalid params" do | |
| ... | |
| end | |
| describe "raising exceptions" do | |
| ... | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment