Skip to content

Commit

Permalink
Merge pull request #878 from recurly/add_revrec_features_to_ruby
Browse files Browse the repository at this point in the history
Add RevRec features to ruby
  • Loading branch information
8eth authored Mar 13, 2024
2 parents f33f29f + b3d8546 commit 133f2ec
Show file tree
Hide file tree
Showing 43 changed files with 1,350 additions and 105 deletions.
2 changes: 2 additions & 0 deletions lib/recurly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ module Recurly
require 'recurly/currency_percentage_tier'
require 'recurly/sub_add_on_percentage_tier'
require 'recurly/custom_field_definition'
require 'recurly/general_ledger_account'
require 'recurly/performance_obligation'

@subdomain = nil

Expand Down
2 changes: 1 addition & 1 deletion lib/recurly/add_on.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AddOn < Resource
external_sku
avalara_service_type
avalara_transaction_type
)
) + RevRec::PRODUCT_ATTRIBUTES
alias to_param add_on_code
alias quantity default_quantity

Expand Down
2 changes: 1 addition & 1 deletion lib/recurly/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Adjustment < Resource
avalara_transaction_type
avalara_service_type
refundable_total_in_cents
]
] + RevRec::ALL_ATTRIBUTES
alias to_param uuid

# @return ["charge", "credit", nil] The type of adjustment.
Expand Down
5 changes: 4 additions & 1 deletion lib/recurly/business_entity.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module Recurly
class BusinessEntity < Resource
belongs_to :site
belongs_to :default_revenue_gl_account, class_name: 'GeneralLedgerAccount'
belongs_to :default_liability_gl_account, class_name: 'GeneralLedgerAccount'

has_many :invoices

has_many :accounts

define_attribute_methods %w(
Expand All @@ -15,6 +16,8 @@ class BusinessEntity < Resource
subscriber_location_countries
default_vat_number
default_registration_number
default_revenue_gl_account_id
default_liability_gl_account_id
created_at
updated_at
)
Expand Down
16 changes: 16 additions & 0 deletions lib/recurly/general_ledger_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Recurly
class GeneralLedgerAccount < Resource
belongs_to :site

define_attribute_methods %w(
id
code
description
account_type
)

def self.collection_path
"general_ledger_accounts"
end
end
end
2 changes: 1 addition & 1 deletion lib/recurly/gift_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GiftCard < Resource
gifter_account_code
recipient_account_code
invoice_number
)
) + RevRec::PRODUCT_ATTRIBUTES
alias to_param id

# Preview a GiftCard given some attributes
Expand Down
2 changes: 1 addition & 1 deletion lib/recurly/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Item < Resource
created_at
updated_at
deleted_at
)
) + RevRec::PRODUCT_ATTRIBUTES

def changed_attributes
attrs = super
Expand Down
15 changes: 15 additions & 0 deletions lib/recurly/performance_obligation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Recurly
class PerformanceObligation < Resource

define_attribute_methods %w(
id
name
created_at
updated_at
)

def self.collection_path
"performance_obligations"
end
end
end
4 changes: 3 additions & 1 deletion lib/recurly/plan.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Recurly
# Recurly Documentation: https://dev.recurly.com/docs/list-plans
class Plan < Resource

# @return [Pager<AddOn>, []]
has_many :add_ons

Expand All @@ -13,6 +14,7 @@ class Plan < Resource
# @return [[CustomField], []]
has_many :custom_fields, class_name: :CustomField, readonly: false

# Define attribute methods
define_attribute_methods %w(
plan_code
name
Expand Down Expand Up @@ -50,7 +52,7 @@ class Plan < Resource
custom_fields
created_at
updated_at
)
) + RevRec::PLAN_ATTRIBUTES
alias to_param plan_code
end
end
1 change: 1 addition & 0 deletions lib/recurly/resource.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'date'
require 'erb'
require_relative './rev_rec'

module Recurly
# The base class for all Recurly resources (e.g. {Account}, {Subscription},
Expand Down
18 changes: 18 additions & 0 deletions lib/recurly/rev_rec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Recurly
module RevRec
POB_ATTRIBUTE = %i(performance_obligation_id).freeze
POB_NATIVE_ATTRIBUTE = %i(performance_obligation_identifier).freeze

GLA_ID_ATTRIBUTES = %i(liability_gl_account_id revenue_gl_account_id).freeze
GLA_CODE_ATTRIBUTES = %i(liability_gl_account_code revenue_gl_account_code).freeze

PRODUCT_NATIVE_ATTRS = (POB_NATIVE_ATTRIBUTE + GLA_ID_ATTRIBUTES).freeze
PRODUCT_ATTRIBUTES = (POB_ATTRIBUTE + GLA_ID_ATTRIBUTES).freeze

LINE_ITEM_ATTRIBUTES = (POB_ATTRIBUTE + GLA_CODE_ATTRIBUTES).freeze
ALL_ATTRIBUTES = (PRODUCT_ATTRIBUTES + LINE_ITEM_ATTRIBUTES).uniq.freeze

SETUP_FEE_ATTRIBUTES = PRODUCT_ATTRIBUTES.map { |key| :"setup_fee_#{key}" }.freeze
PLAN_ATTRIBUTES = [*PRODUCT_ATTRIBUTES, *SETUP_FEE_ATTRIBUTES].freeze
end
end
2 changes: 1 addition & 1 deletion lib/recurly/shipping_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ShippingMethod < Resource
tax_code
created_at
updated_at
)
) + RevRec::PRODUCT_ATTRIBUTES
alias to_param code
end
end
2 changes: 1 addition & 1 deletion lib/recurly/xml/rexml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def each_element xpath = nil
def each element = root
element.each_element do |el|
yield el
each el, &Proc.new
each el, &Proc.new { |e| yield e }
end
end

Expand Down
39 changes: 39 additions & 0 deletions spec/fixtures/adjustments/create-201-with-revrec.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
HTTP/1.1 201 Created
Content-Type: application/xml; charset=utf-8
Location: https://api.recurly.com/v2/adjustments/67360781108ceacd23dee747b7b1f5af

<adjustment href="https://api.recurly.com/v2/adjustments/67360781108ceacd23dee747b7b1f5af">
<account href="https://api.recurly.com/v2/accounts/7747ed3b-1771-4a03-b32a-503e9d65d3ad"/>
<bill_for_account href="https://api.recurly.com/v2/accounts/account1"/>
<item_code nil="nil"/>
<external_sku nil="nil"/>
<credit_adjustments href="https://api.recurly.com/v2/adjustments/67360781108ceacd23dee747b7b1f5af/credit_adjustments"/>
<refundable_total_in_cents type="integer">0</refundable_total_in_cents>
<uuid>67360781108ceacd23dee747b7b1f5af</uuid>
<state>pending</state>
<description nil="nil"/>
<accounting_code nil="nil"/>
<product_code nil="nil"/>
<origin>debit</origin>
<unit_amount_in_cents type="integer">5000</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<discount_in_cents type="integer">0</discount_in_cents>
<tax_in_cents type="integer">0</tax_in_cents>
<total_in_cents type="integer">5000</total_in_cents>
<currency>USD</currency>
<proration_rate nil="nil"/>
<start_date type="datetime">2023-01-18T23:34:52Z</start_date>
<end_date nil="nil"/>
<created_at type="datetime">2023-01-18T23:34:52Z</created_at>
<updated_at type="datetime">2023-01-18T23:34:52Z</updated_at>
<revenue_schedule_type>at_invoice</revenue_schedule_type>
<liability_gl_account_code>liability_gla</liability_gl_account_code>
<revenue_gl_account_code>revenue_gla</revenue_gl_account_code>
<performance_obligation_id>5</performance_obligation_id>
<custom_fields type="array">
<custom_field>
<name>field1</name>
<value>priceless</value>
</custom_field>
</custom_fields>
</adjustment>
64 changes: 64 additions & 0 deletions spec/fixtures/adjustments/show-200-with-revrec.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<adjustment href="https://api.recurly.com/v2/adjustments/abcdef1234567890" type="charge">
<account href="https://api.recurly.com/v2/accounts/account"/>
<bill_for_account href="https://api.recurly.com/v2/accounts/abcdef1234567890"/>
<invoice href="https://api.recurly.com/v2/invoices/created-invoice"/>
<subscription href="https://api.recurly.com/v2/subscriptions/abcdef1234567890"/>
<credit_adjustments href="https://api.recurly.com/v2/adjustments/abcdef1234567890/credit_adjustments"/>
<item href="https://api.recurly.com/v2/items/plastic_gloves"/>
<item_code>plastic_gloves</item_code>
<external_sku>plastic_gloves</external_sku>
<uuid>abcdef1234567890</uuid>
<state>pending</state>
<accounting_code nil="nil"></accounting_code>
<origin>plan</origin>
<description>$12 Annual Subscription</description>
<unit_amount_in_cents type="integer">1200</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<quantity_decimal>1.2</quantity_decimal>
<discount_in_cents type="integer">0</discount_in_cents>
<tax_in_cents type="integer">5000</tax_in_cents>
<tax_type>usst</tax_type>
<tax_region>CA</tax_region>
<tax_rate type="float">0.0875</tax_rate>
<total_in_cents type="integer">1200</total_in_cents>
<currency>USD</currency>
<product_code>basic</product_code>
<revenue_schedule_type>evenly</revenue_schedule_type>
<surcharge_in_cents type="integer">100</surcharge_in_cents>
<avalara_service_type type="integer">3</avalara_service_type>
<avalara_transaction_type type="integer">600</avalara_transaction_type>
<liability_gl_account_code>liability_gla</liability_gl_account_code>
<revenue_gl_account_code>revenue_gla</revenue_gl_account_code>
<performance_obligation_id>5</performance_obligation_id>
<tax_details type="array">
<tax_detail>
<name>california</name>
<type>state</type>
<tax_rate type="float">0.065</tax_rate>
<tax_in_cents type="integer">3000</tax_in_cents>
<level>state</level>
<billable type="boolean">true</billable>
</tax_detail>
<tax_detail>
<name>san francisco</name>
<type>county</type>
<tax_rate type="float">0.02</tax_rate>
<tax_in_cents type="integer">2000</tax_in_cents>
</tax_detail>
</tax_details>
<proration_rate type="float">0.5</proration_rate>
<original_adjustment_uuid>abcdefg1234567</original_adjustment_uuid>
<start_date type="datetime">2011-04-30T07:00:00Z</start_date>
<end_date type="datetime">2011-04-30T07:00:00Z</end_date>
<created_at type="datetime">2011-08-31T03:30:00Z</created_at>
<custom_fields>
<custom_field>
<name>field1</name>
<value>priceless</value>
</custom_field>
</custom_fields>
</adjustment>
5 changes: 4 additions & 1 deletion spec/fixtures/business_entities/index-200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Link: <https://api.recurly.com/v2/business_entities?cursor=1234567890&per_page=2
</tax_address>
<default_vat_number nil="nil"></default_vat_number>
<default_registration_number nil="nil"></default_registration_number>
<default_liability_gl_account_id>12345</default_liability_gl_account_id>
<default_revenue_gl_account_id>56789</default_revenue_gl_account_id>
<created_at type="datetime">2023-03-01T16:26:40Z</created_at>
<updated_at type="datetime">2023-04-01T16:26:40Z</updated_at>
</business_entity>
Expand All @@ -55,7 +57,8 @@ Link: <https://api.recurly.com/v2/business_entities?cursor=1234567890&per_page=2
<phone nil="nil"></phone>
</tax_address>
<default_vat_number nil="nil"></default_vat_number>
<default_registration_number nil="nil"></default_registration_number>
<default_liability_gl_account_id>abc123</default_liability_gl_account_id>
<default_revenue_gl_account_id>xyz789</default_revenue_gl_account_id>
<created_at type="datetime">2023-06-01T16:26:40Z</created_at>
<updated_at type="datetime">2023-07-01T16:26:40Z</updated_at>
</business_entity>
Expand Down
60 changes: 29 additions & 31 deletions spec/fixtures/business_entities/show-200.xml
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8
Link: <https://api.recurly.com/v2/business_entities?cursor=1234567890&per_page=20>; rel="start", <https://api.recurly.com/v2/business_entities?cursor=1234566890&per_page=20>; rel="next"


<?xml version="1.0" encoding="UTF-8"?>
<business_entities type="array">
<business_entity href="https://api.recurly.com/v2/business_entities/sbup2j0fx800">
<id>sbup2j0fx800</id>
<code>default</code>
<name>Default Business Entity</name>
<invoice_display_address>
<address1>123 Main Ave</address1>
<address2 nil="nil"></address2>
<city>San Francisco</city>
<state>CA</state>
<zip>90210</zip>
<country>US</country>
<phone nil="nil"></phone>
</invoice_display_address>
<tax_address>
<address1>123 Main Ave</address1>
<address2 nil="nil"></address2>
<city>San Francisco</city>
<state>CA</state>
<zip>90210</zip>
<country>US</country>
<phone nil="nil"></phone>
</tax_address>
<default_vat_number nil="nil"></default_vat_number>
<default_registration_number nil="nil"></default_registration_number>
<created_at type="datetime">2023-03-01T16:26:40Z</created_at>
<updated_at type="datetime">2023-04-01T16:26:40Z</updated_at>
</business_entity>
</business_entities>
<business_entity href="https://api.recurly.com/v2/business_entities/sbup2j0fx800">
<id>sbup2j0fx800</id>
<code>default</code>
<name>Default Business Entity</name>
<invoice_display_address>
<address1>123 Main Ave</address1>
<address2 nil="nil"></address2>
<city>San Francisco</city>
<state>CA</state>
<zip>90210</zip>
<country>US</country>
<phone nil="nil"></phone>
</invoice_display_address>
<tax_address>
<address1>123 Main Ave</address1>
<address2 nil="nil"></address2>
<city>San Francisco</city>
<state>CA</state>
<zip>90210</zip>
<country>US</country>
<phone nil="nil"></phone>
</tax_address>
<default_vat_number nil="nil"></default_vat_number>
<default_registration_number nil="nil"></default_registration_number>
<default_liability_gl_account_id>12345</default_liability_gl_account_id>
<default_revenue_gl_account_id>56789</default_revenue_gl_account_id>
<created_at type="datetime">2023-03-01T16:26:40Z</created_at>
<updated_at type="datetime">2023-04-01T16:26:40Z</updated_at>
</business_entity>
12 changes: 12 additions & 0 deletions spec/fixtures/general_ledger_accounts/create-201.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
HTTP/1.1 201 Created
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<general_ledger_account href="https://api.recurly.com/v2/general_ledger_accounts/u90r5deeaxix">
<id>u90r5deeaxix</id>
<code>code1</code>
<account_type>revenue</account_type>
<description>string</description>
<created_at type="datetime">2024-01-16T14:34:16Z</created_at>
<updated_at type="datetime">2024-01-16T14:34:16Z</updated_at>
</general_ledger_account>
Loading

0 comments on commit 133f2ec

Please sign in to comment.