Skip to content

Commit

Permalink
fix(customer): Fix update customer webhook (#2904)
Browse files Browse the repository at this point in the history
## Context

`customer.created` webhook was called when updating a customer via API
instead of `customer.updated`.

## Description

This PR fixes the issue and calls the correct `customer.updated`
webhook.
  • Loading branch information
ivannovosad authored Dec 3, 2024
1 parent ffb60c8 commit 589455d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ PLATFORMS
aarch64-linux-musl
arm64-darwin-22
arm64-darwin-23
arm64-darwin-24
x86_64-darwin-21
x86_64-darwin-22
x86_64-darwin-23
Expand Down
8 changes: 7 additions & 1 deletion app/services/customers/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ def create_from_api(organization:, params:)
new_customer:
)

if new_customer
SendWebhookJob.perform_later('customer.created', customer)
else
SendWebhookJob.perform_later('customer.updated', customer)
end

track_customer_created(customer)
result
rescue BaseService::ServiceFailure => e
Expand Down Expand Up @@ -196,6 +202,7 @@ def create(**args)
new_customer: true
)

SendWebhookJob.perform_later('customer.created', customer)
track_customer_created(customer)
result
rescue ActiveRecord::RecordInvalid => e
Expand Down Expand Up @@ -335,7 +342,6 @@ def track_customer_created(customer)
organization_id: customer.organization_id
}
)
SendWebhookJob.perform_later("customer.created", customer)
end

def should_create_billing_configuration?(billing, customer)
Expand Down
8 changes: 8 additions & 0 deletions spec/services/customers/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,14 @@
end
end

it 'calls SendWebhookJob with customer.updated' do
customers_service.create_from_api(
organization:,
params: create_args
)
expect(SendWebhookJob).to have_received(:perform_later).with('customer.updated', customer)
end

context 'with provider customer' do
let(:payment_provider) { create(:stripe_provider, organization:) }
let(:stripe_customer) { create(:stripe_customer, customer:, payment_provider:) }
Expand Down

0 comments on commit 589455d

Please sign in to comment.