Skip to content

Commit 3b8a826

Browse files
authored
Merge pull request #48 from ConvertKit/webhook-functions
Add Webhook functions
2 parents f5c21e9 + 4152e05 commit 3b8a826

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed

src/ConvertKit_API.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,121 @@ public function get_subscriber_tags(int $subscriber_id)
820820
);
821821
}
822822

823+
/**
824+
* Returns all created webhooks.
825+
*
826+
* @since 1.0.0
827+
*
828+
* @return false|object
829+
*/
830+
public function get_webhooks()
831+
{
832+
return $this->get(
833+
'automations/hooks',
834+
[
835+
'api_secret' => $this->api_secret,
836+
],
837+
);
838+
}
839+
840+
/**
841+
* Creates a webhook that will be called based on the chosen event types.
842+
*
843+
* @param string $url URL to receive event.
844+
* @param string $event Event to subscribe to.
845+
* @param string $parameter Optional parameter depending on the event.
846+
*
847+
* @since 1.0.0
848+
*
849+
* @see https://developers.convertkit.com/#create-a-webhook
850+
*
851+
* @throws \InvalidArgumentException If the event is not supported.
852+
*
853+
* @return false|object
854+
*/
855+
public function create_webhook(string $url, string $event, string $parameter = '')
856+
{
857+
// Depending on the event, build the required event array structure.
858+
switch ($event) {
859+
case 'subscriber.subscriber_activate':
860+
case 'subscriber.subscriber_unsubscribe':
861+
case 'purchase.purchase_create':
862+
$eventData = ['name' => $event];
863+
break;
864+
865+
case 'subscriber.form_subscribe':
866+
$eventData = [
867+
'name' => $event,
868+
'form_id' => $parameter,
869+
];
870+
break;
871+
872+
case 'subscriber.course_subscribe':
873+
case 'subscriber.course_complete':
874+
$eventData = [
875+
'name' => $event,
876+
'course_id' => $parameter,
877+
];
878+
break;
879+
880+
case 'subscriber.link_click':
881+
$eventData = [
882+
'name' => $event,
883+
'initiator_value' => $parameter,
884+
];
885+
break;
886+
887+
case 'subscriber.product_purchase':
888+
$eventData = [
889+
'name' => $event,
890+
'product_id' => $parameter,
891+
];
892+
break;
893+
894+
case 'subscriber.tag_add':
895+
case 'subscriber.tag_remove':
896+
$eventData = [
897+
'name' => $event,
898+
'tag_id' => $parameter,
899+
];
900+
break;
901+
902+
default:
903+
throw new \InvalidArgumentException(sprintf('The event %s is not supported', $event));
904+
}//end switch
905+
906+
// Send request.
907+
return $this->post(
908+
'automations/hooks',
909+
[
910+
'api_secret' => $this->api_secret,
911+
'target_url' => $url,
912+
'event' => $eventData,
913+
]
914+
);
915+
}
916+
917+
/**
918+
* Deletes an existing webhook.
919+
*
920+
* @param integer $rule_id Rule ID.
921+
*
922+
* @since 1.0.0
923+
*
924+
* @see https://developers.convertkit.com/#destroy-webhook
925+
*
926+
* @return false|object
927+
*/
928+
public function destroy_webhook(int $rule_id)
929+
{
930+
return $this->delete(
931+
sprintf('automations/hooks/%s', $rule_id),
932+
[
933+
'api_secret' => $this->api_secret,
934+
]
935+
);
936+
}
937+
823938
/**
824939
* List custom fields.
825940
*

tests/ConvertKitAPITest.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,108 @@ public function testGetSubscriberTagsWithInvalidSubscriberID()
12371237
$subscriber = $this->api->get_subscriber_tags(12345);
12381238
}
12391239

1240+
/**
1241+
* Test that get_webhooks() returns the expected data.
1242+
*
1243+
* @since 1.0.0
1244+
*
1245+
* @return void
1246+
*/
1247+
public function testGetWebhooks()
1248+
{
1249+
// Create a webhook first.
1250+
$result = $this->api->create_webhook(
1251+
'https://webhook.site/2705fef6-34ef-4252-9c78-d511c540b58d',
1252+
'subscriber.subscriber_activate',
1253+
);
1254+
$ruleID = $result->rule->id;
1255+
1256+
// List webhooks, confirming the webhook created exists in the list.
1257+
$webhooks = $this->api->get_webhooks();
1258+
$this->assertIsArray($webhooks);
1259+
1260+
// Destroy the webhook.
1261+
$result = $this->api->destroy_webhook($ruleID);
1262+
$this->assertEquals($result->success, true);
1263+
}
1264+
1265+
/**
1266+
* Test that create_webhook() and destroy_webhook() works.
1267+
*
1268+
* We do both, so we don't end up with unnecessary webhooks remaining
1269+
* on the ConvertKit account when running tests.
1270+
*
1271+
* @since 1.0.0
1272+
*
1273+
* @return void
1274+
*/
1275+
public function testCreateAndDestroyWebhook()
1276+
{
1277+
// Create a webhook first.
1278+
$result = $this->api->create_webhook(
1279+
'https://webhook.site/2705fef6-34ef-4252-9c78-d511c540b58d',
1280+
'subscriber.subscriber_activate',
1281+
);
1282+
$ruleID = $result->rule->id;
1283+
1284+
// Destroy the webhook.
1285+
$result = $this->api->destroy_webhook($ruleID);
1286+
$this->assertEquals($result->success, true);
1287+
}
1288+
1289+
/**
1290+
* Test that create_webhook() and destroy_webhook() works with an event parameter.
1291+
*
1292+
* We do both, so we don't end up with unnecessary webhooks remaining
1293+
* on the ConvertKit account when running tests.
1294+
*
1295+
* @since 1.0.0
1296+
*
1297+
* @return void
1298+
*/
1299+
public function testCreateAndDestroyWebhookWithEventParameter()
1300+
{
1301+
// Create a webhook first.
1302+
$result = $this->api->create_webhook(
1303+
'https://webhook.site/2705fef6-34ef-4252-9c78-d511c540b58d',
1304+
'subscriber.form_subscribe',
1305+
$_ENV['CONVERTKIT_API_FORM_ID']
1306+
);
1307+
$ruleID = $result->rule->id;
1308+
1309+
// Destroy the webhook.
1310+
$result = $this->api->destroy_webhook($ruleID);
1311+
$this->assertEquals($result->success, true);
1312+
}
1313+
1314+
/**
1315+
* Test that create_webhook() throws an InvalidArgumentException when an invalid
1316+
* event is specified.
1317+
*
1318+
* @since 1.0.0
1319+
*
1320+
* @return void
1321+
*/
1322+
public function testCreateWebhookWithInvalidEvent()
1323+
{
1324+
$this->expectException(\InvalidArgumentException::class);
1325+
$this->api->create_webhook('https://webhook.site/2705fef6-34ef-4252-9c78-d511c540b58d', 'invalid.event');
1326+
}
1327+
1328+
/**
1329+
* Test that destroy_webhook() throws a ClientException when an invalid
1330+
* rule ID is specified.
1331+
*
1332+
* @since 1.0.0
1333+
*
1334+
* @return void
1335+
*/
1336+
public function testDestroyWebhookWithInvalidRuleID()
1337+
{
1338+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
1339+
$this->api->destroy_webhook(12345);
1340+
}
1341+
12401342
/**
12411343
* Test that get_custom_fields() returns the expected data.
12421344
*

0 commit comments

Comments
 (0)