From cbf48ad9484997b3aa1a89d7a2a5da8796bc364a Mon Sep 17 00:00:00 2001 From: r888888888 Date: Tue, 28 Oct 2014 11:05:21 -0700 Subject: [PATCH] add stripe integration for safebooru --- Gemfile | 1 + Gemfile.lock | 14 +++ .../stylesheets/specific/users.css.scss | 4 + app/controllers/user_upgrades_controller.rb | 49 ++++++++++ app/controllers/users_controller.rb | 18 ---- app/helpers/user_upgrades_helper.rb | 20 ++++ app/logical/current_user.rb | 2 +- app/logical/user_promotion.rb | 7 +- app/models/user.rb | 4 +- .../user_upgrades/_stripe_payment.html.erb | 8 ++ .../_unavailable_payment.html.erb | 1 + app/views/user_upgrades/new.html.erb | 98 +++++++++++++++++++ app/views/user_upgrades/show.html.erb | 22 +++++ app/views/users/_secondary_links.html.erb | 4 +- app/views/users/_upgrade_notice.html.erb | 2 +- app/views/users/upgrade_information.html.erb | 89 ++++++++++++++++- config/danbooru_default_config.rb | 14 ++- config/initializers/stripe.rb | 1 + config/routes.rb | 3 +- .../user_upgrades_controller_test.rb | 7 ++ 20 files changed, 337 insertions(+), 31 deletions(-) create mode 100644 app/controllers/user_upgrades_controller.rb create mode 100644 app/helpers/user_upgrades_helper.rb create mode 100644 app/views/user_upgrades/_stripe_payment.html.erb create mode 100644 app/views/user_upgrades/_unavailable_payment.html.erb create mode 100644 app/views/user_upgrades/new.html.erb create mode 100644 app/views/user_upgrades/show.html.erb create mode 100644 config/initializers/stripe.rb create mode 100644 test/controllers/user_upgrades_controller_test.rb diff --git a/Gemfile b/Gemfile index 7aa5dc465..f70f08194 100644 --- a/Gemfile +++ b/Gemfile @@ -45,6 +45,7 @@ gem 'capistrano-ext' gem 'radix62', '~> 1.0.1' gem 'streamio-ffmpeg' gem 'rubyzip', :require => "zip" +gem 'stripe', :git => "https://github.com/stripe/stripe-ruby" # needed for looser jpeg header compat gem 'ruby-imagespec', :require => "image_spec", :git => "https://github.com/r888888888/ruby-imagespec.git", :branch => "exif-fixes" diff --git a/Gemfile.lock b/Gemfile.lock index ab92e44d8..7581f49dc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -14,6 +14,15 @@ GIT specs: ruby-imagespec (0.3.1) +GIT + remote: https://github.com/stripe/stripe-ruby + revision: da216fd53b7a5386c136e93bdfe5efaff20682b7 + specs: + stripe (1.16.0) + json (~> 1.8.1) + mime-types (>= 1.25, < 3.0) + rest-client (~> 1.4) + GEM remote: https://rubygems.org/ specs: @@ -131,6 +140,7 @@ GEM net-ssh (2.8.0) net-ssh-gateway (1.2.0) net-ssh (>= 2.6.5) + netrc (0.8.0) newrelic_rpm (3.9.6.257) nokogiri (1.6.1) mini_portile (~> 0.5.0) @@ -168,6 +178,9 @@ GEM raindrops (0.13.0) rake (10.3.2) ref (1.0.5) + rest-client (1.7.2) + mime-types (>= 1.16, < 3.0) + netrc (~> 0.7) rmagick (2.13.3) ruby-prof (0.14.2) rubyzip (1.1.6) @@ -284,6 +297,7 @@ DEPENDENCIES sprockets-rails statistics2 streamio-ffmpeg + stripe! term-ansicolor therubyracer timecop diff --git a/app/assets/stylesheets/specific/users.css.scss b/app/assets/stylesheets/specific/users.css.scss index 35e99de38..cdb7e0fdd 100644 --- a/app/assets/stylesheets/specific/users.css.scss +++ b/app/assets/stylesheets/specific/users.css.scss @@ -87,6 +87,10 @@ div#c-users { max-width: 40em; font-size: 1.2em; + form.stripe { + display: inline; + } + div#feature-comparison { overflow: hidden; margin-bottom: 1em; diff --git a/app/controllers/user_upgrades_controller.rb b/app/controllers/user_upgrades_controller.rb new file mode 100644 index 000000000..64ac1c2ae --- /dev/null +++ b/app/controllers/user_upgrades_controller.rb @@ -0,0 +1,49 @@ +class UserUpgradesController < ApplicationController + before_filter :member_only + + def create + if params[:desc] == "Upgrade to Gold" + level = User::Levels::GOLD + cost = 2000 + + elsif params[:desc] == "Upgrade to Platinum" + level = User::Levels::PLATINUM + cost = 4000 + + elsif params[:desc] == "Upgrade Gold to Platinum" && CurrentUser.user.level == User::Levels::GOLD + level = User::Levels::PLATINUM + cost = 2000 + + else + raise "Invalid desc" + end + + @user = CurrentUser.user + stripe_token = params[:stripeToken] + + begin + charge = Stripe::Charge.create( + :amount => cost, + :currency => "usd", + :card => params[:stripeToken], + :description => params[:desc] + ) + @user.promote_to!(level, :skip_feedback => true) + UserMailer.upgrade(@user, params[:email]).deliver + flash[:success] = true + rescue Stripe::CardError => e + flash[:error] = e.message + end + + redirect_to user_upgrade_path + end + + def new + unless CurrentUser.user.is_anonymous? + TransactionLogItem.record_account_upgrade_view(CurrentUser.user, request.referer) + end + end + + def show + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a337573ae..3049a56bc 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -59,24 +59,6 @@ class UsersController < ApplicationController respond_with(@user) end - def upgrade_information - unless CurrentUser.user.is_anonymous? - TransactionLogItem.record_account_upgrade_view(CurrentUser.user, request.referer) - end - end - - def upgrade - @user = User.find(params[:id]) - - if params[:email] =~ /paypal/ - UserMailer.upgrade_fail(params[:email]).deliver - else - UserMailer.upgrade(@user, params[:email]).deliver - end - - redirect_to user_path(@user), :notice => "Email was sent" - end - def cache @user = User.find(params[:id]) @user.update_cache diff --git a/app/helpers/user_upgrades_helper.rb b/app/helpers/user_upgrades_helper.rb new file mode 100644 index 000000000..960ba1977 --- /dev/null +++ b/app/helpers/user_upgrades_helper.rb @@ -0,0 +1,20 @@ +module UserUpgradesHelper + def stripe_button(desc, cost) + html = %{ +
+ + #{hidden_field_tag(:desc, desc)} + +
+ } + + raw(html) + end +end diff --git a/app/logical/current_user.rb b/app/logical/current_user.rb index 69efcb215..272f50c0e 100644 --- a/app/logical/current_user.rb +++ b/app/logical/current_user.rb @@ -74,7 +74,7 @@ class CurrentUser end def self.set_safe_mode(req) - if req.host =~ /safe/ + if req.host =~ /safe/ || req.params[:safe_mode] Thread.current[:safe_mode] = true else Thread.current[:safe_mode] = false diff --git a/app/logical/user_promotion.rb b/app/logical/user_promotion.rb index 15f01db8c..cd6e5b1c6 100644 --- a/app/logical/user_promotion.rb +++ b/app/logical/user_promotion.rb @@ -1,10 +1,11 @@ class UserPromotion - attr_reader :user, :promoter, :new_level + attr_reader :user, :promoter, :new_level, :options - def initialize(user, promoter, new_level) + def initialize(user, promoter, new_level, options = {}) @user = user @promoter = promoter @new_level = new_level + @options = options end def promote! @@ -14,7 +15,7 @@ class UserPromotion user.inviter_id = promoter.id create_transaction_log_item - create_user_feedback + create_user_feedback unless options[:skip_feedback] create_dmail user.save diff --git a/app/models/user.rb b/app/models/user.rb index 6e3a0fdac..6dee9045a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -271,8 +271,8 @@ class User < ActiveRecord::Base end end - def promote_to!(new_level) - UserPromotion.new(self, CurrentUser.user, new_level).promote! + def promote_to!(new_level, options = {}) + UserPromotion.new(self, CurrentUser.user, new_level, options).promote! end def promote_to_admin_if_first_user diff --git a/app/views/user_upgrades/_stripe_payment.html.erb b/app/views/user_upgrades/_stripe_payment.html.erb new file mode 100644 index 000000000..a26c8ae68 --- /dev/null +++ b/app/views/user_upgrades/_stripe_payment.html.erb @@ -0,0 +1,8 @@ +

Upgrading from Gold to Platinum will only cost $20. If you have any further questions or concerns, feel free to contact me at <%= mail_to Danbooru.config.contact_email, nil, :encode => :javascript %>.

+ +<% if CurrentUser.user.level < User::Levels::GOLD %> + <%= stripe_button("Upgrade to Gold", 2000) %> + <%= stripe_button("Upgrade to Platinum", 4000) %> +<% elsif CurrentUser.user.level < User::Levels::PLATINUM %> + <%= stripe_button("Upgrade Gold to Platinum", 2000) %> +<% end %> diff --git a/app/views/user_upgrades/_unavailable_payment.html.erb b/app/views/user_upgrades/_unavailable_payment.html.erb new file mode 100644 index 000000000..d857d4577 --- /dev/null +++ b/app/views/user_upgrades/_unavailable_payment.html.erb @@ -0,0 +1 @@ +

You can upgrade your account at <%= link_to "Safebooru", new_user_upgrade_path(:protocol => "https", :host => "safebooru.donmai.us", :only_path => false) %>.

\ No newline at end of file diff --git a/app/views/user_upgrades/new.html.erb b/app/views/user_upgrades/new.html.erb new file mode 100644 index 000000000..bc07910bf --- /dev/null +++ b/app/views/user_upgrades/new.html.erb @@ -0,0 +1,98 @@ +
+
+

Upgrade Your Account

+ +

Annoyed by ads? Want more searching power? Upgrade your account and become a power user of the best database of anime artwork on the internet.

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
BasicGoldPlatinum
CostFree$20

One time fee

$40

One time fee

Tag Limit2612
Favorite Limit10,00020,000Unlimited
Page Limit1,0002,0005,000
Tag SubscriptionsNoYesYes
See Censored TagsNoYesYes
API Hourly Limit3,00010,00020,000
Database Timeout3 sec6 sec9 sec
Variable Posts Per PageNoYesYes
Name ChangesNoYesYes
+
+ + <% if CurrentUser.safe_mode? %> + <%= render "stripe_payment" %> + <% else %> + <%= render "unavailable_payment" %> + <% end %> +
+
+ +<%= render "users/secondary_links" %> + +<% content_for(:page_title) do %> + Upgrade - <%= Danbooru.config.app_name %> +<% end %> diff --git a/app/views/user_upgrades/show.html.erb b/app/views/user_upgrades/show.html.erb new file mode 100644 index 000000000..443c54920 --- /dev/null +++ b/app/views/user_upgrades/show.html.erb @@ -0,0 +1,22 @@ +
+
+ <% if flash[:disable] %> +

You can upgrade your account at <%= link_to "Safebooru", new_user_upgrade_path(:protocol => "https", :host => "safebooru.donmai.us", :only_path => false) %>.

+ <% elsif flash[:success] %> +

Congradulations!

+ +

You are now a <%= CurrentUser.user.level_string %> level account. Thanks for supporting the site!

+

<%= link_to "Return to posts", posts_path %>

+ <% elsif flash[:error] %> +

An error occurred!

+

<%= flash[:error] %>

+

<%= link_to "Try again", new_user_upgrade_path %>

+ <% end %> +
+
+ +<%= render "users/secondary_links" %> + +<% content_for(:page_title) do %> + Upgrade - <%= Danbooru.config.app_name %> +<% end %> diff --git a/app/views/users/_secondary_links.html.erb b/app/views/users/_secondary_links.html.erb index f65559561..d08d9d25b 100644 --- a/app/views/users/_secondary_links.html.erb +++ b/app/views/users/_secondary_links.html.erb @@ -21,8 +21,8 @@
  • <%= link_to "Send message", new_dmail_path(:dmail => {:to_id => @user.id}) %>
  • <% end %> - <% if !CurrentUser.is_gold? %> -
  • <%= link_to "Upgrade", upgrade_information_users_path %>
  • + <% if CurrentUser.is_member? || CurrentUser.is_gold? %> +
  • <%= link_to "Upgrade", new_user_upgrade_path %>
  • <% end %> <% if CurrentUser.is_moderator? %> diff --git a/app/views/users/_upgrade_notice.html.erb b/app/views/users/_upgrade_notice.html.erb index 2a3eb5a90..f6e8f6141 100644 --- a/app/views/users/_upgrade_notice.html.erb +++ b/app/views/users/_upgrade_notice.html.erb @@ -1,4 +1,4 @@
    -

    <%= link_to "Upgrade your account for only $20!", upgrade_information_users_path %>

    +

    <%= link_to "Upgrade your account for only $20!", new_user_upgrade_path %>

    <%= link_to "No thanks", "#", :id => "hide-upgrade-account-notice" %>

    diff --git a/app/views/users/upgrade_information.html.erb b/app/views/users/upgrade_information.html.erb index ca34cba1c..43516000e 100644 --- a/app/views/users/upgrade_information.html.erb +++ b/app/views/users/upgrade_information.html.erb @@ -2,7 +2,94 @@

    Upgrade Your Account

    -

    Upgrades are currently not available.

    +

    Annoyed by ads? Want more searching power? Upgrade your account and become a power user of the best database of anime artwork on the internet.

    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    BasicGoldPlatinum
    CostFree$20

    One time fee

    $40

    One time fee

    Tag Limit2612
    Favorite Limit10,00020,000Unlimited
    Page Limit1,0002,0005,000
    Tag SubscriptionsNoYesYes
    See Censored TagsNoYesYes
    API Hourly Limit3,00010,00020,000
    Database Timeout3 sec6 sec9 sec
    Variable Posts Per PageNoYesYes
    Name ChangesNoYesYes
    +
    +

    Upgrading from Gold to Platinum will only cost $20. If you have any further questions or concerns, feel free to contact me at <%= mail_to Danbooru.config.contact_email, nil, :encode => :javascript %>.

    + + <% if true || CurrentUser.user.level < User::Levels::GOLD %> + <%= stripe_button("Upgrade to Gold", 2000) %> + <%= stripe_button("Upgrade to Platinum", 4000) %> + <% elsif CurrentUser.user.level < User::Levels::PLATINUM %> + <%= stripe_button("Upgrade Gold to Platinum", 2000) %> + <% end %>
    diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index a1a65005a..7a053c87e 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -9,7 +9,11 @@ module Danbooru # The name of this Danbooru. def app_name - "Danbooru" + if CurrentUser.safe_mode? + "Safebooru" + else + "Danbooru" + end end # The hostname of the server. @@ -322,5 +326,13 @@ module Danbooru def shared_dir_path "/var/www/danbooru2/shared" end + + def stripe_secret_key + "sk_test_kHviyCxbt9kBxeu46TeefJQH" + end + + def stripe_publishable_key + "pk_test_5lKN65jYpUw8EActq8RMkQxH" + end end end diff --git a/config/initializers/stripe.rb b/config/initializers/stripe.rb new file mode 100644 index 000000000..43495044b --- /dev/null +++ b/config/initializers/stripe.rb @@ -0,0 +1 @@ +Stripe.api_key = Danbooru.config.stripe_secret_key diff --git a/config/routes.rb b/config/routes.rb index fc44b092f..518d30050 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -234,16 +234,15 @@ Rails.application.routes.draw do resources :uploads resources :users do collection do - get :upgrade_information get :search get :custom_style end member do delete :cache - post :upgrade end end + resource :user_upgrade, :only => [:new, :create, :show] resources :user_feedbacks do collection do get :search diff --git a/test/controllers/user_upgrades_controller_test.rb b/test/controllers/user_upgrades_controller_test.rb new file mode 100644 index 000000000..23b973d29 --- /dev/null +++ b/test/controllers/user_upgrades_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UserUpgradesControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end