-
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.
- Loading branch information
1 parent
0047ca5
commit 606ad89
Showing
20 changed files
with
1,316 additions
and
139 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
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
32 changes: 32 additions & 0 deletions
32
app/services/payment_providers/cashfree/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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module PaymentProviders | ||
module Cashfree | ||
class HandleEventService < BaseService | ||
EVENT_MAPPING = { | ||
"PAYMENT_LINK_EVENT" => PaymentProviders::Cashfree::Webhooks::PaymentLinkEventService | ||
}.freeze | ||
|
||
def initialize(organization:, event_json:) | ||
@organization = organization | ||
@event_json = event_json | ||
|
||
super | ||
end | ||
|
||
def call | ||
EVENT_MAPPING[event["type"]].call!(organization_id: organization.id, event_json:) | ||
|
||
result | ||
end | ||
|
||
private | ||
|
||
attr_reader :organization, :event_json | ||
|
||
def event | ||
@event ||= JSON.parse(event_json) | ||
end | ||
end | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
app/services/payment_providers/cashfree/webhooks/base_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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module PaymentProviders | ||
module Cashfree | ||
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) | ||
end | ||
end | ||
end | ||
end | ||
end |
51 changes: 51 additions & 0 deletions
51
app/services/payment_providers/cashfree/webhooks/payment_link_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,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module PaymentProviders | ||
module Cashfree | ||
module Webhooks | ||
class PaymentLinkEventService < BaseService | ||
LINK_STATUS_ACTIONS = %w[PAID].freeze | ||
|
||
PAYMENT_SERVICE_CLASS_MAP = { | ||
"Invoice" => Invoices::Payments::CashfreeService, | ||
"PaymentRequest" => PaymentRequests::Payments::CashfreeService | ||
}.freeze | ||
|
||
def call | ||
return result unless LINK_STATUS_ACTIONS.include?(link_status) | ||
return result if provider_payment_id.nil? | ||
|
||
payment_service_class.new.update_payment_status( | ||
organization_id: organization.id, | ||
status: link_status, | ||
cashfree_payment: PaymentProviders::CashfreeProvider::CashfreePayment.new( | ||
id: provider_payment_id, | ||
status: link_status, | ||
metadata: event.dig("data", "link_notes").to_h.symbolize_keys || {} | ||
) | ||
).raise_if_error! | ||
end | ||
|
||
private | ||
|
||
def payment_service_class | ||
PAYMENT_SERVICE_CLASS_MAP.fetch(payable_type || "Invoice") do | ||
raise NameError, "Invalid lago_payable_type: #{payable_type}" | ||
end | ||
end | ||
|
||
def link_status | ||
@link_status ||= event.dig("data", "link_status") | ||
end | ||
|
||
def provider_payment_id | ||
@provider_payment_id ||= event.dig("data", "link_notes", "lago_invoice_id") || event.dig("data", "link_notes", "lago_payable_id") | ||
end | ||
|
||
def payable_type | ||
@payable_type ||= event.dig("data", "link_notes", "lago_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
Oops, something went wrong.