Skip to content

Commit

Permalink
first run at adding basic stripe functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
turino committed Jan 7, 2016
1 parent 64381e5 commit 4e9a3e6
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ TWILIO_RECORDING_APP_SID='APddddddddddddddddddddddddddddddddd'
TWILIO_APP_PHONE_NUMBER='555-555-5555'
TWILIO_APP_RECORDING_NUMBER='555-555-5555'
TWILIO_AUDIO_AWS_BUCKET_URL='https://s3.amazonaws.com/whatever/'

STRIPE_PUBLISHABLE_KEY=pk_test_xxxxxxxxxxxxxxxxxxxxxxxx
STRIPE_SECRET_KEY=sk_test_xxxxxxxxxxxxxxxxxxxxxxxx
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ end
gem 'unicorn'
gem 'airbrake'

# Payment processing
gem 'stripe'

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
stripe (1.31.0)
json (~> 1.8.1)
rest-client (~> 1.4)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.1)
Expand Down Expand Up @@ -305,6 +308,7 @@ DEPENDENCIES
sinatra
spring
spring-commands-rspec
stripe
timecop
twilio-ruby
unicorn
Expand Down
23 changes: 23 additions & 0 deletions app/controllers/v1/payments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class V1::PaymentsController < V1::BaseController

def create

charge = Stripe::Charge.create(payment_params)

render json: { charge_id: charge.id }

rescue Stripe::CardError => e
render json: { error: e.message }
end

private

def payment_params
defaults = {
'currency' => 'usd',
'description' => 'donation from [email protected]'
}

defaults.merge(params.require(:payment).permit(:amount, :source))
end
end
6 changes: 6 additions & 0 deletions config/initializers/stripe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Rails.configuration.stripe = {
:publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'],
:secret_key => ENV['STRIPE_SECRET_KEY']
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
post :targets, on: :collection, action: :targets
get '/:identifier', on: :collection, action: :show, constraints: { identifier: /[^\/]+/} #allow email as identifier
end
resources :payments, only: :create
resources :stats, only: :index
resources :actions, only: :create do
get :count, on: :collection
Expand Down
27 changes: 27 additions & 0 deletions spec/controllers/v1/payments_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'rails_helper'

describe V1::PaymentsController, type: :controller do

describe "POST create" do
it "works" do
stub_stripe_charge(amount: 400, source: 'test token', charge_id: 'test id')
post :create, payment: { amount: 400, source: 'test token' }
expect(json(response)['charge_id']).to eq 'test id'
end
end

end

def json(response)
JSON.parse(response.body)
end

def stub_stripe_charge(amount: 100, source: 'token', charge_id: 'id')
charge = double('charge', id: charge_id)
allow(Stripe::Charge).to receive(:create).
with(hash_including(
'amount' => amount.to_s,
'source' => source,
'currency' => 'usd')).
and_return(charge)
end
2 changes: 1 addition & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
end
end

0 comments on commit 4e9a3e6

Please sign in to comment.