Skip to content

Commit 1a35c36

Browse files
committed
Add sku to products; reflect required and optional fields
1 parent b82e8de commit 1a35c36

File tree

2 files changed

+104
-24
lines changed

2 files changed

+104
-24
lines changed

src/ConvertKit_API.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,15 +1750,16 @@ public function get_purchase(int $purchase_id)
17501750
*
17511751
* @param string $email_address Email Address.
17521752
* @param string $transaction_id Transaction ID.
1753+
* @param array<string,int|float|string> $products Products.
1754+
* @param string $currency ISO Currency Code.
1755+
* @param string $first_name First Name.
17531756
* @param string $status Order Status.
17541757
* @param float $subtotal Subtotal.
17551758
* @param float $tax Tax.
17561759
* @param float $shipping Shipping.
17571760
* @param float $discount Discount.
17581761
* @param float $total Total.
1759-
* @param string $currency ISO Currency Code.
17601762
* @param \DateTime $transaction_time Transaction date and time.
1761-
* @param array<string,int|float|string> $products Products.
17621763
*
17631764
* @see https://developers.convertkit.com/v4.html#create-a-purchase
17641765
*
@@ -1767,31 +1768,48 @@ public function get_purchase(int $purchase_id)
17671768
public function create_purchase(
17681769
string $email_address,
17691770
string $transaction_id,
1770-
string $status,
1771+
array $products,
1772+
string $currency = 'USD',
1773+
string $first_name = null,
1774+
string $status = null,
17711775
float $subtotal = 0,
17721776
float $tax = 0,
17731777
float $shipping = 0,
17741778
float $discount = 0,
17751779
float $total = 0,
1776-
string $currency = 'usd',
1777-
\DateTime $transaction_time = null,
1778-
array $products = []
1780+
\DateTime $transaction_time = null
17791781
) {
17801782
// Build parameters.
17811783
$options = [
1784+
// Required fields.
17821785
'email_address' => $email_address,
17831786
'transaction_id' => $transaction_id,
1787+
'products' => $products,
1788+
'currency' => $currency, // Required, but if not provided, API will default to USD.
1789+
1790+
// Optional fields.
1791+
'first_name' => $first_name,
17841792
'status' => $status,
17851793
'subtotal' => $subtotal,
17861794
'tax' => $tax,
17871795
'shipping' => $shipping,
17881796
'discount' => $discount,
17891797
'total' => $total,
1790-
'currency' => $currency,
17911798
'transaction_time' => (!is_null($transaction_time) ? $transaction_time->format('Y-m-d H:i:s') : ''),
1792-
'products' => $products,
17931799
];
17941800

1801+
// Iterate through options, removing blank and null entries.
1802+
foreach ($options as $key => $value) {
1803+
if (is_null($value)) {
1804+
unset($options[$key]);
1805+
continue;
1806+
}
1807+
1808+
if (is_string($value) && strlen($value) === 0) {
1809+
unset($options[$key]);
1810+
}
1811+
}
1812+
17951813
return $this->post('purchases', $options);
17961814
}
17971815

@@ -2015,8 +2033,8 @@ public function get(string $endpoint, array $args = [])
20152033
/**
20162034
* Performs a POST request to the API.
20172035
*
2018-
* @param string $endpoint API Endpoint.
2019-
* @param array<string, bool|integer|float|string|array<int|string, float|integer|string|array<string|string>>> $args Request arguments.
2036+
* @param string $endpoint API Endpoint.
2037+
* @param array<string, bool|integer|float|string|null|array<int|string, float|integer|string|array<string|string>>> $args Request arguments.
20202038
*
20212039
* @return false|mixed
20222040
*/
@@ -2054,9 +2072,9 @@ public function delete(string $endpoint, array $args = [])
20542072
/**
20552073
* Performs an API request using Guzzle.
20562074
*
2057-
* @param string $endpoint API Endpoint.
2058-
* @param string $method Request method.
2059-
* @param array<string, bool|integer|float|string|array<int|string, float|integer|string|array<string|string>>> $args Request arguments.
2075+
* @param string $endpoint API Endpoint.
2076+
* @param string $method Request method.
2077+
* @param array<string, bool|integer|float|string|null|array<int|string, float|integer|string|array<string|string>>> $args Request arguments.
20602078
*
20612079
* @throws \Exception If JSON encoding arguments failed.
20622080
*

tests/ConvertKitAPITest.php

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4226,32 +4226,37 @@ public function testGetPurchaseWithInvalidID()
42264226
public function testCreatePurchase()
42274227
{
42284228
$purchase = $this->api->create_purchase(
4229+
// Required fields.
42294230
email_address: $this->generateEmailAddress(),
42304231
transaction_id: str_shuffle('wfervdrtgsdewrafvwefds'),
4231-
status: 'paid',
4232-
subtotal: 20.00,
4233-
tax: 2.00,
4234-
shipping: 2.00,
4235-
discount: 3.00,
4236-
total: 21.00,
42374232
currency: 'usd',
4238-
transaction_time: new DateTime('now'),
42394233
products: [
42404234
[
42414235
'name' => 'Floppy Disk (512k)',
4236+
'sku' => '7890-ijkl',
42424237
'pid' => 9999,
42434238
'lid' => 7777,
42444239
'quantity' => 2,
42454240
'unit_price' => 5.00,
42464241
],
42474242
[
42484243
'name' => 'Telephone Cord (data)',
4244+
'sku' => 'mnop-1234',
42494245
'pid' => 5555,
42504246
'lid' => 7778,
42514247
'quantity' => 1,
42524248
'unit_price' => 10.00,
42534249
],
42544250
],
4251+
// Optional fields.
4252+
first_name: 'Tim',
4253+
status: 'paid',
4254+
subtotal: 20.00,
4255+
tax: 2.00,
4256+
shipping: 2.00,
4257+
discount: 3.00,
4258+
total: 21.00,
4259+
transaction_time: new DateTime('now'),
42554260
);
42564261

42574262
$this->assertInstanceOf('stdClass', $purchase);
@@ -4260,19 +4265,76 @@ public function testCreatePurchase()
42604265

42614266
/**
42624267
* Test that create_purchase() throws a ClientException when an invalid
4263-
* purchase data is specified.
4268+
* email address is specified.
42644269
*
4265-
* @since 1.0.0
4270+
* @since 2.0.0
42664271
*
42674272
* @return void
42684273
*/
4269-
public function testCreatePurchaseWithInvalidData()
4274+
public function testCreatePurchaseWithInvalidEmailAddress()
42704275
{
42714276
$this->expectException(ClientException::class);
42724277
$this->api->create_purchase(
42734278
email_address: 'not-an-email-address',
42744279
transaction_id: str_shuffle('wfervdrtgsdewrafvwefds'),
4275-
status: 'paid'
4280+
currency: 'usd',
4281+
products: [
4282+
[
4283+
'name' => 'Floppy Disk (512k)',
4284+
'sku' => '7890-ijkl',
4285+
'pid' => 9999,
4286+
'lid' => 7777,
4287+
'quantity' => 2,
4288+
'unit_price' => 5.00,
4289+
],
4290+
],
4291+
);
4292+
}
4293+
4294+
/**
4295+
* Test that create_purchase() throws a ClientException when a blank
4296+
* transaction ID is specified.
4297+
*
4298+
* @since 2.0.0
4299+
*
4300+
* @return void
4301+
*/
4302+
public function testCreatePurchaseWithBlankTransactionID()
4303+
{
4304+
$this->expectException(ClientException::class);
4305+
$this->api->create_purchase(
4306+
email_address: $this->generateEmailAddress(),
4307+
transaction_id: '',
4308+
currency: 'usd',
4309+
products: [
4310+
[
4311+
'name' => 'Floppy Disk (512k)',
4312+
'sku' => '7890-ijkl',
4313+
'pid' => 9999,
4314+
'lid' => 7777,
4315+
'quantity' => 2,
4316+
'unit_price' => 5.00,
4317+
],
4318+
],
4319+
);
4320+
}
4321+
4322+
/**
4323+
* Test that create_purchase() throws a ClientException when no products
4324+
* are specified.
4325+
*
4326+
* @since 2.0.0
4327+
*
4328+
* @return void
4329+
*/
4330+
public function testCreatePurchaseWithNoProducts()
4331+
{
4332+
$this->expectException(ClientException::class);
4333+
$this->api->create_purchase(
4334+
email_address: $this->generateEmailAddress(),
4335+
transaction_id: str_shuffle('wfervdrtgsdewrafvwefds'),
4336+
currency: 'usd',
4337+
products: [],
42764338
);
42774339
}
42784340

0 commit comments

Comments
 (0)