user upgrades: allow using promo codes during checkout.

Allow promo codes to be used during checkout if a secret promo=true url
param is passed. Allows promo codes to be offered without having the
promo code option always appear even when there aren't any active promos.
This commit is contained in:
evazion
2021-01-01 04:24:24 -06:00
parent 1d15ce2bcd
commit ecd29c1a66
4 changed files with 19 additions and 5 deletions

View File

@@ -4,7 +4,8 @@ class UserUpgradesController < ApplicationController
def create
@user_upgrade = authorize UserUpgrade.create(recipient: recipient, purchaser: CurrentUser.user, status: "pending", upgrade_type: params[:upgrade_type])
@country = params[:country] || CurrentUser.country || "US"
@checkout = @user_upgrade.create_checkout!(country: @country)
@allow_promotion_codes = params[:promo].to_s.truthy?
@checkout = @user_upgrade.create_checkout!(country: @country, allow_promotion_codes: @allow_promotion_codes)
respond_with(@user_upgrade)
end

View File

@@ -148,7 +148,7 @@ class UserUpgrade < ApplicationRecord
end
concerning :StripeMethods do
def create_checkout!(country: "US")
def create_checkout!(country: "US", allow_promotion_codes: false)
methods = payment_method_types(country)
currency = preferred_currency(country)
price_id = upgrade_price_id(currency)
@@ -160,6 +160,7 @@ class UserUpgrade < ApplicationRecord
client_reference_id: "user_upgrade_#{id}",
customer_email: purchaser.email_address&.address,
payment_method_types: methods,
allow_promotion_codes: allow_promotion_codes,
line_items: [{
price: price_id,
quantity: 1,

View File

@@ -102,12 +102,12 @@
<td><%= link_to "Get #{Danbooru.config.canonical_app_name} Platinum", login_path(url: new_user_upgrade_path), class: "login-button" %></td>
<% elsif @recipient.level == User::Levels::MEMBER %>
<td></td>
<td><%= button_to "Get #{Danbooru.config.canonical_app_name} Gold", user_upgrades_path(user_id: @recipient.id, upgrade_type: "gold", country: params[:country]), remote: true, disable_with: "Redirecting..." %></td>
<td><%= button_to "Get #{Danbooru.config.canonical_app_name} Platinum", user_upgrades_path(user_id: @recipient.id, upgrade_type: "platinum", country: params[:country]), remote: true, disable_with: "Redirecting..." %></td>
<td><%= button_to "Get #{Danbooru.config.canonical_app_name} Gold", user_upgrades_path(user_id: @recipient.id, upgrade_type: "gold", country: params[:country], promo: params[:promo]), remote: true, disable_with: "Redirecting..." %></td>
<td><%= button_to "Get #{Danbooru.config.canonical_app_name} Platinum", user_upgrades_path(user_id: @recipient.id, upgrade_type: "platinum", country: params[:country], promo: params[:promo]), remote: true, disable_with: "Redirecting..." %></td>
<% elsif @recipient.level == User::Levels::GOLD %>
<td></td>
<td><%= button_to "Get #{Danbooru.config.canonical_app_name} Gold", nil, disabled: true %></td>
<td><%= button_to "Get #{Danbooru.config.canonical_app_name} Platinum", user_upgrades_path(user_id: @recipient.id, upgrade_type: "gold_to_platinum", country: params[:country]), remote: true, disable_with: "Redirecting..." %></td>
<td><%= button_to "Get #{Danbooru.config.canonical_app_name} Platinum", user_upgrades_path(user_id: @recipient.id, upgrade_type: "gold_to_platinum", country: params[:country], promo: params[:promo]), remote: true, disable_with: "Redirecting..." %></td>
<% else %>
<td></td>
<td><%= button_to "Get #{Danbooru.config.canonical_app_name} Gold", nil, disabled: true %></td>

View File

@@ -38,6 +38,18 @@ class UserUpgradesControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end
should "render for the country param" do
get new_user_upgrade_path(country: "DE")
assert_response :success
end
should "render for the promo param" do
get new_user_upgrade_path(promo: "true")
assert_response :success
end
should "render for an anonymous user" do
get new_user_upgrade_path