Skip to content

Commit

Permalink
fix(customer): Fix update customer webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad committed Dec 3, 2024
1 parent ffb60c8 commit 111f26b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
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
28 changes: 16 additions & 12 deletions app/services/customers/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ def create_from_api(organization:, params:)
unless valid_metadata_count?(metadata: params[:metadata])
return result.single_validation_failure!(
field: :metadata,
error_code: "invalid_count"
error_code: 'invalid_count'
)
end

unless valid_finalize_zero_amount_invoice?(params[:finalize_zero_amount_invoice])
return result.single_validation_failure!(
field: :finalize_zero_amount_invoice,
error_code: "invalid_value"
error_code: 'invalid_value'
)
end

unless valid_integration_customers_count?(integration_customers: params[:integration_customers])
return result.single_validation_failure!(
field: :integration_customers,
error_code: "invalid_count_per_integration_type"
error_code: 'invalid_count_per_integration_type'
)
end

Expand All @@ -52,7 +52,7 @@ def create_from_api(organization:, params:)
customer.legal_number = params[:legal_number] if params.key?(:legal_number)
customer.net_payment_term = params[:net_payment_term] if params.key?(:net_payment_term)
customer.external_salesforce_id = params[:external_salesforce_id] if params.key?(:external_salesforce_id)
customer.finalize_zero_amount_invoice = params[:finalize_zero_amount_invoice] || "inherit" if params.key?(:finalize_zero_amount_invoice)
customer.finalize_zero_amount_invoice = params[:finalize_zero_amount_invoice] || 'inherit' if params.key?(:finalize_zero_amount_invoice)
customer.firstname = params[:firstname] if params.key?(:firstname)
customer.lastname = params[:lastname] if params.key?(:lastname)
customer.customer_type = params[:customer_type] if params.key?(:customer_type)
Expand Down 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 All @@ -108,8 +114,6 @@ def create_from_api(organization:, params:)
result.record_validation_failure!(record: e.record)
rescue BaseService::FailedResult => e
result.fail_with_error!(e)
rescue ActiveRecord::RecordNotUnique
result.single_validation_failure!(field: :external_id, error_code: "value_already_exist")
end

def create(**args)
Expand All @@ -119,7 +123,7 @@ def create(**args)
unless valid_metadata_count?(metadata: args[:metadata])
return result.single_validation_failure!(
field: :metadata,
error_code: "invalid_count"
error_code: 'invalid_count'
)
end

Expand Down Expand Up @@ -196,6 +200,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 @@ -306,11 +311,11 @@ def handle_api_billing_configuration(customer, params, new_customer)

def create_or_update_provider_customer(customer, billing_configuration = {})
provider_class = case billing_configuration[:payment_provider] || customer.payment_provider
when "stripe"
when 'stripe'
PaymentProviderCustomers::StripeCustomer
when "gocardless"
when 'gocardless'
PaymentProviderCustomers::GocardlessCustomer
when "adyen"
when 'adyen'
PaymentProviderCustomers::AdyenCustomer
end

Expand All @@ -327,15 +332,14 @@ def create_or_update_provider_customer(customer, billing_configuration = {})
def track_customer_created(customer)
SegmentTrackJob.perform_later(
membership_id: CurrentContext.membership,
event: "customer_created",
event: 'customer_created',
properties: {
customer_id: customer.id,
created_at: customer.created_at,
payment_provider: customer.payment_provider,
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 111f26b

Please sign in to comment.