-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3837 from luca-landa/improve-psp-switching-errors
Improve payment service providers switching errors
- Loading branch information
Showing
7 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
core/db/migrate/20201127212108_add_type_before_removal_to_spree_payment_methods.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddTypeBeforeRemovalToSpreePaymentMethods < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :spree_payment_methods, :type_before_removal, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :payment_method do | ||
desc "Deactivates old payment methods and fixes ActiveRecord::SubclassNotFound error, "\ | ||
"which happens after switching Payment Service Provider." | ||
task deactivate_unsupported_payment_methods: :environment do | ||
Spree::PaymentMethod.pluck(:id, :type).select do |id, type| | ||
ActiveSupport::Dependencies.constantize(type) | ||
rescue NameError | ||
fix_payment_method_record(id, type) | ||
end | ||
end | ||
|
||
def fix_payment_method_record(id, previous_type) | ||
connection = ActiveRecord::Base.connection | ||
false_value = connection.quoted_false | ||
connection.exec_update(<<-SQL | ||
UPDATE spree_payment_methods | ||
SET | ||
type='#{Spree::PaymentMethod.name}', | ||
type_before_removal='#{previous_type}', | ||
active=#{false_value}, | ||
available_to_users=#{false_value}, | ||
available_to_admin=#{false_value} | ||
WHERE id=#{id}; | ||
SQL | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'payment_method:deactivate_unsupported_payment_methods' do | ||
include_context( | ||
'rake', | ||
task_name: 'payment_method:deactivate_unsupported_payment_methods', | ||
task_path: Spree::Core::Engine.root.join('lib/tasks/payment_method.rake'), | ||
) | ||
|
||
let!(:unsupported_payment_method_id) { 0 } | ||
let!(:unsupported_payment_method) { create(:payment_method, id: unsupported_payment_method_id) } | ||
let!(:supported_payment_method) { create(:payment_method, id: 1) } | ||
|
||
def unsupported_payment_method_reloaded | ||
Spree::PaymentMethod.find_by(id: unsupported_payment_method_id) | ||
end | ||
|
||
before do | ||
unsupported_payment_method.update type: 'UnsupportedPaymentMethod' | ||
end | ||
|
||
context "with an unsupported payment method" do | ||
it "allows payment method records retrieval" do | ||
task.invoke | ||
|
||
expect { | ||
Spree::PaymentMethod.find_by(id: unsupported_payment_method_id) | ||
}.not_to raise_error | ||
end | ||
end | ||
|
||
context "on an unsupported payment method" do | ||
before { task.invoke } | ||
subject { unsupported_payment_method_reloaded } | ||
|
||
it "sets payment method type to 'Spree::PaymentMethod'" do | ||
expect(subject.type).to eq 'Spree::PaymentMethod' | ||
end | ||
|
||
it "sets payment method type_before_removal correctly" do | ||
expect(subject.type_before_removal).to eq 'UnsupportedPaymentMethod' | ||
end | ||
|
||
it "resets payment method active flag" do | ||
expect(subject.active).to be false | ||
end | ||
|
||
it "resets payment method available_to_users flag" do | ||
expect(subject.available_to_users).to be false | ||
end | ||
|
||
it "resets payment method available_to_admin flag" do | ||
expect(subject.available_to_admin).to be false | ||
end | ||
end | ||
|
||
context "on a supported payment method" do | ||
it "does not change payment method attributes" do | ||
expect { task.invoke }.not_to change { supported_payment_method.reload } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters