Skip to content

Commit

Permalink
Add new Stripe code option (forem#21236)
Browse files Browse the repository at this point in the history
* Add new Stripe code option

* Stripe code
  • Loading branch information
benhalpern authored Aug 27, 2024
1 parent bf12426 commit 781f298
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 21 deletions.
13 changes: 11 additions & 2 deletions app/controllers/stripe_subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ class StripeSubscriptionsController < ApplicationController

def new
Stripe.api_key = Settings::General.stripe_api_key

item_code = if ENV.fetch("STRIPE_TAG_MODERATOR_ITEM_CODE", nil).present? && current_user&.tag_moderator?
ENV.fetch("STRIPE_TAG_MODERATOR_ITEM_CODE", nil)
elsif params[:item].present? && params[:item] != ENV.fetch("STRIPE_TAG_MODERATOR_ITEM_CODE", nil)
params[:item]
else
ENV.fetch("STRIPE_BASE_ITEM_CODE", nil)
end

session = Stripe::Checkout::Session.create(
line_items: [
{
price: params[:item] || ENV.fetch("STRIPE_BASE_ITEM_CODE", nil),
price: item_code,
quantity: 1
},
],
mode: params[:mode] || "subscription",
success_url: URL.url(ENV["SUBSCRIPTION_SUCCESS_URL"] || "/settings/billing"),
cancel_url: URL.url("/settings/billing"),
cancel_url: URL.url(ENV["SUBSCRIPTION_CANCEL_URL"] || "/settings/billing"),
customer_email: current_user.email,
metadata: {
user_id: current_user.id
Expand Down
99 changes: 80 additions & 19 deletions spec/requests/stripe_subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
let(:stripe_helper) { StripeMock.create_test_helper }
let(:stripe_api_key) { Settings::General.stripe_api_key }
let(:default_item_code) { ENV.fetch("STRIPE_BASE_ITEM_CODE", "default_code") }
let(:tag_moderator_item_code) { ENV.fetch("STRIPE_TAG_MODERATOR_ITEM_CODE", "tag_moderator_code") }
let(:subscription_success_url) { ENV["SUBSCRIPTION_SUCCESS_URL"] || "/settings/billing" }
let(:session_url) { "https://checkout.stripe.com/pay/test_session_id" }

describe "GET /stripe_subscriptions/new" do
before do
ENV["STRIPE_BASE_ITEM_CODE"] = "default_code"
ENV["STRIPE_TAG_MODERATOR_ITEM_CODE"] = "tag_moderator_code"
end

context "when the user is not signed in" do
it "redirects to the sign in page" do
get new_stripe_subscription_path
Expand All @@ -26,30 +32,85 @@

after { StripeMock.stop }

it "creates a new Stripe Checkout Session and redirects to the session URL" do
get new_stripe_subscription_path, params: { item: default_item_code }
context "when the user is a tag moderator" do
before do
allow(user).to receive(:tag_moderator?).and_return(true)
end

it "uses the tag moderator item code" do
get new_stripe_subscription_path

expect(Stripe::Checkout::Session).to have_received(:create).with(
line_items: [
{
price: default_item_code,
quantity: 1
expect(Stripe::Checkout::Session).to have_received(:create).with(
line_items: [
{
price: tag_moderator_item_code,
quantity: 1
},
],
mode: "subscription",
success_url: URL.url(subscription_success_url),
cancel_url: URL.url("/settings/billing"),
customer_email: user.email,
metadata: {
user_id: user.id
},
],
mode: "subscription",
success_url: URL.url(subscription_success_url),
cancel_url: URL.url("/settings/billing"),
customer_email: user.email,
metadata: {
user_id: user.id
},
)

expect(response).to redirect_to(session_url)
)

expect(response).to redirect_to(session_url)
end
end

context "when the user is not a tag moderator" do
before { allow(user).to receive(:tag_moderator?).and_return(false) }

it "uses the provided item code if it is different from the tag moderator item code" do
custom_item_code = "custom_item_code"
get new_stripe_subscription_path, params: { item: custom_item_code }

expect(Stripe::Checkout::Session).to have_received(:create).with(
line_items: [
{
price: custom_item_code,
quantity: 1
},
],
mode: "subscription",
success_url: URL.url(subscription_success_url),
cancel_url: URL.url("/settings/billing"),
customer_email: user.email,
metadata: {
user_id: user.id
},
)

expect(response).to redirect_to(session_url)
end

it "falls back to the default item code if no valid item code is provided" do
get new_stripe_subscription_path

expect(Stripe::Checkout::Session).to have_received(:create).with(
line_items: [
{
price: default_item_code,
quantity: 1
},
],
mode: "subscription",
success_url: URL.url(subscription_success_url),
cancel_url: URL.url("/settings/billing"),
customer_email: user.email,
metadata: {
user_id: user.id
},
)

expect(response).to redirect_to(session_url)
end
end

it "allows other host redirection" do
get new_stripe_subscription_path, params: { item: default_item_code }
get new_stripe_subscription_path
expect(response).to have_http_status(:found)
expect(response.headers["Location"]).to eq(session_url)
end
Expand Down

0 comments on commit 781f298

Please sign in to comment.