-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc(stripe): Refact webhook handling
- Loading branch information
1 parent
3e0dd92
commit 6a7f7bf
Showing
11 changed files
with
380 additions
and
388 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
99 changes: 99 additions & 0 deletions
99
app/services/payment_providers/stripe/handle_event_service.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,99 @@ | ||
# frozen_string_literal: true | ||
|
||
module PaymentProviders | ||
module Stripe | ||
class HandleEventService < BaseService | ||
EVENT_MAPPING = { | ||
'setup_intent.succeeded' => PaymentProviders::Stripe::Webhooks::SetupIntentSucceededService, | ||
'customer.updated' => PaymentProviders::Stripe::Webhooks::CustomerUpdatedService, | ||
'charge.dispute.closed' => PaymentProviders::Stripe::Webhooks::ChargeDisputeClosedService | ||
}.freeze | ||
|
||
PAYMENT_SERVICE_CLASS_MAP = { | ||
"Invoice" => Invoices::Payments::StripeService, | ||
"PaymentRequest" => PaymentRequests::Payments::StripeService | ||
}.freeze | ||
|
||
def initialize(organization:, event_json:) | ||
@organization = organization | ||
@event_json = event_json | ||
|
||
super | ||
end | ||
|
||
def call | ||
unless PaymentProviders::StripeProvider::WEBHOOKS_EVENTS.include?(event.type) | ||
Rails.logger.warn("Unexpected stripe event type: #{event.type}") | ||
return result | ||
end | ||
|
||
if EVENT_MAPPING[event.type].present? | ||
EVENT_MAPPING[event.type].call( | ||
organization_id: organization.id, | ||
event: | ||
).raise_if_error! | ||
|
||
return result | ||
end | ||
|
||
case event.type | ||
when 'charge.succeeded' | ||
payment_service_klass(event) | ||
.new.update_payment_status( | ||
organization_id: organization.id, | ||
provider_payment_id: event.data.object.payment_intent, | ||
status: 'succeeded', | ||
metadata: event.data.object.metadata.to_h.symbolize_keys | ||
).raise_if_error! | ||
when 'payment_intent.payment_failed', 'payment_intent.succeeded' | ||
status = (event.type == 'payment_intent.succeeded') ? 'succeeded' : 'failed' | ||
payment_service_klass(event) | ||
.new.update_payment_status( | ||
organization_id: organization.id, | ||
provider_payment_id: event.data.object.id, | ||
status:, | ||
metadata: event.data.object.metadata.to_h.symbolize_keys | ||
).raise_if_error! | ||
when 'payment_method.detached' | ||
PaymentProviderCustomers::StripeService | ||
.new | ||
.delete_payment_method( | ||
organization_id: organization.id, | ||
stripe_customer_id: event.data.object.customer, | ||
payment_method_id: event.data.object.id, | ||
metadata: event.data.object.metadata.to_h.symbolize_keys | ||
).raise_if_error! | ||
when 'charge.refund.updated' | ||
CreditNotes::Refunds::StripeService | ||
.new.update_status( | ||
provider_refund_id: event.data.object.id, | ||
status: event.data.object.status, | ||
metadata: event.data.object.metadata.to_h.symbolize_keys | ||
) | ||
end | ||
rescue BaseService::NotFoundFailure => e | ||
# NOTE: Error with stripe sandbox should be ignord | ||
raise if event.livemode | ||
|
||
Rails.logger.warn("Stripe resource not found: #{e.message}. JSON: #{event_json}") | ||
BaseService::Result.new # NOTE: Prevents error from being re-raised | ||
end | ||
|
||
private | ||
|
||
attr_reader :organization, :body, :event_json | ||
|
||
def event | ||
@event ||= ::Stripe::Event.construct_from(JSON.parse(event_json)) | ||
end | ||
|
||
def payment_service_klass(event) | ||
payable_type = event.data.object.metadata.to_h[:lago_payable_type] || "Invoice" | ||
|
||
PAYMENT_SERVICE_CLASS_MAP.fetch(payable_type) do | ||
raise NameError, "Invalid lago_payable_type: #{payable_type}" | ||
end | ||
end | ||
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
4 changes: 2 additions & 2 deletions
4
app/services/payment_providers/stripe/webhooks/setup_intent_succeeded_service.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
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
Oops, something went wrong.