charge and record donations

This commit is contained in:
Emi Matchu 2014-09-09 21:06:38 -05:00
parent 2a1a3c61fa
commit 595b1c2fc5
17 changed files with 93 additions and 16 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ db/*.sqlite3
log/*.log
tmp/**/*
Capfile
.env

View file

@ -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

View file

@ -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)

View file

@ -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;

View file

@ -0,0 +1,6 @@
class DonationsController < ApplicationController
def create
@donation = Donation.create_from_charge(current_user, params[:donation])
render text: @donation.inspect
end
end

26
app/models/donation.rb Normal file
View file

@ -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

View file

@ -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

View file

@ -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]

View file

@ -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

View file

@ -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

View file

@ -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|

View file

@ -0,0 +1,5 @@
require 'spec_helper'
describe Donation do
pending "add some examples to (or delete) #{__FILE__}"
end

BIN
vendor/cache/dotenv-0.11.1.gem vendored Normal file

Binary file not shown.

BIN
vendor/cache/dotenv-deployment-0.0.2.gem vendored Normal file

Binary file not shown.

BIN
vendor/cache/dotenv-rails-0.11.1.gem vendored Normal file

Binary file not shown.

Binary file not shown.

BIN
vendor/cache/json-1.8.1.gem vendored Normal file

Binary file not shown.