Skip to content

Commit

Permalink
misc(PaymentProviders): Refact Webhooks folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Nov 15, 2024
1 parent 5abc45b commit 3e0dd92
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 61 deletions.
24 changes: 24 additions & 0 deletions app/services/payment_providers/adyen/webhooks/base_service.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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']
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/services/payment_providers/adyen_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
)
Expand Down
40 changes: 40 additions & 0 deletions app/services/payment_providers/stripe/webhooks/base_service.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

module PaymentProviders
module Webhooks
module Stripe
module Webhooks
class SetupIntentSucceededService < BaseService
include Customers::PaymentProviderFinder

Expand Down
6 changes: 3 additions & 3 deletions app/services/payment_providers/stripe_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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!
Expand Down
38 changes: 0 additions & 38 deletions app/services/payment_providers/webhooks/base_service.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
8 changes: 4 additions & 4 deletions spec/services/payment_providers/stripe_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 3e0dd92

Please sign in to comment.