Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Admin: Open source the new payment methods UI

Damian Legawiec03024dfcbc4e
diff --git a/admin/app/controllers/spree/admin/payment_methods_controller.rb b/admin/app/controllers/spree/admin/payment_methods_controller.rb
new file mode 100644
index 0000000000..af7200b699
--- /dev/null
+++ b/admin/app/controllers/spree/admin/payment_methods_controller.rb
@@ -0,0 +1,31 @@
+module Spree
+ module Admin
+ class PaymentMethodsController < ResourceController
+ prepend_before_action :require_payment_type, only: [:new, :create]
+
+ private
+
+ def build_resource
+ @object = params[:payment_method].delete(:type).constantize.new if params[:payment_method].present?
+ end
+
+ def collection
+ return @collection if @collection.present?
+
+ params[:q] ||= {}
+
+ @collection = super.order(position: :asc)
+ @search = @collection.ransack(params[:q])
+ @collection = @search.result.page(params[:page]).per(params[:per_page])
+ end
+
+ def update_turbo_stream_enabled?
+ true
+ end
+
+ def require_payment_type
+ redirect_to spree.admin_payment_methods_path unless params.dig(:payment_method, :type).present?
+ end
+ end
+ end
+end
diff --git a/admin/app/helpers/spree/admin/payments_helper.rb b/admin/app/helpers/spree/admin/payments_helper.rb
index d2394fa6b9..b2200357b5 100644
--- a/admin/app/helpers/spree/admin/payments_helper.rb
+++ b/admin/app/helpers/spree/admin/payments_helper.rb
@@ -17,7 +17,7 @@ module Spree
end
def available_payment_methods
- @available_payment_methods ||= Spree::PaymentMethod.providers.map(&:new).delete_if { |payment_method| !payment_method.show_in_admin? && current_store.payment_methods.pluck(:type).include?(payment_method.type) }
+ @available_payment_methods ||= Spree::PaymentMethod.providers.map { |provider| provider.name.constantize.new }.delete_if { |payment_method| !payment_method.show_in_admin? || current_store.payment_methods.pluck(:type).include?(payment_method.type) }
end
end
end
diff --git a/admin/app/views/spree/admin/payment_methods/_available_payment_method.html.erb b/admin/app/views/spree/admin/payment_methods/_available_payment_method.html.erb
new file mode 100644
index 0000000000..3ab6464487
--- /dev/null
+++ b/admin/app/views/spree/admin/payment_methods/_available_payment_method.html.erb
@@ -0,0 +1,21 @@
+<tr data-controller="row-link">
+ <td class="cursor-pointer w-30" data-action="click->row-link#openLink">
+ <div class="d-flex align-items-center">
+ <% if payment_method_icon_tag(payment_method.payment_icon_name).present? %>
+ <%= payment_method_icon_tag(payment_method.payment_icon_name, height: 30, class: 'mr-2') %>
+ <% end %>
+
+ <%= payment_method.name %>
+ </div>
+ </td>
+ <td class="cursor-pointer w-40 text-wrap" data-action="click->row-link#openLink">
+ <% if payment_method.try(:description_partial_name) %>
+ <%= render "spree/admin/payment_methods/descriptions/#{payment_method.description_partial_name}", payment_method: payment_method %>
+ <% end %>
+ </td>
+ <td class="actions w-30">
+ <div class="d-flex justify-content-end">
+ <%= link_to_with_icon 'plus', Spree.t(:add), spree.new_admin_payment_method_path(payment_method: { type: payment_method.type }), class: 'btn btn-light btn-sm', data: { row_link_target: :link } %>
+ </div>
+ </td>
+</tr>
diff --git a/admin/app/views/spree/admin/payment_methods/_check.html.erb b/admin/app/views/spree/admin/payment_methods/_check.html.erb
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/admin/app/views/spree/admin/payment_methods/_form.html.erb b/admin/app/views/spree/admin/payment_methods/_form.html.erb
new file mode 100644
index 0000000000..060aa34a3f
--- /dev/null
+++ b/admin/app/views/spree/admin/payment_methods/_form.html.erb
@@ -0,0 +1,66 @@
+<% if @object.respond_to?(:configuration_guide_partial_name) %>
+ <%= render "spree/admin/payment_methods/configuration_guides/#{@object.configuration_guide_partial_name}", f: f %>
+<% end %>
+
+
+<div class="row">
+ <% if !preference_fields(@object, f).empty? || @object.respond_to?(:custom_form_fields_partial_name) %>
+ <div class="col-12 col-md-6">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="mb-0"><%= Spree.t(:payment_provider_settings) %></h5>
+ </div>
+ <div class="card-body">
+ <%= preference_fields(@object, f) unless preference_fields(@object, f).empty? %>
+
+ <% if @object.respond_to?(:custom_form_fields_partial_name) %>
+ <%= render "spree/admin/payment_methods/custom_form_fields/#{@object.custom_form_fields_partial_name}", f: f %>
+ <% end %>
+ </div>
+ </div>
+ </div>
+ <% end %>
+
+ <div class="col-12 col-md-6">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="mb-0"><%= Spree.t(:display_settings) %></h5>
+ </div>
+ <div class="card-body">
+ <div class="form-group">
+ <%= label_tag :payment_method_name, Spree.t(:name) %>
+ <%= text_field :payment_method, :name, class: 'form-control' %>
+ <span class="text-muted form-text mt-2">
+ This name will be used to identify the payment method on the storefront
+ </span>
+
+ <%= error_message_on :payment_method, :name %>
+ </div>
+
+ <% if can?(:manage, Spree::Store) && available_stores.count > 1 %>
+ <div class="form-group">
+ <%= label_tag :payment_method_stores, Spree.t(:stores) %>
+ <%= collection_select(:payment_method, :store_ids, available_stores, :id, :unique_name, {}, { multiple: true, class: 'select2' }) %>
+ </div>
+ <% end %>
+
+ <div class="form-group">
+ <%= label_tag :payment_method_display_on, Spree.t(:display) %>
+ <%= f.select :display_on, display_on_options, {}, { class: 'custom-select' } %>
+ </div>
+ <div class="form-group">
+ <%= label_tag :payment_method_auto_capture, Spree.t(:auto_capture) %>
+ <%= select(:payment_method, :auto_capture, [["#{Spree.t(:use_app_default)} (#{Spree::Config[:auto_capture]})", ''], [Spree.t(:say_yes), true], [Spree.t(:say_no), false]], {}, {class: 'custom-select'}) %>
+ </div>
+
+ <div class="form-group">
+ <label><%= Spree.t(:active) %></label>
+ <div class="custom-control custom-switch">
+ <%= f.check_box :active, class: 'custom-control-input' %>
+ <%= f.label :active, '&nbsp;'.html_safe, class: 'custom-control-label' %>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+</div>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/payment_methods/_payment_method.html.erb b/admin/app/views/spree/admin/payment_methods/_payment_method.html.erb
new file mode 100644
index 0000000000..39c0721dd7
--- /dev/null
+++ b/admin/app/views/spree/admin/payment_methods/_payment_method.html.erb
@@ -0,0 +1,36 @@
+<tr id="<%= spree_dom_id payment_method %>" data-controller="row-link" data-sortable-update-url="<%= spree.admin_payment_method_path(payment_method) %>">
+ <td class="w-10 move-handle text-center">
+ <%= icon('grip-vertical', class: 'rounded hover-gray p-2') %>
+ </td>
+ <td class="cursor-pointer w-55 text-wrap" data-action="click->row-link#openLink">
+ <div class="d-flex align-items-center">
+ <% if payment_method_icon_tag(payment_method.payment_icon_name).present? %>
+ <%= payment_method_icon_tag(payment_method.payment_icon_name, height: 60, class: 'with-tip', title: payment_method.name) %>
+ <% else %>
+ <%= payment_method_icon_tag('credit-card', height: 60, class: 'with-tip', title: payment_method.name) %>
+ <% end %>
+
+ <% if payment_method.try(:description_partial_name) %>
+ <div class="mx-3">
+ <%= render "spree/admin/payment_methods/descriptions/#{payment_method.description_partial_name}", payment_method: payment_method %>
+ </div>
+ <% end %>
+ </div>
+ </td>
+ <td class="w-20">
+ <%= form_for payment_method, as: :payment_method, url: spree.admin_payment_method_path(payment_method), method: :patch, data: { controller: 'auto-submit' } do |f| %>
+ <%= f.select :display_on, display_on_options, {}, { class: 'custom-select custom-select-sm w-auto', data: { action: 'auto-submit#submit' } } %>
+ <% end %>
+ </td>
+ <td class="w-10 text-center">
+ <%= form_for payment_method, as: :payment_method, url: spree.admin_payment_method_path(payment_method), method: :patch, data: { controller: 'auto-submit' } do |f| %>
+ <div class="custom-control custom-switch">
+ <%= f.check_box :active, class: 'custom-control-input', data: { action: 'auto-submit#submit' }, id: "payment_method_active_#{payment_method.id}" %>
+ <%= f.label :active, '&nbsp;'.html_safe, class: 'custom-control-label', for: "payment_method_active_#{payment_method.id}" %>
+ </div>
+ <% end %>
+ </td>
+ <td class="actions w-5">
+ <%= link_to_edit(payment_method, data: { row_link_target: :link }) %>
+ </td>
+</tr>
diff --git a/admin/app/views/spree/admin/payment_methods/descriptions/_check.html.erb b/admin/app/views/spree/admin/payment_methods/descriptions/_check.html.erb
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/admin/app/views/spree/admin/payment_methods/descriptions/_gateway.html.erb b/admin/app/views/spree/admin/payment_methods/descriptions/_gateway.html.erb
new file mode 100644
index 0000000000..1cc19ab02a
--- /dev/null
+++ b/admin/app/views/spree/admin/payment_methods/descriptions/_gateway.html.erb
@@ -0,0 +1 @@
+Classic ActiveMerchant gateway provided by community.
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/payment_methods/descriptions/_store_credit.html.erb b/admin/app/views/spree/admin/payment_methods/descriptions/_store_credit.html.erb
new file mode 100644
index 0000000000..b738b2016a
--- /dev/null
+++ b/admin/app/views/spree/admin/payment_methods/descriptions/_store_credit.html.erb
@@ -0,0 +1 @@
+Allow customers to use store credit for payment. Issue store credit to customers from the admin.
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/payment_methods/edit.html.erb b/admin/app/views/spree/admin/payment_methods/edit.html.erb
new file mode 100644
index 0000000000..e44011187c
--- /dev/null
+++ b/admin/app/views/spree/admin/payment_methods/edit.html.erb
@@ -0,0 +1,16 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_payment_methods_path %>
+ <%= @payment_method.name %>
+<% end %>
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @payment_method } %>
+
+<% if @error %>
+ <div class="alert alert-danger">
+ <%= auto_link(@error) %>
+ </div>
+<% end %>
+
+<%= form_for @payment_method, as: :payment_method, url: spree.admin_payment_method_path(@payment_method) do |f| %>
+ <%= render 'form', f: f %>
+ <%= render 'spree/admin/shared/edit_resource_links', f: f %>
+<% end %>
diff --git a/admin/app/views/spree/admin/payment_methods/index.html.erb b/admin/app/views/spree/admin/payment_methods/index.html.erb
new file mode 100644
index 0000000000..a437dd793a
--- /dev/null
+++ b/admin/app/views/spree/admin/payment_methods/index.html.erb
@@ -0,0 +1,38 @@
+<%= content_for(:page_title) do %>
+ <%= Spree.t(:payment_methods) %>
+<% end %>
+
+<div class="table-responsive card-lg p-0 mb-4">
+ <table class="table" id='listing_shipping_methods'>
+ <thead class="text-muted">
+ <tr>
+ <th></th>
+ <th><%= Spree.t(:payment_method) %></th>
+ <th><%= Spree.t(:visibility) %></th>
+ <th class="text-center"><%= Spree.t(:active) %></th>
+ <th class="actions"></th>
+ </tr>
+ </thead>
+ <tbody data-controller="sortable" data-sortable-handle-value=".move-handle" data-sortable-resource-name-value="payment_method" data-sortable-response-kind-value="turbo-stream">
+ <%= render partial: 'spree/admin/payment_methods/payment_method', collection: @payment_methods %>
+ </tbody>
+ </table>
+</div>
+
+<% if available_payment_methods.present? %>
+ <h5 class="pb-2 pt-3">Available Payment Methods</h5>
+ <div class="table-responsive card-lg p-0">
+ <table class="table" id='listing_shipping_methods'>
+ <thead class="text-muted">
+ <tr>
+ <th><%= Spree.t(:name) %></th>
+ <th><%= Spree.t(:description) %></th>
+ <th class="actions"></th>
+ </tr>
+ </thead>
+ <tbody>
+ <%= render partial: 'spree/admin/payment_methods/available_payment_method', collection: available_payment_methods, as: :payment_method %>
+ </tbody>
+ </table>
+ </div>
+<% end %>
diff --git a/admin/app/views/spree/admin/payment_methods/new.html.erb b/admin/app/views/spree/admin/payment_methods/new.html.erb
new file mode 100644
index 0000000000..ba5a17f7cd
--- /dev/null
+++ b/admin/app/views/spree/admin/payment_methods/new.html.erb
@@ -0,0 +1,16 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_payment_methods_path %>
+ <%= @payment_method.name %>
+<% end %>
+<% if @error %>
+ <div class="alert alert-danger">
+ <%= auto_link(@error) %>
+ </div>
+<% end %>
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @payment_method } %>
+
+<%= form_for @payment_method, as: :payment_method, url: spree.admin_payment_methods_path do |f| %>
+ <%= f.hidden_field :type %>
+ <%= render 'form', f: f %>
+ <%= render 'spree/admin/shared/new_resource_links' %>
+<% end %>
diff --git a/admin/app/views/spree/admin/payment_methods/update.turbo_stream.erb b/admin/app/views/spree/admin/payment_methods/update.turbo_stream.erb
new file mode 100644
index 0000000000..1bda62b309
--- /dev/null
+++ b/admin/app/views/spree/admin/payment_methods/update.turbo_stream.erb
@@ -0,0 +1 @@
+<%= turbo_render_alerts %>
diff --git a/admin/config/routes.rb b/admin/config/routes.rb
index 12592ae907..fc53329051 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -17,6 +17,7 @@ Spree::Core::Engine.add_routes do
resource :profile, controller: 'profile', only: %i[edit update]
# store settings
+ resources :payment_methods, except: :show
resources :shipping_categories, except: :show
resources :store_credit_categories
resources :tax_rates, except: :show
diff --git a/admin/spec/controllers/spree/admin/payment_methods_controller_spec.rb b/admin/spec/controllers/spree/admin/payment_methods_controller_spec.rb
new file mode 100644
index 0000000000..4207be9a23
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/payment_methods_controller_spec.rb
@@ -0,0 +1,47 @@
+require 'spec_helper'
+
+describe Spree::Admin::PaymentMethodsController, type: :controller do
+ stub_authorization!
+
+ describe '#index' do
+ it 'renders the index template' do
+ get :index
+ expect(response.status).to eq(200)
+ end
+ end
+
+ describe '#new' do
+ context 'without type param' do
+ it 'redirects to payment methods index' do
+ get :new
+ expect(response).to redirect_to(spree.admin_payment_methods_path)
+ end
+ end
+
+ context 'with type param' do
+ it 'renders the new template' do
+ get :new, params: { payment_method: { type: 'Spree::Gateway::Bogus' } }
+ expect(response.status).to eq(200)
+ end
+
+ it 'assigns the type to the payment method' do
+ get :new, params: { payment_method: { type: 'Spree::Gateway::Bogus' } }
+ expect(assigns(:payment_method).type).to eq('Spree::Gateway::Bogus')
+ end
+ end
+ end
+
+ describe '#create' do
+ it 'creates the payment method' do
+ expect { post :create, params: { payment_method: { type: 'Spree::Gateway::Bogus' } } }.to change(Spree::Gateway::Bogus, :count).by(1)
+ end
+ end
+
+ describe '#update' do
+ let(:payment_method) { create(:credit_card_payment_method) }
+
+ it 'updates the payment method' do
+ expect { put :update, params: { id: payment_method.id, payment_method: { preferred_dummy_key: 'NEW_VALUE' } } }.to change { payment_method.reload.preferred_dummy_key }.to('NEW_VALUE')
+ end
+ end
+end
diff --git a/core/app/models/spree/gateway.rb b/core/app/models/spree/gateway.rb
index 84553c9b60..b2fe8a959f 100644
--- a/core/app/models/spree/gateway.rb
+++ b/core/app/models/spree/gateway.rb
@@ -4,7 +4,7 @@ module Spree
delegate :authorize, :purchase, :capture, :void, :credit, to: :provider
- before_validation :set_name
+ after_initialize :set_name, if: :new_record?
validates :type, presence: true, inclusion: { in: :valid_providers_list }
@@ -80,7 +80,7 @@ module Spree
private
def set_name
- self.name ||= provider_class.name.demodulize.titleize
+ self.name ||= self.class.name.demodulize.titleize.gsub(/Gateway/, '').strip
end
def valid_providers_list
diff --git a/core/app/models/spree/payment_method.rb b/core/app/models/spree/payment_method.rb
index 0768004b71..5dcc887faa 100644
--- a/core/app/models/spree/payment_method.rb
+++ b/core/app/models/spree/payment_method.rb
@@ -16,6 +16,7 @@ module Spree
scope :available_on_back_end, -> { active.where(display_on: [:back_end, :both]) }
validates :name, presence: true
+ auto_strip_attributes :name
has_many :store_payment_methods, class_name: 'Spree::StorePaymentMethod'
has_many :stores, class_name: 'Spree::Store', through: :store_payment_methods
@@ -47,7 +48,7 @@ module Spree
end
def payment_icon_name
- method_type
+ type.demodulize.gsub(/(^Spree::Gateway::|Gateway$)/, '').downcase.gsub(/\s+/, '').strip
end
def self.find_with_destroyed(*args)
diff --git a/core/app/models/spree/payment_method/store_credit.rb b/core/app/models/spree/payment_method/store_credit.rb
index a7694fea52..8e1a09c3ba 100644
--- a/core/app/models/spree/payment_method/store_credit.rb
+++ b/core/app/models/spree/payment_method/store_credit.rb
@@ -4,6 +4,10 @@ module Spree
::Spree::StoreCredit
end
+ def method_type
+ 'store_credit'
+ end
+
def can_capture?(payment)
['checkout', 'pending'].include?(payment.state)
end
diff --git a/core/spec/models/spree/gateway_spec.rb b/core/spec/models/spree/gateway_spec.rb
index 8db85fe329..3347dca451 100644
--- a/core/spec/models/spree/gateway_spec.rb
+++ b/core/spec/models/spree/gateway_spec.rb
@@ -31,16 +31,20 @@ describe Spree::Gateway, type: :model do
context 'Validations' do
before do
- expect(Spree::PaymentMethod).to receive(:providers).and_return([TestGateway, Spree::Gateway::Bogus])
+ allow(Spree::PaymentMethod).to receive(:providers).and_return([TestGateway, Spree::Gateway::Bogus])
end
it 'validates the type' do
- expect(TestGateway.new).to be_valid
+ expect(TestGateway.new.valid?).to be_truthy
+ end
+
+ it 'automatically sets the name' do
+ expect(TestGateway.new.name).to eq('Test')
end
end
context 'fetching payment sources' do
- let(:store) { create(:store) }
+ let(:store) { Spree::Store.default }
let(:order) { store.orders.create(user_id: 1) }
let(:has_card) { create(:credit_card_payment_method, stores: [store]) }
diff --git a/core/spec/models/spree/payment_method_spec.rb b/core/spec/models/spree/payment_method_spec.rb
index f5105f3f08..57550255ce 100644
--- a/core/spec/models/spree/payment_method_spec.rb
+++ b/core/spec/models/spree/payment_method_spec.rb
@@ -3,7 +3,7 @@ require 'spec_helper'
describe Spree::PaymentMethod, type: :model do
it_behaves_like 'metadata'
- let(:store) { create(:store) }
+ let(:store) { Spree::Store.default }
# register test gateways
before do
@@ -127,7 +127,6 @@ describe Spree::PaymentMethod, type: :model do
end
describe '#available_for_store?' do
- let!(:store) { create(:store) }
let!(:store_1) { create(:store) }
let!(:pm) { create(:credit_card_payment_method, stores: [store]) }
@@ -154,8 +153,12 @@ describe Spree::PaymentMethod, type: :model do
end
describe '#payment_source_class' do
- let(:payment_method) { create(:credit_card_payment_method) }
+ let(:payment_method) { build(:credit_card_payment_method) }
it { expect(payment_method.payment_source_class).to eq(Spree::CreditCard) }
end
+
+ describe '#payment_icon_name' do
+ it { expect(build(:credit_card_payment_method, type: 'Spree::Gateway::AuthorizeNetGateway').payment_icon_name).to eq('authorizenet') }
+ end
end