Skip to content

Commit f5c21e9

Browse files
authored
Merge pull request #47 from ConvertKit/purchase-functions
Add `get_purchase()` function
2 parents 53e8485 + 377b8d1 commit f5c21e9

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/ConvertKit_API.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,8 @@ public function delete_custom_field(int $id)
932932
*
933933
* @param array<string, string> $options Request options.
934934
*
935+
* @see https://developers.convertkit.com/#list-purchases
936+
*
935937
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
936938
*
937939
* @return false|object
@@ -948,11 +950,32 @@ public function list_purchases(array $options)
948950
return $this->get('purchases', $options);
949951
}
950952

953+
/**
954+
* Retuns a specific purchase.
955+
*
956+
* @param integer $purchase_id Purchase ID.
957+
*
958+
* @see https://developers.convertkit.com/#retrieve-a-specific-purchase
959+
*
960+
* @return false|object
961+
*/
962+
public function get_purchase(int $purchase_id)
963+
{
964+
return $this->get(
965+
sprintf('purchases/%s', $purchase_id),
966+
[
967+
'api_secret' => $this->api_secret,
968+
]
969+
);
970+
}
971+
951972
/**
952973
* Creates a purchase.
953974
*
954975
* @param array<string, string> $options Purchase data.
955976
*
977+
* @see https://developers.convertkit.com/#create-a-purchase
978+
*
956979
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
957980
*
958981
* @return false|object

tests/ConvertKitAPITest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,41 @@ public function testListPurchases()
14291429
$this->assertArrayHasKey('purchases', get_object_vars($purchases));
14301430
}
14311431

1432+
/**
1433+
* Test that get_purchases() returns the expected data.
1434+
*
1435+
* @since 1.0.0
1436+
*
1437+
* @return void
1438+
*/
1439+
public function testGetPurchase()
1440+
{
1441+
// Get ID of first purchase.
1442+
$purchases = $this->api->list_purchases([
1443+
'page' => 1,
1444+
]);
1445+
$id = $purchases->purchases[0]->id;
1446+
1447+
// Get purchase.
1448+
$result = $this->api->get_purchase($id);
1449+
$this->assertInstanceOf('stdClass', $result);
1450+
$this->assertEquals($result->id, $id);
1451+
}
1452+
1453+
/**
1454+
* Test that get_purchases() throws a ClientException when an invalid
1455+
* purchase ID is specified.
1456+
*
1457+
* @since 1.0.0
1458+
*
1459+
* @return void
1460+
*/
1461+
public function testGetPurchaseWithInvalidID()
1462+
{
1463+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
1464+
$this->api->get_purchase(12345);
1465+
}
1466+
14321467
/**
14331468
* Test that create_purchase() returns the expected data.
14341469
*
@@ -1475,6 +1510,24 @@ public function testCreatePurchase()
14751510
$this->assertArrayHasKey('transaction_id', get_object_vars($purchase));
14761511
}
14771512

1513+
/**
1514+
* Test that create_purchase() throws a ClientException when an invalid
1515+
* purchase data is specified.
1516+
*
1517+
* @since 1.0.0
1518+
*
1519+
* @return void
1520+
*/
1521+
public function testCreatePurchaseWithMissingData()
1522+
{
1523+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
1524+
$this->api->create_purchase([
1525+
'invalid-key' => [
1526+
'transaction_id' => str_shuffle('wfervdrtgsdewrafvwefds'),
1527+
],
1528+
]);
1529+
}
1530+
14781531
/**
14791532
* Test that fetching a legacy form's markup works.
14801533
*

0 commit comments

Comments
 (0)