diff --git a/Changelog.md b/Changelog.md index 27c3744a..2ad7cee4 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,7 @@ +## 2.4.1 (2023-03-03) + +- Allow `stripe_elements_tag` to accept a block. Thanks @chip ! + ## 2.4.0 (2023-02-04) - Add `tax_behavior` attribute to Price. Thanks @szechyjs ! diff --git a/Gemfile b/Gemfile index 4c60bec3..318d4131 100644 --- a/Gemfile +++ b/Gemfile @@ -16,7 +16,6 @@ group :test do gem 'webmock' # System tests gem 'capybara' - gem 'puma', '< 6' # https://github.com/teamcapybara/capybara/issues/2598 - gem 'selenium-webdriver' - gem 'webdrivers' + gem 'puma' + gem 'selenium-webdriver', '~> 4.18', '>= 4.18.1' end diff --git a/README.md b/README.md index bc5b449e..c6b8cb14 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ This gem can help your rails application integrate with Stripe in the following - [Configuring your plans and coupons](#configuring-your-plans-and-coupons) [Stripe Elements](#stripe-elements) +- [Custom Elements](#custom-elements) [Webhooks](#webhooks) @@ -322,6 +323,22 @@ Simply include the `stripe_elements_tag` anywhere below the `stripe_javascript_t <%= stripe_elements_tag submit_path: billing_path %> ``` +Additionally, you can pass a block containing custom form elements to stripe_elements_tag: + +## Custom Elements + +> Stripe::Rails allows you to easily include your own custom form elements +> within the Stripe form by including those form elements in a block passed to +> `stripe_elements_tag`: + +```erb +<%= stripe_javascript_tag %> +<%= stripe_elements_tag(submit_path: billing_path) do %> + <%= label_tag 'email', 'Email' %> + <%= text_field :user, :email %> +<% end %> +``` + ### Configuration options Stripe::Rails comes bundled with default CSS and Javascript for Stripe elements, making it easy to drop in to your app. You can also specify your own assets paths: diff --git a/app/helpers/stripe/javascript_helper.rb b/app/helpers/stripe/javascript_helper.rb index 32150afa..9b63efd1 100644 --- a/app/helpers/stripe/javascript_helper.rb +++ b/app/helpers/stripe/javascript_helper.rb @@ -10,14 +10,16 @@ def stripe_javascript_tag(stripe_js_version = DEFAULT_STRIPE_JS_VERSION) def stripe_elements_tag(submit_path:, css_path: asset_path("stripe_elements.css"), - js_path: asset_path("stripe_elements.js")) + js_path: asset_path("stripe_elements.js"), + &block) render partial: 'stripe/elements', locals: { submit_path: submit_path, label_text: t('stripe_rails.elements.label_text'), submit_button_text: t('stripe_rails.elements.submit_button_text'), css_path: css_path, - js_path: js_path + js_path: js_path, + block: block } end end diff --git a/app/views/stripe/_elements.html.erb b/app/views/stripe/_elements.html.erb index b9ea5631..73f454cd 100644 --- a/app/views/stripe/_elements.html.erb +++ b/app/views/stripe/_elements.html.erb @@ -5,6 +5,11 @@ <%= form_tag submit_path, id: "stripe-form" do %> + <% if local_assigns[:block] %> +
+ <%= capture(&local_assigns[:block]) %> +
+ <% end %> <%= label_tag :card_element, label_text %>
<%= submit_tag submit_button_text %> diff --git a/lib/stripe/rails/version.rb b/lib/stripe/rails/version.rb index c2f21077..df7b9c91 100644 --- a/lib/stripe/rails/version.rb +++ b/lib/stripe/rails/version.rb @@ -1,5 +1,5 @@ module Stripe module Rails - VERSION = '2.4.0'.freeze + VERSION = '2.4.1'.freeze end end diff --git a/test/javascript_helper_spec.rb b/test/javascript_helper_spec.rb index 8a321973..d0e3b104 100644 --- a/test/javascript_helper_spec.rb +++ b/test/javascript_helper_spec.rb @@ -77,5 +77,15 @@ end end end + + describe 'with block' do + let(:markup) { ''.html_safe } + + it 'should display block contents' do + block = lambda { markup } + result = view.stripe_elements_tag(submit_path: '/charge', &block) + assert_match %r%%, result + end + end end end diff --git a/test/spec_helper.rb b/test/spec_helper.rb index 78abc266..a876be2a 100644 --- a/test/spec_helper.rb +++ b/test/spec_helper.rb @@ -11,7 +11,6 @@ # Chrome Setup require 'selenium-webdriver' require 'capybara' -require 'webdrivers' Capybara.register_driver :selenium do |app| Capybara::Selenium::Driver.new(app, :browser => :chrome) end