diff --git a/app/controllers/settings/payments_controller.rb b/app/controllers/settings/payments_controller.rb index e94463311..4912cab27 100644 --- a/app/controllers/settings/payments_controller.rb +++ b/app/controllers/settings/payments_controller.rb @@ -5,14 +5,10 @@ class Settings::PaymentsController < ApplicationController def show @family = Current.family - @one_time_contribution_url = stripe&.payment_link_url(payment_link_id:) if payment_link_id.present? + @one_time_contribution_url = stripe&.one_time_contribution_url end private - def payment_link_id - ENV["STRIPE_PAYMENT_LINK_ID"] - end - def stripe @stripe ||= Provider::Registry.get_provider(:stripe) end diff --git a/app/models/provider/stripe.rb b/app/models/provider/stripe.rb index 3639451b5..402e32134 100644 --- a/app/models/provider/stripe.rb +++ b/app/models/provider/stripe.rb @@ -67,6 +67,10 @@ class Provider::Stripe client.v1.customers.update(customer_id, metadata: metadata) end + def one_time_contribution_url + payment_link_url(payment_link_id: ENV["STRIPE_PAYMENT_LINK_ID"]) + end + def payment_link_url(payment_link_id:) return nil if payment_link_id.blank? diff --git a/test/controllers/settings/payments_controller_test.rb b/test/controllers/settings/payments_controller_test.rb index 636914157..446db4fc6 100644 --- a/test/controllers/settings/payments_controller_test.rb +++ b/test/controllers/settings/payments_controller_test.rb @@ -15,10 +15,8 @@ class Settings::PaymentsControllerTest < ActionDispatch::IntegrationTest test "shows payment settings when family has stripe_customer_id" do @family.update!(stripe_customer_id: "cus_test123") - Settings::PaymentsController.any_instance.stubs(:payment_link_id).returns("plink_test123") stripe = mock - stripe.expects(:payment_link_url) - .with(payment_link_id: "plink_test123") + stripe.expects(:one_time_contribution_url) .returns("https://buy.stripe.com/test_payment_link") Provider::Registry.stubs(:get_provider).with(:stripe).returns(stripe) @@ -33,10 +31,8 @@ class Settings::PaymentsControllerTest < ActionDispatch::IntegrationTest test "shows payment settings without contribution link when payment link is unavailable" do @family.update!(stripe_customer_id: "cus_test123") - Settings::PaymentsController.any_instance.stubs(:payment_link_id).returns("plink_test123") stripe = mock - stripe.expects(:payment_link_url) - .with(payment_link_id: "plink_test123") + stripe.expects(:one_time_contribution_url) .returns(nil) Provider::Registry.stubs(:get_provider).with(:stripe).returns(stripe) @@ -50,18 +46,4 @@ class Settings::PaymentsControllerTest < ActionDispatch::IntegrationTest assert_select "p", text: I18n.t("views.settings.payments.show.payment_via_stripe") end - test "shows payment settings without contribution link when payment link id is missing" do - @family.update!(stripe_customer_id: "cus_test123") - Settings::PaymentsController.any_instance.stubs(:payment_link_id).returns(nil) - Provider::Registry.expects(:get_provider).with(:stripe).never - - get settings_payment_path - assert_response :success - assert_select( - "a", - text: I18n.t("views.settings.payments.show.one_time_contribution_link_text"), - count: 0 - ) - assert_select "p", text: I18n.t("views.settings.payments.show.payment_via_stripe") - end end diff --git a/test/models/provider/stripe_test.rb b/test/models/provider/stripe_test.rb index 197d7d056..763025033 100644 --- a/test/models/provider/stripe_test.rb +++ b/test/models/provider/stripe_test.rb @@ -61,6 +61,32 @@ class Provider::StripeTest < ActiveSupport::TestCase ) end + test "retrieves one-time contribution url from configured payment link" do + stripe = Provider::Stripe.new(secret_key: "foo", webhook_secret: "bar") + stripe.expects(:payment_link_url) + .with(payment_link_id: "plink_test123") + .returns("https://buy.stripe.com/test_payment_link") + + original_payment_link_id = ENV["STRIPE_PAYMENT_LINK_ID"] + ENV["STRIPE_PAYMENT_LINK_ID"] = "plink_test123" + + assert_equal "https://buy.stripe.com/test_payment_link", stripe.one_time_contribution_url + ensure + ENV["STRIPE_PAYMENT_LINK_ID"] = original_payment_link_id + end + + test "returns nil for one-time contribution url when payment link id is missing" do + stripe = Provider::Stripe.new(secret_key: "foo", webhook_secret: "bar") + stripe.expects(:payment_link_url).with(payment_link_id: nil).returns(nil) + + original_payment_link_id = ENV["STRIPE_PAYMENT_LINK_ID"] + ENV.delete("STRIPE_PAYMENT_LINK_ID") + + assert_nil stripe.one_time_contribution_url + ensure + ENV["STRIPE_PAYMENT_LINK_ID"] = original_payment_link_id + end + test "returns nil when payment link retrieval fails" do payment_links = mock payment_links.expects(:retrieve)