Skip to content

Commit 8354a55

Browse files
authored
Merge pull request #89 from ConvertKit/v4-api-purchases-sku
v4 API: Purchases: SKU and Required / Optional Fields
2 parents 7e8b632 + 1a35c36 commit 8354a55

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
@@ -4263,32 +4263,37 @@ public function testGetPurchaseWithInvalidID()
42634263
public function testCreatePurchase()
42644264
{
42654265
$purchase = $this->api->create_purchase(
4266+
// Required fields.
42664267
email_address: $this->generateEmailAddress(),
42674268
transaction_id: str_shuffle('wfervdrtgsdewrafvwefds'),
4268-
status: 'paid',
4269-
subtotal: 20.00,
4270-
tax: 2.00,
4271-
shipping: 2.00,
4272-
discount: 3.00,
4273-
total: 21.00,
42744269
currency: 'usd',
4275-
transaction_time: new DateTime('now'),
42764270
products: [
42774271
[
42784272
'name' => 'Floppy Disk (512k)',
4273+
'sku' => '7890-ijkl',
42794274
'pid' => 9999,
42804275
'lid' => 7777,
42814276
'quantity' => 2,
42824277
'unit_price' => 5.00,
42834278
],
42844279
[
42854280
'name' => 'Telephone Cord (data)',
4281+
'sku' => 'mnop-1234',
42864282
'pid' => 5555,
42874283
'lid' => 7778,
42884284
'quantity' => 1,
42894285
'unit_price' => 10.00,
42904286
],
42914287
],
4288+
// Optional fields.
4289+
first_name: 'Tim',
4290+
status: 'paid',
4291+
subtotal: 20.00,
4292+
tax: 2.00,
4293+
shipping: 2.00,
4294+
discount: 3.00,
4295+
total: 21.00,
4296+
transaction_time: new DateTime('now'),
42924297
);
42934298

42944299
$this->assertInstanceOf('stdClass', $purchase);
@@ -4297,19 +4302,76 @@ public function testCreatePurchase()
42974302

42984303
/**
42994304
* Test that create_purchase() throws a ClientException when an invalid
4300-
* purchase data is specified.
4305+
* email address is specified.
43014306
*
4302-
* @since 1.0.0
4307+
* @since 2.0.0
43034308
*
43044309
* @return void
43054310
*/
4306-
public function testCreatePurchaseWithInvalidData()
4311+
public function testCreatePurchaseWithInvalidEmailAddress()
43074312
{
43084313
$this->expectException(ClientException::class);
43094314
$this->api->create_purchase(
43104315
email_address: 'not-an-email-address',
43114316
transaction_id: str_shuffle('wfervdrtgsdewrafvwefds'),
4312-
status: 'paid'
4317+
currency: 'usd',
4318+
products: [
4319+
[
4320+
'name' => 'Floppy Disk (512k)',
4321+
'sku' => '7890-ijkl',
4322+
'pid' => 9999,
4323+
'lid' => 7777,
4324+
'quantity' => 2,
4325+
'unit_price' => 5.00,
4326+
],
4327+
],
4328+
);
4329+
}
4330+
4331+
/**
4332+
* Test that create_purchase() throws a ClientException when a blank
4333+
* transaction ID is specified.
4334+
*
4335+
* @since 2.0.0
4336+
*
4337+
* @return void
4338+
*/
4339+
public function testCreatePurchaseWithBlankTransactionID()
4340+
{
4341+
$this->expectException(ClientException::class);
4342+
$this->api->create_purchase(
4343+
email_address: $this->generateEmailAddress(),
4344+
transaction_id: '',
4345+
currency: 'usd',
4346+
products: [
4347+
[
4348+
'name' => 'Floppy Disk (512k)',
4349+
'sku' => '7890-ijkl',
4350+
'pid' => 9999,
4351+
'lid' => 7777,
4352+
'quantity' => 2,
4353+
'unit_price' => 5.00,
4354+
],
4355+
],
4356+
);
4357+
}
4358+
4359+
/**
4360+
* Test that create_purchase() throws a ClientException when no products
4361+
* are specified.
4362+
*
4363+
* @since 2.0.0
4364+
*
4365+
* @return void
4366+
*/
4367+
public function testCreatePurchaseWithNoProducts()
4368+
{
4369+
$this->expectException(ClientException::class);
4370+
$this->api->create_purchase(
4371+
email_address: $this->generateEmailAddress(),
4372+
transaction_id: str_shuffle('wfervdrtgsdewrafvwefds'),
4373+
currency: 'usd',
4374+
products: [],
43134375
);
43144376
}
43154377

0 commit comments

Comments
 (0)