diff options
author | Alessandro Desantis <desa.alessandro@gmail.com> | 2020-10-09 14:04:49 +0200 |
---|---|---|
committer | Alessandro Desantis <desa.alessandro@gmail.com> | 2020-10-09 14:07:36 +0200 |
commit | d7369ba04b3657e47e21e6d24af516886ef0dbc4 (patch) | |
tree | e8279d237a87c52a931721c182bf02f55e6370fa | |
parent | d0d2abca6c2f3c57ed16087a816d2638134208d8 (diff) |
Fire events for subscription payment method updates
4 files changed, 71 insertions, 2 deletions
diff --git a/app/decorators/models/solidus_subscriptions/spree/wallet_payment_source/report_default_change_to_subscriptions.rb b/app/decorators/models/solidus_subscriptions/spree/wallet_payment_source/report_default_change_to_subscriptions.rb new file mode 100644 index 0000000..6ee754e --- /dev/null +++ b/app/decorators/models/solidus_subscriptions/spree/wallet_payment_source/report_default_change_to_subscriptions.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module SolidusSubscriptions + module Spree + module WalletPaymentSource + module ReportDefaultChangeToSubscriptions + def self.prepended(base) + base.after_save :report_default_change_to_subscriptions + end + + private + + def report_default_change_to_subscriptions + return if !previous_changes.key?('default') || !default? + + user.subscriptions.with_default_payment_source.each do |subscription| + ::Spree::Event.fire( + 'solidus_subscriptions.subscription_payment_method_changed', + subscription: subscription, + ) + end + end + end + end + end +end + +Spree::WalletPaymentSource.prepend(SolidusSubscriptions::Spree::WalletPaymentSource::ReportDefaultChangeToSubscriptions) diff --git a/app/models/solidus_subscriptions/subscription.rb b/app/models/solidus_subscriptions/subscription.rb index d3dd2f5..08de3c9 100644 --- a/app/models/solidus_subscriptions/subscription.rb +++ b/app/models/solidus_subscriptions/subscription.rb @@ -74,6 +74,10 @@ module SolidusSubscriptions joins(:installments).merge(Installment.unfulfilled) end) + scope :with_default_payment_source, (lambda do + where(payment_method: nil, payment_source: nil) + end) + def self.ransackable_scopes(_auth_object = nil) [:in_processing_state] end @@ -325,6 +329,13 @@ module SolidusSubscriptions subscription: self, ) end + + if previous_changes.key?('payment_source_id') || previous_changes.key?('payment_source_type') || previous_changes.key?('payment_method_id') + ::Spree::Event.fire( + 'solidus_subscriptions.subscription_payment_method_changed', + subscription: self, + ) + end end end end diff --git a/spec/models/solidus_subscriptions/subscription_spec.rb b/spec/models/solidus_subscriptions/subscription_spec.rb index 60a37e6..21e1015 100644 --- a/spec/models/solidus_subscriptions/subscription_spec.rb +++ b/spec/models/solidus_subscriptions/subscription_spec.rb @@ -64,6 +64,18 @@ RSpec.describe SolidusSubscriptions::Subscription, type: :model do subscription: subscription, ) end + + it 'tracks payment method changes' do + stub_const('Spree::Event', class_spy(Spree::Event)) + + subscription = create(:subscription) + subscription.update!(payment_source: create(:credit_card)) + + expect(Spree::Event).to have_received(:fire).with( + 'solidus_subscriptions.subscription_payment_method_changed', + subscription: subscription, + ) + end end describe '#cancel' do @@ -445,7 +457,7 @@ RSpec.describe SolidusSubscriptions::Subscription, type: :model do end context 'when the subscription has no payment method' do - it "returns the default source from the user's wallet" do + it "returns the default source from the user's wallet_payment_source" do user = create(:user) payment_source = create(:credit_card, gateway_customer_profile_id: 'BGS-123', user: user) wallet_payment_source = user.wallet.add(payment_source) @@ -487,7 +499,7 @@ RSpec.describe SolidusSubscriptions::Subscription, type: :model do end context 'when the subscription has no payment method' do - it "returns the method from the default source in the user's wallet" do + it "returns the method from the default source in the user's wallet_payment_source" do user = create(:user) payment_source = create(:credit_card, gateway_customer_profile_id: 'BGS-123', user: user) wallet_payment_source = user.wallet.add(payment_source) diff --git a/spec/models/spree/wallet_payment_source_spec.rb b/spec/models/spree/wallet_payment_source_spec.rb new file mode 100644 index 0000000..374e235 --- /dev/null +++ b/spec/models/spree/wallet_payment_source_spec.rb @@ -0,0 +1,18 @@ +RSpec.describe Spree::WalletPaymentSource do + describe 'setting it as the default' do + it 'reports a payment method changed event for subscriptions that use the default payment source' do + stub_const('Spree::Event', class_spy(Spree::Event)) + user = create(:user) + subscription = create(:subscription, user: user) + payment_source = create(:credit_card, user: user) + wallet_payment_source = user.wallet.add(payment_source) + + user.wallet.default_wallet_payment_source = wallet_payment_source + + expect(Spree::Event).to have_received(:fire).with( + 'solidus_subscriptions.subscription_payment_method_changed', + subscription: subscription, + ) + end + end +end |