diff --git a/.gitignore b/.gitignore index 61d87a69..09eb7d2a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ db/*.sqlite3 log/*.log tmp/**/* Capfile +.env diff --git a/Gemfile b/Gemfile index 44903513..2add685c 100644 --- a/Gemfile +++ b/Gemfile @@ -1,11 +1,13 @@ source 'http://rubygems.org' +gem 'dotenv-rails', :groups => [:development, :test] + gem 'rails', '= 3.2.18' #gem 'sqlite3-ruby', '~> 1.3.1', :require => 'sqlite3' gem 'mysql2', '>= 0.3.11' # https://groups.google.com/d/topic/rubyonrails-security/4_YvCpLzL58/discussion -gem 'json', '~> 1.7.7' +gem 'json', '~> 1.8.1' gem 'haml', '~> 4.0.0' gem 'rdiscount', '~> 1.6.5' @@ -66,6 +68,8 @@ gem 'rack-attack', '~> 2.2.0' gem 'react-rails', '~> 0.8.0.0' +gem 'stripe', :git => 'https://github.com/stripe/stripe-ruby' + # Needed for the new asset pipeline group :assets do diff --git a/Gemfile.lock b/Gemfile.lock index 5f6ca19c..2cf09e89 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -72,6 +72,15 @@ GIT specs: RocketAMF (1.0.0) +GIT + remote: https://github.com/stripe/stripe-ruby + revision: 2c6f4caa99916c33d3f9af57f66422ff5ea913cf + specs: + stripe (1.15.0) + json (~> 1.8.1) + mime-types (>= 1.25, < 3.0) + rest-client (~> 1.4) + GEM remote: http://rubygems.org/ specs: @@ -129,6 +138,11 @@ GEM railties (~> 3.1) warden (~> 1.2.1) diff-lcs (1.2.1) + dotenv (0.11.1) + dotenv-deployment (~> 0.0.2) + dotenv-deployment (0.0.2) + dotenv-rails (0.11.1) + dotenv (= 0.11.1) dye (0.1.4) em-socksify (0.2.1) eventmachine (>= 1.0.0.beta.4) @@ -159,7 +173,7 @@ GEM http_parser.rb (0.5.3) i18n (0.6.9) journey (1.0.4) - json (1.7.7) + json (1.8.1) mail (2.5.4) mime-types (~> 1.16) treetop (~> 1.4.8) @@ -310,6 +324,7 @@ DEPENDENCIES character-encodings (~> 0.4.1) compass-rails (~> 1.0.3) devise (~> 2.2.3) + dotenv-rails em-http-request! em-mysqlplus! em-synchrony! @@ -320,7 +335,7 @@ DEPENDENCIES globalize3! haml (~> 4.0.0) http_accept_language! - json (~> 1.7.7) + json (~> 1.8.1) memcache-client (~> 1.8.5) mini_magick (~> 3.4) msgpack (~> 0.5.3) @@ -347,6 +362,7 @@ DEPENDENCIES rspec-rails (~> 2.0.0.beta.22) sanitize (~> 2.0.3) sass-rails (~> 3.2.6) + stripe! swf_converter (~> 0.0.3) uglifier (>= 1.0.3) whenever (~> 0.7.3) diff --git a/app/assets/javascripts/static/donate.js b/app/assets/javascripts/static/donate.js index ee714f12..3efde440 100644 --- a/app/assets/javascripts/static/donate.js +++ b/app/assets/javascripts/static/donate.js @@ -1,11 +1,12 @@ (function() { var donationForm = document.getElementById('donation-form'); - var amountField = donationForm.amount; + var amountField = donationForm.querySelector( + '[name=donation\\[amount\\]]'); var tokenField = donationForm.querySelector( '[name=donation\\[stripe_token\\]]'); var checkout = StripeCheckout.configure({ - key: 'pk_test_wEvgn4baD9W5ld5C9JCS9Ahf', // TODO + key: donationForm.getAttribute('data-checkout-publishable-key'), image: donationForm.getAttribute('data-checkout-image'), token: function(token) { tokenField.value = token.id; diff --git a/app/controllers/donations_controller.rb b/app/controllers/donations_controller.rb new file mode 100644 index 00000000..03c77bbc --- /dev/null +++ b/app/controllers/donations_controller.rb @@ -0,0 +1,6 @@ +class DonationsController < ApplicationController + def create + @donation = Donation.create_from_charge(current_user, params[:donation]) + render text: @donation.inspect + end +end diff --git a/app/models/donation.rb b/app/models/donation.rb new file mode 100644 index 00000000..5e7b269c --- /dev/null +++ b/app/models/donation.rb @@ -0,0 +1,26 @@ +class Donation < ActiveRecord::Base + belongs_to :user + + def self.create_from_charge(user, params) + amount = (BigDecimal.new(params[:amount]) * 100).floor + + customer = Stripe::Customer.create( + card: params[:stripe_token] + ) + + charge = Stripe::Charge.create( + :customer => customer.id, + :amount => amount, + :description => 'Donation (thank you!)', + :currency => 'usd' + ) + + donation = Donation.new + donation.amount = amount + donation.charge_id = charge.id + donation.user_id = user.try(:id) + donation.save! + + donation + end +end diff --git a/app/views/static/donate.html.haml b/app/views/static/donate.html.haml index 81a21d0f..034b84cb 100644 --- a/app/views/static/donate.html.haml +++ b/app/views/static/donate.html.haml @@ -7,7 +7,8 @@ toward our hosting costs this year. Thanks so much! = form_tag donations_path, method: 'POST', id: 'donation-form', - 'data-checkout-image' => image_path('default-preview.png') do + 'data-checkout-image' => image_path('default-preview.png'), + 'data-checkout-publishable-key' => Rails.configuration.stripe[:publishable_key] do = hidden_field_tag 'donation[stripe_token]' %header %p#donation-form-title @@ -16,7 +17,7 @@ All donations go directly to our hosting costs. Thanks for your help! %div = precede '$' do - = text_field_tag 'amount', '10.00' + = text_field_tag 'donation[amount]', '10.00' %button{:type => 'submit'} Donate now! #description diff --git a/config/initializers/stripe.rb b/config/initializers/stripe.rb new file mode 100644 index 00000000..7e4a7875 --- /dev/null +++ b/config/initializers/stripe.rb @@ -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] diff --git a/config/routes.rb b/config/routes.rb index f7d0121a..68fabb3c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -78,6 +78,8 @@ OpenneoImpressItems::Application.routes.draw do only: [:create, :destroy] end + resources :donations, only: [:create] + match 'users/current-user/closet' => 'closet_hangers#index', :as => :your_items match 'users/top-contributors' => 'users#top_contributors', :as => :top_contributors diff --git a/db/migrate/20140910014231_create_donations.rb b/db/migrate/20140910014231_create_donations.rb new file mode 100644 index 00000000..90b30644 --- /dev/null +++ b/db/migrate/20140910014231_create_donations.rb @@ -0,0 +1,12 @@ +class CreateDonations < ActiveRecord::Migration + def change + create_table :donations do |t| + t.integer :amount, null: false + t.string :charge_id, null: false + t.integer :user_id + t.string :donor_name + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c28eec92..eb4e6e57 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140403034558) do +ActiveRecord::Schema.define(:version => 20140910014231) do create_table "auth_servers", :force => true do |t| t.string "short_name", :limit => 10, :null => false @@ -100,15 +100,12 @@ ActiveRecord::Schema.define(:version => 20140403034558) do add_index "contributions", ["user_id"], :name => "index_contributions_on_user_id" create_table "donations", :force => true do |t| - t.integer "amount_cents" - t.boolean "processed" - t.datetime "created_at" - t.datetime "updated_at" - t.string "transaction_id" - t.string "access_token" - t.integer "campaign_id" - t.integer "outfit_features_count", :default => 0, :null => false + t.integer "amount", :null => false + t.string "charge_id", :null => false + t.integer "user_id" t.string "donor_name" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "donations_old", :force => true do |t| diff --git a/spec/models/donation_spec.rb b/spec/models/donation_spec.rb new file mode 100644 index 00000000..bc31731a --- /dev/null +++ b/spec/models/donation_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Donation do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/vendor/cache/dotenv-0.11.1.gem b/vendor/cache/dotenv-0.11.1.gem new file mode 100644 index 00000000..41a74fae Binary files /dev/null and b/vendor/cache/dotenv-0.11.1.gem differ diff --git a/vendor/cache/dotenv-deployment-0.0.2.gem b/vendor/cache/dotenv-deployment-0.0.2.gem new file mode 100644 index 00000000..30876f95 Binary files /dev/null and b/vendor/cache/dotenv-deployment-0.0.2.gem differ diff --git a/vendor/cache/dotenv-rails-0.11.1.gem b/vendor/cache/dotenv-rails-0.11.1.gem new file mode 100644 index 00000000..e043ea11 Binary files /dev/null and b/vendor/cache/dotenv-rails-0.11.1.gem differ diff --git a/vendor/cache/json-1.7.7.gem b/vendor/cache/json-1.7.7.gem deleted file mode 100644 index db6f006f..00000000 Binary files a/vendor/cache/json-1.7.7.gem and /dev/null differ diff --git a/vendor/cache/json-1.8.1.gem b/vendor/cache/json-1.8.1.gem new file mode 100644 index 00000000..d903086b Binary files /dev/null and b/vendor/cache/json-1.8.1.gem differ