From 289adad73c606ff8a57574c02bd0cc41dce36387 Mon Sep 17 00:00:00 2001 From: Petr Kopac Date: Mon, 29 May 2017 16:45:16 +0200 Subject: [PATCH] [DP-468] Retrieve Invoice endpoint (#29) --- README.md | 8 ++++- src/Invoice.php | 3 ++ tests/Unit/InvoiceTest.php | 69 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ac3b8c5..2930c31 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ $ds = ChartMogul\DataSource::create([ ]); ``` -**Get a Datasource by UUID** +**Get a Datasource** ```php ChartMogul\DataSource::retrieve($uuid); @@ -398,6 +398,12 @@ $invoices = ChartMogul\Invoice::all([ ]); ``` +**Retrieve an Invoice** + +```php +$invoice = ChartMogul\Invoice::retrieve('inv_uuid'); +``` + ### Transactions **Create a Transaction** diff --git a/src/Invoice.php b/src/Invoice.php index 0845f7e..e1305f7 100644 --- a/src/Invoice.php +++ b/src/Invoice.php @@ -8,6 +8,7 @@ use ChartMogul\LineItems\Subscription as SubsItem; use ChartMogul\Service\AllTrait; use ChartMogul\Service\DestroyTrait; +use ChartMogul\Service\GetTrait; use ChartMogul\Resource\AbstractResource; use ChartMogul\Transactions\AbstractTransaction; use ChartMogul\Transactions\Payment; @@ -21,6 +22,8 @@ class Invoice extends AbstractResource { use AllTrait; use DestroyTrait; + use GetTrait; + /** * @ignore */ diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php index 4dedaa0..801fd73 100644 --- a/tests/Unit/InvoiceTest.php +++ b/tests/Unit/InvoiceTest.php @@ -62,6 +62,53 @@ class InvoiceTest extends PHPUnit_Framework_TestCase "total_pages": 3 }'; + const RETRIEVE_INVOICE_JSON = '{ + "uuid": "inv_565c73b2-85b9-49c9-a25e-2b7df6a677c9", + "external_id": "INV0001", + "date": "2015-11-01T00:00:00.000Z", + "due_date": "2015-11-15T00:00:00.000Z", + "currency": "USD", + "line_items": [ + { + "uuid": "li_d72e6843-5793-41d0-bfdf-0269514c9c56", + "external_id": null, + "type": "subscription", + "subscription_uuid": "sub_e6bc5407-e258-4de0-bb43-61faaf062035", + "plan_uuid": "pl_eed05d54-75b4-431b-adb2-eb6b9e543206", + "prorated": false, + "service_period_start": "2015-11-01T00:00:00.000Z", + "service_period_end": "2015-12-01T00:00:00.000Z", + "amount_in_cents": 5000, + "quantity": 1, + "discount_code": "PSO86", + "discount_amount_in_cents": 1000, + "tax_amount_in_cents": 900, + "account_code": null + }, + { + "uuid": "li_0cc8c112-beac-416d-af11-f35744ca4e83", + "external_id": null, + "type": "one_time", + "description": "Setup Fees", + "amount_in_cents": 2500, + "quantity": 1, + "discount_code": "PSO86", + "discount_amount_in_cents": 500, + "tax_amount_in_cents": 450, + "account_code": null + } + ], + "transactions": [ + { + "uuid": "tr_879d560a-1bec-41bb-986e-665e38a2f7bc", + "external_id": null, + "type": "payment", + "date": "2015-11-05T00:14:23.000Z", + "result": "successful" + } + ] + }'; + public function testAllInvoices() { $stream = Psr7\stream_for(InvoiceTest::ALL_INVOICE_JSON); @@ -114,4 +161,26 @@ public function testDestroyInvoiceNotFound() $cmClient = new Client(null, $mockClient); $result = (new Invoice(["uuid" => "inv_123"], $cmClient))->destroy(); } + + public function testRetrieveInvoice() + { + $stream = Psr7\stream_for(InvoiceTest::RETRIEVE_INVOICE_JSON); + $response = new Response(200, ['Content-Type' => 'application/json'], $stream); + $mockClient = new \Http\Mock\Client(); + $mockClient->addResponse($response); + + $uuid = 'inv_565c73b2-85b9-49c9-a25e-2b7df6a677c9'; + + $cmClient = new Client(null, $mockClient); + $result = Invoice::retrieve($uuid, $cmClient); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("GET", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("", $uri->getQuery()); + $this->assertEquals("/v1/invoices/".$uuid, $uri->getPath()); + + $this->assertTrue($result instanceof Invoice); + $this->assertEquals($uuid, $result->uuid); + } }