mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-04 22:16:08 +00:00
Prevent Stripe payment methods without account owners from being saved
This commit is contained in:
@@ -74,7 +74,7 @@ module Spree
|
||||
|
||||
def restrict_stripe_account_change
|
||||
return unless @payment_method.try(:type) == "Spree::Gateway::StripeConnect"
|
||||
return unless @payment_method.preferred_enterprise_id
|
||||
return unless @payment_method.preferred_enterprise_id.andand > 0
|
||||
|
||||
@stripe_account_holder = Enterprise.find(@payment_method.preferred_enterprise_id)
|
||||
return if spree_current_user.enterprises.include? @stripe_account_holder
|
||||
|
||||
@@ -3,6 +3,8 @@ module Spree
|
||||
class StripeConnect < Gateway
|
||||
preference :enterprise_id, :integer
|
||||
|
||||
validate :ensure_enterprise_selected
|
||||
|
||||
attr_accessible :preferred_enterprise_id
|
||||
|
||||
CARD_TYPE_MAPPING = {
|
||||
@@ -132,6 +134,11 @@ module Spree
|
||||
Rails.logger.error("Stripe Error: #{e}")
|
||||
nil
|
||||
end
|
||||
|
||||
def ensure_enterprise_selected
|
||||
return if preferred_enterprise_id.andand > 0
|
||||
errors.add(:stripe_account_owner, I18n.t(:error_required))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -20,13 +20,37 @@ describe Spree::Admin::PaymentMethodsController do
|
||||
end
|
||||
end
|
||||
|
||||
context "as a user that does not manage the existing stripe account holder" do
|
||||
context "as a user that manages the existing stripe account holder" do
|
||||
before { enterprise2.update_attributes!(owner_id: user.id) }
|
||||
|
||||
it "allows the stripe account holder to be updated" do
|
||||
spree_put :update, params
|
||||
expect(payment_method.reload.preferred_enterprise_id).to eq enterprise1.id
|
||||
end
|
||||
|
||||
context "when no enterprise is selected as the account holder" do
|
||||
before { payment_method.update_attribute(:preferred_enterprise_id, nil) }
|
||||
|
||||
context "id not provided at all" do
|
||||
before { params[:payment_method].delete(:preferred_enterprise_id) }
|
||||
|
||||
it "does not save the payment method" do
|
||||
spree_put :update, params
|
||||
expect(response).to render_template :edit
|
||||
expect(assigns(:payment_method).errors.messages[:stripe_account_owner]).to include I18n.t(:error_required)
|
||||
end
|
||||
end
|
||||
|
||||
context "enterprise_id of 0" do
|
||||
before { params[:payment_method][:preferred_enterprise_id] = 0 }
|
||||
|
||||
it "does not save the payment method" do
|
||||
spree_put :update, params
|
||||
expect(response).to render_template :edit
|
||||
expect(assigns(:payment_method).errors.messages[:stripe_account_owner]).to include I18n.t(:error_required)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user