Skip to content

Commit

Permalink
Read and Write custom invoice notes from API
Browse files Browse the repository at this point in the history
  • Loading branch information
bhelx committed Nov 26, 2014
1 parent 2a07c14 commit 261a6d8
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/recurly/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class Account < Resource

# @return [Invoice] A newly-created invoice.
# @raise [Invalid] Raised if the account cannot be invoiced.
def invoice!
Invoice.from_response API.post(invoices.uri)
def invoice!(attrs={})
Invoice.from_response API.post(invoices.uri, attrs.empty? ? nil : Invoice.to_xml(attrs))
rescue Recurly::API::UnprocessableEntity => e
raise Invalid, e.message
end
Expand Down
7 changes: 7 additions & 0 deletions lib/recurly/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@ class Invoice < Resource
amount_remaining_in_cents
line_items
transactions
terms_and_conditions
customer_notes
)
alias to_param invoice_number

def self.to_xml(attrs)
invoice = new attrs
invoice.to_xml
end

# Marks an invoice as paid successfully.
#
# @return [true, false] +true+ when successful, +false+ when unable to
Expand Down
2 changes: 2 additions & 0 deletions lib/recurly/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Subscription < Resource
tax_type
tax_rate
bulk
terms_and_conditions
customer_notes
)
alias to_param uuid

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

<?xml version="1.0" encoding="UTF-8"?>
<invoice href="https://api.recurly.com/v2/invoices/created-invoice">
<account href="https://api.recurly.com/v2/accounts/abcdef1234567890"/>
<subscription href="https://api.recurly.com/v2/subscriptions/abcdef1234567890"/>
<uuid>created-invoice</uuid>
<state>open</state>
<invoice_number type="integer">1000</invoice_number>
<po_number nil="nil"></po_number>
<vat_number nil="nil"></vat_number>
<subtotal_in_cents type="integer">300</subtotal_in_cents>
<tax_in_cents type="integer">0</tax_in_cents>
<total_in_cents type="integer">300</total_in_cents>
<currency>USD</currency>
<created_at type="datetime">2011-04-30T08:00:00Z</created_at>
<terms_and_conditions>Some Terms and Conditions</terms_and_conditions>
<customer_notes>Some Customer Notes</customer_notes>
<line_items>
<adjustment href="https://api.recurly.com/v2/adjustments/charge" type="charge">
<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"/>
<uuid>charge</uuid>
<state>invoiced</state>
<description>Special charge</description>
<origin>one_time</origin>
<unit_amount_in_cents type="integer">100</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">100</total_in_cents>
<currency>USD</currency>
<taxable type="boolean">false</taxable>
<start_date nil="nil"></start_date>
<end_date nil="nil"></end_date>
<created_at type="datetime">2011-04-30T08:00:00Z</created_at>
</adjustment>
</line_items>
<transactions>
<transaction href="https://api.recurly.com/v2/transactions/transaction" type="credit_card">
<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"/>
<uuid>transaction</uuid>
<action>purchase</action>
<amount_in_cents type="integer">300</amount_in_cents>
<tax_in_cents type="integer">0</tax_in_cents>
<currency>USD</currency>
<status>success</status>
<reference nil="nil"></reference>
<recurring type="boolean">false</recurring>
<test type="boolean">true</test>
<voidable type="boolean">true</voidable>
<refundable type="boolean">true</refundable>
<cvv_result code="" nil="nil"></cvv_result>
<avs_result code="" nil="nil"></avs_result>
<avs_result_street nil="nil"></avs_result_street>
<avs_result_postal nil="nil"></avs_result_postal>
<created_at type="datetime">2011-04-30T08:00:00Z</created_at>
<details>
<account>
<account_code>abcdef1234567890</account_code>
<first_name>Lucille</first_name>
<last_name>Bluth</last_name>
<company nil="nil"></company>
<email>[email protected]</email>
<billing_info type="credit_card">
<first_name>George</first_name>
<last_name>Bluth</last_name>
<address1 nil="nil"></address1>
<address2 nil="nil"></address2>
<city nil="nil"></city>
<state nil="nil"></state>
<zip nil="nil"></zip>
<country nil="nil"></country>
<phone nil="nil"></phone>
<vat_number nil="nil"></vat_number>
<card_type>Visa</card_type>
<year type="integer">2014</year>
<month type="integer">1</month>
<first_six>411111</first_six>
<last_four>1111</last_four>
</billing_info>
</account>
</details>
<a name="refund" href="https://api.recurly.com/v2/transactions/transaction" method="delete"/>
</transaction>
</transactions>
</invoice>
89 changes: 89 additions & 0 deletions spec/fixtures/invoices/show-200-with-notes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<invoice href="https://api.recurly.com/v2/invoices/created-invoice">
<account href="https://api.recurly.com/v2/accounts/abcdef1234567890"/>
<uuid>created-invoice</uuid>
<state>open</state>
<invoice_number type="integer">1000</invoice_number>
<po_number nil="nil"></po_number>
<vat_number nil="nil"></vat_number>
<subtotal_in_cents type="integer">300</subtotal_in_cents>
<tax_in_cents type="integer">100</tax_in_cents>
<total_in_cents type="integer">300</total_in_cents>
<currency>USD</currency>
<created_at type="datetime">2011-04-30T08:00:00Z</created_at>
<tax_type>usst</tax_type>
<customer_notes>Some Customer Notes</customer_notes>
<terms_and_conditions>Some Terms and Conditions</terms_and_conditions>
<line_items>
<adjustment href="https://api.recurly.com/v2/adjustments/charge" type="charge">
<account href="https://api.recurly.com/v2/accounts/abcdef1234567890"/>
<invoice href="https://api.recurly.com/v2/invoices/created-invoice"/>
<uuid>charge</uuid>
<state>invoiced</state>
<description>Special charge</description>
<origin>one_time</origin>
<unit_amount_in_cents type="integer">300</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<discount_in_cents type="integer">0</discount_in_cents>
<tax_in_cents type="integer">100</tax_in_cents>
<total_in_cents type="integer">300</total_in_cents>
<currency>USD</currency>
<taxable type="boolean">false</taxable>
<start_date nil="nil"></start_date>
<end_date nil="nil"></end_date>
<created_at type="datetime">2011-04-30T08:00:00Z</created_at>
</adjustment>
</line_items>
<transactions>
<transaction href="https://api.recurly.com/v2/transactions/transaction" type="credit_card">
<account href="https://api.recurly.com/v2/accounts/abcdef1234567890"/>
<invoice href="https://api.recurly.com/v2/invoices/created-invoice"/>
<uuid>transaction</uuid>
<action>purchase</action>
<amount_in_cents type="integer">300</amount_in_cents>
<tax_in_cents type="integer">0</tax_in_cents>
<currency>USD</currency>
<status>success</status>
<reference nil="nil"></reference>
<recurring type="boolean">false</recurring>
<test type="boolean">true</test>
<voidable type="boolean">true</voidable>
<refundable type="boolean">true</refundable>
<cvv_result code="" nil="nil"></cvv_result>
<avs_result code="" nil="nil"></avs_result>
<avs_result_street nil="nil"></avs_result_street>
<avs_result_postal nil="nil"></avs_result_postal>
<created_at type="datetime">2011-04-30T08:00:00Z</created_at>
<details>
<account>
<account_code>abcdef1234567890</account_code>
<first_name>Lucille</first_name>
<last_name>Bluth</last_name>
<company nil="nil"></company>
<email>[email protected]</email>
<billing_info type="credit_card">
<first_name>George</first_name>
<last_name>Bluth</last_name>
<address1 nil="nil"></address1>
<address2 nil="nil"></address2>
<city nil="nil"></city>
<state nil="nil"></state>
<zip nil="nil"></zip>
<country nil="nil"></country>
<phone nil="nil"></phone>
<vat_number nil="nil"></vat_number>
<card_type>Visa</card_type>
<year type="integer">2014</year>
<month type="integer">1</month>
<first_six>411111</first_six>
<last_four>1111</last_four>
</billing_info>
</account>
</details>
<a name="refund" href="https://api.recurly.com/v2/transactions/transaction" method="delete"/>
</transaction>
</transactions>
</invoice>
2 changes: 1 addition & 1 deletion spec/fixtures/subscriptions/serialize-automatic.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<subscription><account><account_code>1</account_code><billing_info><month>1</month><number>4111-1111-1111-1111</number><year>2014</year></billing_info><email>[email protected]</email><first_name>Verena</first_name><last_name>Example</last_name></account><currency>EUR</currency><plan_code>gold</plan_code></subscription>
<subscription><account><account_code>1</account_code><billing_info><month>1</month><number>4111-1111-1111-1111</number><year>2014</year></billing_info><email>[email protected]</email><first_name>Verena</first_name><last_name>Example</last_name></account><currency>EUR</currency><customer_notes>Some Customer Notes</customer_notes><plan_code>gold</plan_code><terms_and_conditions>Some Terms and Conditions</terms_and_conditions></subscription>
2 changes: 1 addition & 1 deletion spec/fixtures/subscriptions/serialize-token.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<subscription><account><account_code>1</account_code><billing_info><token_id>abc123</token_id></billing_info><email>[email protected]</email><first_name>Verena</first_name><last_name>Example</last_name></account><currency>EUR</currency><plan_code>gold</plan_code></subscription>
<subscription><account><account_code>1</account_code><billing_info><token_id>abc123</token_id></billing_info><email>[email protected]</email><first_name>Verena</first_name><last_name>Example</last_name></account><currency>EUR</currency><customer_notes>Some Customer Notes</customer_notes><plan_code>gold</plan_code><terms_and_conditions>Some Terms and Conditions</terms_and_conditions></subscription>
2 changes: 1 addition & 1 deletion spec/fixtures/subscriptions/serialize-with-bulk.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<subscription><account><account_code>1</account_code><billing_info><month>1</month><number>4111-1111-1111-1111</number><year>2014</year></billing_info><email>[email protected]</email><first_name>Verena</first_name><last_name>Example</last_name></account><bulk>true</bulk><currency>EUR</currency><plan_code>gold</plan_code></subscription>
<subscription><account><account_code>1</account_code><billing_info><month>1</month><number>4111-1111-1111-1111</number><year>2014</year></billing_info><email>[email protected]</email><first_name>Verena</first_name><last_name>Example</last_name></account><bulk>true</bulk><currency>EUR</currency><customer_notes>Some Customer Notes</customer_notes><plan_code>gold</plan_code><terms_and_conditions>Some Terms and Conditions</terms_and_conditions></subscription>
14 changes: 14 additions & 0 deletions spec/recurly/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@
account.invoice!.must_be_instance_of Invoice
end

it "must add optional attributes to the invoice if given" do
stub_api_request(
:post, 'accounts/abcdef1234567890/invoices', 'invoices/create-with-optionals-201'
)
invoice = account.invoice!({
terms_and_conditions: 'Some Terms and Conditions',
customer_notes: 'Some Customer Notes'
})

invoice.must_be_instance_of Invoice
invoice.customer_notes.must_equal 'Some Customer Notes'
invoice.terms_and_conditions.must_equal 'Some Terms and Conditions'
end

it "must raise an exception if unsuccessful" do
stub_api_request(
:post, 'accounts/abcdef1234567890/invoices', 'invoices/create-422'
Expand Down
8 changes: 8 additions & 0 deletions spec/recurly/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
invoice = Invoice.find 'taxed-invoice'
invoice.tax_type.must_equal 'usst'
end

it 'can access notes if there' do
stub_api_request :get, 'invoices/created-invoice', 'invoices/show-200-with-notes'

invoice = Invoice.find 'created-invoice'
invoice.customer_notes.must_equal 'Some Customer Notes'
invoice.terms_and_conditions.must_equal 'Some Terms and Conditions'
end
end

describe "line item refund" do
Expand Down
7 changes: 6 additions & 1 deletion spec/recurly/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
tax_rate
total_billing_cycles
remaining_billing_cycles
bulk}
bulk
terms_and_conditions
customer_notes
}

subject.attribute_names.sort.must_equal expected_attributes.sort
end
Expand All @@ -40,6 +43,8 @@
{
plan_code: 'gold',
currency: 'EUR',
terms_and_conditions: 'Some Terms and Conditions',
customer_notes: 'Some Customer Notes',
account: {
account_code: '1',
email: '[email protected]',
Expand Down

0 comments on commit 261a6d8

Please sign in to comment.