From 3e0dd9272e7ec39770c77477d60b92179bd8ee18 Mon Sep 17 00:00:00 2001 From: Vincent Pochet Date: Fri, 15 Nov 2024 18:09:24 +0100 Subject: [PATCH] misc(PaymentProviders): Refact Webhooks folder structure --- .../adyen/webhooks/base_service.rb | 24 +++++++++++ .../webhooks}/chargeback_service.rb | 8 +--- .../payment_providers/adyen_service.rb | 2 +- .../stripe/webhooks/base_service.rb | 40 +++++++++++++++++++ .../charge_dispute_closed_service.rb | 4 +- .../webhooks}/customer_updated_service.rb | 4 +- .../setup_intent_succeeded_service.rb | 2 +- .../payment_providers/stripe_service.rb | 6 +-- .../webhooks/base_service.rb | 38 ------------------ .../webhooks}/chargeback_service_spec.rb | 2 +- .../charge_dispute_closed_service_spec.rb | 2 +- .../customer_updated_service_spec.rb | 2 +- .../setup_intent_succeeded_service_spec.rb | 2 +- .../payment_providers/stripe_service_spec.rb | 8 ++-- 14 files changed, 83 insertions(+), 61 deletions(-) create mode 100644 app/services/payment_providers/adyen/webhooks/base_service.rb rename app/services/payment_providers/{webhooks/adyen => adyen/webhooks}/chargeback_service.rb (81%) create mode 100644 app/services/payment_providers/stripe/webhooks/base_service.rb rename app/services/payment_providers/{webhooks/stripe => stripe/webhooks}/charge_dispute_closed_service.rb (95%) rename app/services/payment_providers/{webhooks/stripe => stripe/webhooks}/customer_updated_service.rb (96%) rename app/services/payment_providers/{webhooks/stripe => stripe/webhooks}/setup_intent_succeeded_service.rb (98%) delete mode 100644 app/services/payment_providers/webhooks/base_service.rb rename spec/services/payment_providers/{webhooks/adyen => adyen/webhooks}/chargeback_service_spec.rb (97%) rename spec/services/payment_providers/{webhooks/stripe => stripe/webhooks}/charge_dispute_closed_service_spec.rb (97%) rename spec/services/payment_providers/{webhooks/stripe => stripe/webhooks}/customer_updated_service_spec.rb (97%) rename spec/services/payment_providers/{webhooks/stripe => stripe/webhooks}/setup_intent_succeeded_service_spec.rb (98%) diff --git a/app/services/payment_providers/adyen/webhooks/base_service.rb b/app/services/payment_providers/adyen/webhooks/base_service.rb new file mode 100644 index 00000000000..9ab50c37aae --- /dev/null +++ b/app/services/payment_providers/adyen/webhooks/base_service.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module PaymentProviders + module Adyen + module Webhooks + class BaseService < BaseService + def initialize(organization_id:, event_json:) + @organization = Organization.find(organization_id) + @event_json = event_json + + super + end + + private + + attr_reader :organization, :event_json + + def event + @event ||= JSON.parse(event_json)['notificationItems'].first&.dig('NotificationRequestItem') + end + end + end + end +end diff --git a/app/services/payment_providers/webhooks/adyen/chargeback_service.rb b/app/services/payment_providers/adyen/webhooks/chargeback_service.rb similarity index 81% rename from app/services/payment_providers/webhooks/adyen/chargeback_service.rb rename to app/services/payment_providers/adyen/webhooks/chargeback_service.rb index 77727bc70c1..8b1f286e0e0 100644 --- a/app/services/payment_providers/webhooks/adyen/chargeback_service.rb +++ b/app/services/payment_providers/adyen/webhooks/chargeback_service.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true module PaymentProviders - module Webhooks - module Adyen + module Adyen + module Webhooks class ChargebackService < BaseService def call status = event['additionalData']['disputeStatus'] @@ -21,10 +21,6 @@ def call private - def event - @event ||= JSON.parse(event_json)['notificationItems'].first&.dig('NotificationRequestItem') - end - def payment_dispute_lost_at Time.zone.parse(event['eventDate']) end diff --git a/app/services/payment_providers/adyen_service.rb b/app/services/payment_providers/adyen_service.rb index fe01f8c9bc2..6ce14609fe6 100644 --- a/app/services/payment_providers/adyen_service.rb +++ b/app/services/payment_providers/adyen_service.rb @@ -82,7 +82,7 @@ def handle_event(organization:, event_json:) result = service.update_status(provider_refund_id:, status:) result.raise_if_error! when 'CHARGEBACK' - PaymentProviders::Webhooks::Adyen::ChargebackService.call( + PaymentProviders::Adyen::Webhooks::ChargebackService.call( organization_id: organization.id, event_json: ) diff --git a/app/services/payment_providers/stripe/webhooks/base_service.rb b/app/services/payment_providers/stripe/webhooks/base_service.rb new file mode 100644 index 00000000000..cc49656203e --- /dev/null +++ b/app/services/payment_providers/stripe/webhooks/base_service.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module PaymentProviders + module Stripe + module Webhooks + class BaseService < BaseService + def initialize(organization_id:, event_json:) + @organization = Organization.find(organization_id) + @event_json = event_json + + super + end + + private + + attr_reader :organization, :event_json + + def event + @event ||= ::Stripe::Event.construct_from(JSON.parse(event_json)) + end + + def metadata + @metadata ||= event.data.object.metadata.to_h.symbolize_keys + end + + def handle_missing_customer + # NOTE: Stripe customer was not created from lago + return result unless metadata&.key?(:lago_customer_id) + + # NOTE: Customer does not belong to this lago instance or + # exists but does not belong to the organizations + # (Happens when the Stripe API key is shared between organizations) + return result if Customer.find_by(id: metadata[:lago_customer_id], organization_id: organization.id).nil? + + result.not_found_failure!(resource: 'stripe_customer') + end + end + end + end +end diff --git a/app/services/payment_providers/webhooks/stripe/charge_dispute_closed_service.rb b/app/services/payment_providers/stripe/webhooks/charge_dispute_closed_service.rb similarity index 95% rename from app/services/payment_providers/webhooks/stripe/charge_dispute_closed_service.rb rename to app/services/payment_providers/stripe/webhooks/charge_dispute_closed_service.rb index bd4e5086392..b32360839b9 100644 --- a/app/services/payment_providers/webhooks/stripe/charge_dispute_closed_service.rb +++ b/app/services/payment_providers/stripe/webhooks/charge_dispute_closed_service.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true module PaymentProviders - module Webhooks - module Stripe + module Stripe + module Webhooks class ChargeDisputeClosedService < BaseService def call status = event.data.object.status diff --git a/app/services/payment_providers/webhooks/stripe/customer_updated_service.rb b/app/services/payment_providers/stripe/webhooks/customer_updated_service.rb similarity index 96% rename from app/services/payment_providers/webhooks/stripe/customer_updated_service.rb rename to app/services/payment_providers/stripe/webhooks/customer_updated_service.rb index 355444280b0..42f819b3c80 100644 --- a/app/services/payment_providers/webhooks/stripe/customer_updated_service.rb +++ b/app/services/payment_providers/stripe/webhooks/customer_updated_service.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true module PaymentProviders - module Webhooks - module Stripe + module Stripe + module Webhooks class CustomerUpdatedService < BaseService def call return handle_missing_customer unless stripe_customer diff --git a/app/services/payment_providers/webhooks/stripe/setup_intent_succeeded_service.rb b/app/services/payment_providers/stripe/webhooks/setup_intent_succeeded_service.rb similarity index 98% rename from app/services/payment_providers/webhooks/stripe/setup_intent_succeeded_service.rb rename to app/services/payment_providers/stripe/webhooks/setup_intent_succeeded_service.rb index d82a7416eda..0014e16f45d 100644 --- a/app/services/payment_providers/webhooks/stripe/setup_intent_succeeded_service.rb +++ b/app/services/payment_providers/stripe/webhooks/setup_intent_succeeded_service.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true module PaymentProviders - module Webhooks module Stripe + module Webhooks class SetupIntentSucceededService < BaseService include Customers::PaymentProviderFinder diff --git a/app/services/payment_providers/stripe_service.rb b/app/services/payment_providers/stripe_service.rb index 1b00b1426af..3f1553394a3 100644 --- a/app/services/payment_providers/stripe_service.rb +++ b/app/services/payment_providers/stripe_service.rb @@ -93,12 +93,12 @@ def handle_event(organization:, event_json:) case event.type when 'setup_intent.succeeded' - PaymentProviders::Webhooks::Stripe::SetupIntentSucceededService.call( + PaymentProviders::Stripe::Webhooks::SetupIntentSucceededService.call( organization_id: organization.id, event_json: ).raise_if_error! when 'customer.updated' - PaymentProviders::Webhooks::Stripe::CustomerUpdatedService.call( + PaymentProviders::Stripe::Webhooks::CustomerUpdatedService.call( organization_id: organization.id, event_json: ).raise_if_error! @@ -111,7 +111,7 @@ def handle_event(organization:, event_json:) metadata: event.data.object.metadata.to_h.symbolize_keys ).raise_if_error! when 'charge.dispute.closed' - PaymentProviders::Webhooks::Stripe::ChargeDisputeClosedService.call( + PaymentProviders::Stripe::Webhooks::ChargeDisputeClosedService.call( organization_id: organization.id, event_json: ).raise_if_error! diff --git a/app/services/payment_providers/webhooks/base_service.rb b/app/services/payment_providers/webhooks/base_service.rb deleted file mode 100644 index 9e0268f822f..00000000000 --- a/app/services/payment_providers/webhooks/base_service.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -module PaymentProviders - module Webhooks - class BaseService < BaseService - def initialize(organization_id:, event_json:) - @organization = Organization.find(organization_id) - @event_json = event_json - - super - end - - private - - attr_reader :organization, :event_json - - def event - @event ||= ::Stripe::Event.construct_from(JSON.parse(event_json)) - end - - def metadata - @metadata ||= event.data.object.metadata.to_h.symbolize_keys - end - - def handle_missing_customer - # NOTE: Stripe customer was not created from lago - return result unless metadata&.key?(:lago_customer_id) - - # NOTE: Customer does not belong to this lago instance or - # exists but does not belong to the organizations - # (Happens when the Stripe API key is shared between organizations) - return result if Customer.find_by(id: metadata[:lago_customer_id], organization_id: organization.id).nil? - - result.not_found_failure!(resource: 'stripe_customer') - end - end - end -end diff --git a/spec/services/payment_providers/webhooks/adyen/chargeback_service_spec.rb b/spec/services/payment_providers/adyen/webhooks/chargeback_service_spec.rb similarity index 97% rename from spec/services/payment_providers/webhooks/adyen/chargeback_service_spec.rb rename to spec/services/payment_providers/adyen/webhooks/chargeback_service_spec.rb index 9a359213817..ecaf93600e7 100644 --- a/spec/services/payment_providers/webhooks/adyen/chargeback_service_spec.rb +++ b/spec/services/payment_providers/adyen/webhooks/chargeback_service_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe PaymentProviders::Webhooks::Adyen::ChargebackService, type: :service do +RSpec.describe PaymentProviders::Adyen::Webhooks::ChargebackService, type: :service do subject(:service) { described_class.new(organization_id:, event_json:) } let(:organization_id) { organization.id } diff --git a/spec/services/payment_providers/webhooks/stripe/charge_dispute_closed_service_spec.rb b/spec/services/payment_providers/stripe/webhooks/charge_dispute_closed_service_spec.rb similarity index 97% rename from spec/services/payment_providers/webhooks/stripe/charge_dispute_closed_service_spec.rb rename to spec/services/payment_providers/stripe/webhooks/charge_dispute_closed_service_spec.rb index b5a05d64d9c..a21d37cc7ef 100644 --- a/spec/services/payment_providers/webhooks/stripe/charge_dispute_closed_service_spec.rb +++ b/spec/services/payment_providers/stripe/webhooks/charge_dispute_closed_service_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe PaymentProviders::Webhooks::Stripe::ChargeDisputeClosedService, type: :service do +RSpec.describe PaymentProviders::Stripe::Webhooks::ChargeDisputeClosedService, type: :service do subject(:service) { described_class.new(organization_id:, event_json:) } let(:organization_id) { organization.id } diff --git a/spec/services/payment_providers/webhooks/stripe/customer_updated_service_spec.rb b/spec/services/payment_providers/stripe/webhooks/customer_updated_service_spec.rb similarity index 97% rename from spec/services/payment_providers/webhooks/stripe/customer_updated_service_spec.rb rename to spec/services/payment_providers/stripe/webhooks/customer_updated_service_spec.rb index 2b327fee3be..a5b70fcfc55 100644 --- a/spec/services/payment_providers/webhooks/stripe/customer_updated_service_spec.rb +++ b/spec/services/payment_providers/stripe/webhooks/customer_updated_service_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe PaymentProviders::Webhooks::Stripe::CustomerUpdatedService, type: :service do +RSpec.describe PaymentProviders::Stripe::Webhooks::CustomerUpdatedService, type: :service do subject(:webhook_service) { described_class.new(organization_id: organization.id, event_json:) } let(:organization) { create(:organization) } diff --git a/spec/services/payment_providers/webhooks/stripe/setup_intent_succeeded_service_spec.rb b/spec/services/payment_providers/stripe/webhooks/setup_intent_succeeded_service_spec.rb similarity index 98% rename from spec/services/payment_providers/webhooks/stripe/setup_intent_succeeded_service_spec.rb rename to spec/services/payment_providers/stripe/webhooks/setup_intent_succeeded_service_spec.rb index d3023b51d6c..ee620e630b5 100644 --- a/spec/services/payment_providers/webhooks/stripe/setup_intent_succeeded_service_spec.rb +++ b/spec/services/payment_providers/stripe/webhooks/setup_intent_succeeded_service_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe PaymentProviders::Webhooks::Stripe::SetupIntentSucceededService, type: :service do +RSpec.describe PaymentProviders::Stripe::Webhooks::SetupIntentSucceededService, type: :service do subject(:webhook_service) { described_class.new(organization_id: organization.id, event_json:) } let(:organization) { create(:organization) } diff --git a/spec/services/payment_providers/stripe_service_spec.rb b/spec/services/payment_providers/stripe_service_spec.rb index 3074546f051..ae86a69addb 100644 --- a/spec/services/payment_providers/stripe_service_spec.rb +++ b/spec/services/payment_providers/stripe_service_spec.rb @@ -397,7 +397,7 @@ end before do - allow(PaymentProviders::Webhooks::Stripe::SetupIntentSucceededService).to receive(:call) + allow(PaymentProviders::Stripe::Webhooks::SetupIntentSucceededService).to receive(:call) .and_return(service_result) end @@ -409,7 +409,7 @@ expect(result).to be_success - expect(PaymentProviders::Webhooks::Stripe::SetupIntentSucceededService).to have_received(:call) + expect(PaymentProviders::Stripe::Webhooks::SetupIntentSucceededService).to have_received(:call) end end @@ -420,7 +420,7 @@ end before do - allow(PaymentProviders::Webhooks::Stripe::CustomerUpdatedService).to receive(:call) + allow(PaymentProviders::Stripe::Webhooks::CustomerUpdatedService).to receive(:call) .and_return(service_result) end @@ -432,7 +432,7 @@ expect(result).to be_success - expect(PaymentProviders::Webhooks::Stripe::CustomerUpdatedService).to have_received(:call) + expect(PaymentProviders::Stripe::Webhooks::CustomerUpdatedService).to have_received(:call) end end