diff --git a/app/controllers/settings/payments_controller.rb b/app/controllers/settings/payments_controller.rb index a5f014c3f..7a5f3dd53 100644 --- a/app/controllers/settings/payments_controller.rb +++ b/app/controllers/settings/payments_controller.rb @@ -5,10 +5,14 @@ class Settings::PaymentsController < ApplicationController def show @family = Current.family - @one_time_contribution_url = stripe&.payment_link_url + @one_time_contribution_url = stripe&.payment_link_url(payment_link_id:) 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 b7266e065..3639451b5 100644 --- a/app/models/provider/stripe.rb +++ b/app/models/provider/stripe.rb @@ -67,7 +67,7 @@ class Provider::Stripe client.v1.customers.update(customer_id, metadata: metadata) end - def payment_link_url(payment_link_id: ENV["STRIPE_PAYMENT_LINK_ID"]) + def payment_link_url(payment_link_id:) return nil if payment_link_id.blank? client.v1.payment_links.retrieve(payment_link_id).url diff --git a/test/controllers/settings/payments_controller_test.rb b/test/controllers/settings/payments_controller_test.rb index 5b00f3de8..2c45c31a9 100644 --- a/test/controllers/settings/payments_controller_test.rb +++ b/test/controllers/settings/payments_controller_test.rb @@ -15,8 +15,11 @@ class Settings::PaymentsControllerTest < ActionDispatch::IntegrationTest test "shows payment settings when family has stripe_customer_id" do @family.update!(stripe_customer_id: "cus_test123") + ENV.stubs(:[]).with("STRIPE_PAYMENT_LINK_ID").returns("plink_test123") stripe = mock - stripe.expects(:payment_link_url).returns("https://buy.stripe.com/test_payment_link") + stripe.expects(:payment_link_url) + .with(payment_link_id: "plink_test123") + .returns("https://buy.stripe.com/test_payment_link") Provider::Registry.stubs(:get_provider).with(:stripe).returns(stripe) get settings_payment_path @@ -30,8 +33,11 @@ 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") + ENV.stubs(:[]).with("STRIPE_PAYMENT_LINK_ID").returns("plink_test123") stripe = mock - stripe.expects(:payment_link_url).returns(nil) + stripe.expects(:payment_link_url) + .with(payment_link_id: "plink_test123") + .returns(nil) Provider::Registry.stubs(:get_provider).with(:stripe).returns(stripe) get settings_payment_path diff --git a/test/models/provider/stripe_test.rb b/test/models/provider/stripe_test.rb index 41fd8fa53..197d7d056 100644 --- a/test/models/provider/stripe_test.rb +++ b/test/models/provider/stripe_test.rb @@ -72,6 +72,8 @@ class Provider::StripeTest < ActiveSupport::TestCase Stripe::StripeClient.stubs(:new).returns(client) Sentry.expects(:capture_exception).with(instance_of(StandardError)) + Rails.logger.expects(:error) + .with("Error fetching payment link plink_test123: not found") stripe = Provider::Stripe.new(secret_key: "foo", webhook_secret: "bar") assert_nil stripe.payment_link_url(payment_link_id: "plink_test123")