Skip to content

Commit f7eae78

Browse files
committed
feat(integration): Update billing entity mappings in integration payload
1 parent 8601a12 commit f7eae78

File tree

11 files changed

+61
-35
lines changed

11 files changed

+61
-35
lines changed

app/services/fees/create_pay_in_advance_service.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def apply_provider_taxes(fees_result)
232232
taxes_result
233233
end
234234

235-
FakeInvoice = Data.define(:id, :issuing_date, :currency, :customer)
235+
FakeInvoice = Data.define(:id, :issuing_date, :currency, :customer, :billing_entity)
236236

237237
def invoice
238238
result.invoice_id = SecureRandom.uuid
@@ -241,7 +241,8 @@ def invoice
241241
id: result.invoice_id,
242242
issuing_date: Time.current.in_time_zone(customer.applicable_timezone).to_date,
243243
currency: subscription.plan.amount_currency,
244-
customer:
244+
customer:,
245+
billing_entity: customer.billing_entity
245246
)
246247
end
247248

app/services/integrations/aggregator/base_payload.rb

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,45 @@ def initialize(result, code:)
1313
end
1414
end
1515

16-
def initialize(integration:)
16+
def initialize(integration:, billing_entity:)
1717
@integration = integration
18+
@billing_entity = billing_entity
1819
end
1920

2021
def billable_metric_item(fee)
21-
integration
22-
.integration_mappings
23-
.find_by(mappable_type: "BillableMetric", mappable_id: fee.billable_metric.id) || fallback_item
22+
lookup_mapping("BillableMetric", fee.billable_metric.id)
2423
end
2524

2625
def add_on_item(fee)
27-
integration
28-
.integration_mappings
29-
.find_by(mappable_type: "AddOn", mappable_id: fee.add_on_id) || fallback_item
26+
lookup_mapping("AddOn", fee.add_on_id)
3027
end
3128

3229
def account_item
33-
@account_item ||= collection_mapping(:account) || fallback_item
30+
lookup_collection_mapping(:account)
3431
end
3532

3633
def tax_item
37-
@tax_item ||= collection_mapping(:tax)
34+
lookup_collection_mapping(:tax, with_fallback_item: false)
3835
end
3936

4037
def commitment_item
41-
@commitment_item ||= collection_mapping(:minimum_commitment) || fallback_item
38+
lookup_collection_mapping(:minimum_commitment)
4239
end
4340

4441
def subscription_item
45-
@subscription_item ||= collection_mapping(:subscription_fee) || fallback_item
42+
lookup_collection_mapping(:subscription_fee)
4643
end
4744

4845
def coupon_item
49-
@coupon_item ||= collection_mapping(:coupon) || fallback_item
46+
lookup_collection_mapping(:coupon)
5047
end
5148

5249
def credit_item
53-
@credit_item ||= collection_mapping(:prepaid_credit) || fallback_item
50+
lookup_collection_mapping(:prepaid_credit)
5451
end
5552

5653
def credit_note_item
57-
@credit_note_item ||= collection_mapping(:credit_note) || fallback_item
58-
end
59-
60-
def fallback_item
61-
@fallback_item ||= collection_mapping(:fallback_item)
54+
lookup_collection_mapping(:credit_note)
6255
end
6356

6457
def amount(amount_cents, resource:)
@@ -67,13 +60,46 @@ def amount(amount_cents, resource:)
6760
amount_cents.round.fdiv(currency.subunit_to_unit)
6861
end
6962

70-
def collection_mapping(type)
71-
integration.integration_collection_mappings.where(mapping_type: type)&.first
63+
private
64+
65+
attr_reader :integration, :billing_entity
66+
67+
def fallback_item(scope)
68+
mappings = integration.integration_collection_mappings
69+
fallback_items = mappings.filter { |mapping| mapping.mapping_type.to_sym == :fallback_item }
70+
if scope == :billing_entity && billing_entity
71+
return fallback_items.find { |mapping| mapping.billing_entity_id == billing_entity.id }
72+
end
73+
74+
fallback_items.find { |mapping| mapping.billing_entity_id.nil? }
7275
end
7376

74-
private
77+
def lookup_collection_mapping(mapping_type, with_fallback_item: true)
78+
mappings = integration.integration_collection_mappings
79+
matching_mappings = mappings.filter { |mapping| mapping.mapping_type.to_sym == mapping_type.to_sym }
80+
billing_entity_mapping = matching_mappings.find { |mapping| mapping.billing_entity_id == billing_entity.id }
81+
organization_mapping = matching_mappings.find { |mapping| mapping.billing_entity_id.nil? }
82+
if with_fallback_item
83+
return billing_entity_mapping ||
84+
fallback_item(:billing_entity) ||
85+
organization_mapping ||
86+
fallback_item(:organization)
87+
end
7588

76-
attr_reader :integration
89+
billing_entity_mapping ||
90+
organization_mapping
91+
end
92+
93+
def lookup_mapping(mappable_type, mappable_id)
94+
mappings = integration.integration_mappings
95+
matching_mappings = mappings.filter { |mapping| mapping.mappable_type == mappable_type && mapping.mappable_id == mappable_id }
96+
billing_entity_mapping = matching_mappings.find { |mapping| mapping.billing_entity_id == billing_entity.id }
97+
organization_mapping = matching_mappings.find { |mapping| mapping.billing_entity_id.nil? }
98+
billing_entity_mapping ||
99+
fallback_item(:billing_entity) ||
100+
organization_mapping ||
101+
fallback_item(:organization)
102+
end
77103

78104
def tax_item_complete?
79105
tax_item&.tax_nexus.present? && tax_item&.tax_type.present? && tax_item&.tax_code.present?

app/services/integrations/aggregator/contacts/payloads/base_payload.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Contacts
66
module Payloads
77
class BasePayload < Integrations::Aggregator::BasePayload
88
def initialize(integration:, customer:, integration_customer: nil, subsidiary_id: nil)
9-
super(integration:)
9+
super(integration:, billing_entity: customer.billing_entity)
1010

1111
@customer = customer
1212
@integration_customer = integration_customer

app/services/integrations/aggregator/credit_notes/payloads/base_payload.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module CreditNotes
66
module Payloads
77
class BasePayload < Integrations::Aggregator::BasePayload
88
def initialize(integration_customer:, credit_note:)
9-
super(integration: integration_customer.integration)
9+
super(integration: integration_customer.integration, billing_entity: credit_note.customer.billing_entity)
1010

1111
@credit_note = credit_note
1212
@integration_customer = integration_customer

app/services/integrations/aggregator/invoices/payloads/base_payload.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Invoices
66
module Payloads
77
class BasePayload < Integrations::Aggregator::BasePayload
88
def initialize(integration_customer:, invoice:)
9-
super(integration: integration_customer.integration)
9+
super(integration: integration_customer.integration, billing_entity: integration_customer.customer.billing_entity)
1010

1111
@invoice = invoice
1212
@integration_customer = integration_customer

app/services/integrations/aggregator/payments/payloads/base_payload.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Payments
66
module Payloads
77
class BasePayload < Integrations::Aggregator::BasePayload
88
def initialize(integration:, payment:)
9-
super(integration:)
9+
super(integration:, billing_entity: payment.payable.customer.billing_entity)
1010

1111
@payment = payment
1212
end

app/services/integrations/aggregator/subscriptions/payloads/base_payload.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Subscriptions
66
module Payloads
77
class BasePayload < Integrations::Aggregator::BasePayload
88
def initialize(integration_customer:, subscription:)
9-
super(integration: integration_customer.integration)
9+
super(integration: integration_customer.integration, billing_entity: subscription.customer.billing_entity)
1010

1111
@subscription = subscription
1212
@integration_customer = integration_customer

app/services/integrations/aggregator/taxes/credit_notes/payloads/anrok.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module CreditNotes
77
module Payloads
88
class Anrok < BasePayload
99
def initialize(integration:, customer:, integration_customer:, credit_note:)
10-
super(integration:)
10+
super(integration:, billing_entity: customer.billing_entity)
1111

1212
@customer = customer
1313
@integration_customer = integration_customer

app/services/integrations/aggregator/taxes/credit_notes/payloads/avalara.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module CreditNotes
77
module Payloads
88
class Avalara < BasePayload
99
def initialize(integration:, customer:, integration_customer:, credit_note:)
10-
super(integration:)
10+
super(integration:, billing_entity: customer.billing_entity)
1111

1212
@customer = customer
1313
@integration_customer = integration_customer

app/services/integrations/aggregator/taxes/invoices/payloads/anrok.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Invoices
77
module Payloads
88
class Anrok < BasePayload
99
def initialize(integration:, customer:, invoice:, integration_customer:, fees: [])
10-
super(integration:)
10+
super(integration:, billing_entity: customer.billing_entity)
1111

1212
@customer = customer
1313
@integration_customer = integration_customer

0 commit comments

Comments
 (0)