Skip to content

Instantly share code, notes, and snippets.

@christopherhein
Created November 29, 2012 19:14
Show Gist options
  • Select an option

  • Save christopherhein/4171205 to your computer and use it in GitHub Desktop.

Select an option

Save christopherhein/4171205 to your computer and use it in GitHub Desktop.
Abstraction Of payments layer
# 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