diff --git a/app/controllers/settings/payments_controller.rb b/app/controllers/settings/payments_controller.rb index 69a422d52..a5f014c3f 100644 --- a/app/controllers/settings/payments_controller.rb +++ b/app/controllers/settings/payments_controller.rb @@ -5,5 +5,11 @@ class Settings::PaymentsController < ApplicationController def show @family = Current.family + @one_time_contribution_url = stripe&.payment_link_url end + + private + def stripe + @stripe ||= Provider::Registry.get_provider(:stripe) + end end diff --git a/app/models/provider/stripe.rb b/app/models/provider/stripe.rb index 3a75dd257..e0786bf83 100644 --- a/app/models/provider/stripe.rb +++ b/app/models/provider/stripe.rb @@ -67,6 +67,12 @@ class Provider::Stripe client.v1.customers.update(customer_id, metadata: metadata) end + def payment_link_url(payment_link_id: ENV["STRIPE_PAYMENT_LINK_ID"]) + return nil if payment_link_id.blank? + + client.v1.payment_links.retrieve(payment_link_id).url + end + private attr_reader :client, :webhook_secret diff --git a/app/views/settings/payments/show.html.erb b/app/views/settings/payments/show.html.erb index 9967b9116..7836da5a5 100644 --- a/app/views/settings/payments/show.html.erb +++ b/app/views/settings/payments/show.html.erb @@ -60,18 +60,22 @@
<%= image_tag "stripe-logo.svg", class: "w-5 h-5 shrink-0" %>

- <% contribution_link = link_to( - t(".one_time_contribution_link_text"), - Rails.application.config.x.stripe.one_time_contribution_url, - class: "font-medium text-primary hover:underline transition", - target: "_blank", - rel: "noopener noreferrer" - ) %> + <% if @one_time_contribution_url.present? %> + <% contribution_link = link_to( + t(".one_time_contribution_link_text"), + @one_time_contribution_url, + class: "font-medium text-primary hover:underline transition", + target: "_blank", + rel: "noopener noreferrer" + ) %> - <%= t( - ".payment_via_stripe_html", - contribution_link: contribution_link - ) %> + <%= t( + ".payment_via_stripe_html", + contribution_link: contribution_link + ) %> + <% else %> + <%= t(".payment_via_stripe") %> + <% end %>

diff --git a/config/initializers/stripe.rb b/config/initializers/stripe.rb deleted file mode 100644 index 2ff289863..000000000 --- a/config/initializers/stripe.rb +++ /dev/null @@ -1,6 +0,0 @@ -Rails.application.configure do - config.x.stripe.one_time_contribution_url = ENV.fetch( - "STRIPE_ONE_TIME_CONTRIBUTION_URL", - "https://buy.stripe.com/3cIcN6euM23D7GQ3wT97G00" - ) -end diff --git a/config/locales/views/settings/en.yml b/config/locales/views/settings/en.yml index da8cc1bb8..4d394e9e1 100644 --- a/config/locales/views/settings/en.yml +++ b/config/locales/views/settings/en.yml @@ -24,6 +24,7 @@ en: payments: show: page_title: Payments + payment_via_stripe: Payment via Stripe payment_via_stripe_html: Payment via Stripe (%{contribution_link}) one_time_contribution_link_text: one-time contribution here subscription_subtitle: Update your credit card details diff --git a/test/controllers/settings/payments_controller_test.rb b/test/controllers/settings/payments_controller_test.rb index 430a0eab7..fd87e4011 100644 --- a/test/controllers/settings/payments_controller_test.rb +++ b/test/controllers/settings/payments_controller_test.rb @@ -15,12 +15,15 @@ class Settings::PaymentsControllerTest < ActionDispatch::IntegrationTest test "shows payment settings when family has stripe_customer_id" do @family.update!(stripe_customer_id: "cus_test123") + stripe = mock + stripe.expects(:payment_link_url).returns("https://buy.stripe.com/test_payment_link") + Provider::Registry.stubs(:get_provider).with(:stripe).returns(stripe) get settings_payment_path assert_response :success assert_select( "a[href=?]", - Rails.application.config.x.stripe.one_time_contribution_url, + "https://buy.stripe.com/test_payment_link", text: I18n.t("views.settings.payments.show.one_time_contribution_link_text") ) end diff --git a/test/models/provider/stripe_test.rb b/test/models/provider/stripe_test.rb index 68fb8ad8e..e4240a9bf 100644 --- a/test/models/provider/stripe_test.rb +++ b/test/models/provider/stripe_test.rb @@ -42,4 +42,22 @@ class Provider::StripeTest < ActiveSupport::TestCase assert_match /sub_.*/, result.subscription_id end end + + test "retrieves payment link url from stripe" do + payment_links = mock + payment_links.expects(:retrieve) + .with("plink_test123") + .returns(OpenStruct.new(url: "https://buy.stripe.com/test_payment_link")) + + client = mock + client.stubs(:v1).returns(OpenStruct.new(payment_links: payment_links)) + + Stripe::StripeClient.stubs(:new).returns(client) + stripe = Provider::Stripe.new(secret_key: "foo", webhook_secret: "bar") + + assert_equal( + "https://buy.stripe.com/test_payment_link", + stripe.payment_link_url(payment_link_id: "plink_test123") + ) + end end