From 89f4b88721e15c5e1193e36bff6f3260c7a4648b Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 13 Mar 2023 16:18:01 +0000 Subject: [PATCH 1/4] Added get_purchase() API function and tests --- src/ConvertKit_API.php | 17 +++++++++++++++++ tests/ConvertKitAPITest.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 1921d42..134c72a 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -610,6 +610,23 @@ public function list_purchases(array $options) return $this->get('purchases', $options); } + /** + * Retuns a specific purchase. + * + * @param integer $purchase_id Purchase ID. + * + * @return false|object + */ + public function get_purchase(int $purchase_id) + { + return $this->get( + sprintf('purchases/%s', $purchase_id), + [ + 'api_secret' => $this->api_secret, + ] + ); + } + /** * Creates a purchase. * diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index b0dda34..bb748a8 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -712,6 +712,41 @@ public function testListPurchases() $this->assertArrayHasKey('purchases', get_object_vars($purchases)); } + /** + * Test that get_purchases() returns the expected data. + * + * @since 1.0.0 + * + * @return void + */ + public function testGetPurchase() + { + // Get ID of first purchase. + $purchases = $this->api->list_purchases([ + 'page' => 1, + ]); + $id = $purchases->purchases[0]->id; + + // Get purchase. + $result = $this->api->get_purchase($id); + $this->assertInstanceOf('stdClass', $result); + $this->assertEquals($result->id, $id); + } + + /** + * Test that get_purchases() throws a ClientException when an invalid + * purchase ID is specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testGetPurchaseWithInvalidID() + { + $this->expectException(GuzzleHttp\Exception\ClientException::class); + $this->api->get_purchase(12345); + } + /** * Test that create_purchase() returns the expected data. * From ea92379faa0efc47f7a9dfe53b193c6d8ea22635 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 13 Mar 2023 16:19:31 +0000 Subject: [PATCH 2/4] Add testCreatePurchaseWithMissingData test --- tests/ConvertKitAPITest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index bb748a8..ba6e0de 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -793,6 +793,24 @@ public function testCreatePurchase() $this->assertArrayHasKey('transaction_id', get_object_vars($purchase)); } + /** + * Test that create_purchase() throws a ClientException when an invalid + * purchase data is specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testCreatePurchaseWithMissingData() + { + $this->expectException(GuzzleHttp\Exception\ClientException::class); + $this->api->create_purchase([ + 'invalid-key' => [ + 'transaction_id' => str_shuffle('wfervdrtgsdewrafvwefds'), + ], + ]); + } + /** * Test that fetching a legacy form's markup works. * From b3bb9e3361d5b53a7e032e46b0a3db02da0a67c2 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 13 Mar 2023 16:22:54 +0000 Subject: [PATCH 3/4] Add missing links to API docs --- src/ConvertKit_API.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 134c72a..b93f641 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -593,6 +593,8 @@ public function get_subscriber_tags(int $subscriber_id) * List purchases. * * @param array $options Request options. + * + * @see https://developers.convertkit.com/#list-purchases * * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * @@ -614,6 +616,8 @@ public function list_purchases(array $options) * Retuns a specific purchase. * * @param integer $purchase_id Purchase ID. + * + * @see https://developers.convertkit.com/#retrieve-a-specific-purchase * * @return false|object */ @@ -631,6 +635,8 @@ public function get_purchase(int $purchase_id) * Creates a purchase. * * @param array $options Purchase data. + * + * @see https://developers.convertkit.com/#create-a-purchase * * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * From 377b8d16a784ffadf1e8c9764317a977f4ba4367 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 13 Mar 2023 16:24:03 +0000 Subject: [PATCH 4/4] Coding standards --- src/ConvertKit_API.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index b93f641..51400c2 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -593,7 +593,7 @@ public function get_subscriber_tags(int $subscriber_id) * List purchases. * * @param array $options Request options. - * + * * @see https://developers.convertkit.com/#list-purchases * * @throws \InvalidArgumentException If the provided arguments are not of the expected type. @@ -616,7 +616,7 @@ public function list_purchases(array $options) * Retuns a specific purchase. * * @param integer $purchase_id Purchase ID. - * + * * @see https://developers.convertkit.com/#retrieve-a-specific-purchase * * @return false|object @@ -635,7 +635,7 @@ public function get_purchase(int $purchase_id) * Creates a purchase. * * @param array $options Purchase data. - * + * * @see https://developers.convertkit.com/#create-a-purchase * * @throws \InvalidArgumentException If the provided arguments are not of the expected type.