diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 1921d42..51400c2 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -594,6 +594,8 @@ public function get_subscriber_tags(int $subscriber_id) * * @param array $options Request options. * + * @see https://developers.convertkit.com/#list-purchases + * * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * * @return false|object @@ -610,11 +612,32 @@ public function list_purchases(array $options) return $this->get('purchases', $options); } + /** + * Retuns a specific purchase. + * + * @param integer $purchase_id Purchase ID. + * + * @see https://developers.convertkit.com/#retrieve-a-specific-purchase + * + * @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. * * @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. * * @return false|object diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index b0dda34..ba6e0de 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. * @@ -758,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. *