Skip to content

Commit

Permalink
Add Invoice#invoice_number_prefix and Invoice#invoiceNumberWithPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarton committed Jan 9, 2015
1 parent 0161ef8 commit 99bb538
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Added ability to read and write custom invoice notes [115](https://github.com/recurly/recurly-client-php/pull/115)
* Added vat_location_valid field to Account [127](https://github.com/recurly/recurly-client-php/pull/127)
* Added updateNotes() and vat_reverse_charge to Subscription. Added vat_reverse_charge_notes to Invoice. [128](https://github.com/recurly/recurly-client-php/pull/128)
* Added `invoice_number_prefix` and `invoiceNumberWithPrefix()` to `Invoice`. This is to be used with the Country Invoice Sequencing feature. [130](https://github.com/recurly/recurly-client-php/pull/130)

## Version 2.3.1 (Sept 26th, 2014)

Expand Down
28 changes: 19 additions & 9 deletions Tests/Recurly/Invoice_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,36 @@ class Recurly_InvoiceTest extends Recurly_TestCase

function defaultResponses() {
return array(
array('GET', '/invoices/abcdef1234567890', 'invoices/show-200.xml'),
array('GET', '/invoices/1001', 'invoices/show-200.xml'),
array('GET', '/invoices/1002', 'invoices/show-with-prefix-200.xml'),
);
}

public function testGetInvoice() {
$invoice = Recurly_Invoice::get('abcdef1234567890', $this->client);
$invoice = Recurly_Invoice::get('1001', $this->client);

$this->assertInstanceOf('Recurly_Invoice', $invoice);
$this->assertInstanceOf('Recurly_Stub', $invoice->account);
$this->assertInstanceOf('Recurly_Stub', $invoice->subscription);
$this->assertEquals($invoice->state, 'collected');
$this->assertEquals($invoice->total_in_cents, 2995);
$this->assertEquals($invoice->getHref(),'https://api.recurly.com/v2/invoices/abcdef1234567890');
$this->assertEquals($invoice->getHref(),'https://api.recurly.com/v2/invoices/1001');
$this->assertInstanceOf('Recurly_TransactionList', $invoice->transactions);
$this->assertEquals($invoice->transactions->current()->uuid, '012345678901234567890123456789ab');
$this->assertEquals($invoice->transactions->count(), 1);
$this->assertEquals($invoice->tax_type, 'usst');
$this->assertEquals($invoice->terms_and_conditions, 'Some Terms and Conditions');
$this->assertEquals($invoice->customer_notes, 'Some Customer Notes');
$this->assertEquals($invoice->vat_reverse_charge_notes, 'Some VAT Notes');
$this->assertEquals($invoice->invoice_number_prefix, '');
$this->assertEquals($invoice->invoiceNumberWithPrefix(), '1001');
}

public function testGetInvoiceWithPrefix() {
$invoice = Recurly_Invoice::get('1002', $this->client);
$this->assertEquals($invoice->invoice_number, '1002');
$this->assertEquals($invoice->invoice_number_prefix, 'GB');
$this->assertEquals($invoice->invoiceNumberWithPrefix(), 'GB1002');
}

public function testInvoicePendingCharges() {
Expand Down Expand Up @@ -91,24 +101,24 @@ public function testMarkSuccessful() {
// - Recurly_Resource::_save() passes the current XML into the PUT which
// doesn't seem quite right but I don't want to change it without
// understanding what side effects it would have.
$this->client->addResponse('PUT', 'https://api.recurly.com/v2/invoices/abcdef1234567890/mark_successful', 'invoices/mark_successful-200.xml');
$this->client->addResponse('PUT', 'https://api.recurly.com/v2/invoices/1001/mark_successful', 'invoices/mark_successful-200.xml');

$invoice = Recurly_Invoice::get('abcdef1234567890', $this->client);
$invoice = Recurly_Invoice::get('1001', $this->client);
$invoice->markSuccessful();
$this->assertEquals($invoice->state, 'collected');
}

public function testMarkFailed() {
// See the notes in testMarkSuccessful().
$this->client->addResponse('PUT', 'https://api.recurly.com/v2/invoices/abcdef1234567890/mark_failed', 'invoices/mark_failed-200.xml');
$this->client->addResponse('PUT', 'https://api.recurly.com/v2/invoices/1001/mark_failed', 'invoices/mark_failed-200.xml');

$invoice = Recurly_Invoice::get('abcdef1234567890', $this->client);
$invoice = Recurly_Invoice::get('1001', $this->client);
$invoice->markFailed();
$this->assertEquals($invoice->state, 'failed');
}

public function testGetInvoicePdf() {
$result = Recurly_Invoice::getInvoicePdf('abcdef1234567890', 'en-GB', $this->client);
$this->assertEquals(array('/invoices/abcdef1234567890', 'en-GB'), $result);
$result = Recurly_Invoice::getInvoicePdf('1001', 'en-GB', $this->client);
$this->assertEquals(array('/invoices/1001', 'en-GB'), $result);
}
}
1 change: 1 addition & 0 deletions Tests/fixtures/invoices/create-201.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Location: https://api.recurly.com/v2/invoices/created-invoice
<uuid>012345678901234567890123456789ab</uuid>
<state>open</state>
<invoice_number type="integer">1000</invoice_number>
<invoice_number_prefix></invoice_number_prefix>
<currency>USD</currency>
<total_in_cents type="integer">300</total_in_cents>
<subtotal_in_cents type="integer">300</subtotal_in_cents>
Expand Down
7 changes: 4 additions & 3 deletions Tests/fixtures/invoices/mark_failed-200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ 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/abcdef1234567890">
<invoice href="https://api.recurly.com/v2/invoices/1001">
<account href="https://api.recurly.com/v2/accounts/1"/>
<uuid>012345678901234567890123456789aa</uuid>
<state>failed</state>
<invoice_number type="integer">abcdef1234567890</invoice_number>
<invoice_number type="integer">1001</invoice_number>
<invoice_number_prefix></invoice_number_prefix>
<po_number nil="nil"></po_number>
<vat_number nil="nil"></vat_number>
<subtotal_in_cents type="integer">2995</subtotal_in_cents>
Expand All @@ -17,7 +18,7 @@ Content-Type: application/xml; charset=utf-8
<line_items type="array">
<adjustment href="https://api.recurly.com/v2/adjustments/190357944dad4d461272774dd184a17e" type="charge">
<account href="https://api.recurly.com/v2/accounts/1"/>
<invoice href="https://api.recurly.com/v2/invoices/abcdef1234567890"/>
<invoice href="https://api.recurly.com/v2/invoices/1001"/>
<uuid>012345678901234567890123456789ab</uuid>
<state>invoiced</state>
<description>Silver Plan</description>
Expand Down
7 changes: 4 additions & 3 deletions Tests/fixtures/invoices/mark_successful-200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ 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/abcdef1234567890">
<invoice href="https://api.recurly.com/v2/invoices/1001">
<account href="https://api.recurly.com/v2/accounts/1"/>
<uuid>012345678901234567890123456789aa</uuid>
<state>collected</state>
<invoice_number type="integer">abcdef1234567890</invoice_number>
<invoice_number type="integer">1001</invoice_number>
<invoice_number_prefix></invoice_number_prefix>
<po_number nil="nil"></po_number>
<vat_number nil="nil"></vat_number>
<subtotal_in_cents type="integer">2995</subtotal_in_cents>
Expand All @@ -17,7 +18,7 @@ Content-Type: application/xml; charset=utf-8
<line_items type="array">
<adjustment href="https://api.recurly.com/v2/adjustments/190357944dad4d461272774dd184a17e" type="charge">
<account href="https://api.recurly.com/v2/accounts/1"/>
<invoice href="https://api.recurly.com/v2/invoices/abcdef1234567890"/>
<invoice href="https://api.recurly.com/v2/invoices/1001"/>
<uuid>012345678901234567890123456789ab</uuid>
<state>invoiced</state>
<description>Silver Plan</description>
Expand Down
1 change: 1 addition & 0 deletions Tests/fixtures/invoices/preview-200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Content-Type: application/xml; charset=utf-8
<uuid>012345678901234567890123456789ab</uuid>
<state>open</state>
<invoice_number type="integer">1000</invoice_number>
<invoice_number_prefix></invoice_number_prefix>
<currency>USD</currency>
<total_in_cents type="integer">300</total_in_cents>
<subtotal_in_cents type="integer">300</subtotal_in_cents>
Expand Down
9 changes: 5 additions & 4 deletions Tests/fixtures/invoices/show-200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ 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/abcdef1234567890">
<invoice href="https://api.recurly.com/v2/invoices/1001">
<account href="https://api.recurly.com/v2/accounts/1"/>
<subscription href="https://api.recurly.com/v2/subscriptions/1234567890abcdef"/>
<uuid>012345678901234567890123456789aa</uuid>
<state>collected</state>
<invoice_number type="integer">abcdef1234567890</invoice_number>
<invoice_number type="integer">1001</invoice_number>
<invoice_number_prefix></invoice_number_prefix>
<po_number></po_number>
<vat_number></vat_number>
<subtotal_in_cents type="integer">2995</subtotal_in_cents>
Expand All @@ -25,7 +26,7 @@ Content-Type: application/xml; charset=utf-8
<line_items type="array">
<adjustment href="https://api.recurly.com/v2/adjustments/012345678901234567890123456789ab" type="charge">
<account href="https://api.recurly.com/v2/accounts/1"/>
<invoice href="https://api.recurly.com/v2/invoices/abcdef1234567890"/>
<invoice href="https://api.recurly.com/v2/invoices/1001"/>
<subscription href="https://api.recurly.com/v2/subscriptions/1234567890abcdef"/>
<uuid>012345678901234567890123456789ab</uuid>
<state>invoiced</state>
Expand All @@ -48,7 +49,7 @@ Content-Type: application/xml; charset=utf-8
<transactions type="array">
<transaction href="https://api.recurly.com/v2/transactions/012345678901234567890123456789ab" type="credit_card">
<account href="https://api.recurly.com/v2/accounts/1"/>
<invoice href="https://api.recurly.com/v2/invoices/abcdef1234567890"/>
<invoice href="https://api.recurly.com/v2/invoices/1001"/>
<subscription href="https://api.recurly.com/v2/subscriptions/1234567890abcdef"/>
<uuid>012345678901234567890123456789ab</uuid>
<action>purchase</action>
Expand Down
101 changes: 101 additions & 0 deletions Tests/fixtures/invoices/show-with-prefix-200.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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/1002">
<account href="https://api.recurly.com/v2/accounts/1"/>
<subscription href="https://api.recurly.com/v2/subscriptions/1234567890abcdef"/>
<uuid>012345678901234567890123456789aa</uuid>
<state>collected</state>
<invoice_number type="integer">1002</invoice_number>
<invoice_number_prefix>GB</invoice_number_prefix>
<po_number></po_number>
<vat_number></vat_number>
<subtotal_in_cents type="integer">2995</subtotal_in_cents>
<tax_in_cents type="integer">0</tax_in_cents>
<total_in_cents type="integer">2995</total_in_cents>
<currency>USD</currency>
<created_at type="datetime">2012-05-28T17:44:13Z</created_at>
<closed_at type="datetime">2012-05-28T17:44:13Z</closed_at>
<net_terms type="integer">0</net_terms>
<collection_method>automatic</collection_method>
<tax_type>usst</tax_type>
<terms_and_conditions>Some Terms and Conditions</terms_and_conditions>
<customer_notes>Some Customer Notes</customer_notes>
<vat_reverse_charge_notes>Some VAT Notes</vat_reverse_charge_notes>
<line_items type="array">
<adjustment href="https://api.recurly.com/v2/adjustments/012345678901234567890123456789ab" type="charge">
<account href="https://api.recurly.com/v2/accounts/1"/>
<invoice href="https://api.recurly.com/v2/invoices/1002"/>
<subscription href="https://api.recurly.com/v2/subscriptions/1234567890abcdef"/>
<uuid>012345678901234567890123456789ab</uuid>
<state>invoiced</state>
<description>Silver Plan</description>
<accounting_code></accounting_code>
<product_code nil="nil"></product_code>
<origin>plan</origin>
<unit_amount_in_cents type="integer">2995</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">2995</total_in_cents>
<currency>USD</currency>
<taxable type="boolean">false</taxable>
<start_date type="datetime">2012-05-28T17:44:13Z</start_date>
<end_date type="datetime">2013-05-28T17:44:13Z</end_date>
<created_at type="datetime">2012-05-28T17:44:13Z</created_at>
</adjustment>
</line_items>
<transactions type="array">
<transaction href="https://api.recurly.com/v2/transactions/012345678901234567890123456789ab" type="credit_card">
<account href="https://api.recurly.com/v2/accounts/1"/>
<invoice href="https://api.recurly.com/v2/invoices/1002"/>
<subscription href="https://api.recurly.com/v2/subscriptions/1234567890abcdef"/>
<uuid>012345678901234567890123456789ab</uuid>
<action>purchase</action>
<amount_in_cents type="integer">2995</amount_in_cents>
<tax_in_cents type="integer">0</tax_in_cents>
<currency>USD</currency>
<status>success</status>
<payment_method>credit_card</payment_method>
<reference>12345</reference>
<source>subscription</source>
<recurring type="boolean">false</recurring>
<test type="boolean">true</test>
<voidable type="boolean">true</voidable>
<refundable type="boolean">true</refundable>
<cvv_result code="M">Match</cvv_result>
<avs_result code="D">Street address and postal code match.</avs_result>
<avs_result_street>Y</avs_result_street>
<avs_result_postal>Y</avs_result_postal>
<created_at type="datetime">2012-05-28T17:44:13Z</created_at>
<details>
<account>
<account_code>1</account_code>
<first_name>John</first_name>
<last_name>Doe</last_name>
<company nil="nil"></company>
<email>john@example.com</email>
<billing_info type="credit_card">
<first_name nil="nil"></first_name>
<last_name nil="nil"></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">2013</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/012345678901234567890123456789ab" method="delete"/>
</transaction>
</transactions>
</invoice>
1 change: 1 addition & 0 deletions Tests/fixtures/subscriptions/preview-200-change.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Content-Type: application/xml; charset=utf-8
<uuid>abcdefg123</uuid>
<state>open</state>
<invoice_number nil="nil"></invoice_number>
<invoice_number_prefix></invoice_number_prefix>
<po_number nil="nil"></po_number>
<vat_number></vat_number>
<subtotal_in_cents type="integer">5000</subtotal_in_cents>
Expand Down
8 changes: 6 additions & 2 deletions lib/recurly/invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function markFailed() {
$this->_save(Recurly_Client::PUT, $this->uri() . '/mark_failed');
}

public function invoiceNumberWithPrefix() {
return $this->invoice_number_prefix . $this->invoice_number;
}

protected function getNodeName() {
return 'invoice';
}
Expand All @@ -82,8 +86,8 @@ protected function getRequiredAttributes() {
protected function uri() {
if (!empty($this->_href))
return $this->getHref();
else if (!empty($this->invoice_number))
return Recurly_Invoice::uriForInvoice($this->invoice_number);
else if (!empty($this->invoiceNumberWithPrefix()))
return Recurly_Invoice::uriForInvoice($this->invoiceNumberWithPrefix());
else
throw new Recurly_Error("Invoice number not specified");
}
Expand Down

0 comments on commit 99bb538

Please sign in to comment.