Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,8 @@ public function get_subscriber_tags(int $subscriber_id)
*
* @param array<string, string> $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
Expand All @@ -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<string, string> $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
Expand Down
53 changes: 53 additions & 0 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down