-
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
Showing
11 changed files
with
320 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module InboundWebhooks | ||
class ProcessJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(inbound_webhook:) | ||
InboundWebhooks::ProcessService.call(inbound_webhook:).raise_if_error! | ||
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
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,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module InboundWebhooks | ||
class CreateService < BaseService | ||
def initialize(organization_id:, webhook_source:, payload:, event_type:, code: nil, signature: nil) | ||
@organization_id = organization_id | ||
@webhook_source = webhook_source | ||
@code = code | ||
@payload = payload | ||
@signature = signature | ||
@event_type = event_type | ||
|
||
super | ||
end | ||
|
||
def call | ||
inbound_webhook = InboundWebhook.create!( | ||
organization_id:, | ||
source: webhook_source, | ||
code:, | ||
payload:, | ||
signature:, | ||
event_type: | ||
) | ||
|
||
after_commit do | ||
InboundWebhooks::ProcessJob.perform_later(inbound_webhook:) | ||
end | ||
|
||
result.inbound_webhook = inbound_webhook | ||
result | ||
rescue ActiveRecord::RecordInvalid => e | ||
result.record_validation_failure!(record: e.record) | ||
end | ||
|
||
private | ||
|
||
attr_reader :organization_id, :webhook_source, :code, :payload, :signature, :event_type | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module InboundWebhooks | ||
class ProcessService < BaseService | ||
WEBHOOK_HANDLER_SERVICES = { | ||
stripe: PaymentProviders::Stripe::HandleIncomingWebhookService | ||
} | ||
|
||
def initialize(inbound_webhook:) | ||
@inbound_webhook = inbound_webhook | ||
|
||
super | ||
end | ||
|
||
def call | ||
inbound_webhook.processing! | ||
|
||
handler_result = handler_service_klass.call(inbound_webhook:) | ||
|
||
unless handler_result.success? | ||
inbound_webhook.failed! | ||
return handler_result | ||
end | ||
|
||
inbound_webhook.processed! | ||
|
||
result.inbound_webhook = inbound_webhook | ||
result | ||
rescue | ||
inbound_webhook.failed! | ||
raise | ||
end | ||
|
||
private | ||
|
||
attr_reader :inbound_webhook | ||
|
||
def handler_service_klass | ||
WEBHOOK_HANDLER_SERVICES.fetch(webhook_source) do | ||
raise NameError, "Invalid inbound webhook source: #{webhook_source}" | ||
end | ||
end | ||
|
||
def webhook_source | ||
inbound_webhook.source.to_sym | ||
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
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe InboundWebhooks::ProcessJob, type: :job do | ||
subject(:process_job) { described_class } | ||
|
||
let(:inbound_webhook) { create :inbound_webhook } | ||
let(:result) { BaseService::Result.new } | ||
|
||
before do | ||
allow(InboundWebhooks::ProcessService).to receive(:call).and_return(result) | ||
end | ||
|
||
it "calls the process webhook service" do | ||
process_job.perform_now(inbound_webhook:) | ||
|
||
expect(InboundWebhooks::ProcessService) | ||
.to have_received(:call) | ||
.with(inbound_webhook:) | ||
end | ||
|
||
context "when result is a failure" do | ||
let(:result) do | ||
BaseService::Result.new.service_failure!(code: "error", message: "error message") | ||
end | ||
|
||
it "raises an error" do | ||
expect { process_job.perform_now(inbound_webhook:) } | ||
.to raise_error(BaseService::FailedResult) | ||
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
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,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe InboundWebhooks::CreateService, type: :service do | ||
subject(:result) do | ||
described_class.call( | ||
organization_id: organization.id, | ||
webhook_source:, | ||
code:, | ||
payload:, | ||
signature:, | ||
event_type: | ||
) | ||
end | ||
|
||
let(:organization) { create :organization } | ||
let(:code) { "stripe_1" } | ||
let(:webhook_source) { "stripe" } | ||
let(:signature) { "signature" } | ||
let(:payload) { event.merge(code:).to_json } | ||
let(:event_type) { "payment_intent.successful" } | ||
|
||
let(:event) do | ||
path = Rails.root.join("spec/fixtures/stripe/payment_intent_event.json") | ||
JSON.parse(File.read(path)) | ||
end | ||
|
||
it "creates an inbound webhook" do | ||
expect { result }.to change(InboundWebhook, :count).by(1) | ||
end | ||
|
||
it "returns a pending inbound webhook in the result" do | ||
expect(result.inbound_webhook).to be_a(InboundWebhook) | ||
expect(result.inbound_webhook).to be_pending | ||
end | ||
|
||
it "queues an InboundWebhook::ProcessJob job" do | ||
result | ||
|
||
expect(InboundWebhooks::ProcessJob) | ||
.to have_been_enqueued | ||
.with(inbound_webhook: result.inbound_webhook) | ||
end | ||
|
||
context "with validation error" do | ||
let(:webhook_source) { nil } | ||
|
||
it "returns an error" do | ||
expect(result).not_to be_success | ||
expect(result.error).to be_a(BaseService::ValidationFailure) | ||
expect(result.error.messages[:source]).to eq(["value_is_mandatory"]) | ||
end | ||
|
||
it "does not queue an InboundWebhook::ProcessJob job" do | ||
result | ||
|
||
expect(InboundWebhooks::ProcessJob).not_to have_been_enqueued | ||
end | ||
end | ||
end |
Oops, something went wrong.